It is currently Sun Mar 29, 2020 8:07 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: [Done] SetFrameRate() eats up CPU!
PostPosted: Tue Oct 03, 2017 8:58 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
If i set SetFrameRate() < 60 my CPU gets eaten up (8 - 20%)!
Tested on PB v.5.61 & 5.42 x64 & x86

Code:
#TITLE = "TestWindow"
#SCREEN_WIDTH = 800
#SCREEN_HEIGHT = 600

Global Window.i, Event.i, Exit.i

Window = OpenWindow(#PB_Any,#Null,#Null,#SCREEN_WIDTH,#SCREEN_HEIGHT,#TITLE,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
If Window And InitSprite() And InitKeyboard()
  If OpenWindowedScreen(WindowID(Window),#Null,#Null,#SCREEN_WIDTH,#SCREEN_HEIGHT,#True,#Null,#Null)
    SetFrameRate(30);If < 60 CPU gets eaten!
    Repeat
      Repeat
        Event = WindowEvent()
        If Event = #PB_Event_CloseWindow
          Exit = #True
        EndIf
      Until Event = #Null
      ClearScreen(0)
      FlipBuffers()
    Until Exit = #True
  EndIf
EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: SetFrameRate() eats up CPU!
PostPosted: Fri Feb 08, 2019 1:50 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
PB [5.70]
SetFrameRate() is still unusable (cpu ~ 25% for nothing!) with framerates < 60!


Top
 Profile  
Reply with quote  
 Post subject: Re: SetFrameRate() eats up CPU!
PostPosted: Tue Feb 04, 2020 11:08 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14009
Location: France
SetFrameRate() is built with performance in mind, so it's the expected behaviour. We could may be add a mode to have a less precise frame rate but release some CPU time


Top
 Profile  
Reply with quote  
 Post subject: Re: SetFrameRate() eats up CPU!
PostPosted: Tue Feb 04, 2020 11:41 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2318
Location: Germany
Some calculate with ElapsedMilliseconds a stable delay time for Delay()
Code:
Define msec_pro_frame = 1000 / 60

Repeat
  msec_start = ElapsedMilliseconds()
 
  ;
  Delay(Random(8))
  ;
 
  msec_diff = ElapsedMilliseconds() - msec_start
 
  msec_delay = msec_pro_frame - msec_diff
  If msec_delay > 0
    Debug "Delay " + msec_delay
    Delay(msec_delay)
  EndIf
Until msec_start > 10000

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: SetFrameRate() eats up CPU!
PostPosted: Wed Feb 05, 2020 1:10 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 05, 2007 5:31 pm
Posts: 702
Location: Linz, Austria
Mijikai wrote:
PB [5.70]
SetFrameRate() is still unusable (cpu ~ 25% for nothing!) with framerates < 60!
This is especially annoying since you have to SetFrameRate(59) to get real 60fps!

If i run the loop 1000 times with SetFrameRate(60) it takes 16.6518 seconds (= 60.053 FPS), with SetFrameRate(59) it takes 16.6659 seconds (= 60.002 FPS)


Top
 Profile  
Reply with quote  
 Post subject: Re: SetFrameRate() eats up CPU!
PostPosted: Wed Feb 05, 2020 8:15 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
Issues with the PB 2D game loop.

With vSync:
A game will never run correctly on all systems without setting the framerate (additional timing) or using the deltatime (or both).

Without vSync or SetFrameRate():
Timing code needed!

But a game will still not run correctly on all systems if we use a sheduler bound delay for timing.
(Ex. sheduler bound delays like Sleep_() have a very poor resolution ~ 10 ms)
A busy loop will also work - basically we just block the cpu for no reason - ugly.


In both cases we will need the deltatime...

However if we solely use the deltatime for timing we will have a very hard time doing physics.
(Ex. for physics u would need to implement a step loop to account for any displacement which is ugly and slow.
So only relying on the deltatime is not a good solution.)
Only use it for doing the final adjustments.


It would be nice to have an example on how to do the timing for 2D games.


Top
 Profile  
Reply with quote  
 Post subject: Re: SetFrameRate() eats up CPU!
PostPosted: Wed Feb 05, 2020 9:42 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14009
Location: France
Delay() has now a very good resolution (about 1 ms) so it could be used for this. I will dig up a bit more to see if I can do a set framerate which is a bit easier on CPU while still having perfect resolution.


Top
 Profile  
Reply with quote  
 Post subject: Re: SetFrameRate() eats up CPU!
PostPosted: Wed Feb 05, 2020 12:57 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
Fred wrote:
Delay() has now a very good resolution (about 1 ms) so it could be used for this. I will dig up a bit more to see if I can do a set framerate which is a bit easier on CPU while still having perfect resolution.


Sounds great :)

Just some idea:

Mby. Delay() can return the resolution of the internal clock.
ElpasedMilliseconds() may need a extra function.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye