Skip to content

Instantly share code, notes, and snippets.

@jpleau
Created January 18, 2014 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpleau/8485849 to your computer and use it in GitHub Desktop.
Save jpleau/8485849 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <SFML/Graphics.hpp>
#define FPS 60
#define MOVEMENT 10
typedef struct Box {
float x, y;
sf::Color color;
} Box;
int main (int argc, char *argv[]) {
sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
window.setFramerateLimit(FPS);
sf::Event event;
Box box;
box.x = 650;
box.y = 400;
box.color = sf::Color::Blue;
Box box2;
box2.y = box.y;
box2.color = sf::Color::Red;
sf::RectangleShape rect1;
sf::RectangleShape rect2;
rect1.setSize(sf::Vector2f(100, 100));
rect2.setSize(rect1.getSize());
rect1.setFillColor(box.color);
rect2.setFillColor(box2.color);
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
if (sf::Keyboard::isKeyPressed((sf::Keyboard::Right))) {
box.x += MOVEMENT;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
box.x -= MOVEMENT;
}
window.clear(sf::Color::White);
rect1.setPosition(box.x, box.y);
bool drawingBox2 = false;
if (rect1.getPosition().x + rect1.getSize().x >= window.getSize().x) {
drawingBox2 = true;
float diff = window.getSize().x -rect1.getPosition().x - rect1.getSize().x;
box2.x = -diff - rect2.getSize().x;
if (box2.x >= 0) {
drawingBox2 = false;
box.x = box2.x;
rect1.setPosition(box.x, box.y);
} else {
rect2.setPosition(box2.x, box2.y);
}
}
if (rect1.getPosition().x < 0) {
drawingBox2 = true;
float diff = rect1.getPosition().x;
box2.x = window.getSize().x + diff;
if (box2.x <= window.getSize().x - rect2.getSize().x) {
drawingBox2 = false;
box.x = box2.x;
rect1.setPosition(box.x, box.y);
} else {
rect2.setPosition(box2.x, box2.y);
}
}
window.draw(rect1);
if (drawingBox2) {
window.draw(rect2);
}
window.display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment