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

Author Topic: Window freezes with threads  (Read 2190 times)

0 Members and 1 Guest are viewing this topic.

Sheppard

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Window freezes with threads
« on: March 19, 2013, 07:29:59 pm »
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 :P

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

sf::RenderWindow app_window(sf::VideoMode(1280, 720, 32), "Game"); //Okno g&#322;ówne

class Hero
{
        bool thread; //czy w&#261;tek ma chodzi&#263;?
        sf::RectangleShape hero; //figura

        public:

        Hero()
        {              
                this->load();
                this->thread = true;

                sf::Thread hero_thread(&Hero::run, this);
                hero_thread.launch(); //Start w&#261;tku
        }

        /**
        * Rysowanie tego syfu
        */

        void draw()
        {
                app_window.draw(hero);
        }

        /**
        * Stopowanie tego syfu
        */

        void stop_thread()
        {
                this->thread = false;
        }

        /**
        * Funkcja wywo&#322;ywana przy tworzeniu w&#261;tku
        */

        void run()
        {
                while(this->thread)
                {
                       

                        //sf::sleep(sf::milliseconds(100));
                }
        }


        private:
       
        /**
        * Funkcja &#322;aduj&#261;ca podstwowy wygl&#261;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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Window freezes with threads
« Reply #1 on: March 19, 2013, 09:09:01 pm »
Please read the thread tutorial again, the solution is explicitely written at the end.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: Window freezes with threads
« Reply #2 on: March 19, 2013, 10:29:01 pm »
Besides that you don't want to use a thread for your hero. Threads will not make things easier or better managable, they'll make things worse in such a case.
Just pack the whole logic and rendering into one thread.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sheppard

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Window freezes with threads
« Reply #3 on: March 19, 2013, 11:33:07 pm »
Ok thx, I see that thread's launching cannot be into a constructor. Now I have another problems. I wanted to make simple "Text" on the screen which says what the mouse position is (for example X = 13.45 Y = 36.123). I read tutorial for v.2.0 and did everything like there was said but my Text isn't rendered. Where is the problem?

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

sf::RenderWindow app_window(sf::VideoMode(1280, 720, 32), "Game"); //Okno g&#322;ówne

class Hero
{
        bool thread; //czy w&#261;tek ma chodzi&#263;?
        sf::RectangleShape hero; //figura

        public:

        Hero()
        {              
                this->load();
                this->thread = true;


        }

        /**
        * Rysowanie tego syfu
        */

        void draw()
        {
                app_window.draw(hero);
        }

        /**
        * Przemieszczanie tego syfu
        */

        void move(float x, float y)
        {
                hero.setPosition(x, y);
        }

        /**
        * Stopowanie tego syfu
        */

        void stop_thread()
        {
                this->thread = false;
        }

        /**
        * Funkcja wywo&#322;ywana przy tworzeniu w&#261;tku
        */

        void run()
        {

                while(this->thread)
                {

                        //sf::sleep(sf::milliseconds(100));
                }
        }


        private:
       
        /**
        * Funkcja &#322;aduj&#261;ca podstwowy wygl&#261;d hirosa
        */

        void load()
        {
                hero.setPosition((unsigned int)(app_window.getSize().x / 2), (unsigned int)(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
        sf::Thread hero_thread(&Hero::run, hero);
        hero_thread.launch(); //Start w&#261;tku

        sf::Text Text;
        Text.setString("asdasd");
        Text.setCharacterSize(30);
        Text.setStyle(sf::Text::Bold);
        Text.setColor(sf::Color::Blue);
        Text.setPosition(100, 100);


        while(app_window.isOpen())
        {
                sf::Event event;

                while (app_window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                        {
                                hero->stop_thread();
                                app_window.close();
                        }
                        if(event.type == sf::Event::MouseButtonReleased)
                        {
                                sf::Vector2i position = sf::Mouse::getPosition();
                                hero->move(position.x, position.y);

                        }
                }      
                app_window.clear(sf::Color(255, 255, 255));

                hero->draw();
                app_window.draw(Text);

                app_window.display();
        }


        delete hero;

    return 0;
}
 

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Window freezes with threads
« Reply #4 on: March 19, 2013, 11:38:21 pm »
The standard font is removed in the lastest version of SFML, so you have to load a font by your own and set it for each text object


AlexAUT

Sheppard

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Window freezes with threads
« Reply #5 on: March 19, 2013, 11:43:25 pm »
Solved, thx
« Last Edit: March 19, 2013, 11:50:35 pm by Sheppard »