Author Topic: Windows 10 timer resolution - jerky movement even with _LIMIT  (Read 5939 times)

0 Members and 1 Guest are viewing this topic.

Offline IfThenOrElse

  • Newbie
  • Posts: 8
    • View Profile
Re: Windows 10 timer resolution - jerky movement even with _LIMIT
« Reply #15 on: November 05, 2021, 06:29:12 pm »
I have been experimenting a bit and found that if I measure the time in a _GL sub (normally for open GL rendering) it seem to work the way I want it to. It's a hack-ish way of doing it and i dont know why it works, if the timer get the time from the GPU instead just because its inside the _GL sub or if the measured time just happened to be at the right place... someone might know..

I found this because I was going to try to learn how to render with open GL instead of just using putimage like Ive done so far. I have noticed that programs that use open GL seem very smooth. The one where they simulate the pandemic in particular: https://www.qb64.org/forum/index.php?topic=2827.0
The green dots simulating people moves super smooth in the open GL version that can be found some posts down the page. If you change the size of them to larger ones its really noticable.

Anyway, below is the new code I made. Since i am new to this I dont really understand why it works better, it may just be better placement of the timers and have completely nothing to do with the _GL sub per see. By the way, the 320x240 res screen is just from me experimenting as well. If I manage to make a game I will try to adjust it so i have like a native low resolution for the game and then scale it to whatever the player has as desktop. But thats for later.

Do anyone else see a difference in movement?

