Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Render function freezes before while is Open  (Read 1920 times)

0 Members and 1 Guest are viewing this topic.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Render function freezes before while is Open
« 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);

        }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: Render function freezes before while is Open
« Reply #1 on: October 02, 2016, 04:24:00 pm »
Why do you call setActive anyways?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Render function freezes before while is Open
« Reply #2 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();

        }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: Render function freezes before while is Open
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Render function freezes before while is Open
« Reply #4 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);

        }

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Render function freezes before while is Open
« Reply #5 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.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Render function freezes before while is Open
« Reply #6 on: October 05, 2016, 12:27:29 am »
Can someone help me please :/?

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Render function freezes before while is Open
« Reply #7 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.

I would like a spanish/latin community...
Problems building for Android? Look here