I might be missing something, but... how are you guys doing (assuming you are) frame rate limiting?
Specifically, when developing on the desktop I don't want my app running as fast as possible, I want some known maximum. This would also be true in many cases on a PPC, but on the desktop for sure.
Any comments? Thanks!
This subject has been covered a countless number of times in these forums. Just do a search?
Ok, you officially have permission to slap the crap out of me... I DID do a search originally, but for whatever reason didn't find a suitable thread. I just tried after seeing this and guess what?? FOUND A THREAD!
Worse...
ONE I STARTED!!
Argh.
I had the same problem when I tried to choose a method for frame limiting. But my problem was the opposite one I found too many ways to make it.
So I mixed all solutions up... and here is my solution that I use in my !PYROMANIA! project
First of all what it does? It just limits the frame rate to desired one + reduce the power expense (hope so)
And it is fast sensible system - if something slows down it reduces FPS in the current loop
(remember reader, I'm not so expirienced programmer as fzammetti or Thierry, for example, so don't judge me
rough)
- Code:
//in Globals.cpp
ui16 G_min_time_per_frame; //This time has to limit FPS
uint32_t G_last_clock; //previous end of loop clock
ui16 G_fps; //FPS from previous loop
ui16 G_msec; //Milliseconds from previous loop
//in GameInit.cpp
bool Pyromania::GameInit()
{
G_min_time_per_frame = 1000/50; // For 50 fps
G_last_clock = PocketPC::GetTickCount();
G_fps = 50; //for 50 fps
G_msec = 1000/50; // For 50 fps
return true;
}
//in MainLoop.cpp
void Pyromania::GameLoop ()
{
pDisplay = GetDisplay();
//Draw the sun, blue skies... green hills... and hordes of monsters
while(PocketPC::GetTickCount() - G_last_clock < G_min_time_per_frame)
{
Sleep(1);
//It is the main trick...
//To sleep until the right time comes...
//No, no its not a fairy-tail approach )))
}
G_msec = (ui16)(PocketPC::GetTickCount() - G_last_clock);// How many ms I used to blit this time?
if(G_msec == 0){G_msec = 1;}// Prevent divizion by zero
G_fps = 1000/G_msec;//Updating fps
if(G_fps == 0){G_fps = 1;}// Prevent divizion by zero
G_last_clock = PocketPC::GetTickCount();
pDisplay->Update();
}
Please add your comments below

Does anybody find it useful?
Is it a good way to limit FPS?
What do you think, fzammetti?
What is the sleep(1) cycle for?
Just how it workes to me. I'm not usually use frame limiting, but I've made it because phisics is simplier with fixed time delta.
- Code:
float dt = (PocketPC::GetTickCount() - g_time)/1000.0f;
g_time = PocketPC::GetTickCount();
if (dt < 0.046f)
Sleep(1000.0f*(0.046f-dt));
PS: Yes, it could be faster with fixed point numbers

.
I took the simpler approach...
In the header:
uint32_t lastTime;
uint32_t ticksPerFrame;
In GameInit():
ticksPerFrame = 1000 / 30; // 30 FPS
lastTime = PocketPC::GetTickCount();
In GameLoop() at the end:
while(PocketPC::GetTickCount() - lastTime < ticksPerFrame);
lastTime = PocketPC::GetTickCount();
Thank you, works well enough for me!
The assumption of course is that at no time will a frame take more than 33ms to render, but assuming that is the case (and frankly it was in Invasion: Trivia on my old stock Casio E-125!), it's fine. At least for what I'm doing now, the KISS approach does me just fine

Thank you, jaguard, for your improvement. I'll try it.
fzammetti, I used your code with pure while cycle, when I started my project, thank you for posting it in some previous thread. But then I added sleep into your while cycle (yes I also read about this in some previous thread) and 100% time CPU usage dropped down to about 30% on my PC.
I don't know is it important for PocketPC power saving? I had not done any tests.
P.S. and with sleep(1) it still could be a pretty tender K.I.S.S.
What do you think about powersaving?
My suspicion is that the power savings wouldn't be noticeable. Especially when your frame render times are close to the limit anyway, there's not going to be a difference big enough to matter.
Though, I could be wrong

