Speed issues with my game

New to FreeBASIC? Post your questions here.
Post Reply
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Speed issues with my game

Post by datwill310 »

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.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Speed issues with my game

Post by vdecampo »

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
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Speed issues with my game

Post by datwill310 »

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
Thanks for your reply,

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?
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Speed issues with my game

Post by vdecampo »

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

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)

Something like that.

-Vince
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Speed issues with my game

Post by datwill310 »

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 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)

Something like that.

-Vince
Thanks for the help!
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Speed issues with my game

Post by dodicat »

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.

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)
     
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Speed issues with my game

Post by datwill310 »

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.
Hi dodicat, I will try out your method. Because it seems simply running the game loop on a timer isn't working.
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.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Speed issues with my game

Post by datwill310 »

I've noticed that the speed up occurs when I have Google Chrome open and viewing a Google Drive webpage!
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.
I've figured out that ANY program running on my GPU causes speed issues for the game.
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.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Speed issues with my game

Post by MrSwiss »

datwill310 wrote:I've figured out that ANY program running on my GPU causes speed issues for Horatio.
Why does this speed difference occur?
I may be stating the obvious again:
  • 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.)
This is totally independent of the graphic sub-system used.

It is also, out of the hands, of the application's programmer.
Unless, one uses higher than normal priority, on the application.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Speed issues with my game

Post by dodicat »

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.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Speed issues with my game

Post by datwill310 »

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.
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.
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....
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Speed issues with my game

Post by datwill310 »

MrSwiss wrote:
datwill310 wrote:I've figured out that ANY program running on my GPU causes speed issues for Horatio.
Why does this speed difference occur?
I may be stating the obvious again:
  • 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.)
This is totally independent of the graphic sub-system used.

It is also, out of the hands, of the application's programmer.
Unless, one uses higher than normal priority, on the application.
I'm feeling like I'm being misunderstood here...

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:
MrSwiss wrote:This is totally independent of the graphic sub-system used.
But HOW can I solve this issue? Because it's pretty bad.

Is it something to do specifically with my code, or is it something else?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Speed issues with my game

Post by MrSwiss »

Base the run-time on a high speed timer -and-
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
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Speed issues with my game

Post by datwill310 »

MrSwiss wrote:Base the run-time on a high speed timer -and-
burn up unused time, with a timer based loop, similar to:
Thanks for the advise, and I will try it out.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Speed issues with my game

Post by datwill310 »

MrSwiss wrote:Base the run-time on a high speed timer -and-
burn up unused time, with a timer based loop, similar to:
Thank you, this solution has also helped me regulate speed!
Thanks again to the three of you!
Post Reply