SFML community forums

Help => Graphics => Topic started by: Putarda on October 02, 2016, 03:11:46 pm

Title: Render function freezes before while is Open
Post by: Putarda on October 02, 2016, 03:11:46 pm
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);

        }
Title: AW: Render function freezes before while is Open
Post by: eXpl0it3r on October 02, 2016, 04:24:00 pm
Why do you call setActive anyways?
Title: Re: Render function freezes before while is Open
Post by: Putarda on October 02, 2016, 07:44:22 pm
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();

        }
Title: AW: Render function freezes before while is Open
Post by: eXpl0it3r on October 02, 2016, 08:08:59 pm
Did it not occure to you that it might be important to mention that you operate in a multi-threaded environment? ;)

Can you provide a minimal and complete example. With these code snippets we can't really say.
Title: Re: Render function freezes before while is Open
Post by: Putarda on October 03, 2016, 12:34:17 am
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);

        }
Title: Re: Render function freezes before while is Open
Post by: Putarda on October 03, 2016, 12:36:08 am
And my render and update functions are in separated threads. Render function is in the main thread ofcourse.
Title: Re: Render function freezes before while is Open
Post by: Putarda on October 05, 2016, 12:27:29 am
Can someone help me please :/?
Title: Re: Render function freezes before while is Open
Post by: DarkRoku12 on October 05, 2016, 04:36:36 am
I can help you if i could have the full code, if you will provide it send it in a zip file into an attachment.

But a suggestion:

Before implementing something uncertain, do a minimal test on another project this will reduce the question "Where is the bug?" and will be easy to know where are the problems.

It seems that you have few experience with both SFML and Threads.

So its even better that you adquire this experience experimenting in very small projects and when the implementation is robust then you can add it to your main/bigger project.