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.