Code: QB64: [Select]
  1. tvscreen43& = _NEWIMAGE(320, 240, 32)
  2.  
  3. SCREEN tvscreen43&
  4.  
  5.  
  6.  
  7. CONST ScreenEndLeft = 0
  8. CONST ScreenEndRight = 320 '1920
  9. CONST ScreenEndTop = 0
  10. CONST ScreenEndBottom = 240 '1080
  11. CONST ScreenWidth = 320 '1920
  12. CONST ScreenHeight = 240 '1080
  13. CONST ScreenCenterX = (ScreenWidth / 2)
  14. CONST ScreenCenterY = (ScreenHeight / 2)
  15.  
  16.  
  17. TYPE Shot
  18.     alive AS _BYTE
  19.     x AS SINGLE
  20.     y AS SINGLE
  21.     speed AS SINGLE
  22.     spriteheight AS INTEGER
  23.     spritewidth AS INTEGER
  24.     dir AS _BYTE
  25.     angle AS SINGLE
  26.     stepy AS SINGLE
  27.     stepx AS SINGLE
  28.     sprite AS INTEGER
  29.  
  30. REDIM SHARED Shots(0) AS Shot
  31.  
  32. 'Shot sprite
  33. '8x8px
  34. '(Part of code is borrowed from somewhere like a tutorial or something...).
  35. ShotSpriteData:
  36. DATA &HFFFC00FC,&HFFFC00FC,&HFF000000,&HFF000000,&HFF000000,&HFF000000,&HFFFC00FC,&HFFFC00FC
  37. DATA &HFFFC00FC,&HFF000000,&HFFF7D8A5,&HFFF7D8A5,&HFFF7D8A5,&HFFF7D8A5,&HFF000000,&HFFFC00FC
  38. DATA &HFF000000,&HFFF7D8A5,&HFFF7D8A5,&HFFFFFEFF,&HFFFFFEFF,&HFFF7D8A5,&HFFF7D8A5,&HFF000000
  39. DATA &HFF000000,&HFFF7D8A5,&HFFFFFEFF,&HFFFFFEFF,&HFFFFFEFF,&HFFFFFEFF,&HFFF7D8A5,&HFF000000
  40. DATA &HFF000000,&HFFF7D8A5,&HFFFFFEFF,&HFFFFFEFF,&HFFFFFEFF,&HFFFFFEFF,&HFFF7D8A5,&HFF000000
  41. DATA &HFF000000,&HFFF7D8A5,&HFFF7D8A5,&HFFFFFEFF,&HFFFFFEFF,&HFFF7D8A5,&HFFF7D8A5,&HFF000000
  42. DATA &HFFFC00FC,&HFF000000,&HFFF7D8A5,&HFFF7D8A5,&HFFF7D8A5,&HFFF7D8A5,&HFF000000,&HFFFC00FC
  43. DATA &HFFFC00FC,&HFFFC00FC,&HFF000000,&HFF000000,&HFF000000,&HFF000000,&HFFFC00FC,&HFFFC00FC
  44.  
  45. RESTORE ShotSpriteData
  46. FOR y = 0 TO 7: FOR x = 0 TO 7
  47.         READ sprite#: PSET (x, y), sprite#
  48. NEXT x: NEXT y
  49. DIM SHARED shotsprite AS INTEGER
  50. _SETALPHA 0, _RGB32(252, 0, 252), shotsprite%
  51. shotsprite% = _NEWIMAGE(8, 8, 32)
  52. _PUTIMAGE (0, 0), 0, shotsprite%
  53.  
  54.  
  55. 'Set some properties of the shot
  56. Shots(0).x! = 0
  57. Shots(0).y! = 100
  58. Shots(0).spritewidth% = 8
  59. Shots(0).spriteheight% = 8
  60. Shots(0).dir%% = 0
  61. Shots(0).sprite% = shotsprite%
  62.  
  63. SCREEN tvscreen43&
  64.  
  65.  
  66. DIM SHARED AllowGL AS _BYTE
  67. DIM SHARED endtime_gltest AS DOUBLE
  68. DIM SHARED starttime_gltest AS DOUBLE
  69. DIM SHARED delta_gltest AS DOUBLE
  70.  
  71. AllowGL%% = -1
  72.  
  73.     _LIMIT 60
  74.  
  75.     Shots(0).x! = (Shots(0).x! + 2)
  76.  
  77.     IF Shots(0).x! >= (ScreenEndRight + Shots(0).spritewidth%) THEN Shots(0).x! = (ScreenEndLeft - Shots(0).spritewidth%)
  78.  
  79.  
  80. _FREEIMAGE shotsprite%
  81.  
  82.  
  83. SUB _GL ()
  84.     IF AllowGL%% = 0 THEN EXIT SUB
  85.  
  86.     endtime_gltest# = TIMER(0.001)
  87.     starttime_gltest# = TIMER(0.001)
  88.  
  89.     delta_gltest# = (endtime_gltest# - starttime_gltest#)
  90.  
  91.     IF delta_gltest# <= (1 / 60) THEN
  92.  
  93.         DO
  94.             delta_gltest# = (TIMER(0.001) - starttime_gltest#)
  95.         LOOP UNTIL delta_gltest# >= (1 / 60)
  96.         CLS
  97.         _PUTIMAGE (INT(Shots(0).x!), Shots(0).y!), Shots(0).sprite%
  98.         PRINT (delta_gltest# * 1000)
  99.         _DISPLAY
  100.  
  101.     END IF
  102.  
  103.     '   _GLEND
  104.     '    _GLFLUSH
  105.  
  106.  

Offline IfThenOrElse

  • Newbie
  • Posts: 8
    • View Profile
Re: Windows 10 timer resolution - jerky movement even with _LIMIT
« Reply #16 on: November 05, 2021, 06:48:17 pm »
Well.. the timers did not make sense, doing this is enough to wait for a while. The thing seem to be to find a balance where the "game loop" manage to do its thing and then "render" the image as fast as possible. A lot to learn...:

Code: QB64: [Select]
  1.     starttime_gltest# = TIMER(0.001)
  2.  
  3.     DO
  4.         delta_gltest# = (TIMER(0.001) - starttime_gltest#)
  5.     LOOP UNTIL delta_gltest# >= (1 / 70)
  6.     CLS
  7.     _PUTIMAGE (INT(Shots(0).x!), Shots(0).y!), Shots(0).sprite%
  8.     PRINT (delta_gltest# * 1000)
  9.     _DISPLAY


Even this seem to work in the _GL sub:

Code: QB64: [Select]
  1.     _DELAY (1 / 60)
  2.     CLS
  3.     _PUTIMAGE (INT(Shots(0).x!), Shots(0).y!), Shots(0).sprite%
  4.     PRINT (delta_gltest# * 1000)
  5.     _DISPLAY
  6.  
« Last Edit: November 05, 2021, 06:57:02 pm by IfThenOrElse »