Games How important is it to support higher refresh rates screens?

andymc

Well-Known Member
Licensed User
Longtime User
Well, I can report after getting the new phone, that my games do indeed run at double speed when at 120hz mode. the phone has a setting to switch to 60hz mode, but most users will likely leave it on 120hz mode, as it's noticeably smoother when scrolling around other apps. I'll report back in a few days once I've had a chance to try a few different ideas.
 

andymc

Well-Known Member
Licensed User
Longtime User
okay, so after some trial and error I've got my space invaders game running smoothly on 60hz and 120hz settings. I added this function to calculate the current average screen update rate:
first, declare a float in globals called delta

then add this sub:
B4X:
Sub UpdateDelta
    ' Add the current frame's delta time to the list
    deltaTimes.Add(Round2(lGdx.Graphics.DeltaTime,3))
   
    ' Keep only the last 100 delta times
    If deltaTimes.Size > 100 Then
        deltaTimes.RemoveAt(0)
    End If
   
    ' Calculate the average of the delta times
    Dim total As Float = 0
    For Each deltaTime As Float In deltaTimes
        total = total + deltaTime
    Next
    averageDeltaTime = total / deltaTimes.Size
   
    ' Log the average delta time
    Log("Average Delta Time : " & Floor(averageDeltaTime*1000))
    delta = Round2(averageDeltaTime*60,2)
    Log ("Delta = " & delta)

End Sub

Delta is now a float where 1 is 60fps, 0.5 is 120fps, 0.666 is 90hz, etc.... So in the gameplay loop, I now multiple any movement value by delta which accounts for the change in the refresh rate. for example, if my player moves by doing "player.x = player.x + mx", then I change this to "player.x = player.x + mx*delta"
As the delta function is taking an average value, it accounts for any tiny blips in framerate from other apps taking CPU time, so things stay smooth.

So for movement values, multiplying by delta will reduce them for 120hz displays, but then when I have values that decrease by one for each frame, like an explosion life, or a countdown, I now divide by the delta. In my invaders game, I would create score messages on screen to show when the player shoots an alien, this normaly fade out over the next 40 frames, so now I divide this by delta when I create the value, so it takes more frames to disappear. I do the same for other vlaues that are affected by the "frame" value, such as how quickly the background scrolls, or what animation frame the characters are on. My aliens are drawn with the frame number selected based on (frame / 120 mod 2) meaning it will be either 0 or 1, changing every 60 frames, I now update frames by speed*delta so the frames only go up half as fast when at 120hz.

I'll try to get the flappy bird code updated, tidied up and fixed to use this method in the next few days.

There's probably a better way to do this, but this is how I've managed to do it. I'll update Invaders on the store soon and will need people to test on 90hz devices.


@ilan I checked your Android games at 120hz. Pixel knight runs fine, it's just the animation frames that run too fast on the sprites, so I'd image setting the "frames" variable to float, and then updating it by speed*delta should fix this.

@melonZgz All your games run fine at 120hz and look really nice and smooth, how did you manage this???

Note: basically anything involved in the speed of anything in your game now needs to be a float type. I'll republish the cloney bird tutorial next week with source code and youtube video explianer.

I still think B4A with Libgdx is a good tool to use for simple 2D games. It has box2d (which I've not tried), and @melonZgz games prove that these can look very professional.

I know we also have x2 but honestly I've not really tried it, if someone wants to doe a flappy bird tutorial using x2 then I'd be interested to see it.
 
Last edited:

andymc

Well-Known Member
Licensed User
Longtime User
Okay, here we go. I've attached my old Cloney Bird (Flappy Bird Clone) project, updated to use delta timing for everything, including animating sprites. this should run smoothly on devices and screen refresh rates. I've tested on a 120hz and 60hz device, and also on the emulator which on my PC can drop below 60 quite often.

The only additional library required is the LibGDX library from here: https://www.b4x.com/android/forum/threads/libgdx-game-engine.32594/#content

I'll be updating my store apps soon to support this. I'd love it if you could all test on any 90hz or 144hz devices you have to ensure this example runs smoothly. A pipe should take about 4 seconds to cross the screen.

You'll have to ignore some of the strange looking maths in this code, I converted it from a flappy bird game I made using blitz basic, so the screen dimensions were slightly off.
 

Attachments

  • Cloney Bird LibGDX.zip
    105.5 KB · Views: 107
Last edited:

ilan

Expert
Licensed User
Longtime User
hi andy, i just have checked your example. so from what i understand you limit the game (cloney bird) to run max 60 frames right?
so if someone has 120hrz screen the game will run only on 60 frames correct?
 

ilan

Expert
Licensed User
Longtime User
actually i did a really simple fix to limit my game for 60 frames and it works fine on my daughter 120 hrz samsung phone.

all i did is this:

B4X:
    Do While (DateTime.now - lastdeltaTime) < (1000/60)
        'do nothing (wait until 16.667 ms has past from the last draw)
    Loop
  
    LG_Update '<--- here is all rendering happaning'
  
    lastdeltaTime = DateTime.Now 'set the new deltatime

now it runs on 60 hrz and 120hrz the same speed :oops:

i have uploded a new build of Pixel Knight (v1.22).
if someone can test it please, i would like to hear from you guys if it runs on 120hrz better now.

thanx

EDIT: i am including cloney bird update with the solution above. seems to do the job as well
 

Attachments

  • Cloney Bird LibGDX.zip
    103.4 KB · Views: 107
Last edited:

andymc

Well-Known Member
Licensed User
Longtime User
hi andy, i just have checked your example. so from what i understand you limit the game (cloney bird) to run max 60 frames right?
so if someone has 120hrz screen the game will run only on 60 frames correct?
Hi ilan,

no , the way I've done it is to adjust the game movement speed to suit the screen update rate. So if something moves 4 pixels at 60hz, then it will move 2 pixels at 120hz. It calculates a delta value as a ratio of the 60hz speed, so 120hz, would be a ratio of 0.5, as 60/120 is 0.5, so all movement values would be multiplied by 0.5. If I have a value that decrements each frame, so something like a countdown for a level start, that I would set to 300 for 60hz, making it a 5 second countdown, then you divide this by the delta ratio of 0.5, making it 600, so at 120hz, 600 takes 5 seconds to count down to zero. I hope that makes sense.

I like you're reply giving the other option of delaying the frame render to keep it at 60hz, this is also a good solution, however on 144hz phones, where the display is not a double factor of 60hz, it may make things look jerky? I don't know, I don't have a 144hz phone to test with.
 
Top