Untitled
#include <SFML/Graphics.hpp> #include <iostream> sf::Color changeColor(sf::Color color, int &state, int speed = 10) { int red = color.r; int green = color.g; int blue = color.b; //std::cout << state << ' ' << red << ' ' << green << ' ' << blue << std::endl; switch(state) { case 0: { green += speed; if (green >= 255) { green = 255; state++; } break; } case 1: { red -= speed; if (red <= 0) { red = 0; state++; } break; } case 2: { blue += speed; if (blue >= 255) { blue = 255; state++; } break; } case 3: { green -= speed; if (green <= 0) { green = 0; state++; } break; } case 4: { red += speed; if (red >= 255) { red = 255; state++; } break; } case 5: { blue -= speed; if (blue <= 0) { blue = 0; state++; } break; } default: { state = 0; } } sf::Color newColor(red, green, blue); return newColor; } int main() { int WINDOW_WIDTH = 800; int WINDOW_HEIGHT = 600; int RADIUS = 100; int red = 255; int green = 0; int blue = 0; int state = 0; int bgState = 3; sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "AAAA"); sf::Color circleColor(255, 0, 0); sf::Color bgColor(0, 255, 255); sf::CircleShape circle0(RADIUS); circle0.setFillColor(circleColor); circle0.setPosition(sf::Vector2f(WINDOW_WIDTH / 2 - RADIUS, WINDOW_HEIGHT / 2- RADIUS)); window.setFramerateLimit(50); while (window.isOpen()) { // check all the window's events that were triggered since the last iteration of the loop sf::Event event; while (window.pollEvent(event)) { // "close requested" event: we close the window if (event.type == sf::Event::Closed) window.close(); } circleColor = changeColor(circleColor, state); circle0.setFillColor(circleColor); bgColor = changeColor(bgColor, bgState, 25); // clear the window with black color window.clear(bgColor); window.draw(circle0); window.display(); } return 0; }
Leave a Comment