Hi, I have a simple code. One rectangle which I want to move. I created new thread for this becouse this rectangle will be a "hero" in my game in the future
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
sf::RenderWindow app_window(sf::VideoMode(1280, 720, 32), "Game"); //Okno główne
class Hero
{
bool thread; //czy wątek ma chodzić?
sf::RectangleShape hero; //figura
public:
Hero()
{
this->load();
this->thread = true;
sf::Thread hero_thread(&Hero::run, this);
hero_thread.launch(); //Start wątku
}
/**
* Rysowanie tego syfu
*/
void draw()
{
app_window.draw(hero);
}
/**
* Stopowanie tego syfu
*/
void stop_thread()
{
this->thread = false;
}
/**
* Funkcja wywoływana przy tworzeniu wątku
*/
void run()
{
while(this->thread)
{
//sf::sleep(sf::milliseconds(100));
}
}
private:
/**
* Funkcja ładująca podstwowy wygląd hirosa
*/
void load()
{
hero.setPosition(app_window.getSize().x / 2, app_window.getSize().y / 2);
hero.setSize(sf::Vector2f(50, 50));
hero.setFillColor(sf::Color::Blue);
}
};
int main()
{
app_window.setFramerateLimit(60); //Limit klatków
Hero * hero = new Hero(); //Tworzenie bohatera
while(app_window.isOpen())
{
sf::Event event;
while (app_window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
{
app_window.close();
}
if(event.key.code == sf::Keyboard::A)
{
//this->hero.move(-5, 0);
}
}
app_window.clear(sf::Color(255, 255, 255));
hero->draw();
app_window.display();
}
hero->stop_thread();
delete hero;
return 0;
}
but after compilation and debug window freezes and don't respond. My shape has not been drown. What I did wrong? I know that events have to be in the same thread where window was created. In my code they are.
Sorry for Polish comments.