Timing code burning up CPU time
Blitz3D Forums/Blitz3D Beginners Area/Timing code burning up CPU time
| ||
What's the best method in Blitz to keep the time (FPS) constant? I have used this bit of code but it just eats up all of the CPU processing power while waiting for the next loop:While GameRunning Cls curTime=MilliSecs() While MilliSecs() < curTime + 33 Wend ;perform game functions here Flip Wend Their is no problem with my game running stand-alone but if the user wants to run something else at the same time (and who doesn't with a multi-tasking OS) then the other programs suffer horribly. This wasn't a problem for me while programming in C++ because I would just set a callback timer, thereby freeing up the cpu until the next loop. But I'm not aware if something like this exists in Blitz. |
| ||
why not do something likefps = CreateTimer(60) ;whatever fps you want to limit it to While GameRunning Cls ;perform game functions here Flip WaitTimer(fps) Wend if that still doesnt give you what you want you could write a dll, or i could if you wanted, that would manually reduce the Programs priority... :D that would give the other program the time they needed |
| ||
Basically your code wasn't bad, WolRon... It's pretty well found, but the CPU gets eaten up, because it keeps looping and looping and looping and... You know... So it keeps eating commands, and commands eat up your CPU processor capacity.... The "WaitTimer" code SSS constructed is an internal Blitz feature, that is coded that way that the PC will take care of things, and doesn't need to worry about anything until the timer is over, and the CPU will not pay attention to your code unless that time has passed, and thus less CPU capacity is eaten... So far my technobabble... |
| ||
It's the waiting for the vertical sync that eats processor time. You can turn vsync off in the Flip command and code your own delay. You may get some visual 'tearing', but in the end it's whichever method works best for you. Of course, you could code a vsync on/off option as part of your in game options.Graphics 640,480,0,2 Const frame_delay=1000/60 ; delay required for 60 FPS Global frame_wait ; time to wait for next frame Global frame_time ; holds system time for reference SetBuffer BackBuffer() While Not KeyDown(1) Cls frame_time=MilliSecs() ; get system time Color Rand(150,200),Rand(150,200),Rand(150,200) Text 320,240,"Press ESC to exit",1,1 frame_wait=MilliSecs()-frame_time ; calculate delay required If frame_wait>0 Then Delay frame_wait ; force delay if needed Flip False ; flip screen immediately (no VWAIT!) Wend On my machine it gets my processor usage down to 30-50%. |
| ||
If its just for FPS consistency, there is code in the code archives which limits FPS and if too low, will drop unnecesary frames (according to text) |