Page 1 of 1

Slow code !! Why ?

Posted: Sun Oct 12, 2025 11:33 am
by SPH
Hi,
How come it's so slow?
PB6.20 or lower

Code: Select all

InitSprite()
InitMouse()
OpenWindow(0, 0, 0, 800, 800, "Pan!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 800)

CreateSprite(0,200,200)
StartDrawing(SpriteOutput(0))
DrawingMode(#PB_2DDrawing_Outlined)      
LineXY(99,0,99,200,RGB(0,0,20))
LineXY(0,99,200,99,RGB(0,0,20))
LineXY(101,0,101,200,RGB(0,0,20))
LineXY(0,101,200,101,RGB(0,0,20))
Circle(100,100,80,RGB(0,0,20))
Circle(100,100,40,RGB(0,0,20))
StopDrawing()

CreateSprite(1, 800, 800)
StartDrawing(SpriteOutput(1))
DrawingMode(#PB_2DDrawing_Default)      
For i= 400 To 10 Step -50
  If u=1
    rvb=RGB(255,50,50)
  Else
    rvb=RGB(250,250,250)
  EndIf
  u+1
  u%2
  Circle(400, 400, i, rvb)
Next
LineXY(0,400,800,400,RGB(0,0,0))
LineXY(400,0,400,800,RGB(0,0,0))
;     DrawText(50, 50, "PureBasic")
StopDrawing() 


Repeat
  Event = WaitWindowEvent()
  
  DisplaySprite(1, 0, 0)
  ExamineMouse()
  MX=MouseX()
  MY=MouseY()
  
  DisplayTransparentSprite(0,MX,MY)
  If MouseButton(1)
    End
  EndIf
  
  FlipBuffers()
Until Event = #PB_Event_CloseWindow




Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 11:43 am
by Cyllceaux
It's the "WaitWindowEvents". I had the same problem. WaitWindowsEvent do not work here as you expected. You can do something like that WaitWindowsEvent (10) or do a WindowEvent instead.

Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 11:47 am
by SPH
Even with 10, 100, or even 1000, it's still intolerably slow!
What did I miss compared to the smooth codes of 10 years ago?

Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 12:01 pm
by BarryG
Looks like ExamineMouse() is causing the slowdown. Same code without that command:

Code: Select all

InitSprite()
InitMouse()

OpenWindow(0, 0, 0, 800, 800, "Pan!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 800)

CreateSprite(0,200,200)
StartDrawing(SpriteOutput(0))
DrawingMode(#PB_2DDrawing_Outlined)      
LineXY(99,0,99,200,RGB(0,0,20))
LineXY(0,99,200,99,RGB(0,0,20))
LineXY(101,0,101,200,RGB(0,0,20))
LineXY(0,101,200,101,RGB(0,0,20))
Circle(100,100,80,RGB(0,0,20))
Circle(100,100,40,RGB(0,0,20))
StopDrawing()

CreateSprite(1, 800, 800)
StartDrawing(SpriteOutput(1))
DrawingMode(#PB_2DDrawing_Default)      

For i= 400 To 10 Step -50
  If u=1
    rvb=RGB(255,50,50)
  Else
    rvb=RGB(250,250,250)
  EndIf
  u+1
  u%2
  Circle(400, 400, i, rvb)
Next

LineXY(0,400,800,400,RGB(0,0,0))
LineXY(400,0,400,800,RGB(0,0,0))
;     DrawText(50, 50, "PureBasic")
StopDrawing() 

Repeat
  
  Event = WaitWindowEvent()
  
  newx=WindowMouseX(0)-100
  newy=WindowMouseY(0)-100
  
  If newx<>oldx Or newy<>oldy
    oldx=newx
    oldy=newy
    DisplaySprite(1, 0, 0)
    DisplayTransparentSprite(0,newx,newy)
    FlipBuffers()
  EndIf
  
  If GetAsyncKeyState_(#VK_LBUTTON) & $8000 <> 0 ; Detect left mouse click on Windows.
    End
  EndIf
  
Until Event = #PB_Event_CloseWindow

Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 12:06 pm
by Cyllceaux
Or you can Try this:

OpenWindowedScreen(WindowID(0), 0, 0, 800, 800,#False,0,0,#PB_Screen_NoSynchronization)

Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 12:18 pm
by BarryG
Cyllceaux wrote: Sun Oct 12, 2025 12:06 pm#PB_Screen_NoSynchronization
That works, too. But for the best efficiency you should only do the display and FlipBuffers() if X/Y is actually different to the prev X/Y as well, like I showed. No point updating the pointer with every event unless it's actually moved. ;)

Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 12:38 pm
by pjay
The mouse movements are being caught in a queue as you're only processing one OS event per frame, whereas you should be handling all of them.

Replace:

Code: Select all

Event = WaitWindowEvent()
With:

Code: Select all

  Repeat
    Event = WindowEvent()
    Select Event
       Case #PB_Event_CloseWindow : End 
    EndSelect
  Until Event = 0

Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 1:14 pm
by RASHAD
Hi

Code: Select all

InitSprite()
InitMouse()
OpenWindow(0, 0, 0, 800, 800, "Pan!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 800,0,0,0,#PB_Screen_NoSynchronization )

CreateSprite(0,200,200,#PB_Sprite_AlphaBlending)
StartDrawing(SpriteOutput(0))
DrawingMode(#PB_2DDrawing_Outlined)      
LineXY(99,0,99,200,RGB(0,0,20))
LineXY(0,99,200,99,RGB(0,0,20))
LineXY(101,0,101,200,RGB(0,0,20))
LineXY(0,101,200,101,RGB(0,0,20))
Circle(100,100,80,RGB(0,0,20))
Circle(100,100,40,RGB(0,0,20))
StopDrawing()

CreateSprite(1, 800, 800,#PB_Sprite_AlphaBlending)
StartDrawing(SpriteOutput(1))
DrawingMode(#PB_2DDrawing_Default)      
For i= 400 To 10 Step -50
  If u=1
    rvb=RGB(255,50,50)
  Else
    rvb=RGB(250,250,250)
  EndIf
  u+1
  u%2
  Circle(400, 400, i, rvb)
Next
LineXY(0,400,800,400,RGB(0,0,0))
LineXY(400,0,400,800,RGB(0,0,0))
;     DrawText(50, 50, "PureBasic")
StopDrawing() 


Repeat
  Event = WaitWindowEvent(10)
  ExamineMouse()  
  DisplaySprite(1, 0, 0) 
  MX=WindowMouseX(0)-100
  MY=WindowMouseY(0)-100
  
  DisplayTransparentSprite(0,MX,MY)  
  If MouseButton(1)
    End
  Else
    FlipBuffers()
  EndIf  
  
Until Event = #PB_Event_CloseWindow





Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 2:14 pm
by NicTheQuick
pjay wrote: Sun Oct 12, 2025 12:38 pm The mouse movements are being caught in a queue as you're only processing one OS event per frame, whereas you should be handling all of them.
That's the right way to do it.

Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 3:01 pm
by AZJIO
It would be nice to add a #PB_Screen_NoEsc constant to the OpenScreen() and OpenWindowedScreen() functions, which would work during debugging, so that the window could be easily closed with the Esc key by default.

Re: Slow code !! Why ?

Posted: Sun Oct 12, 2025 4:39 pm
by SPH
pjay wrote: Sun Oct 12, 2025 12:38 pm The mouse movements are being caught in a queue as you're only processing one OS event per frame, whereas you should be handling all of them.

Replace:

Code: Select all

Event = WaitWindowEvent()
With:

Code: Select all

  Repeat
    Event = WindowEvent()
    Select Event
       Case #PB_Event_CloseWindow : End 
    EndSelect
  Until Event = 0
Of course, you're right!!
I was only processing one event per loop...
Thanks

and thx for all users :idea: