Tweening + Delay on high FPS

BlitzMax Forums/BlitzMax Programming/Tweening + Delay on high FPS

Trader3564(Posted 2008) [#1]
Hello,

I have found a good tweening code that i changed a bit to count both UPS (Updates Per Second) and (Frames Per Second)
With this code, the engine always runs at a fixed game logic rate, so it is very handy!
Now, what else i want todo is cap the FPS if w exceed the UPS. Meening, If the UPS 35, we should never allow more then +-35FPS. If the UPS is 50, we should not allow more then +-50FPS.
Currently, my logic runs nice at 50UPS, because i fixed it. However, FPS runs over 120 FPS eating CPU.
As you can see i tried to delay (to save CPU) and reduce the FPS back to 50, but it works not as i hoped. Because it will toggle between 120FPS 50FPS 120FPS 50 FPS, etc... (which is logical :P)
I smell that i should do it timebased, but i dont yet really see how i can achieve that.

Graphics 800, 600,, , GRAPHICS_BACKBUFFER

Global UPDATE_FREQUENCY:Int = 50
Global update_time:Float = 1000 / UPDATE_FREQUENCY
Global t:Int, dt:Int
Global execution_time:Float = 0

t = MilliSecs() 
While Not KeyDown(KEY_ESCAPE) 
	dt = MilliSecs() - t
	t = MilliSecs() 
	execution_time:+dt
 
    While execution_time >= update_time
		'UPS.Update()
		'Update()
		execution_time:- update_time
	Wend
	
	'FPS.Update() 
	'Render(execution_time / update_time) 

	rem
	If FPS.count > 0 And UPS.count > 0 And FPS.count > UPS.count
		Delay(Float(FPS.count) / Float(UPS.count) * 10) 
	End If
	endrem
Wend



TaskMaster(Posted 2008) [#2]
Let the FPS go higher, don't cap it. Then use tweening when rendering to the screen. It will look much better.


Gabriel(Posted 2008) [#3]
I don't see the purpose in arbitrarily capping the render speed. The whole point of render tweening is to let the video update be as smooth as it can be, that's why you decouple it from the physics updates.


Trader3564(Posted 2008) [#4]
Ok. i may need to study more on game-loops. i thoughd to understand the prupose of the various techniques.. may need some more work there. Does anyone know a sheet or anything, that explains the purpose of various loops?

How i see it is:
> Capping your UPS or LPS (dunno what is the official term for it) is something i want because i want the game to run steady on any machine. I forsee a problem tough if the UPS is lower then the max.
> Capping your FPS is something i want for my RPG. It is not a fast paced action shooter. Just 2D and 2D effects and explosions and stuff.. i think it will do at 50FPS. Reason for capping it, is to allow free processing time for other applications. If i wouldn't cap my Game anywhere, it would take 100% CPU. That is why i tried to delay it.
> Maybe make capping FPS optional.

The tweening in my code, i noticed only works when the UPS rans low... but when would that happen? even if it happens, that meens i only get to see 1 flip, = 1FPS. So whats the point anyway?

Im a bit lost... any docs will be very apriciated.


MGE(Posted 2008) [#5]
Goldstar - I've been involved with some of these projects in the past. I would suggest keeping the logic loop (all moving, calculations, input, etc,) at a locked frame rate. For something like a 2D RPG, you really don't need tweening, others may debate that, if you were using 3d models, in a 3d world, I would suggest tweening. Alot of the RPG makers over seas only run at 20 or 30 fps. I would suggest a timing loop of 60fps. The following link is to some code I found last year when I was learning Blitzmax.

My game engine supports both time based and frame based logic, for the frame based I use this exact code and it works fine. On slower machines it will render fewer frames while still keeping your logic speed at a constant. (It just calls the logic loop more often if the render speed is slower.)

Check it out here, I think it's what you want. ;)

http://www.blitzbasic.com/Community/posts.php?topic=71030#793856


Czar Flavius(Posted 2008) [#6]
Maybe I'm wrong but it just uses 100% CPU time because there is nothing else using the processor? And should another program request use of the processor, the FPS of your game will lower down naturally as the operating system distributes processor time?


Trader3564(Posted 2008) [#7]
If we would all write our software and games like that its a race and battle on who gains the most CPU. By delaying the loop, to save unneeded cycles, you give the CPU some rest. It is more system friendly. Why stress it if you dont need to.


Dreamora(Posted 2008) [#8]
Its especially notebook runtime friendly and the way that Intel and Microsoft tried to get the devs to work for years now ...


Trader3564(Posted 2008) [#9]
what is "notebook runtime friendly "? adding a delay or leaving it ran to the max?


Dreamora(Posted 2008) [#10]
Adding a delay so the CPU can use a slower clocking


ImaginaryHuman(Posted 2008) [#11]
Ahhh, gone are the days of switching off multitasking and having full low-level access to the entire machine. :-D


Czar Flavius(Posted 2008) [#12]
Maybe power friendly but computers are not athletes and the CPU does not get tired or require 'rest'. I believe Flip 1 will cap your rendering at the screen's refresh rate.

Unless this is causing you problems, don't worry about it.


computercoder(Posted 2008) [#13]
Actually, Flip 1 will wait for the refresh to occur, then renders the screen draw. So if a refresh is about to happen, using Flip 1 will say let the refresh go on, then draw, otherwise draw.


Trader3564(Posted 2008) [#14]
@Czar Flavius, nonetheless you save energy :-P


Czar Flavius(Posted 2008) [#15]
Well it's flip some other number! Pancakes!


Dreamora(Posted 2008) [#16]
Czar: It has nothing to do with cpu getting tired. Its just noob style to push 100% if you only need 30% of the calculation power. If all apps would behave that stupid, you wouldn't even be able to start 2 media player applications side a side.

Putting a little brain in code so far never has harmed anyone.


ImaginaryHuman(Posted 2008) [#17]
Yeah, think of others ;-D


MGE(Posted 2008) [#18]
I don't know, if they're playing your game, run full speed ahead! When the window's not active or minimized then give some cpu time back. ;)


tin(Posted 2008) [#19]
cant we just do like real distance calculation in physics?
such as.
2 pixles per second for and object such as

time_frame : int = 1
A.speed = 2 pixels per seconds
A.x         = 0

time_frame = MilliSecs() 
repeat
    time_frame = MilliSecs()  - time_frame
    A.x = A.x + A.speed * time_frame
    draw A.x
    time_frame = MilliSecs() 
forever
//////////////////////////////////////

Explain:
according to physics mathematics
x move 2 pixels per seconds, if 3 seconds it already moved 6 pixels. if 4 seconds, it already moved 8 pixels.
if it the frame rate is too fast and your loop only took 0.1 seconds, it only moved 0.2 pixels... Bingo!!!

distance = speed * time taken
////////////////////////////////////////

time = currenttime

L1:
    time = currenttime - time  'see how much time passed 
    X = X + speed * time  'update current total distance
    show object on screen 
    time = currenttime 'now record current time
GOTO L1:

i hope this way can do too.!

in this way the objects will be drawn regardless of FPS.
if FPS is higher, will get smooth display, if lower will get Jitter display with correct displacement of objects.

Here is my proof of concept,
the code provided here is very dirty code.


Graphics 800, 600
Local x:Float =0
Local sp:Int= 60     ' pixels per seconds adjust this for ur moving speed speed
Local t#
Local fps=0
Local c#=0
Local counter = 0

t=MilliSecs()
fps =0
c=MilliSecs()

Repeat
	
	t= MilliSecs() - t

	x = x + sp * t/1000
	DrawRect x,40,20,60
	DrawRect 800-x,130,20,60
	DrawRect x,230,20,60
	DrawText "FPS : " + counter,10,10
	fps:+1
	If MilliSecs()-c>=1000 Then 
		c=MilliSecs()
		counter = fps
		fps=0

	EndIf

	If x>800 Then 
		x=0 
	EndIf

	t = MilliSecs()
	Flip;Cls
	
Until KeyDown(KEY_ESCAPE)



Paposo(Posted 2008) [#20]
Hello.

I use a variant in java.

UPS are fixed and FPS are normally fixed to UPS
If UPS is minor to expected any number stablished of frames are skiped.

Is not needed that FPS>UPS. The screen are unaltered.
In java use sleep() for lease resources to other threads.
BMax is not multitask. Not need sleep().

Bye,
Paposo


Gabriel(Posted 2008) [#21]
cant we just do like real distance calculation in physics?

Yes, but doing so means that you have a variable timestep, which will only be suitable for simple games. Any physics or complex maths and things will start breaking ( missed collisions, unpredictable behaviour. )


HrdNutz(Posted 2008) [#22]
take a loot at this thread for a tweening sample
http://www.blitzbasic.com/Community/posts.php?topic=70516#795336