Frame Limit

BlitzMax Forums/BlitzMax Beginners Area/Frame Limit

Eric(Posted 2006) [#1]
I am trying to do Frame Limiting code... This is what I came up with. Any help would be appreciated.

I use timer ticks if there is more than one per frame I am skipping the drawing phase.

Regards,
Eric


SuperStrict 
Global FrameTimer:TTimer=CreateTimer(60)
Global Skipped:Float 
Global Drawn:Float 
Graphics 640,480
Repeat

	Local Ticks:Int=Frametimer.Wait()
	
	If KeyDown(Key_Space)
		Delay (14)
	End If 
	'Update Logic Every Frame
	If Ticks<=1
		Cls
                'Update Draw As much as possible
                DrawText "Draw Frame",320,0
		DrawText "Frames Skipped"+Skipped,320,15
		DrawText Float(Skipped/(Drawn+Skipped))+"% Skipped",320,75
		Drawn:+1
		Flip 0
	Else
		Skipped:+1
	End If 
Until KeyHit(Key_Escape) Or AppTerminate()



Eric(Posted 2006) [#2]
Well...This is working for crap.

How do people get such smooth movement in their games?


bradford6(Posted 2006) [#3]
SuperStrict 
Global FrameTimer:TTimer=CreateTimer(60)
Global old_tick:Int , new_tick:Int
Global counter:Int
Graphics 640,480

Repeat

'independent of tick

counter:+1

new_tick = TimerTicks(Frametimer)
If new_tick>old_tick
	Cls
	DrawText "Ticks:"+new_tick,100,100
	DrawText "Counter:"+counter,100,116
	Flip
	old_tick = new_tick
EndIf

Until KeyHit(Key_Escape) Or AppTerminate()




Eric(Posted 2006) [#4]
Bradford,

This is what you are using in your game?

Eric


bradford6(Posted 2006) [#5]
similar. this is just another way of interleaving processes between a timed render

you can set logic timers that fire off in a staggered way.

you might not want to update AI or physics every frame drawn or if you are doing networking you could time packets


Grey Alien(Posted 2006) [#6]
just don't use timers. Track the last time and compare to the current time to work out a different (your delta) then move everything multiplied by this amount and then draw a frame. That's simple delta timing. Fixed rate logic is similar but allows you to run logic at a high FPS.


bradford6(Posted 2006) [#7]
Grey is right. I don't know what i was thinking on the earlier post. Using a Delta might be a better way to do this. here is a simple one i put together




Eric(Posted 2006) [#8]
I Just tried the above code and I still get "Hiccups" in the movement. Do you? I have to stop coding my game until I figure out how to get smooth running graphics without the hiccups in movement.


Grey Alien(Posted 2006) [#9]
good little demo, smooth here. I would make a teeny change though. Change Before = MilliSecs() to Before = RightNow so that Millisecs() is only called once and it's value is transferred to Before via RightNow.


Eric(Posted 2006) [#10]
I'll have to try it on my home computer.

Grey out of curiosity do you have a CRT or LCD. It seems that since I got an LCD things do not run as smoothly.

I'm not sure why...Can a monitor do that or am I just losing it.


N(Posted 2006) [#11]
My timing code.

[codearc 1698]


Eric(Posted 2006) [#12]
Noel,

Cool piece of code, I have no idea how it works because I truly don't understand Hooks.. Can you explain this to me.. I try not to just plug in code until I understand how it works.

Thanks,
Eric


N(Posted 2006) [#13]
Basically, hook functions are functions that are called when a hook is run. Think of a hook function as something that handles an event. In the case of my code, calling flip is an event that you want the frame limiter to be aware of since it means that you're done drawing and the frame is done with.

The flip hook is called before the actual flip occurs (that is, Flip() calls the hooks then swaps the buffers), so you can draw stuff in a flip hook as well. It's rather convenient, but beside the point.

Anyways, what my code does is that when you call Flip, it increments the frame counter and gets a new delta time value. There is also a timer that is run only once per second. Each time the timer ticks, it queues an event that the hook picks up. When it ticks, the FPS is changed and the frame counter is reset (frames per second, resets once per second, etc.).

One thing to note: my code does not work with multiple canvases with MaxGUI as far as I know. That can be changed simply by making the timing update code a normal function instead of a hook and split the timer hook off into a new function. I'd do this myself, but the existing implementation is sufficient for my purposes.

I need to stop writing vaguely lengthy posts like this...


bradford6(Posted 2006) [#14]
Grey,
thanks. changed my code.

Noel,
That's some fancy code you have there. a little too complicated for my feeble brain :)

Eric,
I'll see if i can figure out why you are getting hiccups in teh movement. what is your setup (PC, Graphics card, OS, etc)
try to comment out this line
SetGraphicsDriver GLMax2DDriver()


the defaut is the DX driver. maybe that will work better on your PC


N(Posted 2006) [#15]
Mini-tutorial in code.




Eric(Posted 2006) [#16]
Bradford,

I ran the code and it hiccups both ways until the program runs for a while then it settle out.. I'm wondering if my computer is the cuplrit.

Noel,

Wow thanks... I think I finally understand this hook business as little better. Time to dig in and see what I can come up with.

With the highest regards,
Eric


Eric(Posted 2006) [#17]
Noel,

I'm taking a look at your code for game timing and I like it. So far so good. I have a question...can you explain how you derived?

  deltat = deltat*.005+((ctime-ltime)*fps)*.995


Thanks,
Eric


Dreamora(Posted 2006) [#18]
Thats a quite "easy" and intelligent formula:

deltat*0.005 is like "remembering the previous value" with a bias of 0.005. It takes the old knowledge partially into equation for the new knowledge.

The other half is the regular timing equation for deltatiming.

ctime - ltime = time difference since last render (currentTime - lastTime)

fps is the fps modifier for the desired fps rate (1 / fps#) *at least I assume so* and 0.995 is the rest from 1.0 (0.005 is used for remembering the past deltat) which is needed to calculate 1.0 (= 100%) of the current deltat.


Grey Alien(Posted 2006) [#19]
Why is noel using 0.5% of the previous delta time value? Does this make smoother animation?


Dreamora(Posted 2006) [#20]
don't know ... to me it doesn't make that much sense either as it underweights the "past" ... I would have used a value of 0.2 - 0.3 to prevent "fps fluctuation" and make the FPS beeing smoother


Eric(Posted 2006) [#21]
Ok Thanks Dreamora I missed the .995 and .005 correlation.


Grey Alien(Posted 2006) [#22]
yeah I thought 0.5% is a tad small.


N(Posted 2006) [#23]
It's been working very nicely for me, so I don't have any plans on changing it.


Eric(Posted 2006) [#24]
It seems to be doing nicely for me also... Thanks alot for this Noel.. The only reason I asked was to understand the code.

Best Regards,
Eric