PocketMatrix forums

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

Limiting frame rate

Is there a way to limit the FPS in PocketFrog? Or, perhaps there is a better way to handle things...

What I'm thinking is that on my stock Casio E-125, which I'm considering a "baseline" in terms of performance, if I'm getting 50 FPS on that ofr instance, I would expect more current devices to be considerably higher than that. I'd like my game to run on all devices at a constant frame rate using what the E-125 does full-tilt as the rate.

So, how's the best way to lock in a particular maximum frame rate? Thanks!
The PocketFrog framework doesn't provide any direct way to control the frame rate. The reason for that is that I feel there is no "best" way to do it. What works for one game might not work for another one.

This is not to say that there is no way to do it. You could use threads, although I feel this is overkill.

I would advise using PocketFrog's function "PocketPC::GetTickCount()". This basically returns the value of an internal timer with a 1 ms resolution. Using this timer, you can limit the frame rate. For example, if you want to limit the frame rate to 50 fps, you would do something like this:


Code:

void MyGame::GameLoop()
{
// Limit the frame rate to "fps"
uint32_t fps = 50;
uint32_t tickPerFrame = 1000 / fps;

uint32_t timeNow = PocketPC::GetTickCount();

if (timeNow - m_lastTime < tickPerFrame)
return;
else
m_lastTime = timeNow;

// Game logic goes here
//...
}

So this little piece of code at the beginning of GameLoop() will control the frame rate. It is a simple and efficient way to do it. Of course, don't forget to add the "m_lastTime" variable to your class and initialize it in the constructor.

Let me know how this works for you.
That looks perfect, and is the line I was thinking along myself. I'll implement that and let you know how it goes. Perhaps this is a reasonable addition to the framework down the road? I understand what you mean about one method not working for everyone, and in many cases people may not want to use it anyway, but just a thought anyway.
Well I don't think this will ever be added to the framework. When deciding if I want to add a feature or not, I ask myself if it fits the goals of PocketFrog. If it abstract the device and is useful to everyone, I add it... If it's not, I don't.

The line is not always easy to draw... Features such as display abstraction, rasterizers and surfaces make sense: when hardware acceleration comes, PF will use it. The framework also make sense: you don't want to go through the differences between WinCE 3.0 and PC versions. Rotated blit was a though call... That's almost a 3D rasterizer. But, "fps barrier" is definitely not something that will be useful to everyone and it can be built quite easyly using the existing features.

Nothing prevents you from having your own version of the framework...
Code:

class FzammettiGame : public Game
{
...
};

=)
Yep, your right. I actually have my own "FrogExtensions" library of things I've added especially for my project. Most of it probably wouldn't be of use to anyone else, so your point is well-taken. Thanks!
Actually I made a typical error in my code, you should limit the frame rate just before calling GetDisplay()->Update(), not at the beginning of GameLoop().

You should then loop until the required time has elapsed, after which you call Diplay::Update().
Could you post an example of the correct way - I'm still kinda new to this stuff and don't want to mess it up!

thanks
this seems to work - is this what you meant?

Code:

while (PocketPC::GetTickCount() - m_lastTime < m_tickPerFrame) {}

// Update screen
display->Update();

Yep... That's it! This way, you wait at the right moment and make sure your Update() is at a fixed rate.
I tryied both codes and the first works better on my Casio EM-500, the flow of the game is more smooth
HTK, can you post the exact code you used? I've been more or less using a do-nothing loop right before the update call, I'd like to know how you implemented it. Thanks!
Hi fzammetti, take a look at the second post here, declare the int m_lastTime and then on the game init you initialize it as
m_lastTime = PocketPC::GetTickCount();

then on the game loop start you just use the code as shown
Code:

void MyGame::GameLoop()
{
uint32_t fps = 50; //desired framerate
uint32_t tickPerFrame = 1000 / fps;

uint32_t timeNow = PocketPC::GetTickCount();

if (timeNow - m_lastTime < tickPerFrame)
return;
else
m_lastTime = timeNow;

// Game logic goes here ( your original gameloop )
//...
}



with this, if the time for the frame to show is too short ( less than what you want from the FPS you choose ) then the game loop will exit until the time is right to process the next frame.

should be easy for you to implement this
Thanks HTK, I figured it was that simple, just wanted to be sure. What I'm using seems to work fine, but I might try this just the same.

One thing I did differently that you might consider as well... the fps and tickPerFrame variables I declare as class members outside GameLoop(). It might be a trivial amount, but it seemed more efficient to not do the vision and allocation and such with each function call.

Thanks again!
That´s nice, you optimized the current code
This is a must for a more serious project, the other unoptimized version is cool for the beginners :)
one thing I noticed is that the code i´m using ( the first one kzinti described ) does not work well on the emulator, getting around 35 fps when I choose 50... anyway, the important thing is that it works on the device itself :D

Would be nice if people start posting their ideas on fps limit here
Like someone (I think Kzinti) posted here earlier although I can't seem to find the note anymore, the timing should be done last in the GameLoop. That way you get a more robust timer. I'll post the code once again:
Code:
//This stuff goes in your class:
uint32_t m_LastTime;
uint32_t m_TickPerFrame;

// This stuff goes in your GameInit() or in the constructor (whichever you prefer)
m_TickPerFrame = 1000/50; // For 50 fps
m_LastTime = PocketPC::GetTickCount();

// This stuff goes in the back of GameLoop()
while(PocketPC::GetTickCount() - m_LastTime < m_TickPerFrame);
// Do nothing
m_LastTime = PocketPC::GetTickCount();

display->Update();


NOTE! that the while-loop ends without running any code. If you like to add more clearity to your code, add an empty code section (those curly braces).

If you do the time check at the beginning of GameLoop and your gameloop has a lot of stuff to do, you won't get the correct timing.

On the other hand, blocking the GameLoop will also block the Window Message loop that handles input like the stylus, so don't set the fps to low (but why would you).
I tryied this code, but I got a better result with the other one
another method (dont ask for an implimentation, i'm too tired at the moment :) is to tie the movement speed to the time instead of limiting fps

So instead of moving one pixel per frame, you move a certian number of pixels per second. Not a great method on devices with limited speed as it take a little more power, but it tends to make smoother looking animation, and the faster the host device is, the smoother it looks.
Probably a bad idea because of the extra overhead and extra setup, but a nice thought :)
This technique is great and robust, but I don´t know if it is worthy for pocket pcs, it will use a lot of floating numbers, and that´s a problem for pocket pcs, too much demanding as they don´t have hardware support for float. :?