
-----------------------------------
AsianSensation
Thu May 08, 2003 10:39 pm

[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.

Catalyst has also writen a Module file to use FPS available - http://www.compsci.ca/v3/viewtopic.php?t=1016

-----------------------------------
Catalyst
Thu May 08, 2003 10:41 pm


-----------------------------------
nice tutorial :D 
i used a similar method in my asteroids game to cap the frame rate at 30fps (after i posted)

-----------------------------------
Tony
Thu May 08, 2003 10:46 pm


-----------------------------------
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

-----------------------------------
Homer_simpson
Thu May 08, 2003 10:47 pm


-----------------------------------
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

function timedelay (var r : int, t : int) : boolean
    var tt : int
    clock (tt)
    if r = 0 then
        clock (r)
        result false
    end if
    if tt - r >= t then
        r := tt
        result true
    else
        result false
    end if
end timedelay
var time1, frames, i := 0

View.Set ("offscreenonly")
loop
    drawfillarc (320, 200, 150, 150, i, i + 20, i div 10)
    View.Update
    frames += 1
    i += 1
    if timedelay (time1, 1000) then
        locate (1, 1)
        put frames, " fps"
        View.Update
        frames := 1
        i := 1
    end if
end loop


-----------------------------------
Tony
Thu May 08, 2003 10:51 pm


-----------------------------------
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.

-----------------------------------
Homer_simpson
Thu May 08, 2003 10:52 pm


-----------------------------------
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....?

-----------------------------------
Tony
Thu May 08, 2003 10:57 pm


-----------------------------------
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

-----------------------------------
Homer_simpson
Thu May 08, 2003 11:02 pm


-----------------------------------
i tested them both they both return 0 time


var i, i2 : int
clock (i)
if 1 - 1 > 0 then
    delay (0)
end if
clock (i2)
put i2 - i



var i, i2 : int
clock (i)
delay (0)
clock (i2)
put i2 - i

or maybe my computer is just too fast =Ãž

-----------------------------------
Tony
Thu May 08, 2003 11:05 pm


-----------------------------------
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

-----------------------------------
Homer_simpson
Thu May 08, 2003 11:24 pm


-----------------------------------
actually clock returns in milliseconds
var i, i2 : int
clock (i)
for ii : 1 .. 1000000
    %if 1 - 1 > 0 then
    delay (0)
    %end if
end for
clock (i2)
put i2 - i
var i, i2 : int
clock (i)
for ii : 1 .. 1000000
    if 1 - 1 > 0 then
    delay (0)
    end if
end for
clock (i2)
put i2 - i

But delay(0) takes less time than the statement

-----------------------------------
Tony
Fri May 09, 2003 1:01 am


-----------------------------------
good to know. Though an emply loop is twice as fast as with delay(0)  :o 

thx homer

-----------------------------------
hello
Wed May 14, 2003 3:29 pm

thx
-----------------------------------
thx meng for ur help

-----------------------------------
naoki
Mon May 19, 2003 3:04 pm


-----------------------------------
really useful for a program like mine that has procs and processes popping in and out

*thumbs up*

-----------------------------------
hello
Thu May 22, 2003 7:33 pm


-----------------------------------
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

-----------------------------------
AsianSensation
Thu May 22, 2003 7:43 pm


-----------------------------------
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.

-----------------------------------
gamer
Wed Apr 21, 2004 5:36 pm


-----------------------------------
reli nice tutorial, although alittle confusin :cry: but i'll just try it out myself
