0% found this document useful (0 votes)
51 views3 pages

Un Petit Code SFML

This C++ code defines functions and variables to create a window for drawing shapes using the SFML library. It initializes variables for the window size and object position and velocity. In a loop, it handles keyboard input to move a rectangle and redraws the window, clearing it and drawing the rectangle and four connecting lines. It also tracks window focus and sets the title accordingly.

Uploaded by

Silberg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Un Petit Code SFML

This C++ code defines functions and variables to create a window for drawing shapes using the SFML library. It initializes variables for the window size and object position and velocity. In a loop, it handles keyboard input to move a rectangle and redraws the window, clearing it and drawing the rectangle and four connecting lines. It also tracks window focus and sets the title accordingly.

Uploaded by

Silberg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <SFML/Graphics.

hpp>
#include <SFML/System.hpp>
#include <iostream>

#define Y 1080
#define X 1920
#define VITESSE 10

void actionDeLaFenetre(sf::RenderWindow* fenetre);

int i = 0;
int x = X / 2;

int main()
{
sf::RenderWindow window(sf::VideoMode(X, Y), "SFML works!");

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
i = i - VITESSE;
std::cout << "UP\n";
}
if (event.key.code == sf::Keyboard::Down) {
i = i + VITESSE;
std::cout << "DOWN\n";
}
if (event.key.code == sf::Keyboard::Left) {
x = x - VITESSE;
}
if (event.key.code == sf::Keyboard::Right) {
x = x + VITESSE;
}
}

actionDeLaFenetre(&window);

return 0;
}

void actionDeLaFenetre(sf::RenderWindow* fenetre) {

sf::Event event;

//Début creation rectangle


sf::RectangleShape shape(sf::Vector2f(20.f, 20.f));
shape.setFillColor(sf::Color::Green);
shape.setPosition(x, i);
//fin creation rectangle

//debut creation ligne 1


sf::Vertex ligne1[] = {
sf::Vertex(sf::Vector2f(x - 20, i -20)),
sf::Vertex(sf::Vector2f(x, i))
};
//fin creation ligne

// debut creation ligne 2


sf::Vertex ligne2[] = {
sf::Vertex(sf::Vector2f(x + 20, i - 20)),
sf::Vertex(sf::Vector2f(x, i))
};
//fin creation ligne

// debut creation ligne 3


sf::Vertex ligne3[] = {
sf::Vertex(sf::Vector2f(x + 20, i + 20)),
sf::Vertex(sf::Vector2f(x, i))
};
//fin creation ligne

// debut creation ligne 4


sf::Vertex ligne4[] = {
sf::Vertex(sf::Vector2f(x - 20, i + 20)),
sf::Vertex(sf::Vector2f(x, i))
};
//fin creation ligne
fenetre->clear();
/*std::cout << "Que veut tu dessinner : \n1.rectangle\n2. ligne\n||";
std::cin >> i;*/

//fenetre->draw(shape);

fenetre->draw(ligne1, 40, sf::Lines);


fenetre->draw(ligne2, 40, sf::Lines);
fenetre->draw(ligne3, 40, sf::Lines);
fenetre->draw(ligne4, 40, sf::Lines);

fenetre->display();
bool focus;
focus = fenetre->hasFocus();
if (focus == true) {
fenetre->setTitle("Premier plan");
std::cout << "Aucune Erreur (1)" << std::endl;
}
else if (focus == false) {
fenetre->setTitle("Second Plan");
std::cout << "Aucune Erreur (2)" << std::endl;
}
else {
std::cout << "Ya une fucking erreur quelque part\n";
}
if (x > X) {
x = x - VITESSE;
}
if (x < 0) {
x = x + VITESSE;
}
if (i > Y) {
i = i - VITESSE;
}
if (i < 0) {
i = i + VITESSE;
}
}

You might also like