Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 dynamic frames per second adjustment
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
frank26080115




PostPosted: Wed Dec 27, 2006 8:07 pm   Post subject: dynamic frames per second adjustment

this demo uses "Time.Elapsed" to calculate the real fps and compares it with the desired fps and adjusts the delay before a clearing of the screen accordingly. the number of dots drawn will be raised and lowered to generate different amount of lag. if the delay between frames is 0 or lower, it means the computer and the adjustment can no longer keep up, so i made sure that "framesdelay" can't go below -10 (or else, it actually makes it go too fast after a recovery)

i think this will be useful for people making games like shoobyman's lord of the ring game and such

if you get rid of the useless stuff (for demo only stuff) you can add this into your program

code:

% variables
%%% important part %%%
var framespersecond : int := 20 % change me, this is the desired frames per second
var framesdelay : int := 1000 div framespersecond
var timer1, timer2, timer3, fps : int := 0
%%% / important part %%%

var numofdots : int := 10
var upordown : int := 1

var xpos : int := 0

setscreen ("offscreenonly") % flicker free

% main loop
loop
    % draws some random dots
    for i : 1 .. numofdots
        Draw.Dot (Rand.Int (1, 300), Rand.Int (1, 300), Rand.Int (1, 100))
    end for
    if upordown = 1 then
        numofdots += 60
    elsif upordown = 2 then
        numofdots -= 60
    end if
    if numofdots > 20000 or fps < 6 then
        upordown := 2
    elsif numofdots < 70 then
        upordown := 1
    end if
    Draw.FillOval (xpos, 150, 20, 20, brightred)
    xpos += 10
    if xpos > 300 then
        xpos := 0
    end if
    %%% important part %%%
    % finds actual frames per second
    timer2 := timer1
    timer1 := Time.Elapsed
    timer3 := timer1 - timer2
    fps := 1000 div timer3
    % calculates lag and adjust rate accordingly
    if fps > framespersecond then
        framesdelay += 2
    elsif fps < framespersecond and framesdelay > - 10 then
        framesdelay -= 2
    end if
    Text.Locate (1, 1)
    put "number of dots: ", numofdots
    put "current frames per second: ", fps
    put "current frame delay (in ms): ", framesdelay
    if framesdelay < 1 then
        put "LAG!!!"
    end if
    if upordown = 1 then
        put "slower..."
    else
        put "faster..."
    end if
    delay (framesdelay) % delay
    View.Update % displays all graphics
    cls % clears the screen
    %%% / important part %%%
end loop
Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Thu Dec 28, 2006 1:25 am   Post subject: (No subject)

you can do this simply by using Time.DelaySinceLast
frank26080115




PostPosted: Thu Dec 28, 2006 2:49 am   Post subject: (No subject)

i believe you are living under the impression that my school has money, sure it can afford useless $3000 pan and tilt cameras to see who started a fire, but... this is what i had to work with lol (actually we have the new one but its not available for download)

Posted Image, might have been reduced in size. Click Image to view fullscreen.
(i still press f10 a lot)

damn it i can't even use math.distance so most of the codes you guys post won't work on my computer

so use Time.DelaySinceLast if you can
BenLi




PostPosted: Thu Dec 28, 2006 11:21 am   Post subject: (No subject)

code:

fcn Distance (x, y, x2, y2 : int) : real
    result sqrt ((x2 - x ** 2 +  (y2 - y) ** 2))
end Distance



... Merry Christmas
shoobyman




PostPosted: Fri Dec 29, 2006 2:42 pm   Post subject: (No subject)

that is a cool idea.

but...

our school version does have Time.DelaySinceLast. if your home one doesn't have it, i think the school version updated theirs so you can get it again. All i know is my home version has it and so does the schools.

cool program though.
ericfourfour




PostPosted: Fri Dec 29, 2006 4:20 pm   Post subject: (No subject)

How about frame rate independent gameplay. The code is in c++ but the tutorial is pretty much universal to all languages.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: