[SOLVED]Adding some form of FPS?

Well basically I had to recreate the game space invaders for a task today. The game it self comes out fine. But one of the tasks to implement is "frames per second" into this game.

I'm not to sure how to start adding something like this into a console program. The program was made with a 2D array and structs for the invaders etc. I have a players ship that shoots bullets when you tell it too. All of this is made in the console. But it runs on the speed depending on the computer as its just constantly doing everything.

Thus making the game to fast - so the task at hand is to add fps to it. Just wondering if anyone can point me in the right direction or show me an example of how to implement this using just c++. No extra stuff like SDL's FPS functions and what not.

Cheers,
Myth.
Last edited on
Check a time difference and pause your program if it is smaller than your refreshing rate:
get Time (t0)

do your actions

while Time-t0 < 1/FpS sec
wait before looping
Last edited on
What exactly do you mean by check a time difference?

Would something like this be possible acceptable?

1
2
3
4
5
6
int SECONDS 1;
int FRAMES_PER_SECOND 32;

clock_t endwait;
endwait = clock () + SECOND * FRAMES_PER_SECOND; 
while(clock() < endwait){}
Last edited on
I mean the time passed during the execution of the game functions.

Eg (using clock):
1
2
3
4
5
6
7
clock_t before = clock();


//Your stuff


while ( clock()-before < CLOCKS_PER_SEC / FRAMES_PER_SECOND );
Last edited on
Ok I see what your saying now - only thing is, do you mean seconds not CLOCK_PER_SEC? or is that still equal to 1?

Thanks a heap so far Bazzy
That should be defined in ctime and it holds the number of clocks in a second
If clock()-before == CLOCK_PER_SEC then a second have passed since you gave a value to "before"


http://www.cplusplus.com/reference/clibrary/ctime/CLOCKS_PER_SEC.html
Awesome cheers its 4am so I'm going to call it a night and play with this more in the morning. I'll let you know how I go. Thanks :)
If your using windows. Use the GetFrequencyCounter() API calls within your game loop. They give you a much higher level of accuracy and are used by most modern games.
Ah I've seen this used before I'll check it out now. Thanks

EDIT:
Just wondering Zaita if you have any sites or anything that explains this function a little more. I seen it used a little while ago but I can't find my mates code he used it in. Any sites or what not would be great as googles giving me trash on it at the moment.
Last edited on
Errr... I wouldn't remember any of the URLs. I have my own code developed for doing that at home. Haven't modified it in years now :P
Would you happen to know anything else I can use other than sleep that kills its thread?
That depends on the threading library.

Typically something like Thread.Abort();
Topic archived. No new replies allowed.