Problem with mouse in games

Advanced game related topics
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Problem with mouse in games

Post by skinkairewalker »

is there any way that when the user clicks only on the windowed screen area, it works only there ... and when the user drag the mouse out of the area that the windowed screen occupies, he can use other software?

because according to the example below, the mouse only starts to be "recognized" by the windowed screen when he clicks the "grab input" button, and only releases the mouse if you press f1 ...

is there any way so that the user does not need to press any button to get the mouse input and does not need to press any button to release the mouse input? as if it were a simple form?

example >

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Can't open the sprite system", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 340, 285, "Gadget and sprites!", #PB_Window_ScreenCentered)
  ButtonGadget(1, 10,  10, 100, 25, "Grab input")
  ButtonGadget(2, 120,  10, 100, 25, "Button 2")
  ButtonGadget(3, 230,  10, 100, 25, "Button 3")
  TextGadget  (4, 10, 40, 300, 30, "Mouse and keyboard released")

  If OpenWindowedScreen(WindowID(0), 10, 70, 320, 200, 0, 0, 0)
    LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasicLogo.bmp")
    StickyWindow(0,1)
    direction = 1
    playerX = 1
    playerY = 1
    
    ; Start with released input
    ReleaseMouse(#True)
    InputReleased = 1
    
    Repeat
      Repeat
        ; Always process all the events to flush the queue at every frame
        Event = WindowEvent()
        
        Select Event
          Case #PB_Event_CloseWindow
            Quit = 1
        
          Case #PB_Event_Gadget
            
            ; Do the normal application management here
            Gadget = EventGadget()
        
            Select Gadget
              Case 1
                InputReleased = 0
                ReleaseMouse(#False)
                SetGadgetText(4, "Press 'F1' to ungrab keyboard and mouse")
    
              Case 2, 3
                SetGadgetText(4, "Button "+Str(Gadget)+" pressed.")
            EndSelect
        
        EndSelect
        
      Until Event = 0 ; Quit the event loop only when no more events are available
      
      ExamineKeyboard()
      
      If InputReleased = 0
    
        ExamineMouse()
    
        ; do the sprite & screen management at every frame
        If KeyboardPushed(#PB_Key_Up)    And playerY > 0   : playerY -3 : EndIf  
        If KeyboardPushed(#PB_Key_Down)  And playerY < 280 : playerY +3 : EndIf  
        If KeyboardPushed(#PB_Key_Left)  And playerX > 0   : playerX -3 : EndIf  
        If KeyboardPushed(#PB_Key_Right) And playerX < 300 : playerX +3 : EndIf  
    
        If KeyboardPushed(#PB_Key_F1)
          ReleaseMouse(#True)
          InputReleased = 1
          SetGadgetText(4, "Mouse and keyboard released");
        EndIf
      EndIf
      
      ; Clear the screen and draw our sprites
      ClearScreen(RGB(0,0,0))
      ClipSprite(0, 0, 0, x, x/8)
      DisplaySprite(0, x, 100)
      DisplaySprite(0, x, x)
      DisplaySprite(0, 300-x, x)
      DisplaySprite(0, playerX, playerY)
    
      x + direction
      If x > 300 : direction = -1 : EndIf   ; moving back to the left with negative value
      If x < 0   : direction =  1 : EndIf   ; moving to the right with positive value
        
      FlipBuffers()       ; Inverse the buffers (the back become the front (visible)... and we can do the rendering on the back
    
    Until  Quit Or KeyboardPushed(#PB_Key_Escape)
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
  EndIf
EndIf
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Problem with mouse in games

Post by #NULL »

You can capture/release when the mouse is at the screen edges.
Maybe this works:
viewtopic.php?p=523759#p523759
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Problem with mouse in games

Post by DK_PETER »

Use MouseDeltaX() and MouseDeltaY() together with MouseX() and MouseY()

Currently I have severe problems with Windowed mode and MouseDelta values, but you could
use something like this:

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Can't open the sprite system", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 340, 285, "Gadget and sprites!", #PB_Window_ScreenCentered)
  ButtonGadget(1, 10,  10, 100, 25, "Grab input")
  ButtonGadget(2, 120,  10, 100, 25, "Button 2")
  ButtonGadget(3, 230,  10, 100, 25, "Button 3")
  TextGadget  (4, 10, 40, 300, 30, "Mouse and keyboard released")

  If OpenWindowedScreen(WindowID(0), 10, 70, 320, 200, 0, 0, 0)
    LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasicLogo.bmp")
    StickyWindow(0,1)
    direction = 1
    playerX = 1
    playerY = 1
    ms.i = CreateSprite(#PB_Any, 10, 10)
    StartDrawing(SpriteOutput(ms))
    Circle(5,5,5, $FF00FF)
    StopDrawing()
    ; Start with released input
    ReleaseMouse(#True)
    InputReleased = 1
   
    Repeat
      Repeat
        ; Always process all the events to flush the queue at every frame
        Event = WindowEvent()
       
        Select Event
          Case #PB_Event_CloseWindow
            Quit = 1
       
          Case #PB_Event_Gadget
           
            ; Do the normal application management here
            Gadget = EventGadget()
       
            Select Gadget
              Case 1
                InputReleased = 0
                ReleaseMouse(#False)
                SetGadgetText(4, "Press 'F1' to ungrab keyboard and mouse")
   
              Case 2, 3
                SetGadgetText(4, "Button "+Str(Gadget)+" pressed.")
            EndSelect
       
        EndSelect
       
      Until Event = 0 ; Quit the event loop only when no more events are available
     
      ExamineKeyboard()
     
      If InputReleased = 0
   
        ExamineMouse()
   
        ; do the sprite & screen management at every frame
        If KeyboardPushed(#PB_Key_Up)    And playerY > 0   : playerY -3 : EndIf 
        If KeyboardPushed(#PB_Key_Down)  And playerY < 280 : playerY +3 : EndIf 
        If KeyboardPushed(#PB_Key_Left)  And playerX > 0   : playerX -3 : EndIf 
        If KeyboardPushed(#PB_Key_Right) And playerX < 300 : playerX +3 : EndIf 
   
        If (MouseDeltaX() + MouseX()) < 0 Or (MouseDeltaX() + MouseX()) > ScreenHeight() Or (MouseDeltaY() + MouseY()) < 0 Or (MouseDeltaY()+MouseY()) > ScreenHeight()
          ReleaseMouse(#True)
          InputReleased = 1
          SetGadgetText(4, "Mouse and keyboard released");
        EndIf
      EndIf
     
      ; Clear the screen and draw our sprites
      ClearScreen(RGB(0,0,0))
      ClipSprite(0, 0, 0, x, x/8)
      DisplaySprite(0, x, 100)
      DisplaySprite(0, x, x)
      DisplaySprite(0, 300-x, x)
      DisplaySprite(0, playerX, playerY)
   
      x + direction
      If x > 300 : direction = -1 : EndIf   ; moving back to the left with negative value
      If x < 0   : direction =  1 : EndIf   ; moving to the right with positive value
      DisplaySprite(ms, MouseX(), MouseY())
      FlipBuffers()       ; Inverse the buffers (the back become the front (visible)... and we can do the rendering on the back
   
    Until  Quit Or KeyboardPushed(#PB_Key_Escape)
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
  EndIf
EndIf
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Problem with mouse in games

Post by IdeasVacuum »

.... it's not a problem at all with the OpenGL Gadget (no code required, it works as "just another gadget").

So if enough PB'ers requested, perhaps Fred would create a Screen Gadget?

You could of course define your game in OpenGL instead........

Will Ogre ever be good enough or has the modern Vulcan platform already overtaken it?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Problem with mouse in games

Post by DK_PETER »

IdeasVacuum wrote:.... it's not a problem at all with the OpenGL Gadget (no code required, it works as "just another gadget").

So if enough PB'ers requested, perhaps Fred would create a Screen Gadget?

You could of course define your game in OpenGL instead........

Will Ogre ever be good enough or has the modern Vulcan platform already overtaken it?
Personally, the Ogre3D implementation has done a pretty good job and I really like it.
Granted, an updated version would be a very nice touch, but it can do a lot as it is.
For some reason I really don't like OpenGL and I find it too much of a hazzle/tedious to work with.
Maybe one day, my opinion about opengl will change but it hasn't so far.. :?
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Problem with mouse in games

Post by Mijikai »

Mby also check out this mouse module:
https://www.purebasic.fr/german/viewtop ... 43#p344843
Post Reply