how to control frames per second (fps-rate)

Blitz3D Forums/Blitz3D Beginners Area/how to control frames per second (fps-rate)

darkgon(Posted 2008) [#1]
I am familiar with darkbasic, and they have a Sync command, and sync rate command that can control the frame per second on a game. I am working on a scroller game in blitz3d (using 2d graphics) but it is running way to fast, and short of build some kind of while loop that counts miliseconds, i am trying to determine if there is a global command in blitz that can force a program to execute without going above a particular frame rate.

thanks

- sam


stayne(Posted 2008) [#2]
Here's one way to do it...

http://www.blitzbasic.com/codearcs/codearcs.php?code=9


darkgon(Posted 2008) [#3]
that is using graphics3d

if my game is only using 2d bitmaps/etc the update world etc and those commands would not be used? there stictly 3rd, aren't they?


KillerX(Posted 2008) [#4]
global timer
const fps = 30; number of frames per second
Repeat
;code goes here
If Millisecs() - Timer > 1000/fps
Timer = millisecs()
Forever

This should work.


Pongo(Posted 2008) [#5]
This is how I do it.

Graphics 640,480,0,2
SetBuffer BackBuffer()
HidePointer()

FrameRateMax=CreateTimer(30) ;create a timer to cap the FPS

While Not KeyHit(1)
	WaitTimer(frameRateMax) ; wait to make sure the framerate does not exceed the max framerate
	Rect MouseX(),MouseY(),10,10
	Flip
	Cls
Wend
End


What you do here is simply set the FrameRateMax timer to the frame rate you want things capped at. This will only cap the speed of a fast system, so a slow system will play slow, but it is a very easy way to lock frame rates.


andy_mc(Posted 2008) [#6]
Oh for god's sake, I'll answer it.

timer = createtimer(30)

while not keydown(1)
;main loop
waittimer timer
wend
end

the timer ticks once every 30th of a second, so the game will not run faster than 30 frames per second, change this value as you see fit, remebering not to set it higher than 60 as this is most people screen refresh rate.


Stevie G(Posted 2008) [#7]
@Andy, the post above you says exactly the same?


Fernhout(Posted 2010) [#8]
guy this is not right.
I can not tell what is the right way. cause i am also looking for it. But in you example you forget one thing.

If you code is langer the waittimer () take always the time you give. but it il not take any consideration how long you code is.
That means that you frame rate wil go down if the code wil be more.


Gabriel(Posted 2010) [#9]
guy this is not right.

You're correct. Using a timer is a very bad way to do this.

I can not tell what is the right way.

If you're working in 3D, the link Stayne provided to James' entry in the codearchives is a very good way to do it.