Game loop too fast?

Started by
4 comments, last by Alerion 16 years, 9 months ago
I have been using while(from.Created) and do{}while(form.Created) for my game loops but they seem to be to fast (This is C# by the way). I want an fps anywhere from 30-40 but these are like an fps of at least 100. Someone told that they just use a timer set it to an interval of 31 to give them 32 fps, but I am sure there would have to be a better way. Thanks.
Advertisement
Timers are usually the method by which you would lock your frame rate to 30 or 40 or whatever. However, you may want to figure out a way to make your game perform independent of the frame rate... for example, you should have objects that move at X units per second, rather than X units per frame. Having their motion tied to the framerate will make the game run faster on faster machines... that isn't desirable in most cases.
So what do you suggest to stop everything moving in hyper speed? I am just used to flash, where you set the frame rate.
This is C++ code i used in an asteroids clone to limit the frame rate, while it is not C# you should still be able to figure out how it works since the languages are similar. But as noted above it is not the best solution.

/*	Thanks to Lazy Foo for this class: http://lazyfooproductions.com/SDL_tutorials/lesson10/index.php*/


class Timer
{
private:
//The clock time when the timer started
int startTicks;

//The ticks stored when the timer was paused
int pausedTicks;

//The timer status
bool paused;
bool started;

public:
//Initializes variables
Timer();

//The various clock actions
void start();
void stop();
void pause();
void unpause();

//Get the number of ticks since the timer started
//or gets the number of ticks when the timer was paused
int get_ticks();

//Checks the status of the timer
bool is_started();
bool is_paused();
};

Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}

void Timer::start()
{
//Start the timer
started = true;

//Unpause the timer
paused = false;

//Get the current clock time
startTicks = SDL_GetTicks();
}

void Timer::stop()
{
//Stop the timer
started = false;

//Unpause the timer
paused = false;
}

void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;

//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}

void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;

//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;

//Reset the paused ticks
pausedTicks = 0;
}
}

int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}

//If the timer isn't running
return 0;
}

bool Timer::is_started()
{
return started;
}

bool Timer::is_paused()
{
return paused;
}


Okay, thank you. I will try that out.

//in the game loopstatic float lastTime = (float)timeGetTime( );float currTime = (float)timeGetTime( );float timeDelta = (currTime - lastTime)*EPSILON; //note EPSILON is an arbitrarily small number constant//suppose we want an object to move at 10 units per second:object.speed = 10;object.move( speed * timeDelta );


if you pass timeDelta and multiply it in render and movement calls it standardizes the amount things move around.

sorry if this doesn't make sense I'm a little sleepy not really thinking clearly.

This topic is closed to new replies.

Advertisement