Skip to main content
gamedev.net
🔒 Locked

Threads?

Started by DanielGuest 2 replies 500+ views
Original Post
DanielGuest
DanielGuest
Is it possible to execute a method every X - seconds ? I want to write a animation, a rotating cube. The cube should rotate every 0.2 seconds! It is not possible to use a while-loop, because the animation can be started/stopped by pressing a key.
tudehopet
tudehopet
If you are using MFC then it would be easier to use threads. But in my understanding of the spinning cube lesson, is every time the function is called the cube rotates so much. Therefore it would quite difficult to implement a while loop in that function.

I dont know if this helps because my understanding of that lesson is not quite perfect yet.

No amount of genius can overcome a preoccupation with detail.
No amount of genius can overcome a preoccupation with detail.
Hawkeye3
Hawkeye3
Why waste threads on that? Most games have a nextThink variable which is compared against the current time (You have to have a timer implemented, look at nehe''s timing lesson). If the nextThink variable is less than the current time you can run the if statement. Here is what I do to update my frameRate variable every second:


void CalculateFPS()
{
static double nextThink = time;
static int fps = 0;

if(nextThink <= time) {
frameRate = fps;
fps = 0;
// Set the next execution to be in 1 second
nextThink = time + 1.0f;
}
fps++;
}
Lukerd
Lukerd
At program start:


lastTime = 0;
currentTime = GetTickCount();


then every frame:


currentTime = GetTickCount();

if ((currentTime - lastTime) > 200)
{
lastTime = currentTime;
SpinCube();
}



Kazade.

Hyperdev

"To err is human, to really mess up requires a computer"
"To err is human, to really mess up requires a computer"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.