Frame Rate
Author |
Message |
Flikerator
|
Posted: Thu Dec 22, 2005 9:00 am Post subject: Frame Rate |
|
|
How would I do frame rate in turing? Because my games go different speed on different computers (ie faster computers make it go really fast). |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Flikerator
|
Posted: Thu Dec 22, 2005 9:10 am Post subject: (No subject) |
|
|
No edit button? Also with multi-button when you hold down both (left and right) it equals 1. Anyway to make it equal 101? I want to have the ability to run with the right click and shoot whil STILL running :/ |
|
|
|
|
|
Albrecd
|
Posted: Thu Dec 22, 2005 9:13 am Post subject: (No subject) |
|
|
Frame Rate in Turing is basically the amount of time it takes to get from one View.Update to the next (providing you are using offscreenonly, which you should be). In order to change the Frame Rate, you must Add, Remove, Increase, or Decrease delays between the View.Updates.
Ex:
code: | View.Set ("offscreenonly")
var x, y := 10
loop
cls
Draw.FillBox (x, y, x + 10, y + 10, black)
x += 3
y += 3
View.Update
delay (50)
end loop |
|
|
|
|
|
|
[Gandalf]
|
Posted: Thu Dec 22, 2005 4:20 pm Post subject: (No subject) |
|
|
Albrecd, the problem with that is that the actual execution of the program will take various amounts of time on various computers. The delay then becomes inaccurate since execution is not instantaneous.
If you wish to output the framerate you must follow a structure like this:
code: | var lastFrameTime : int := Time.Elapsed
var wasteTimeBadly : int
loop
for i : 1 .. 5000
wasteTimeBadly := Rand.Int (1, 10) %Wastes time so that everyone won't get a million fps...
end for
cls
put round(1000 / (Time.Elapsed - lastFrameTime)) %This outputs the frame rate (fps)
lastFrameTime := Time.Elapsed
delay (30)
end loop |
Note: This also illustrates how delay() doesn't have a proper effect. I get 27 fps with this program on my computer, how much do you?
To control the frame rate with more stability, you must use Time.DelaySinceLast(). Lets say I wanted the above program to run at 5 fps evenly on everyone's computer, so that no one had a speed advantage...
code: | var lastFrameTime : int := Time.Elapsed
var wasteTimeBadly : int
loop
for i : 1 .. 5000
wasteTimeBadly := Rand.Int (1, 10)
end for
cls
put round(1000 / (Time.Elapsed - lastFrameTime)) %This outputs the frame rate (fps)
lastFrameTime := Time.Elapsed
Time.DelaySinceLast(200)
end loop |
Now on every computer you will recieve 5 frames per second, unless it can't run this program any faster in which case you should get a new computer . |
|
|
|
|
|
[Gandalf]
|
Posted: Thu Dec 22, 2005 4:30 pm Post subject: (No subject) |
|
|
Flikerator wrote: No edit button? Also with multi-button when you hold down both (left and right) it equals 1. Anyway to make it equal 101? I want to have the ability to run with the right click and shoot whil STILL running :/
No edit button? Complain here .
I'm not sure how you would do that, I've rarely used Mouse.ButtonChoose ("multibutton"), but you may want to experiment with adding the values of the mouse.button or mb or whatever you have it as when a new button is pressed. What I would really do is move it to a keyboard button. If it's a shooter game, make shoot be a mouse click, if it's a side scroller or overhead style make mouse click run. |
|
|
|
|
|
Flikerator
|
Posted: Thu Dec 22, 2005 9:57 pm Post subject: (No subject) |
|
|
Albrecd wrote: Frame Rate in Turing is basically the amount of time it takes to get from one View.Update to the next (providing you are using offscreenonly, which you should be). In order to change the Frame Rate, you must Add, Remove, Increase, or Decrease delays between the View.Updates.
Ex:
code: | View.Set ("offscreenonly")
var x, y := 10
loop
cls
Draw.FillBox (x, y, x + 10, y + 10, black)
x += 3
y += 3
View.Update
delay (50)
end loop |
Yah I already had a delay in. The problem is it changes on everyones computer depending on how fast you go.
[Gandalf] wrote: Albrecd, the problem with that is that the actual execution of the program will take various amounts of time on various computers. The delay then becomes inaccurate since execution is not instantaneous.
If you wish to output the framerate you must follow a structure like this:
code: | var lastFrameTime : int := Time.Elapsed
var wasteTimeBadly : int
loop
for i : 1 .. 5000
wasteTimeBadly := Rand.Int (1, 10) %Wastes time so that everyone won't get a million fps...
end for
cls
put round(1000 / (Time.Elapsed - lastFrameTime)) %This outputs the frame rate (fps)
lastFrameTime := Time.Elapsed
delay (30)
end loop |
Note: This also illustrates how delay() doesn't have a proper effect. I get 27 fps with this program on my computer, how much do you?
To control the frame rate with more stability, you must use Time.DelaySinceLast(). Lets say I wanted the above program to run at 5 fps evenly on everyone's computer, so that no one had a speed advantage...
code: | var lastFrameTime : int := Time.Elapsed
var wasteTimeBadly : int
loop
for i : 1 .. 5000
wasteTimeBadly := Rand.Int (1, 10)
end for
cls
put round(1000 / (Time.Elapsed - lastFrameTime)) %This outputs the frame rate (fps)
lastFrameTime := Time.Elapsed
Time.DelaySinceLast(200)
end loop |
Now on every computer you will recieve 5 frames per second, unless it can't run this program any faster in which case you should get a new computer .
Thats what I was looking for! Is this only available in Turing 4.0.5? Thats what I have on my home pc (what im using now). The school has 4.0.4 on all the machines (some have 4.0.5, but not the one I use).
Im pretty sure I know whats happening, but I just need one example ^_^
I just wrote this small (crap) program to test slope (for firing at targets for the shooter im making). It goes from x1,y1 (your character) to x2,x2 (your mouse). You can change the values of them all and it moves. Its really poorly done because I just need to test some things.
Could you insert the framrate into it for me? It would really help me out! =D
code: |
View.Set ("offscreenonly")
var x1, x : real := 50
var x2 : real := 250
var y1, y : real := 50
var y2 : real := 250
var xslope : real := (x2 - x1) / 250
var yslope : real := (y2 - y1) / 250
for i : 1 .. 400
x += xslope
y += yslope
Draw.FillOval (round (x), round (y), 3, 3, red)
Draw.FillOval (round (x1), round (y1), 10, 10, red)
Draw.FillOval (round (x2), round (y2), 10, 10, red)
View.Update
delay (5)
cls
end for
|
EDIT (Before I hit submit I tried some things) -
code: | var lastFrameTime : int := Time.Elapsed
var wasteTimeBadly : int
loop
cls
put round (1000 / (Time.Elapsed - lastFrameTime)) %This outputs the frame rate (fps)
lastFrameTime := Time.Elapsed
Time.DelaySinceLast (200)
end loop
|
What happens when you have a program thats running for a few hours? Wont it exceed the integer value? Guess you could use a real then ^_^
[Gandalf] wrote: Flikerator wrote: No edit button? Also with multi-button when you hold down both (left and right) it equals 1. Anyway to make it equal 101? I want to have the ability to run with the right click and shoot whil STILL running :/
No edit button? Complain here .
I'm not sure how you would do that, I've rarely used Mouse.ButtonChoose ("multibutton"), but you may want to experiment with adding the values of the mouse.button or mb or whatever you have it as when a new button is pressed. What I would really do is move it to a keyboard button. If it's a shooter game, make shoot be a mouse click, if it's a side scroller or overhead style make mouse click run.
For Mouse.Where with the multibutton, it only gives you the one value. Either 1, 10, or 100. If I hold all three buttons down the value returned will be 1. If I hold the middle and the right down I will have a value of 10 (instead of 110, which would be waaay better =P). Ill just use da keyboard =P |
|
|
|
|
|
md
|
Posted: Thu Dec 22, 2005 10:50 pm Post subject: (No subject) |
|
|
** note I haven't read all the posts
One way of trying to limit frame rates in a computer independent way is by dynamically changing the delay; for exampls in sudo code:
code: |
function RunFrame()
{
int time, run_time, delay_time
while( true)
{
time = GetTime()
DoPhysics()
UpdatePositions()
DrawStuff()
Delay(delay_time)
run_time = GetTime() - time
delay = (TimePerSecond() / GOAL_FPS) - run_time
}
}
|
That code times one frame, figures out what the delay should be for the next frame. It's very quick to changes in execution time for things.
If you want the rate at which things change to be more smooth (good idea usually) instead use this for the delay calculations
code: |
if delay + run_time > (TimePerSecond() / GOAL_FPS)
delay--
else if delay + run_time < (TimePerSecond() / GOAL_FPS)
delay++
|
This code makes the transitions smoother; so if one frame takes a really short time and hte next takes a huge time so long as the frames before were relatively stable it won't choke. |
|
|
|
|
|
|
|