Windowed Screen MouseRelease

Just starting out? Need help? Post your questions and find answers here.
mbecerra
User
User
Posts: 15
Joined: Mon Feb 28, 2005 4:59 am

Windowed Screen MouseRelease

Post by mbecerra »

I have this very simple example that opens a Windowed screen. When the screen is opened the mouse is captured to the screen. When the user presses F12 the mouse is released. I added a ActivateWindow event so when the user clicks on the game window, the mouse is recapture. However for some reason that doesn't seem to happen. Any help in this matter would be greatly appreciated.

PureOoze

Code: Select all

; ------------------------------------------------------------

;

;   PureOoze - Simple Game

;

;    (c) PureOoze

;

; ------------------------------------------------------------



; if the sprite subsystem fails to initalize

If InitSprite() = 0

  

  ; display an error message

  MessageRequester("Error", "Failed to open the sprite system", 0)

  

  ;and terminate

  End

  

EndIf



; if the mouse subsystem fails to initalize

If InitMouse() = 0

  

  ; display an error message

  MessageRequester("Error", "Failed to open the mouse system", 0)

  

  ;and terminate

  End

  

EndIf



; if the keyboard subsystem fails to initalize

If InitKeyboard() = 0

  

  ; display an error message

  MessageRequester("Error", "Failed to open the keyboard system", 0)

  

  ;and terminate

  End

  

EndIf



; open the game window

