PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Framerate and Time...

Hello everyone,

I am not a very strong programmer but I am making a little game with GD. I put the framerate to 60fps.

I've made a lot of operations so the framerate falls to 45fps but it's not the problem:

I've seen in most of the games that when the framerate decreases, there are less frames displayed but the speed of the game stay the same.

In my game, when the framerate decreases, the game is slower. And It's very disturbing because I am sure that if the framerate decreases to 30fps, the game would be playable yet.

How can I control the framerate to make it dependent on time like every games?
just separate the functions which processes the game and draws the screen, then call the drawScreen function once per main loop execution and the process function depending on the framerate (i.e. if the framerate is 15 and the base fps is 30, then call it two times)
The down-side to such an approach is that you will get a different "experience" on different devices. While the speed of the game will be the same, the smoothness will be different.

Most developers view this as a positive. The logic being that people with better devices will experiece a smoother gameplay, people with lesser devices will still be able to play properly but it won't look quite as good.

I've always taken a different tact... I'd rather find a FPS setting that will play acceptably on the lowest devices I want to support, and make that my frame rate.

For instace, in your case, your getting 45 real FPS. I don't know what device that's on, but I like to use my old Axim X5 as a baseline, so let's say if you try it on mine you get 30FPS. I would set the frame rate to 30FPS and design the game to work properly at that rate. I can then be pretty well assured that any other device will be able to pull that frame rate because the Axim X5 is known to be one of, if not THE worst, devices out there as far as frame rates go. In that way, I can be sure the game will look and play exactly the same on all devices.

You of course have to be sure that your game will look good at that frame rate. If you tried doing Halo at 30FPS, it probably wouldn't work out too well, so this approach wouldn't work for such a game. I think it can GENERALLY be said that frame rate is only of real importance in 3-D games. Most other genres can work well at lower frame rates, GENERALLY SPEAKING. You of course have to decide that on a game by game basis... A high-energy frenetic 2-D shooter might not work too well either, especially those Japanese seisure-inducing ones for instance.
I don't understand the purpose of "the process function depending on the framerate"

I think I've done what you said, it gives this:

Code:

HRESULT CRaffiApplication::ProcessNextFrame(CGapiSurface& backbuffer, DWORD dwFlags)
{
UpdateRaffi(backbuffer);
UpdateMoney(backbuffer);
UpdateCars(backbuffer);
Collisions(backbuffer);

// Draws the surfaces
DrawScreen(backbuffer);
}


And now?
I want to make the game less smooth if the framerate decreases because the principle of the game is based on the speed of certain elements. If this speed is not then respected we can say that the game is different according to the device which we have. And It is in no way what I want.



(My faults of languages are due to the fact that I am French)
It sounds like your confusing game speed with game smoothness. They are two very different things.

In the code you posted, I'll assume that your UpdateCars function moves a car one pixel per call. If you run this code on a device that can do 30 FPS, then the car obviously will move 30 pixels per second. If you run it on a device that can only do 15 FPS, then it will move 15 pixels per second. The SPEED is different.

Now, let's say that you write your code to take into account the frame rate the game is currently doing on a given device. On the device doing 30 FPS, it will still move 30 pixels per second (one pixel per frame). But on the device doing 15 FPS, it will have to move the car TWO pixels per frame to get the desired 30 pixels moved per second (assuming you designed your game such that a car is SUPPOSED to move 30 pixels per second). Moving the car two pixels per frame will look less SMOOTH than moving it one pixel per frame. But, the SPEED will be constant on either device. Obviously, the faster the object goes, the worse the effect on smoothness will be as the frame rate drops (i.e., an object that is supposed to move 60 pixels per second will have to be moved 4 pixels per frame if the frame rate is 15 FPS).

Most developers will attach a "rate of speed" value to their objects that is used, in conjunction with the FPS being provided by the device, to determine how many pixels to move for a given frame. This is usually done with decimal values so that you might move 1.34 pixels per frame (which might confuse you... obviously you can't move a partial pixel, but if during the next frame your code calculates that it needs to move that object 1.76 pixels because the frame rate dropped slightly, then you moved the object three pixels across two frames, 1 pixel the first frame, 2 the next, which makes sense if you remember that the frame rate dropped, so more pixels per frame have to be done per move to get the object's movement speed to remain constant).

I should also point out that usually the FPS value is an average for some given number of frames (i.e., you only calculate the FPS value, and hence the "speed adjustment" every 10 frames or every 30 frames or whatever because doing it every frame is superfolous and will hurt performance).

Clearly, that is more complex to code than what I suggested previously, but it has it's benefits for sure. You'll have to decide which option is better in your particular case.
Imagine you want to run a game at a basic 50 fps. 50 fps means 20 ms per frame. then you could do something like...

Code:


// get time in miliseconds since last frame
now=getTime();
inc=now-lastTime;

while (inc>=20)
{
processGame();
inc-=20;
}

drawGame();
lastTime=now;


if the time since last frame is 10ms, inc will be 10,
so the game won't be processed and will be drawed.

if the time since last frame is 40 ms (slow device), the processGame will be called 2 times, so it'd be exactly like the game would run at 20ms/frame, only that it has skipped one frame.

of course it's pseudocode, and it's stupid redrawing the frame if the game hasn't been updated, and you should put a maximum frameskip, ... but I hope it helps.
Thanks fzammetti for your clear explanation.

I really want that the speed of the game stays the same, I am thus going to follow these invaluable advices.

But obviously, pass of integers in decimal be going to affect the performances of devices.

I test that at once and I inform you...
You could do it all integer-based, but then you won't get the same level of control because obviously you won't be able to move in partial increments. I don't know if it makes enough of a difference though... My guess is that there is some frame rate above which it won't matter much, but I don't know if it's 30FPS, 26FPS, 21FPS, etc. And I could be wrong ;)
I opted for Kak's solution, I did not want to change the behavior of all the sprites of the game...

This method works perfectly when we have a lower framerate, the game is considerable even in 25-30fps.

Here is the code:

Code:

float nFrameTimeTemp;
m_timer.GetActualFrameTime(&nFrameTimeTemp);
if (nFrameTimeTemp < 16.67)
nFrameTime = (float)16.66;
else
nFrameTime += nFrameTimeTemp;

while (nFrameTime >= 0)
{
UpdateRaffi(backbuffer);
UpdateMoney(backbuffer);
UpdateCars(backbuffer);
Collisions(backbuffer);

nFrameTime -= (float)16.67;
}

DrawScreen(backbuffer);
The only problem with that approach is that you can still see slowdowns and/or decreased smoothness on slower devices.

