Slow code !! Why ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 584
Joined: Tue Jan 04, 2011 6:21 pm

Slow code !! Why ?

Post 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




!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
Cyllceaux
Enthusiast
Enthusiast
Posts: 514
Joined: Mon Jun 23, 2014 1:18 pm

Re: Slow code !! Why ?

Post 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.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 584
Joined: Tue Jan 04, 2011 6:21 pm

Re: Slow code !! Why ?

Post 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?

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
BarryG
Addict
Addict
Posts: 4206
Joined: Thu Apr 18, 2019 8:17 am

Re: Slow code !! Why ?

Post 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
Cyllceaux
Enthusiast
Enthusiast
Posts: 514
Joined: Mon Jun 23, 2014 1:18 pm

Re: Slow code !! Why ?

Post by Cyllceaux »

Or you can Try this:

OpenWindowedScreen(WindowID(0), 0, 0, 800, 800,#False,0,0,#PB_Screen_NoSynchronization)
BarryG
Addict
Addict
Posts: 4206
Joined: Thu Apr 18, 2019 8:17 am

Re: Slow code !! Why ?

Post 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. ;)
pjay
Enthusiast
Enthusiast
Posts: 277
Joined: Thu Mar 30, 2006 11:14 am

Re: Slow code !! Why ?

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4972
Joined: Sun Apr 12, 2009 6:27 am

Re: Slow code !! Why ?

Post 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




Egypt my love
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Slow code !! Why ?

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
AZJIO
Addict
Addict
Posts: 2211
Joined: Sun May 14, 2017 1:48 am

Re: Slow code !! Why ?

Post 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.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 584
Joined: Tue Jan 04, 2011 6:21 pm

Re: Slow code !! Why ?

Post 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:

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
Post Reply