WindowEvent() lag?

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

WindowEvent() lag?

Post by Joubarbe »

I have a little library that opens a screen, either windowed or full screen. The main loop is as follow:

Code: Select all

Procedure MainLoop()
    Define.i event, frame_time, delta_time.f, mouse_released.b
    
    Repeat
      If view\display_mode <> #DISPLAY_FULLSCREEN
        Repeat
          ; Handle window events.
          event = WindowEvent()
          Select event
            Case #PB_Event_ActivateWindow, #PB_Event_LeftClick
              ReleaseMouse(#False) : mouse_released = #False
            Case #PB_Event_DeactivateWindow
              ReleaseMouse(#True) : mouse_released = #True
          EndSelect
          If event
            view\callback_window_events(event)
          EndIf
        Until event = 0
      EndIf
      
      ; Handle input events.
      If mouse_released = #False
        ExamineMouse() : ExamineKeyboard()
        If view\display_mode = #DISPLAY_WINDOWED And KeyboardReleased(#PB_Key_Escape)
          ReleaseMouse(#True) : mouse_released = #True
        EndIf
        view\callback_input_events()
      EndIf
      
      ; Update graphics.
      delta_time = (ElapsedMilliseconds() - frame_time) / 1000
      frame_time = ElapsedMilliseconds()
      view\callback_update(delta_time)
      
      ; Render graphics.
      ClearScreen(0)
      view\callback_render()
      If view\show_debug_bar
        UpdateDebugBar()
        DisplaySprite(view\debug_bar_sprite, 0, 0)
      EndIf
      
      FlipBuffers()
    ForEver
  EndProcedure
  
The problem I encounter is that Windowed or Borderless become very laggy when I move my mouse. The events processing seems to reduce my FPS to up to 30.

To reproduce, download the repo and comment/uncomment those lines in xyView_Tests.pb: (33, 34)

Code: Select all

xyView::StartLoopInWindowed(500, 300, "title") ; Lags when mouse moves.
; xyView::StartLoopInFullScreen("title") ; Doesn't lag.
You can see the FPS at the top right corner. I usually go from 144 to 120 by just continuously moving the mouse.
Would appreciate if someone could take a look at this and confirm :)
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: WindowEvent() lag?

Post by Fred »

You need to prcoess all the event every frame, as shown in the example 1:

https://www.purebasic.com/documentation ... creen.html
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: WindowEvent() lag?

Post by Joubarbe »

Is it not what I'm doing already...?

Code: Select all

Repeat
  ; [...]
  Repeat
    ; Handle window events.
    event = WindowEvent()
    Select event
      Case #PB_Event_ActivateWindow, #PB_Event_LeftClick
        ReleaseMouse(#False) : mouse_released = #False
      Case #PB_Event_DeactivateWindow
        ReleaseMouse(#True) : mouse_released = #True
    EndSelect
    If event
      view\callback_window_events(event)
    EndIf
  Until event = 0
  ; [...]
  FlipBuffers()
ForEver
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: WindowEvent() lag?

Post by Fred »

Yes, sorry. Should work fine then, could you post a full working snippet ?
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: WindowEvent() lag?

Post by Joubarbe »

Would be good if you could test the whole thing with the Github source actually. There's a nice pbp file to run :) And maybe it's a thing I do wrong in that project.

Note that I'm running PB 5.73 LTS.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: WindowEvent() lag?

Post by #NULL »

I get like 40 fps with your code, and 60 fps if I disable the screen drawing block. Maybe that's normal?
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: WindowEvent() lag?

Post by Joubarbe »

If I remove the debug bar thing, I still get a FPS loss (using MSI companion to measure it) when moving the mouse.

EDIT: Do you have the same FPS in fullscreen and other modes?
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: WindowEvent() lag?

Post by Joubarbe »

#NULL wrote: Thu Jan 20, 2022 8:17 pm I get like 40 fps with your code, and 60 fps if I disable the screen drawing block. Maybe that's normal?
Sorry to insist on this, but fullscreen is 144 FPS, other modes go down to 110. That's not normal, and the real-time sprite drawing does not change much (as it's perfectly fine when in fullscreen).
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: WindowEvent() lag?

Post by #NULL »

StartDrawing() is slower than DisplaySprite() etc., that's how it is. And in windowed screen your refresh rate will be limited by your desktop refresh rate.
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: WindowEvent() lag?

Post by Joubarbe »

I think you misunderstand. My refresh rate is 144. In full screen, I can keep that FPS all along, and I cannot do the same in window mode, because as soon as I move my mouse, the rate drops drastically. And my assumption is that the WindowEvent() inside loop is responsible. It has nothing to do with StartDrawing(), as it does not effect my FPS in full screen (and PB is able to do real time drawing on a small surface without being too heavy on performance).
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: WindowEvent() lag?

Post by #NULL »

Yes I misunderstood, got it now. You should try to reduce your lib code to a simple windowedscreen-with-mouse example that reproduces the problem.
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: WindowEvent() lag?

Post by Joubarbe »

Code: Select all


#DISPLAY_FULLSCREEN = #PB_MessageRequester_Yes
#DISPLAY_WINDOW = #PB_MessageRequester_No

display_mode = MessageRequester("", "Run fullscreen mode? No is window.", #PB_MessageRequester_YesNo)

If display_mode = #DISPLAY_FULLSCREEN
  ExamineDesktops()
  width = DesktopWidth(0)
  height = DesktopHeight(0)
Else
  width = 640
  height = 480
EndIf

InitSprite() : InitMouse() : InitKeyboard()
If display_mode = #DISPLAY_FULLSCREEN
  OpenScreen(width, height, 32, "")
ElseIf display_mode = #DISPLAY_WINDOW
  window = OpenWindow(#PB_Any, 0, 0, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(window), 0, 0, width, height)
EndIf

Global fps_sprite = CreateSprite(#PB_Any, 60, 25)

Procedure UpdateFPS()
  Static fps.i, frame_refresh.i
  Define fps$
  
  fps + 1
  If ElapsedMilliseconds() > frame_refresh
    frame_refresh = ElapsedMilliseconds() + 1000
    fps$ = Str(fps) + " FPS"
    fps = 0
  EndIf
  
  StartDrawing(SpriteOutput(fps_sprite))
  DrawText(OutputWidth() - TextWidth(fps$) - 2, 2, fps$, #White, #Black)
  StopDrawing()
EndProcedure

Repeat
  If display_mode = #DISPLAY_WINDOW
    Repeat
      event = WindowEvent()
      If event = #PB_Event_CloseWindow : End : EndIf
    Until event = 0
  EndIf
  
  ExamineMouse() : ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Escape) : End : EndIf
  
  ClearScreen(0)
  
  UpdateFPS()
  DisplaySprite(fps_sprite, 5, 5)
  
  FlipBuffers()
ForEver
1/ So with that code, I only have a max FPS of 45, and I don't know why.
2/ But the problem remains the same. In window mode, moving the mouse makes the FPS go down. Not in fullscreen. Measures taken with MSI tools give the same results.
Last edited by Joubarbe on Wed Jan 26, 2022 3:53 pm, edited 1 time in total.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: WindowEvent() lag?

Post by Paul »

On my PC your code gives 61fps in both FullScreen and Window mode.
FPS does not change when mouse is moved.

Now if I go into Windows Display Settings and then under Advanced Display Settings I see my monitor Refresh Rate is set to 60 Hz.
If I change this number to 50 Hz, then the highest FPS I get from your code is 51 Hz.
Image Image
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: WindowEvent() lag?

Post by #NULL »

I'm on linux, so probably not helpful. But I get 1 fps with your last code. If I comment out the sprite drawing block, or if I move it into the If-Block (where it makes more sense), then I get 60 fps. But either way it's not affected by moving the mouse.
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: WindowEvent() lag?

Post by Joubarbe »

Should I file a bug report or is everything's working as expected...? Three users, three different behaviours, that looks weird :)
Post Reply