For instance, if the portions of code executed within your loop take 50ms (we'll simplify and assume they are constant in their processing time), then the best frame rate your ever going to get is 20FPS. In other words, regardless of how many times the functions get called, your going to update the screen a maximum of 20 times a second.

If they take 25ms, you'll be updating the screen 40 times a second, but presumably doing updates twice. So the results will still be uneven and will be affected by slower devices.

This is why I prefer to pick a lower (but still acceptable) FPS and design for that. The game is DESIGNED to run at a given frame rate, and as long as I've choosen wisely such that any relatively modern device can do it, then the game will always run exactly the same on any device. This to me is preferable than getting very high frame rates (again, type of game makes all the difference).

But if the approach you chose worked for you, then excellent! I think this is a good thread for anyone that asks this question again (and it'll probably be this week!), since we now have three different methods being discussed, pros and cons debated a bit, and people can make a more informed decision. All three can work, there's plenty of example to support them, so it's a matter of which fits the game requirements the best.
Pejo Software - Per · 15 Aug 2004, 21:11 UTC
I think the natural way to do this would be to base movement and so forth on real time. By doing this the game will always act "as it should" no matter wich framerate you have.
Some code (tossed together with snippets borrowed from my current game project):

Code:
class CSprite
{
public:
CSprite();
virtual ~CSprite() {}

virtual void Draw()=0;
virtual void Move(CFixedPoint16_16 TimeElapsed)
{
m_xSpeed+=m_xAcc*TimeElapsed;
m_ySpeed+=m_yAcc*TimeElapsed;
m_zSpeed+=m_zAcc*TimeElapsed;
m_xPos+=m_xSpeed*TimeElapsed;
m_yPos+=m_ySpeed*TimeElapsed;
m_zPos+=m_zSpeed*TimeElapsed;
}


CFixedPoint16_16 m_xPos;
CFixedPoint16_16 m_yPos;
CFixedPoint16_16 m_zPos;
CFixedPoint16_16 m_xSpeed;
CFixedPoint16_16 m_ySpeed;
CFixedPoint16_16 m_zSpeed;
CFixedPoint16_16 m_xAcc;
CFixedPoint16_16 m_yAcc;
CFixedPoint16_16 m_zAcc;

};

HRESULT CApplication::ProcessNextFrame()
{
//Get time elapsed and restart timer
CFixedPoint16_16 tElapsed=m_SpriteTimer.Start(&m_TimerManager);

//Update all Sprites - calls Move
GetActiveSpriteManager()->UpdateSprites(tElapsed);

//Draw all sprites
GetActiveSpriteManager()->DrawSprites();
}
Pejo, doesn't that yield exactly what I've been talking about though, which is that at lower FPS, the game will appear less smooth because objects effectively are moving more pixels per frame? This is what I was talking about, frame rate vs. smoothness is two different things. Am I missing something?
Pejo is correct in that you should always base your sprite animations on time - just as your game logic is based on time. This insures that animations are smooth from a designed framerate (as Frank has mentioned) upward. In other words, on faster devices the animation speed will remain the same. On slower devices, frames are either naturally skipped or the gameplay slowed to compensate for device performance if so desired.

So, Frank and Pejo are both right - its just that they are looking at different portions of the same problem. :)
Pejo Software - Per · 16 Aug 2004, 05:59 UTC
fzammetti wrote:Pejo, doesn't that yield exactly what I've been talking about though?


I'm sorry if I repeat what already has been said! I just saw some of the code posted above and thought that the solutions felt a little awkward. I must admit that I have not read everything you have written Frank, your posts are in general quite long...
ppcStudios wrote:Pejo is correct in that you should always base your sprite animations on time - just as your game logic is based on time. This insures that animations are smooth from a designed framerate (as Frank has mentioned) upward.


I base my animations in time too, however I found that the frameskip option was easier, and norwy didn't ask for "the best solution" but for "a solution" :)

also, his game seems to be very advanced, so changing everything to timed animations would probably be quite painful...
My posts are long? I don't think they're long. Not at all. Quite the contrary actually. I think I write very short posts.

Some people would even say I need to post longer posts because what I post now are too short. Well duh, of course they are I say! But then again, I'm not a babbler. I mean, I'm not going to just post something for the sake of posting something, whether it's long or short makes no nevermind I think. Surely you agree with that?

And besides, what does post length have to do with anything? I mean, if a post is long or if a post is short, it's still a post, is it not? And in the end, is it not the post which matters, not the length of one's post? Oops, terrible pun!

This whole post length thing strikes me as just a distraction anyway, from the obvious point, which is does one post or does one not post at all? I mean, to be a member of a forum is to post, and to post is to be a member of a forum, anonymous posters notwithstanding of course. Surely you would agree with the inevitable logic of that conclusion, would you not?

I mean, God gave us keyboards, so she obviously expects that we will post. And 101 keys seems to indicate to me that posts should be of sufficient length as to require reading on the reader's part, or else they not be called reader but something more like non-reader! If we were using 56-key keyboards, then one could argue, on purely mathematical ground, that posts are meant to be sured as the variety of words which could be formed would not be of sufficient number to affect a large diversity of written language. Therefore, by divine design, posts are meant to be short. Alas, this is not the case, as almost twice as many keys as that leads to a geometrically larger number of possible non-verbal cues with which one can express themselves, therefore God is obviously decreeing that posts be long. This to me makes perfect sense (and most probably to no one else!)

To type is to be a geek, and certainly I am a geek, and therefore I must type, and typing leads to posts, and since posts lead to length, I think you can see the inevitable conclusion one must draw from this!

When one posts a short post, one does not post a long post. That is like saying a woodchuck does not chuck wood. A woodchuck which does not chuck wood can in fact not be called a woodchuck, but instead a squirrel. And since squirrels eat nuts, and since plainly *I* am nuts, I must be a squirrel! But more importantly, squirrels make longer posts than woodcucks, so the logic holds.

So in short, my posts are no longer than anyone else' posts, at least not to a a degree which would self-classify them as something other than posts.

And if you read all THAT , I clearly win the prize of most obnocious, sarcastic weirdo in these here forums! Clearly, without question, that is so :)
Hi everyone,

Finally I kept Kak's solution but i think that i don't have to change the update according to real time directly because GetActualFrameTime() is based to real time so it's the same thing. The other solution seems to give the same result.

fzammetti, I did not notice slowings down, the speed of the sprites seems to be the same in slower configuration. I have tested it on my first PPC (jornada HP565 @206MHz) it runs @ 60fps (it's just a little game with 2D sprite, nothing exceptional...). To test the conditions of a slower devices I've added a heavy loop: 50*FillRect() Method to decrease framerate to 20-25fps, the decrease of smoothness is clear but it's still playable. @40-50fps, the smoothness is still fluid and that's was exactly what I want: less smooth but still fast on lower devices.


fzammetti, continue to write so much, you express yourself in a clear way, that's very important to beginners like me ( We do not still understand everything, especially if you do not say everything).