If OpenWindow(0, 0, 0, 320, 200, "PureOoze", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  

  ;if the game window was opened, open a screen inside the window for double buffering

  If OpenWindowedScreen(WindowID(0), 0, 0, 320, 200, 0, 0, 0) = 0

    

    ; the window couldn't be opened so display an error message

    MessageRequester("Error", "Failed to open double buffered windowed!", 0)

    

    ; and terminate

    End

    

  EndIf

  

Else

  

  ; the game window couldn't be opened so display an error message

    MessageRequester("Error", "Failed to open game window!", 0)

    

    ; and terminate

    End



EndIf



; main game loop

Repeat

  

  ; event loop

  Repeat

    

    ; Always process all the events to flush the queue at every frame

    Event = WindowEvent()

    

    ; determine which event occured

    Select Event

        

      ; the user closed the window

      Case #PB_Event_CloseWindow

        

        ; set quit to true

        Quit = 1

        

      Case #PB_Event_ActivateWindow

        

        ReleaseMouse(#False)

        

        InputReleased = 0

     

    EndSelect

    

  ; Quit the event loop only when no more events are available

  Until Event = 0

  

  ; if the mouse hasn't been released

  If InputReleased = 0

    

    ; check for mouse input

    ExamineMouse()



    ; check for keyboard input

    ExamineKeyboard()

    

    ; if the user pressed F12

    If KeyboardPushed(#PB_Key_F12)

      

      ; release the mouse

      ReleaseMouse(#True)

      

      ; and set InputReleased to true

      InputReleased = 1

      

    EndIf



  EndIf

  

  ; Clear the screen and draw

  ClearScreen(RGB(192,192,192))

  

  ; Inverse the buffers  

  FlipBuffers()



Until  Quit Or KeyboardPushed(#PB_Key_Escape)



; IDE Options = PureBasic 4.51 (Windows - x86)

; EnableXP

; SubSystem = DirectX7
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Re: Windowed Screen MouseRelease

Post by Derek »

Hi, I think it's because you're checking to see if your window gets the focus but it sometimes has it already so it doesn't reactivate. Hope that makes sense.

What I would do is open an invisible window at the same time as your window and then activate that one when you press F12, that way your window always loses focus and then will always regain focus when you click on it again.

I made a couple of alterations in your program, hope it helps.

Code: Select all

; if the sprite subsystem fails to initalize
If InitSprite() = 0
  ; display an error message
  MessageRequester("Error", "Failed to open the sprite system", 0)
  ;and terminate
  End
EndIf
; if the mouse subsystem fails to initalize
If InitMouse() = 0
  ; display an error message
  MessageRequester("Error", "Failed to open the mouse system", 0)
  ;and terminate
  End
EndIf
; if the keyboard subsystem fails to initalize
If InitKeyboard() = 0
  ; display an error message
  MessageRequester("Error", "Failed to open the keyboard system", 0)
  ;and terminate
  End
EndIf

If OpenWindow(1,0,0,0,0,"",#PB_Window_Invisible); **** open dummy invisible window ****
EndIf

; open the game window
If OpenWindow(0, 0, 0, 320, 200, "PureOoze", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ;if the game window was opened, open a screen inside the window for double buffering
  If OpenWindowedScreen(WindowID(0), 0, 0, 320, 200, 0, 0, 0) = 0
    ; the window couldn't be opened so display an error message
    MessageRequester("Error", "Failed to open double buffered windowed!", 0)
    ; and terminate
    End
  EndIf
Else
  ; the game window couldn't be opened so display an error message
    MessageRequester("Error", "Failed to open game window!", 0)
    ; and terminate
    End
EndIf
; main game loop
Repeat
  ; event loop
  Repeat
    ; Always process all the events to flush the queue at every frame
    Event = WindowEvent()
    ; determine which event occured
    Select Event
      ; the user closed the window
      Case #PB_Event_CloseWindow
        ; set quit to true
        Quit = 1
      Case #PB_Event_ActivateWindow
        ReleaseMouse(#False)
        InputReleased = 0
    EndSelect
    
  ; Quit the event loop only when no more events are available
  Until Event = 0
  ; if the mouse hasn't been released
  If InputReleased = 0
    ; check for mouse input
    ExamineMouse()
    ; check for keyboard input
    ExamineKeyboard()
    ; if the user pressed F12
    If KeyboardPushed(#PB_Key_F12)
      ; release the mouse
      ReleaseMouse(#True)
      
      SetActiveWindow(1) ; **** deactivate windowedscreen ****
      
      ; and set InputReleased to true
      InputReleased = 1
    EndIf
  EndIf
  ; Clear the screen and draw
  ClearScreen(RGB(192,192,192))
  ; Inverse the buffers  
  FlipBuffers()
Until  Quit Or KeyboardPushed(#PB_Key_Escape)
; IDE Options = PureBasic 4.51 (Windows - x86)
; EnableXP
; SubSystem = DirectX7
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Windowed Screen MouseRelease

Post by PureLust »

There is no need for an additional Window.

Just add the following line to the code, right after you release the keyboard:

Code: Select all

SetForegroundWindow_(GetDesktopWindow_())
Greetz, PL.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
mbecerra
User
User
Posts: 15
Joined: Mon Feb 28, 2005 4:59 am

Re: Windowed Screen MouseRelease

Post by mbecerra »

PureLust wrote:There is no need for an additional Window.

Just add the following line to the code, right after you release the keyboard:

Code: Select all

SetForegroundWindow_(GetDesktopWindow_())
Greetz, PL.
Is this a windows specific function cause i can't seem to find it in the purebasic documentation at all.
Derek wrote:Hi, I think it's because you're checking to see if your window gets the focus but it sometimes has it already so it doesn't reactivate. Hope that makes sense.

What I would do is open an invisible window at the same time as your window and then activate that one when you press F12, that way your window always loses focus and then will always regain focus when you click on it again.

I made a couple of alterations in your program, hope it helps.

Code: Select all

; if the sprite subsystem fails to initalize
If InitSprite() = 0
  ; display an error message
  MessageRequester("Error", "Failed to open the sprite system", 0)
  ;and terminate
  End
EndIf
; if the mouse subsystem fails to initalize
If InitMouse() = 0
  ; display an error message
  MessageRequester("Error", "Failed to open the mouse system", 0)
  ;and terminate
  End
EndIf
; if the keyboard subsystem fails to initalize
If InitKeyboard() = 0
  ; display an error message
  MessageRequester("Error", "Failed to open the keyboard system", 0)
  ;and terminate
  End
EndIf

If OpenWindow(1,0,0,0,0,"",#PB_Window_Invisible); **** open dummy invisible window ****
EndIf

; open the game window
If OpenWindow(0, 0, 0, 320, 200, "PureOoze", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ;if the game window was opened, open a screen inside the window for double buffering
  If OpenWindowedScreen(WindowID(0), 0, 0, 320, 200, 0, 0, 0) = 0
    ; the window couldn't be opened so display an error message
    MessageRequester("Error", "Failed to open double buffered windowed!", 0)
    ; and terminate
    End
  EndIf
Else
  ; the game window couldn't be opened so display an error message
    MessageRequester("Error", "Failed to open game window!", 0)
    ; and terminate
    End
EndIf
; main game loop
Repeat
  ; event loop
  Repeat
    ; Always process all the events to flush the queue at every frame
    Event = WindowEvent()
    ; determine which event occured
    Select Event
      ; the user closed the window
      Case #PB_Event_CloseWindow
        ; set quit to true
        Quit = 1
      Case #PB_Event_ActivateWindow
        ReleaseMouse(#False)
        InputReleased = 0
    EndSelect
    
  ; Quit the event loop only when no more events are available
  Until Event = 0
  ; if the mouse hasn't been released
  If InputReleased = 0
    ; check for mouse input
    ExamineMouse()
    ; check for keyboard input
    ExamineKeyboard()
    ; if the user pressed F12
    If KeyboardPushed(#PB_Key_F12)
      ; release the mouse
      ReleaseMouse(#True)
      
      SetActiveWindow(1) ; **** deactivate windowedscreen ****
      
      ; and set InputReleased to true
      InputReleased = 1
    EndIf
  EndIf
  ; Clear the screen and draw
  ClearScreen(RGB(192,192,192))
  ; Inverse the buffers  
  FlipBuffers()
Until  Quit Or KeyboardPushed(#PB_Key_Escape)
; IDE Options = PureBasic 4.51 (Windows - x86)
; EnableXP
; SubSystem = DirectX7
Thanks I will give this a try tonight.

PureOoze
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Windowed Screen MouseRelease

Post by PureLust »

mbecerra wrote:Is this a windows specific function cause i can't seem to find it in the purebasic documentation at all.
Yes, it's a so called "API-Function".
Because I did not see any hint in your post that you're looking for a Linux- or Mac-Solution, I assumed that a Win-only Solution would be fine.

It simply sets the Desktop window in the backgroud as the active window, so your window will become inactive and will be ready now to receive an ActivateWindow-Event if you click on it.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
Post Reply