Computer Science Canada [tutorial]Frame per Seconds |
Author: | AsianSensation [ Thu May 08, 2003 10:39 pm ] | ||||||||||
Post subject: | [tutorial]Frame per Seconds | ||||||||||
I decided that I will write this up, since it helped me out so much in my game. Btw, this is my first tutorial, so bear with me if I am not making things clear, also post anything that you don't get. The whole idea is about delaying the game when your game runs too fast. The Idea: if the time between refreshing of the screen is very little, then we put a delay at the end, after our drawscreen procedure (having this procedure will benefit you alot), if the time between refreshing is too long, then we don't delay, because that's the maximum rate your computer can draw.
Time_Frame is the last time your program refreshed Therefore, the frame-rate is the time between the last time your computer refreshed and the current time, all divided into 1000 because the time functions in turing all uses milliseconds.
Here, if the frame rate differ by more than one, usually through some actions taken by your program, we change the last frame rate to the current one, this way, the last frame rate is always updated.
Here we check to see the time between last time this part was accessed and the current time. If the difference between these are very small, then that means your program is running very fast, and the screen is being constantly refreshed, then we want to delay the game for a bit, so the game doesn't run so fast so the player will have time to react. But how long do we delay for? That is usually predetermined by the user under the Time_MaxFrameRate variable. Since Time_MinMilliSecBetwFrames = 1000 div Time_MaxFrameRate, then the bigger u set Time_MaxFrameRate, the smaller Time_MinMilliSecBetwFrames is. The smaller this is, the faster your program runs, because we are delaying an amount proportional to Time_MinMilliSecBetwFrames.
Hopefully this tutorial will help someone, I know it helped me, and I just want to share this with everyone else. If you have a question, suggestion, and especially improvements, please post. Catalyst has also writen a Module file to use FPS available - http://www.compsci.ca/v3/viewtopic.php?t=1016 |
Author: | Catalyst [ Thu May 08, 2003 10:41 pm ] |
Post subject: | |
nice tutorial i used a similar method in my asteroids game to cap the frame rate at 30fps (after i posted) |
Author: | Tony [ Thu May 08, 2003 10:46 pm ] |
Post subject: | |
awesome tutorial +50 Bits though I'd also like to see you use it in an example when its all put together. Doesnt have to be complex, just put all the code together and put something into draw procedure... then attach as a file |
Author: | Homer_simpson [ Thu May 08, 2003 10:47 pm ] | ||
Post subject: | |||
i dont get it... =( why would u need to know how many frames per second u use to make a game and even if u wanna know howmany frames u used in one second u could just simply put frame+=1 after each view.Update and in every second count the number of frames
|
Author: | Tony [ Thu May 08, 2003 10:51 pm ] |
Post subject: | |
the point is to balance out the game speed. Such as in Catalyst's asteroyds demo (he said he fixed it now)... at the beginning when you have all those asteroids game is running slow as there's a lot of calculations and drawings going on... but as you kill it, speed increaes 10 times... using frames per second to adjust your delay with balance the speed issue out so you'll have roughly the same game speed regardless of the situation. |
Author: | Homer_simpson [ Thu May 08, 2003 10:52 pm ] |
Post subject: | |
I see... good thinkin... is my code doing the same thing as asiansensaition is doing...or does it have to be changed to be able to help....? |
Author: | Tony [ Thu May 08, 2003 10:57 pm ] |
Post subject: | |
no, yours just measures the frame rate... his actually goes a step further and adjusts the delay. A little sujestion though... if speed gets really bad (i donno, more objects on speed then you expect) that delay value gets up to 0, it might be faster to use an if statment to bypass it as delay(0) still pauses... someone, please tell me. Whats faster to execute? a loop with delay(0) or a loop with "if 1-1 >0 then delay(0) end if" want to know what takes up more time. Delay(0) or an if statment. Let me know |
Author: | Homer_simpson [ Thu May 08, 2003 11:02 pm ] | ||||
Post subject: | |||||
i tested them both they both return 0 time
or maybe my computer is just too fast =Þ |
Author: | Tony [ Thu May 08, 2003 11:05 pm ] |
Post subject: | |
ofcourse they return 0... cuz clock measures time in seconds... you need to put them in a LOOP... and longer, the better... like for i:1..10,000 |
Author: | Homer_simpson [ Thu May 08, 2003 11:24 pm ] | ||||
Post subject: | |||||
actually clock returns in milliseconds
But delay(0) takes less time than the statement |
Author: | Tony [ Fri May 09, 2003 1:01 am ] |
Post subject: | |
good to know. Though an emply loop is twice as fast as with delay(0) thx homer |
Author: | hello [ Wed May 14, 2003 3:29 pm ] |
Post subject: | thx |
thx meng for ur help |
Author: | naoki [ Mon May 19, 2003 3:04 pm ] |
Post subject: | |
really useful for a program like mine that has procs and processes popping in and out *thumbs up* |
Author: | hello [ Thu May 22, 2003 7:33 pm ] |
Post subject: | |
im still confused because when i run it the is still some difference in speed can u expnad on the tutorial do i need to have the frames part in every proc or process that i have drawn something |
Author: | AsianSensation [ Thu May 22, 2003 7:43 pm ] |
Post subject: | |
k, I'll try to answer that question. Notice how the delay is structured. It is at the very end of the main loop. After each time the screen is refreshed, you will get bunch of values for all the Frame variables. So if you redraw your screen at some other instance other than refreshing it, for example, you want to clear the screen and put whatever is on the screen back on, then you called the drawscreen procedure, but that means that you got a whole new bunch of values for the Frame variables, and when you refresh the screen at the end, the time passed is not that much, so it delays the program more. You can fix this by setting a couple of flags, and only activate the Frame part of your drawscreen procedure if the flag is true, and only turn the flag true if you are trying to refresh the screen. |
Author: | gamer [ Wed Apr 21, 2004 5:36 pm ] |
Post subject: | |
reli nice tutorial, although alittle confusin but i'll just try it out myself |