SFML community forums
Help => Window => Topic started by: N1ghtly on August 24, 2011, 01:26:40 pm
-
I searched around already and it seems it always has got something to do with OpenGL. Well I'm not using openGL. It's the first time I try running different threads ^.^
Here's my code:
void Game::tick()
{
ball.tick();
//get mouse coord
float x = Window.GetInput().GetMouseX();
panel.tick(x);
updateText();
if(killedBricks == levels[currentLvl].amountOfBricks) //level over
{
currentLvl++;
ball.reset();
return;
}
if(prepare)
{
prepare = false;
ball.pause();
sf::Thread thread(&Game::prepare_function, &*this);
thread.Launch();
}
}
void Game::prepare_function()
{
sf::Clock clock;
int i = 0;
sf::Font myfont;
myfont.LoadFromFile("GOUDYSTO.TTF");
sf::Text text("", myfont, 30);
text.SetPosition(300, 300);
while(i < 3)
{
int i = clock.GetElapsedTime();
if(i < 1)
text.SetString("3");
else if (i < 2)
text.SetString("2");
else
text.SetString("1");
Window.Draw(text);
}
ball.start();
}
The purpose is (it's a breakout game btw) that if the ball goes out of screen (thus -1 live) we wait first 3 seconds before releasing the next ball.
I was also thinking of having a separate thread for audio and one for drawing.
help?
-
Basically, a window must be deactivated (SetActive(false)) in the thread where it is active, before being used in another thread.
But you don't need a thread at all for what you do. Your thread spends most of its time doing nothing, and worst, the main thread is waiting until the other thread is finished (you could call prepare_function directly, it would have the exact same effect). So it's absolutely useless ;)
A thread for audio is not necessary, sounds are already played in a separate thread so that the main thread is never blocked.
A thread for drawing... I'd say it's useless too, unless you have already thought about it and have really good arguments for it.
Threading is not magical, it must be used carefully and it involves a lot of new concepts and problems.
-
Wow, thanks for the quick reply Laurent!
I thought, that if I'd call prepare_function directly, it wouldn't keep on drawing?
For example, my paddle won't move anymore, and the numbers will stay on the screen and painted over (ugly)?
EDIT:
If I call it directly it crashes :(
-
Good afternoon everybody. I installed SFML on windows and it happens same error here. I use the codeblocks IDE 16.01 with GCC 4.9.2 TDM (SJLJ) - 32-bit. Nothing program that uses SFML windows is drawned and is showed the same message this topic.
How I can fix this?
-
This thread is over 5 years old, as such the problem won't be the same at all. Please create your own thread.
Also there are other similar and newer topics on this error message, make sure to read them.