I've been a game developer since 2005. After working 3 years in the game industry I've decided to go indie and make games. The first game was on June 2010 David is a DZone MVB and is not an employee of DZone and has posted 19 posts at DZone. You can read more from them at their website. View Full User Profile

Hurry, You Only Have 16ms to Render Everything!

08.09.2011
| 4705 views |
  • submit to reddit

So I’ve been working on my new game and I’m doing some tweaks here and there on the engine.

Mainly with the rendering section, trying to squeeze as much as possible, thus maintaining an acceptable framerate. It’s not like I’m doing a heavily graphic game but I like to know that It can be used even on slower pcs and that I’m not using more cycles than necessary.

This past week and after reading a bunch of stuff and keynotes from John Carmack about Rage (60FPS ftw) I decided to measure my own stuff.

So let’s break this down, your framerate or FPS is the number of images you draw per second, not just that, you have to animate stuff, play sound, collisions, etc.

Most 3D games nowadays target 30 FPS, it’s the minimum acceptable before you start noticing slowdowns or not so smooth animations, for 2D games it’s a pretty realistic objective to target 60FPS.

Let say you want to target 30 FPS,

  1 second = 1000 milliseconds
1000/30 = ~33.3 milliseconds

That means that for 30 FPS you have to calculate, animate, do collision and draw the scene in under 33.3 milliseconds. And do it again 30 more times.
Sounds hard doesn’t it?

At 60 FPS it’s even worse

  1000/60 = ~16.6 milliseconds

16.6 milliseconds to do a full loop on your engine. Actually with modern technology it’s not an impressive accomplishment depending on what you are doing but it’s a good practice to
look at these numbers, consider them while you code.

Now I’ve been making some tests on my own engine and I placed 2000 sprites each with a sprite animation and an A* pathfinding (2000 paths can be cached at a time) rendering at 100 FPS.
It runs well and each loops takes 9ms.

   1000/100 = 10ms

Each loop could take up to 10ms, so I’m wasting 1ms for each loop, bringing the sum up to 100ms per second.
When I say waste it doesn’t mean you could be doing something else but you could make you game a bit more CPU friendly make making it sleep that extra millisecond each cycle.
This will actually make you game use less of a processor and you can even check it on the Windows Task Manager.

At just 60 FPS, let’s say I’m drawing everything at 9ms too, but I could take up to 16.6

   16 - 9 = 7ms
7 * 60 (FPS) = 420ms

You can make your application sleep for almost half of a second and the game would run exactly the same
Isn’t that something?

I’ve made a change on my pump to accommodate this and tested on Vizati, now most of the time it uses 12% of the CPU with bursts of 32%.
Of course by measuring this you can also do cooler stuff like, let’s say you walk by a heavy area, or need to do some calculations, by lowering the intended FPS and drawing less times in that second you can get the other calculations done faster and return to a normal FPS much faster.

Well I hope this wasn’t too boring =)

EDIT:
After talking to some other devs there are mixed feelings about the benefits of putting your application to sleep those extra milliseconds. One thing that’s worth mentioning is that when you put your application to sleep you only guarantee that it will be idle for a minimum of let’s say 5ms, you may only regain control after 7ms, that’s really up to the OS.
So my suggestion is, leave it as an option in the game, CPU throttling or something. Each user may test and see if it benefits, and in a couple of years it may in fact make some difference with to futuristic 30 core cpu where you don’t need to be using 100% of it. Starcraft 1 got a patch to do just that some time ago.

References
Published at DZone with permission of David Amador, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jason Marshall replied on Tue, 2011/08/09 - 3:15pm

I haven't done animation is about forever, but one thing we did with the extra time is start pre-rendering the next frame.  Since we knew a priori what the next frame was, it helped hide other interruptions, like multitasking or GC.   

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.