Page 1 of 1

Problem with mouse in games

Posted: Sun Jan 19, 2020 6:20 am
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

Re: Problem with mouse in games

Posted: Sun Jan 19, 2020 8:54 am
by #NULL
You can capture/release when the mouse is at the screen edges.
Maybe this works:
viewtopic.php?p=523759#p523759

Re: Problem with mouse in games

Posted: Sun Jan 19, 2020 11:05 am
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

Re: Problem with mouse in games

Posted: Sun Jan 19, 2020 1:37 pm
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?

Re: Problem with mouse in games

Posted: Sun Jan 19, 2020 1:51 pm
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.. :?

Re: Problem with mouse in games

Posted: Thu Feb 13, 2020 11:20 am
by Mijikai
Mby also check out this mouse module:
https://www.purebasic.fr/german/viewtop ... 43#p344843