Framerates insane

Started by
29 comments, last by pTymN 19 years, 4 months ago
Recently I made my first SDL game, Phlong(http://home.comcast.net/~joobot/Phlong.zip) and I realized it ran at different speeds on each computer. So I looked up framerates and found SDL_gfx's framrate system and put it in my program and it was very chuggy. What should I do?
Advertisement
You need a way to control time in your game. Put this somewhere in your main loop:
// Provide time delayUint32 now;static Uint32 next_time;now = SDL_GetTicks();if(next_time > now)    SDL_Delay(next_time - now);next_time += 30;

Run it and then play around with the 30 until it's as fast as you want it. This makes the game delay 30 milliseconds between each frame. Only if needed of course.
Rob Loach [Website] [Projects] [Contact]
isn't using SDL_Delay() a bad idea to lock the framerate? this is because it only uses Sleep() (on windows at least), which has an 10 ms resolution. this means if you do SDL_Delay(30), it could actually delay the game for 40 milliseconds. this could lead to jerky game play.

instead, you should waste the rest of the time in an empty while() loop, or do time based movement.
FTA, my 2D futuristic action MMORPG
It still runs differently on every machine.
Quote:Original post by graveyard filla
isn't using SDL_Delay() a bad idea to lock the framerate? this is because it only uses Sleep() (on windows at least), which has an 10 ms resolution.

Actually this is only true for version earlier than win2k. It's not the resuolution that makes Sleep() unreliable, it#s the fact that the time actually spend on other processes is at least the amount of ms you pass to it. Nevertheless some operations run even better (less jerky) if you put a Sleep(0) or a Sleep(1) (can't remember which) in you main loop.
It's also nice because some people (me included) like to run programs in the background while playing and while I can accept 100% CPU usage when Doom 3 is running, I simply don't expect that for a simple Pong clone [smile].
Well I made the framerate 100 using SDL_gfx liek so:

    FPSmanager *framerate = new FPSmanager;    SDL_initFramerate(framerate);    SDL_setFramerate(framerate,100);    //Lock The screen    //main loop    //Unlock    //Flip    SDL_framerateDelay(framerate);


It runs smoothly on my newer computers and on the older computers it's smooth but much slower. Is there a way to figure out like the computers running speed and calculate the ebst framerate?
Using sleep() (or just waiting for x milliseconds), is a bad idea if all you want is that the game runs equally fast on all computers. You should use time-based movement.
Quote:Original post by Kurioes
Using sleep() (or just waiting for x milliseconds), is a bad idea if all you want is that the game runs equally fast on all computers. You should use time-based movement.


Pardon the naive question, but what do you mean by time-based movement? Currently I have a game loop that does something like the following:

while(game_not_done) {

ProcessEvents();

UpdateStatus();

RenderFrame();

SDL_Delay(draw_time);

DrawFrame();

}

I understand what was said about SDL_Delay()/sleep() not having a fine resolution, but what if I don't want my game to consume 100% of the CPU time and still run at a constant framerate across machines?

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

My mini-tutorial may be what you're looking for...
In case you were wondering what to put in your next christian game; don't ask me, because I'm an atheist, an infidel, and all in all an antitheist. If that doesn't bother you, visit my site that has all kinds of small utilities and widgets.
Quote:Original post by Roots
Quote:Original post by Kurioes
Using sleep() (or just waiting for x milliseconds), is a bad idea if all you want is that the game runs equally fast on all computers. You should use time-based movement.


Pardon the naive question, but what do you mean by time-based movement? Currently I have a game loop that does something like the following:

while(game_not_done) {

ProcessEvents();

UpdateStatus();

RenderFrame();

SDL_Delay(draw_time);

DrawFrame();

}

I understand what was said about SDL_Delay()/sleep() not having a fine resolution, but what if I don't want my game to consume 100% of the CPU time and still run at a constant framerate across machines?


time based movement is when you move all of your objects over a period of time, NOT over a hard-coded set velocity. to do this, you calculate how much time has passed to go through a frame. then you move your objects at their velocity * time_passed. what yoru doing is locking the frame-rate with SDL_Delay, which IMO is a bad idea. at least lock the frame-rate with an empty while loop so that it doesnt get screwy (at least on some machines)
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement