2D Game Screen Mouse Move Speed Becomes Slowly

Advanced game related topics
Benares
User
User
Posts: 16
Joined: Sun Aug 18, 2013 10:53 pm

2D Game Screen Mouse Move Speed Becomes Slowly

Post by Benares »

In the 2D Game Screen,
If Use the ExamineMouse(),
The Mouse Move Speed Becomes Slowly Than System.
How to solve this problem?
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by IdeasVacuum »

Can you post a working code snippet that demonstrates the issue?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Benares
User
User
Posts: 16
Joined: Sun Aug 18, 2013 10:53 pm

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by Benares »

Code: Select all

InitMouse()
InitKeyboard()
InitSprite()

OpenWindow(0, 0, 0, 800, 600, "Game Demo", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0))

Repeat
  FlipBuffers()
  ClearScreen(RGB(0, 0, 0))
  ExamineKeyboard()
  ExamineMouse()
  ShowCursor_(1)
  
  
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  
  Event = WindowEvent()
  Select Event
    Case #PB_Event_CloseWindow : End
    Case #PB_Event_MaximizeWindow
  EndSelect
ForEver

In Game Sceeen Mouse Move Speed Significantly Slower Than In The System's Many.

I Guess,
Possible The Game Resolution And Desktop Resolution Different
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by IdeasVacuum »

Try this:

Code: Select all

InitMouse()
InitKeyboard()
InitSprite()
iQuit.i = #False

If OpenWindow(0, 0, 0, 800, 600, "Game Demo [Esc to Quit]", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

        OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0))
        
        ShowCursor_(#True)    
        ExamineMouse()
        
        Repeat ;Windowed Screen Loop
        
                FlipBuffers()
                ClearScreen(RGB(0, 0, 0))
        
                If ExamineKeyboard()
        
                       If KeyboardPushed(#PB_Key_Escape)
        
                                 iQuit = #True
                                 ShowCursor_(#False)
                        EndIf
                EndIf
        
                Repeat ;Host Window Loop
        
                      iEvent = WindowEvent()
                   If iEvent = #PB_Event_CloseWindow : iQuit = #True : EndIf
        
                Until iEvent = #False
        
        Until iQuit = #True
EndIf

End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by Demivec »

@Benares: With a windowed-screen you have to make sure you handle all the window events before continuing with your screen display.

In your example the Repeat/Until loop only handles one window event each time through the loop. This will lead to a backlog of events. Keep retrieving events with WindowEvent() until there are no more left, then continue your main loop. IdeasVacuum's code does this.
Benares
User
User
Posts: 16
Joined: Sun Aug 18, 2013 10:53 pm

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by Benares »

Thank You Very Much , IdeasVacuum & Demivec!!!

But ,IdeasVacuum 's Code Have a Problem,

MouseButton() Is Invalid,

Unless Put the ExamineMouse() in Repeat,

However, The Mouse Move Speed Slow Again....

Code: Select all

InitMouse()
InitKeyboard()
InitSprite()
iQuit.i = #False

If OpenWindow(0, 0, 0, 800, 600, "Game Demo [Esc to Quit]", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

        OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0))
        
        ShowCursor_(#True)    
        ExamineMouse()
        
        Repeat ;Windowed Screen Loop

                FlipBuffers()
                ClearScreen(RGB(0, 0, 0))

                
                If MouseButton(#PB_MouseButton_Left)
                  Debug 1  
                EndIf
                
                If ExamineKeyboard()
        
                       If KeyboardPushed(#PB_Key_Escape)
        
                                 iQuit = #True
                                 ShowCursor_(#False)
                        EndIf
                EndIf
        
                Repeat ;Host Window Loop
        
                      iEvent = WindowEvent()
                   If iEvent = #PB_Event_CloseWindow : iQuit = #True : EndIf
        
                Until iEvent = #False
        
        Until iQuit = #True
EndIf

End
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by IdeasVacuum »

Hmm, tricky - the mouse response ranges from excellent to none-at-all within 6 seconds or so:

Code: Select all

Enumeration
#Win
#Txt
EndEnumeration

InitMouse()
InitKeyboard()
InitSprite()
iQuit.i = #False

If OpenWindow(#Win, 0, 0, 800, 640, "Game Demo [Esc to Quit]", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

                TextGadget(#Txt,10,10,300,20,"")
        OpenWindowedScreen(WindowID(#Win), 0, 40, WindowWidth(#Win), WindowHeight(#Win) - 40)
       
        ClearScreen(RGB(66, 66, 66))
        ShowCursor_(#True)   

        Repeat ;Windowed Screen Loop

                If ExamineMouse()

                        If MouseButton(#PB_MouseButton_Left)

                                 SetGadgetText(#Txt,"X:" + Str(MouseX()) + "  Y:" + Str(MouseY()))
                        EndIf
                EndIf
               
                If ExamineKeyboard()
       
                       If KeyboardPushed(#PB_Key_Escape)
       
                                 iQuit = #True
                                 ShowCursor_(#False)
                        EndIf
                EndIf
       
                Repeat ;Host Window Loop
       
                      iEvent = WindowEvent()
                Until iEvent = #False

                FlipBuffers()

        Until iQuit = #True
EndIf

End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Benares
User
User
Posts: 16
Joined: Sun Aug 18, 2013 10:53 pm

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by Benares »

:cry:

The Speed Feel no Difference.....
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by IdeasVacuum »

Well, the mouse behaviour is erratic - it sometimes looks like the cursor has hit some mysterious, magnetic force field that slows it down! So, either my code is a pile of crap (very likely) or there is something wrong with ExamineMouse(). Need a 2D Games Guru (or Fred/Freak) to show us how it should be done.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by IdeasVacuum »

Another approach:

Code: Select all

UsePNGImageDecoder()

Enumeration
#Win
#Txt
#CursorSprite
EndEnumeration

Global igScreenColour.i = RGB(66, 66, 66)

Global fgMouseX.f = 350
Global fgMouseY.f = 250

InitMouse()
InitKeyboard()
InitSprite()
iQuit.i = #False

If OpenWindow(#Win, 0, 0, 800, 640, "Game Demo [Esc to Quit]", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

                TextGadget(#Txt,10,10,300,20,"")
        OpenWindowedScreen(WindowID(#Win), 0, 40, WindowWidth(#Win), WindowHeight(#Win) - 40)
       
               CatchSprite(#CursorSprite, ?Cursor)
               MouseLocate(fgMouseX, fgMouseY)

        Repeat ;Windowed Screen Loop

                If ExamineKeyboard()
       
                       If KeyboardPushed(#PB_Key_Escape) : iQuit = #True : EndIf
                EndIf

                ExamineMouse()
                fgMouseX = MouseX()
                fgMouseY = MouseY()

                If ( (fgMouseX =< 1) Or (fgMouseY =< 1) Or (fgMouseX >= (WindowWidth(#Win) - 6)) Or (fgMouseY >= (WindowHeight(#Win) - 46)) )

                        ;If mouse is on edge of windowed screen, hide sprite cursor, enable Windows Cursor.
                        ReleaseMouse(1) ;Use Normal Windows mouse cursor

                        ;Hide sprite cursor by sending Sprite off screen
                        DisplayTransparentSprite(#CursorSprite, -100, -100)
                                     FlipBuffers()
                                     ClearScreen(igScreenColour)
                Else
                        ;Sprite Cursor follows mouse movement
                                    ReleaseMouse(0) ;Use Sprite cursor
                        DisplayTransparentSprite(#CursorSprite, fgMouseX, fgMouseY)
                                     FlipBuffers()
                                     ClearScreen(igScreenColour)
                                   SetGadgetText(#Txt,"X:" + Str(fgMouseX) + "  Y:" + Str(fgMouseY))
                EndIf

                Repeat ;Host Window Loop
       
                         iEvent = WindowEvent()
                      If(iEvent = #PB_Event_CloseWindow) : iQuit = #True : EndIf

                Until iEvent = #False

        Until iQuit = #True
EndIf

End

DataSection
  Cursor:
  Data.q $0A1A0A0D474E5089,$524448490D000000,$170000000F000000,$82315B0000000208,$4E527406000000EB
  Data.q $6E00000000000053,$70090000009107A6,$00C40E0000735948,$1B0E2B9501C40E00,$544144497D000000
  Data.q $20C00DCB919D8D38,$498758F20CD2C50C,$44A87A1160ED8A18,$086D921B94F90A51,$0E804A5280174510
  Data.q $BDA1E15E18C60008,$A082DB4AC25DA4C2,$0A3E96C26B4BE10D,$87AE10B4282F7A27,$36EB4A530389AD6E
  Data.q $58A7FD6D956B5CE7,$679E4A63382F69E9,$9E538D16D999BDF5,$88C39AAAEEF7A889,$7FBFE00F2C4EEB3C
  Data.q $8A09FED3D844A903,$4E454900000000F8,$000000826042AE44
EndDataSection
This works better on my machine, but I think it could be improved by somebody that knows what they are doing. Once the cursor is out of the screen region, you can use the mouse anywhere else, as expected, but when you move the mouse back into the screen region it's location is not picked up again. My bug.........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Benares
User
User
Posts: 16
Joined: Sun Aug 18, 2013 10:53 pm

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by Benares »

Thanks IdeasVacuum, is Better Than Before :D
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by Demivec »

IdeasVacuum wrote:Well, the mouse behaviour is erratic - it sometimes looks like the cursor has hit some mysterious, magnetic force field that slows it down! So, either my code is a pile of crap (very likely) or there is something wrong with ExamineMouse(). Need a 2D Games Guru (or Fred/Freak) to show us how it should be done.
I think when you were showing the windows cursor on the windowed screen (i.e. ShowCursor_()) you then had competing events that showed where the mouse pointer was. Both the window and the screen were giving status messages on where the mouse pointer was.

I think the screen only updates the mouse info each time FlipBuffers() is used, followed by ExamineMouse(). It tosses everything in between away. The window does it more or less continuously but waits for the messages to be retrieved.

When you showed the results of the screen events, then processed the window events you were getting a jitter in the mouse position because of a replay of old events.

To prevent this you would use the screen events when the mouse is over the screen and the window events when the mouse is over the window.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by netmaestro »

I believe there's a bug in Purebasic causing this. I looked up a demo I once did for mixing gui elements with mouse-enabled windowed screens and found the same issues with it. When it was written in 2007 it worked fine, but under 5.21 LTS the mouse movement hangs up badly. I made a bug report here: http://purebasic.fr/english/viewtopic.php?f=4&t=57932
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by netmaestro »

Found the cause, it's synchronization. Imho it's a PB bug but in the meantime a workaround is to open the screen with #PB_Screen_NoSynchronization. Then it's nice and smooth again.
BERESHEIT
marc_256
Enthusiast
Enthusiast
Posts: 746
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: 2D Game Screen Mouse Move Speed Becomes Slowly

Post by marc_256 »

netmaestro wrote:Found the cause, it's synchronization. Imho it's a PB bug but in the meantime a workaround is to open the screen with #PB_Screen_NoSynchronization. Then it's nice and smooth again.
Works fine now,
lost a lot of time, thinking my software was wrong.

Thanks
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Post Reply