Author Topic: Sometimes unexpected strong lags  (Read 4539 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
Sometimes unexpected strong lags
« on: November 01, 2019, 10:36:58 am »
In this small program you can move around with w, a, s, d and while I do so, sometimes I got a massive lag.
Sometimes the lag lasts 0.5 till 1 second.

Is this because of my busy laptop, or something in the code?

EDIT:
And the movement isn't as fluent as it should be. It's like a permanent, slight stuttering.

Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3. CONST false = 0
  4. CONST true = -1
  5. CONST locked = 127
  6.  
  7.  
  8. desktopscreen.width = _DESKTOPWIDTH
  9. desktopscreen.height = _DESKTOPHEIGHT
  10.  
  11. screenwindow.width = 1280
  12. screenwindow.height = 720
  13.  
  14. SCREEN _NEWIMAGE(screenwindow.width, screenwindow.height, 32)
  15. _SCREENMOVE (desktopscreen.width * 0.5 - screenwindow.width * 0.5), 0
  16.  
  17.  
  18. cameraX = 640
  19. cameraY = 640
  20.  
  21. DIM framesPerSecond AS SINGLE
  22. DIM frametimeCurrent AS DOUBLE
  23. DIM frametimeLast AS DOUBLE
  24. DIM frametime AS SINGLE
  25. DIM frames AS INTEGER
  26. DIM frametimeStopped AS DOUBLE
  27. DIM frametimeResumption AS DOUBLE
  28. DIM framerate AS INTEGER
  29.  
  30.  
  31.  
  32. backgroundWidthTiles = 50
  33. backgroundHeightTiles = 50
  34.  
  35. DIM drawlayer AS LONG
  36. drawlayer = _NEWIMAGE(64 * backgroundWidthTiles, 64 * backgroundHeightTiles, 32)
  37.  
  38. _DEST drawlayer
  39.  
  40. 'draw backgound layer
  41. drawpositionX = 0
  42. drawpositionY = 0
  43. FOR y = 1 TO backgroundHeightTiles
  44.     FOR x = 1 TO backgroundWidthTiles
  45.  
  46.         'random tile colors
  47.         randomNumber = (RND * 4) + 1
  48.         SELECT CASE randomNumber
  49.             CASE 1:
  50.                 red = 124
  51.                 green = 0
  52.                 blue = 0
  53.             CASE 2:
  54.                 red = 0
  55.                 green = 124
  56.                 blue = 0
  57.             CASE 3:
  58.                 red = 0
  59.                 green = 0
  60.                 blue = 124
  61.             CASE 4:
  62.                 red = 124
  63.                 green = 124
  64.                 blue = 0
  65.             CASE 5:
  66.                 red = 124
  67.                 green = 124
  68.                 blue = 124
  69.         END SELECT
  70.  
  71.         'draw tiles (64x64 pixel) on drawlayer
  72.         LINE (drawpositionX, drawpositionY)-(drawpositionX + 63, drawpositionY + 63), _RGB(red, green, blue), BF
  73.  
  74.         drawpositionX = drawpositionX + 64
  75.         IF drawpositionX > backgroundWidthTiles * 64 THEN drawpositionX = 0: drawpositionY = drawpositionY + 64
  76.        
  77.     NEXT x
  78.  
  79.  
  80.    
  81. 'mainloop
  82.     _LIMIT 240
  83.  
  84.     DO
  85.         frametimeCurrent = TIMER(.001)
  86.         frametime = (frametimeCurrent - frametimeLast)
  87.     LOOP UNTIL frametime >= 1 / 240 '240 is maximum frames per second
  88.  
  89.     frametimeLast = frametimeCurrent
  90.  
  91.     framesPerSecond = 1 / frametime
  92.     IF framesPerSecond < 10 THEN framesPerSecond = 10 '10 is minimum frames per second
  93.  
  94.    
  95.     'check keyinputs
  96.     GOSUB keylistener
  97.  
  98.    
  99.    
  100.     'process keyinputs
  101.     IF key27 = true THEN GOTO shutdown 'close program
  102.  
  103.     IF key119 = true THEN 'move up
  104.         cameraY = cameraY - (320 / framesPerSecond)
  105.         IF cameraY < 0 THEN cameraY = 0
  106.     END IF
  107.    
  108.     IF key115 = true THEN 'move down
  109.         cameraY = cameraY + (320 / framesPerSecond)
  110.         IF cameraY > 50 * 64 - 1 THEN cameraY = 50 * 64 - 1
  111.     END IF
  112.    
  113.     IF key97 = true THEN 'move left
  114.         cameraX = cameraX - (320 / framesPerSecond)
  115.         IF cameraX < 0 THEN cameraX = 0
  116.     END IF
  117.    
  118.     IF key100 = true THEN 'move right
  119.         cameraX = cameraX + (320 / framesPerSecond)
  120.         IF cameraX > 50 * 64 - 1 THEN cameraX = 50 * 64 - 1
  121.     END IF
  122.  
  123.  
  124.    
  125.  
  126.     CLS
  127.    
  128.         'calculate drawposition of drawlayer
  129.         drawpositionY = cameraY - (screenwindow.height * 0.5) + 1
  130.         drawpositionX = cameraX - (screenwindow.width * 0.5) + 1
  131.    
  132.         _PUTIMAGE (0, 0), drawlayer, , (drawpositionX, drawpositionY)-(drawpositionX + screenwindow.width - 1, drawpositionY + screenwindow.height - 1)
  133.  
  134.  
  135.     'print text
  136.     COLOR _RGB(255, 255, 255)
  137.     _PRINTSTRING (0, 16), "w/a/s/d = move"
  138.     _PRINTSTRING (0, 32), "Escape = exit"
  139.      
  140.     _DISPLAY
  141.  
  142.  
  143. shutdown:
  144.  
  145.  
  146. keylistener:
  147. 'Esc
  148. IF _KEYDOWN(27) AND key27 = false THEN
  149.     key27 = _KEYDOWN(27)
  150. IF NOT _KEYDOWN(27) AND key27 <> false THEN
  151.     key27 = false
  152.  
  153. 'w
  154. IF _KEYDOWN(119) AND key119 = false THEN
  155.     key119 = _KEYDOWN(119)
  156. IF NOT _KEYDOWN(119) AND key119 <> false THEN
  157.     key119 = false
  158.  
  159. 'a
  160. IF _KEYDOWN(97) AND key97 = false THEN
  161.     key97 = _KEYDOWN(97)
  162. IF NOT _KEYDOWN(97) AND key97 <> false THEN
  163.     key97 = false
  164.  
  165. 's
  166. IF _KEYDOWN(115) AND key115 = false THEN
  167.     key115 = _KEYDOWN(115)
  168. IF NOT _KEYDOWN(115) AND key115 <> false THEN
  169.     key115 = false
  170.  
  171. 'd
  172. IF _KEYDOWN(100) AND key100 = false THEN
  173.     key100 = _KEYDOWN(100)
  174. IF NOT _KEYDOWN(100) AND key100 <> false THEN
  175.     key100 = false
  176.  

Thanks
« Last Edit: November 01, 2019, 11:01:14 am by MrFreyer »

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #1 on: November 01, 2019, 02:53:08 pm »
Seems to work beautifully for me, MrFreyer. Perhaps you could check your task manager, to see what else your PC is up to?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #2 on: November 01, 2019, 03:02:00 pm »
Seems to work beautifully for me, MrFreyer. Perhaps you could check your task manager, to see what else your PC is up to?

ditto!

Edit: oops no timers here :P
« Last Edit: November 01, 2019, 04:17:26 pm by bplus »

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #3 on: November 01, 2019, 03:34:57 pm »
The taskmanager looks relatively okay... but the program takes 50%-60% of my CPU and about 120MB RAM... guess because of the sortwareimages(?) :o

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Sometimes unexpected strong lags
« Reply #4 on: November 01, 2019, 03:47:01 pm »
Can’t you simplify the key handler quite a bit?  Less calls to KEYDOWN might improve performance.

From:

 
'a
IF _KEYDOWN(97) AND key97 = false THEN
    key97 = _KEYDOWN(97)
END IF
IF NOT _KEYDOWN(97) AND key97 <> false THEN
    key97 = false
END IF


To:

 
IF _KEYDOWN(97) THEN key97 = true ELSE key97 = false ‘a

One simple check to set if it’s down or not.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #5 on: November 01, 2019, 04:11:16 pm »
Can’t you simplify the key handler quite a bit?  Less calls to KEYDOWN might improve performance.

From:

 
'a
IF _KEYDOWN(97) AND key97 = false THEN
    key97 = _KEYDOWN(97)
END IF
IF NOT _KEYDOWN(97) AND key97 <> false THEN
    key97 = false
END IF


To:

 
IF _KEYDOWN(97) THEN key97 = true ELSE key97 = false ‘a

One simple check to set if it’s down or not.

It's maybe slightly (but not much) better...

Normaly I use something like

IF _KEYDOWN(KEYCODE~%(keyA)) AND controlinput.key(keyA) = false THEN
    controlinput.key(keyA) = true
ELSEIF NOT _KEYDOWN(KEYCODE~%(keyA)) AND controlinput.key(keyA) THEN
    controlinput.key(keyA) = false
END IF

as the keyhandle, because sometimes I need to lock a key while pressing, to only do a specific action just one time.

Now I followed your sugestion but it doesn't change much.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #6 on: November 01, 2019, 04:31:20 pm »
This is what is getting the CPU all worked up!
Code: QB64: [Select]
  1.     DO
  2.         frametimeCurrent = TIMER(.001)
  3.         frametime = (frametimeCurrent - frametimeLast)
  4.     LOOP UNTIL frametime >= 1 / 240 '240 is maximum frames per second
  5.  
  6.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #7 on: November 01, 2019, 04:57:23 pm »
Hi MrFreyer

it works well on my notebook  Toshiba celeron 1000 1.80Ghz  RAM 4.00giga, Windows10 64bit, QB64 1.3

but I see a not small different of size of ram used...  [ You are not allowed to view this attachment ]  
and the dimension of my exe is 2,041 kb.
Programming isn't difficult, only it's  consuming time and coffee

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #8 on: November 01, 2019, 05:08:48 pm »
...but why is the CPU usage that high?

Marked as best answer by MrFreyer on November 01, 2019, 02:02:48 pm

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #9 on: November 01, 2019, 05:22:27 pm »
:-)
simple

because you set it high...
Code: QB64: [Select]
  1. 'mainloop
  2. _LIMIT 240
  3.  
remember that the syntax is
Quote
_LIMIT NumberOfAction/FramePerSecond

in fact pulling down the amount of actions to do in a second we get this result
  [ You are not allowed to view this attachment ]  

and this is the secret
Code: QB64: [Select]
  1. 'mainloop
  2.     _LIMIT 40
Good Coding
Programming isn't difficult, only it's  consuming time and coffee

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #10 on: November 01, 2019, 06:01:02 pm »
ahh, I forgot that I set the LIMIT to 240 :D

Offline acjacques

  • Newbie
  • Posts: 33
    • View Profile
Re: Sometimes unexpected strong lags
« Reply #11 on: November 01, 2019, 07:44:30 pm »
Works fine for me too . Very good !
« Last Edit: November 01, 2019, 07:46:56 pm by acjacques »