I'm not certain how much CPU time really matters on modern devices... I'd be willing to bet though that the vast majority of power usage still comes from the things it always has... screen, memory cards, etc. I'd bet the CPU accounts for such a small percentage that it wouldn't make any tangible difference.
But, I can't think of a BAD side to the sleep, and if no one else can either then I'd say cool why not do it and get whatever benefit there might be, regardless of how minor it may or may not be?

The
only reason to limit FPS is to save battery power. If you're not saving battery power with your implementation of FPS limiting, then it's pointless.
If you're using FPS limiting for time based animation and movement, then you need to rework your design. Even if you limit the FPS, slower deviced may run your game at a lower FPS, especially if the user has other things running in the background.
So in short, use a FPS limiting implemention that'll save battery power. Just my 2 cents on that subject.

I don't agree with that. This discussion has been done to death frankly, but still...
The one true benefit of a frame-limiting solution is that you know that your game will run the same on all devices. There is nothing I hate more than a game that runs smooth as silk on one device but runs like a pile of shit on another. Sure, the SPEED is identical, all the TIMING is the same, but the SMOOTHNESS is not. Any time I see a game that gets choppy I walk away from it in a hurry. Frame limiting, WHEN DONE RIGHT, avoids this.
I'd much rather determine the minimum frame rate my game needs, and in fact DESIGN the game to run at some tiven rate on ANY device, and go from there. The trick is to pick a baseline device that no device you will support will perform worse than, and use that as your guide.
I always use my trusty E-125 for that. For those that haven't been around as long, that's a 150MHz MIPS device running the original PocketPC OS. Slow as balls compared to ANY modern device. So, if my game runs consistently at 30FPS on that, I know I'm golden.
Of course, there will always be situations you can't design for... Someone that underclocks their 600MHz PocketPC to 200MHz to save battery is going to make life difficult for any game. I contend you shouldn't be designing for such situations. But, if you target an E-125, or even an original 200MHz iPaq would be safe these days, you will be fine.
Now, to be fair, some games you just CAN'T do this with. I would NEVER use this technique on a 3D first-person shooter. Just wouldn't work right. Many other types of games probably wouldn't work well either. But I dare say that most 2D games probably would be fine.
Aside from the smoothness benefit, you also greatly simplify your code. Well, maybe not *greatly*, but somewhat anyway.
As with most things in development, to say one thing is good or bad, right or wrong in absolute terms is usually a bad thing to do. If you understand the implications and make your decisions accordingly, even the "bad" architectural decisions can be good ones in some cases. Frame limiting is one example of this.
I personally disagree with everything you mentioned, and I'll leave it at that.

One thing about not sleeping... On the x30 I've got - the button presses tend to be sluggish if you don't sleep a little.
I use a combination of frame limiting with time correction - since even with limiting, the frametime is not exactly the target. Plus, if the framerate drops below the target - the game doesn't slow down.
Has anyone tried to estimate the benefit of a limited frame rate on battery life? I would do it myself, but I don't have a device available right now. It seems like running a simple blit demo run until the battery dies at different avg. cpu loads would give you some idea.
fast_rx wrote:I use a combination of frame limiting with time correction - since even with limiting, the frametime is not exactly the target. Plus, if the framerate drops below the target - the game doesn't slow down.
I also use this approach thats why I've added G_fps and G_msec to my code example.
CurrentPos += MovementPerSec/G_fps;
or something like this for my TimeTriggers:
CurrentTime += G_msec;
So any movement is OK with any fps, but if it is more than, say, 50 it's just becomes needless to get some more frames. Thats why I added fps limiting (just explaining my case )
But in my main game menu I should get the exact 30 fps.
And I get it even on my old E-125 device
Goal chooses the way of achievement

CurrentPos += MovementPerSec/G_fps;
If you're doing this a lot, it would be best to go ahead and calc 1.0/G_fps, since divisions are the slowest.
Thank you! I had now idea about that.
Is it because of one instead of two conversion to floating point types? Because I use int/int divizion.
I assumed you were using floats. In my game, the roundoff of using just ints would be too much, so I use either floats or fixed point floats.
fast_rx wrote:I assumed you were using floats.
In my isometrical game I use santimeter precise, it's about 100 santimeters per 15pix for x, any y and 8 pix for z. So rounding would be OK even if I run at 50 fps!
P.S. In case of making lejjo I understand why you use floats
