Hurry you only have 16 ms to render everything
|
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,
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. At 60 FPS it’s even worse
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 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.
Each loop could take up to 10ms, so I’m wasting 1ms for each loop, bringing the sum up to 100ms per second. At just 60 FPS, let’s say I’m drawing everything at 9ms too, but I could take up to 16.6
You can make your application sleep for almost half of a second and the game would run exactly the same 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%. Well I hope this wasn’t too boring =)
|


In javascript, there is a function you can call that makes the browser handle the frame synchronization called requestAnimationFrame. It does some neat things like slow the game down when you are not in the tab (saving battery life of a laptop!) and other great freatures like that. Perhaps there are ways to do this on other platforms? I know that enabling v-sync can cause a similar effect depending on how you set up your rendering pipeline.
google did a presentation on the issue, but I can’t find the youtube video. However you can read more about requestAnimationFrame here: http://www.nczonline.net/blog/2011/05/03/better-javascript-animations-with-requestanimationframe/
Just say no to busywaiting!