[SDL] Same Execution Time != PC's

Started by
12 comments, last by Kylotan 15 years, 5 months ago
Hi gamedev community, I need 60 frames per second in my game. Is this code correct???

//
// Game Loop
//

while (!done) {

    //
    // Begin Frame:
    //

    int initialTicks = SDL_GetTicks();

    // Compute Frame
    .
    .
    .
    // End Compute Frame


    //
    // End Frame
    //

    int finalTicks = SDL_GetTicks();

    // 60 frames per second = 1000 miliseconds / 60 = 16,6 miliseconds
    while (finalTicks - initialTicks < 17)
        finalTicks = SDL_GetTicks();
}
I'm executing my game in two PC's with 2 processors and with 1 processors, and the speed isn't the same :( Thanks!
Advertisement
What about

while ((finalTicks - initialTicks) < 17)

intead of

while (finalTicks - initialTicks < 17)
The change in the above post should make no difference.

What you're probably finding is that you are simply not managing to get 60 frames per second on one or both machines. That code limits you to 60 frames per second if you would normally run faster, but won't speed you up if you were running slower!

Measure your actual frames per second on each computer and see what you get.
I took some tests.
Pc (1 Processor) = 23 fps
Pc (2 Processor) = 47 fps

What can I do??? With my method I can down fps, but I can't increase them.

Thanks!
You need to optimize your code
With SDL that usually means drawing less, or not using any alpha blending, or both. Or moving to OpenGL for rendering.
Thanks for the answers. I found the error. Basically I do this for each frame:
(1) Draw Map
(2) Compute colision
(3) Render Enemies (ship, shots, explosions)
(4) Render player ship (ship, shots, explosions)
(5) Show info (lifes, score, nº level, etc)

(2) to (5): I have developed the code.
(1): I use the SDL_Mappy class to render map.

Then I skipped (1) and I counted 170 fps :)
Maybe I need to write the code to draw the map.
First, you need to let the OS breath. You can't just while like that, the CPU will be stuck at 100% usage. I know some PCs *cough*mac book pro*cough* that will simply shutdown after couple minutes. You need a sleep or wait for the SDL paint event.

Then after that, as Kylotan said, you need to compensate if you have less frames available. Rendering is lagging, and you have 30 fps for example, each frame you will need to update twice.

void SDL_Paint_Even(){    int nbFrameToUpdate = updateTimer(); // In there you check the time elapsed     // between the 2 frames and return the number of frame to update. In a good      // FPS, you will get 0 here often    while (nbFrameToUpdate)    {        UpdateYourGame();        nbFrameToUpdate--;    }    RenderYourGame();}
Quote:Original post by Daivuk
Then after that, as Kylotan said, you need to compensate if you have less frames available. Rendering is lagging, and you have 30 fps for example, each frame you will need to update twice.

What if you get 45 fps, should you update 1.5 times per frame then? Or update once this frame, and twice the next? I think that might result in very jerky movement.

A better solution would be to calculate seconds per frame instead of frames per seconds, and then multiply that with for example the speed of a ship. Assume for example that you want to move the players ship 20 pixels per second. If it takes 0.1 seconds to draw a frame you get 20 * 0.1 = 2, so you move the ship 2 pixels this frame. With this approach it won't matter how many frames you render per second, the movement will be constant.

Peros:
In my example, I'm assuming you are interpolating in the render too. But that works fine anyway without it. it will not look jerky without interpolation.

And I understand your way of doing it, but this breaks the "perfect" simulation. Try to run a physical situation, with your method the simulation will be different every time you run the game.

It's also unfriendly for networked game, for the same reason above: It breaks the simulation.

It also makes collisions more difficult if you have lags.

This topic is closed to new replies.

Advertisement