PB 4.61 crazy, but working mouse code

Linux specific forum
Grunz
User
User
Posts: 59
Joined: Sat Nov 12, 2011 7:21 pm

PB 4.61 crazy, but working mouse code

Post by Grunz »

This example show how you can have a working mouse for a windowed application.
It circumvents the usual problems of grab/release for the mouse by not grabbing at all.
The crazy part here is , that ExamineMouse() is not called at at.
Disadvantage is that the system mouse cursor will stay.
Also note , that this method does not work under Windows.

Code: Select all

InitSprite()
InitKeyboard()

Define sp1

OpenWindow(0, 100, 100, 640, 480, "Window Moving Test")
OpenWindowedScreen(WindowID(0), 0, 0, 640, 480, 0, 0, 0)

 If Not sp1
     sp1 = CreateSprite(#PB_Any, 20, 20, #PB_Sprite_Texture)
      If StartDrawing(SpriteOutput(sp1))
        Box(0, 0, 20, 20, RGB(255, 0, 155))
        Box(5, 5, 10, 10, RGB(155, 0, 255))
        StopDrawing()
     EndIf
   EndIf

If InitMouse()

   Repeat
      Repeat
         ev =WindowEvent()
      Until ev = 0

      i +1

      mouse_x = WindowMouseX(0)
      mouse_y = WindowMouseY(0)
      mouse_button_left  = MouseButton(#PB_MouseButton_Left)
      mouse_button_right = MouseButton(#PB_MouseButton_Right)

      ExamineKeyboard()

      ClearScreen(RGB(0,0,0))

      If IsSprite(sp1) And mouse_x > -1
         DisplaySprite(sp1,mouse_x-10,mouse_y-10)
      EndIf

      FlipBuffers()

      If KeyboardPushed(#PB_Key_Escape)
         Break
      EndIf

   ForEver

Else
   PrintN("Can't access mouse!")
EndIf
Of course, you can still not read the mouse wheel or more than 2 buttons.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: PB 4.61 crazy, but working mouse code

Post by BasicallyPure »

I saw some problems with your code so I changed it a bit.
As it was there was no user feedback about the mouse buttons status.
Since that seemed to be the point of your post I thought this might illustrate it better.

So reading the help document I wouldn't expect this to work without calling 'ExamineMouse()'.
The evidence says otherwise.

Even though it seems to work I wouldn't trust it in any serious application unless we find
out it is reliable.

Code: Select all

EnableExplicit

If InitSprite() And InitKeyboard() And InitMouse()
   
Else
   MessageRequester("Error", "Initialization failed.")
   End
EndIf

OpenWindow(0, 100, 100, 640, 480, "Window Moving Test")
OpenWindowedScreen(WindowID(0), 0, 0, 640, 480, 0, 0, 0)

Define.i sp1 = CreateSprite(#PB_Any, 20, 20, #PB_Sprite_Texture)
If sp1
   If StartDrawing(SpriteOutput(sp1))
      Box(0, 0, 20, 20, RGB(255, 0, 155))
      Box(5, 5, 10, 10, RGB(155, 0, 255))
      StopDrawing()
   EndIf
Else
   MessageRequester("Error", "CreateSprite failed.")
   End
EndIf

SetFrameRate(30)

Repeat
   
   While WindowEvent() : Wend
   
   Define mouse_x = WindowMouseX(0)
   Define mouse_y = WindowMouseY(0)
   
   ; --- this should not work without calling 'ExamineMouse()'---
   ;ExamineMouse()
   If MouseButton(#PB_MouseButton_Left)
      Debug "left button pressed"
   EndIf
   
   If MouseButton(#PB_MouseButton_Right)
      Debug "right button pressed"
   EndIf
   ; -------------------------------------------------------------
   
   If IsSprite(sp1) And mouse_x <> -1 And mouse_y <> -1
      ClearScreen(0)
      DisplaySprite(sp1,mouse_x-10,mouse_y-10)
      FlipBuffers()
   EndIf
   
   ExamineKeyboard()
   
   If KeyboardPushed(#PB_Key_Escape)
      Break
   EndIf
   
ForEver
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
Post Reply