In my function Game::startDraw(), before draw starts I call window.setActive(true), after that rest of the code is freezed. But, if there is std::cout, freeze is declined. Why??
Working code:
void Game::startDraw() {
window.setActive(true);
std::cout << "Don't freeze!";
while (isOpen()) {
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
setRun(STOP);
window.close();
return;
}
}
window.clear(sf::Color::Blue);
for (auto object : getObjects()) {
window.draw(*object->getSprite());
}
window.display();
}
}
I'm not sure but I think problem is caused by window.setActive().
Window create function:
void Window::createWindow(Game::Game* game, sf::RenderWindow* game_window) {
sf::ContextSettings window_settings;
window_settings.antialiasingLevel = 16;
game_window->create(sf::VideoMode(1920, 1080), *game->getName(), sf::Style::Fullscreen, window_settings);
game_window->setVerticalSyncEnabled(true);
game_window->setActive(false);
}
Because of this
Game::Game(std::string game_name) {
setName(game_name);
loadResources();
Window::createWindow(this, &window);
}
void Game::initialize() {
sf::Thread run_thread(&Game::startRun, this);
run_thread.launch();
startDraw();
}
Main:
int main() {
Game::Game* game = new Game::Game("L Speed");
game->initialize();
delete game;
return 0;
}
Game:
Game::Game(std::string game_name) {
setName(game_name);
loadResources();
Window::createWindow(this, &window);
}
void Game::initialize() {
sf::Thread run_thread(&Game::startRun, this);
run_thread.launch();
startDraw();
}
void Game::addObject(Object::Object object) {
objects[*object.getName()] = object;
}
void Game::setName(std::string game_name) {
name = game_name;
}
void Game::setRun(Game_Run game_run) {
run = game_run;
}
void Game::loadResources() {
Object::Object object("ball", "ball.png");
auto object_size = object.getSprite()->getTexture()->getSize();
object.getSprite()->setScale(float(150) / object_size.x, float(150) / object_size.y);
Game::addObject(object);
}
void Game::startRun() {
setRun(START);
Object::Object* object = Game::getObject("ball");
while (isOpen()) {
object->move(2, 2);
sf::sleep(sf::milliseconds(17));
}
}
void Game::startDraw() {
window.setActive(true);
//freeze problem (white screen). until I add std::cout
while (isOpen()) {
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
setRun(STOP);
window.close();
return;
}
}
window.clear(sf::Color::Blue);
for (auto object : getObjects()) {
window.draw(*object->getSprite());
}
window.display();
}
}
Window:
void Window::createWindow(Game::Game* game, sf::RenderWindow* game_window) {
sf::ContextSettings window_settings;
window_settings.antialiasingLevel = 16;
game_window->create(sf::VideoMode(1920, 1080), *game->getName(), sf::Style::Fullscreen, window_settings);
game_window->setVerticalSyncEnabled(true);
game_window->setActive(false);
}