A couple of years ago I developed a game using C with the GLUT library. I originally developed it on an older machine on which the game ran fine. Now when I have a newer and faster machine the game runs so fast that it's impossible to actually play it. I was wondering how the game could be modified so that the running speed is constant on all systems (more or less).

I would assume that I would have to implement some kind of sleep function to be called during the main loop or is there some other kind of technique normally used?

Thanks in advance.

Recommended Answers

All 3 Replies

You are correct. I usually sleep for about 10 ms, that should be enough. I have been thinking about another way too, if your game relies heavily on graphics (your scene takes long to draw), then maybe it's a good thing to try calculate movements etc based on how long it takes to draw your scene. Example:

while(true) // Main loop
   xPosition += movement_per_ms * ms_since_last_time;

I haven't tested this technique yet though.

Emil Olofsson

Yes, the technique is called Time-based animation.

If you are on Windows, there is a function that will help you manage frame counts - C:\Program Files\Microsoft SDKs\Windows\v7.0\Include\WinBase.h\GetTickCount() You'd use it like this -

long start = GetTickCount();

if (GetTickCount() - start >= 30)
    //reset timing
    start = GetTickCount();

    <your code to move things here>

I'm fairly sure that this kind of functionality exists under other OS/Frameworks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.