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

Author Topic: Another sf::Thread topic...  (Read 4111 times)

0 Members and 1 Guest are viewing this topic.

SeriousAlexej

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Another sf::Thread topic...
« on: April 01, 2014, 11:23:34 am »
Hi everybody! I'm new to SFML and this forum.

I have a task at the university to write a game, and I've decided to stick with this library.
Everything has been good so far, but when I've decided to make a separate thread to track button presses and made a draft code, it didn't work.

Here's the thread function:

void EventThread()
{
        while (g_WindowOpened) // THIS IS BOOL!
        {      
                g_Mutex.lock();
                sf::Event event;
                while (MainWindow->pollEvent(event))
                {
                        // Close window : exit
                        switch (event.type)
                        {
                                case sf::Event::Closed:
                                        MainWindow->close();
                                        g_WindowOpened = false;
                                        break;

                                //Mouse input
                                case sf::Event::MouseButtonReleased:
                                {//Mouse works only in menus
                                        if(g_CurrentMenu)
                                        {
                                                if(event.mouseButton.button == sf::Mouse::Left)
                                                        g_CurrentMenu->LButtonActions();
                                                else
                                                if(event.mouseButton.button == sf::Mouse::Right)
                                                        g_CurrentMenu->RButtonActions();
                                        }
                                        break;
                                }
                                       
                                case sf::Event::KeyPressed:
                                {
                                        g_Keyboard[event.key.code+1] = true;
                                        break;
                                }
                                case sf::Event::KeyReleased:
                                {
                                        g_Keyboard[event.key.code+1] = false;
                                        break;
                                }

                        }
                }
                g_Mutex.unlock();
                sf::sleep(sf::milliseconds(1));
        }
}


Part of main() :

...
        sf::Thread eventThread(&EventThread);
        eventThread.launch();

        // GAME LOOP
        while (g_WindowOpened)
        {      
                g_Mutex.lock();
                DrawGLScene();
                MainWindow->display();
                if(g_bGameON)
                        dynamicsWorld->stepSimulation(1.0f/60.f,10);
                g_Mutex.unlock();
                sf::sleep(sf::milliseconds(16));
        }              
       
        eventThread.wait();
...

When I run the game, the thread doesn't seem to be running, only the GAME LOOP.
I'm afraid I've made a stupid mistake somewhere, but I can't figure out where. :(

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Another sf::Thread topic...
« Reply #1 on: April 01, 2014, 11:46:05 am »
On some operating systems, events must be handled in the main thread. Why even multithreading? I doubt that event processing takes so much time at you that it's worthwhile to have its own thread for it. You should rather think about computing the game logic in parallel, but probably even that is unnecessary. Don't use threads if you don't need them.

By the way, your code is quite messy, and not threadsafe. For example, g_WindowOpened is not protected, and the mutexes cover a too large area (why game logic?). You could also consider the RAII class sf::Lock if you have clearly defined scopes for mutexes.

But as mentioned: write a single-threaded program. If you really think you have to use multithreading (usually that's only the case for networking or extensive background tasks), then take the time to get familiar with the topic, as it's quite complex.
« Last Edit: April 01, 2014, 11:48:57 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

SeriousAlexej

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Another sf::Thread topic...
« Reply #2 on: April 01, 2014, 01:02:00 pm »
Thanks for the reply, I was just curious about threads and decided to give it a try.

 

anything