Speed issues with my game
-
- Posts: 355
- Joined: May 29, 2015 20:37
Speed issues with my game
Hi all,
I'm nearly finished with the first version of that game I'm making. That version is coming out very soon!
However, I have a technical issue with my game loop. Sometimes the game runs too fast! How can I regulate speed to make sure it will always play at a specific rate (a majority of the time the speed is correct however most other times it runs too fast and sometimes runs a little too slow)?
I have put "my game" in the title, but i have a feeling this is a general issue.
I'm nearly finished with the first version of that game I'm making. That version is coming out very soon!
However, I have a technical issue with my game loop. Sometimes the game runs too fast! How can I regulate speed to make sure it will always play at a specific rate (a majority of the time the speed is correct however most other times it runs too fast and sometimes runs a little too slow)?
I have put "my game" in the title, but i have a feeling this is a general issue.
Re: Speed issues with my game
How are you timing your game loop? Do you allow so many game ticks per second? You can accomplish this most easily by using a timer variable to limit how many passes per second. More accurately you would process your game movement based on the elapsed time for each game tick but this is more involved if you didn't create the game with this type of frame limiting in mind. Also it is very important to release spare CPU time back to the OS using the SLEEP command or else Window messages will tend to back up and make your game window non-responsive.
-Vince
-Vince
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Speed issues with my game
Thanks for your reply,vdecampo wrote:How are you timing your game loop? Do you allow so many game ticks per second? You can accomplish this most easily by using a timer variable to limit how many passes per second. More accurately you would process your game movement based on the elapsed time for each game tick but this is more involved if you didn't create the game with this type of frame limiting in mind. Also it is very important to release spare CPU time back to the OS using the SLEEP command or else Window messages will tend to back up and make your game window non-responsive.
-Vince
Currently I am using the "sleep 1" command in my loops (game loop plus inner loops such as pause menus). But I do not limit the amount of ticks per second/iterations of the loop per second. Would this be the best way of making sure my game works consistently, and is there anything beyond using timer that I have to know?
Re: Speed issues with my game
IIRC FB uses the high resolution timer internally when available so using a double to store a start time could be used for comparisons.
Pseudo Code
Something like that.
-Vince
Pseudo Code
Code: Select all
Dim loopStart as Double
' Start Game Loop
Do
if (Timer-loopStart > .33) Then 'This is 30 FPS
'Process Logic
'Render Screen
loopStart = Timer
End if
Sleep 1
Loop Until (GameExit)
-Vince
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Speed issues with my game
Thanks for the help!vdecampo wrote:IIRC FB uses the high resolution timer internally when available so using a double to store a start time could be used for comparisons.
Pseudo CodeSomething like that.Code: Select all
Dim loopStart as Double ' Start Game Loop Do if (Timer-loopStart > .33) Then 'This is 30 FPS 'Process Logic 'Render Screen loopStart = Timer End if Sleep 1 Loop Until (GameExit)
-Vince
Re: Speed issues with my game
I use a little function to adjust the time of sleep.
This gives The CPU a break and lets your framerate be at your request.
Your graphics can be within screenlock/unlock or screenset/flip, it doesn't really matter.
I have set 60 frames/second here.
This gives The CPU a break and lets your framerate be at your request.
Your graphics can be within screenlock/unlock or screenset/flip, it doesn't really matter.
I have set 60 frames/second here.
Code: Select all
Function Regulate(Byval MyFps As long,Byref fps As long) As long
Static As Double timervalue,_LastSleepTime,t3,frames
frames+=1
If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
dim as double sleeptime=_LastSleepTime+((1/myfps)-Timer+timervalue)*1000
If sleeptime<1 Then sleeptime=1
_LastSleepTime=sleeptime
timervalue=Timer
Return sleeptime
End Function
dim as long RequiredFramerate=60 '<< -- SET FRAMERATE HERE
screen 18
dim as long fps,x=10,y=10,dx=1,dy=1
do
x+=dx
y+=dy
if x<10 or x>630 then dx=-dx
if y<10 or y>470 then dy=-dy
screenlock
cls
'YOUR GRAPHICS
draw string (20,20),"Framerate = " &fps
circle(x,y),10
screenunlock
sleep regulate(RequiredFramerate,fps) '<< -- Use the function here
loop until len(inkey)
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Speed issues with my game
Hi dodicat, I will try out your method. Because it seems simply running the game loop on a timer isn't working.dodicat wrote:I use a little function to adjust the time of sleep.
This gives The CPU a break and lets your framerate be at your request.
I myself am not experiencing speed issues anymore. But apparently one of the testers for the game made a remark about the game speed running fast and I thought that he could only mean this problem.
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Speed issues with my game
I also noticed that Skype notifications with their Windows 7 client drastically slows down the game.
I hope this list is exhaustive, but there's always the possibility that it's not.
Why does this speed difference occur? I am using GFX (the "default" graphics backend, which IIRC is DirectX or is it different for different users on Windows?). Would this speed issue be solved by using a different backend and which backend should I use?
Last edited by datwill310 on Jul 27, 2017 21:41, edited 1 time in total.
Re: Speed issues with my game
I may be stating the obvious again:datwill310 wrote:I've figured out that ANY program running on my GPU causes speed issues for Horatio.
Why does this speed difference occur?
- on any multitasking OS (exception is DOS),
the size of the time-slice (any running task receives),
depends on the amount of running tasks.
(This is not taking into account: priorities of tasks, threads priorities etc.)
It is also, out of the hands, of the application's programmer.
Unless, one uses higher than normal priority, on the application.
Re: Speed issues with my game
If I am NOT online with google chrome my frame rate is limited to about 65 f.p.s.
If I am online I can get much faster rates.
On my old XP machine, I had a little java code snippet, which, when running gave me a faster framerate for freebasic graphics.
This is why I made the regulator, to keep the whole forum tied to a given framerate, Java running/ google chrome on/ or any other such interference.
If I am online I can get much faster rates.
On my old XP machine, I had a little java code snippet, which, when running gave me a faster framerate for freebasic graphics.
This is why I made the regulator, to keep the whole forum tied to a given framerate, Java running/ google chrome on/ or any other such interference.
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Speed issues with my game
This is the sort of issue I am running into. Except I noticed that with my game the speed only increases dramatically if I have a Google Drive page open and viewing. Maybe the list of webpages goes on. But rn I only have that website as a culprit.dodicat wrote:If I am NOT online with google chrome my frame rate is limited to about 65 f.p.s.
If I am online I can get much faster rates.
The effects are reduced when I turn hardware acceleration off in Chrome settings, but the effect is still noticeable. This is making me more certain that it's to do with the GPU.
The effect STILL happens the same, regardless of if I have either timer system in (by dodicat or vdecampo), or none at all except for a sleep 1 statement.
I have tried removing the higher priority to my graphics back-end using GFX_HIGH_PRIORITY, but with the flag passed or not, the effects are still the same.
Now, I am using DirectX. Would changing the back-end to something like OpenGL fix this issue or is this something that just 'happens'?
But as MrSwiss highlighted it is a hardware/low-level-OS issue so maybe changing the back-end wont do anything....
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Speed issues with my game
I'm feeling like I'm being misunderstood here...MrSwiss wrote:I may be stating the obvious again:datwill310 wrote:I've figured out that ANY program running on my GPU causes speed issues for Horatio.
Why does this speed difference occur?This is totally independent of the graphic sub-system used.
- on any multitasking OS (exception is DOS),
the size of the time-slice (any running task receives),
depends on the amount of running tasks.
(This is not taking into account: priorities of tasks, threads priorities etc.)
It is also, out of the hands, of the application's programmer.
Unless, one uses higher than normal priority, on the application.
The issues don't lie in: the game just gets a bit slower when I run something else with it.
The issue lies in: when playing the main game, play through dramatically speeds UP not DOWN.
Other games do not have this volatility in speed upon something else running with it and I'm curious WHY.
I also realise that changing the back-end will not do anything, from MrSwiss' comment:
But HOW can I solve this issue? Because it's pretty bad.MrSwiss wrote:This is totally independent of the graphic sub-system used.
Is it something to do specifically with my code, or is it something else?
Re: Speed issues with my game
Base the run-time on a high speed timer -and-
burn up unused time, with a timer based loop, similar to:
burn up unused time, with a timer based loop, similar to:
Code: Select all
Dim As Double t1, t2, t3
Do
t1 = Timer, t3 = t1 + .005 ' set your time-slice here
... ' your code
t2 = Timer - t1 ' make certain, that t3 is set: 'larger than' run-time (t2)
While Timer < t3
Asm
nop ' no operation ...
End Asm
Wend
Sleep 1, 1
Loop
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Speed issues with my game
Thanks for the advise, and I will try it out.MrSwiss wrote:Base the run-time on a high speed timer -and-
burn up unused time, with a timer based loop, similar to:
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Speed issues with my game
Thank you, this solution has also helped me regulate speed!MrSwiss wrote:Base the run-time on a high speed timer -and-
burn up unused time, with a timer based loop, similar to:
Thanks again to the three of you!