Screen with autoresizing and mouse

Just starting out? Need help? Post your questions and find answers here.
User avatar
[blendman]
Enthusiast
Enthusiast
Posts: 297
Joined: Thu Apr 07, 2011 1:14 pm
Location: 3 arks
Contact:

Screen with autoresizing and mouse

Post by [blendman] »

Hi

It seems that the auto-resizing of the screen has a bug.
IT work if the window and the screen have the same size, but it doesn't work if the screen and the window haven't the same size.

The mouse should be captured inside the side zoomed of the screen, and it seems the mouse is captured inside the original size of the screen.

Code: Select all

;{ init
If InitSprite() <> 0 : EndIf
If InitKeyboard() <> 0 : EndIf
If InitMouse() <> 0 : EndIf
;}

flag = #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget
If OpenWindow(0,0,0,1200,800, "Game, screen Autoresized", Flag) <> 0   
    If OpenWindowedScreen(WindowID(0), 0,0,600,400,1,0,0) <> 0       
        ScreenW = ScreenWidth() 
        ScreenH = ScreenHeight()
        WinW = WindowWidth(0)
        WinH = WindowHeight(0)
    EndIf
    ShowCursor_(1)
 EndIf

Repeat
   
    Repeat       
        EventID  = WaitWindowEvent(1)       
        Select EventID               
            Case #PB_Event_CloseWindow
                End               
        EndSelect       
    Until event = 0
   
    If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Escape)
            End
        EndIf
    EndIf
      
     RatioX = WindowMouseX(0) / (WinW/ScreenW)
     RatioY = WindowMouseY(0) / (WinH/ScreenH)
        
    If ExamineMouse()
        mx = MouseX()
        my = MouseY()
       
        If MouseButton(#PB_MouseButton_Left)
            clic = 1
        Else
           clic = 0
        EndIf
              
    EndIf
  
    If StartDrawing(ScreenOutput())
        u=0
        DrawText(0,0,"Try to go over "+Str(ScreenW)+" x "+Str(ScreenH)) : u+20
        DrawText(0,u,Str(mx)+"/"+Str(my)) : u+20
        ; DrawText(0,u,Str(RatioX)+"/"+Str(RatioY)) : u+20
        DrawText(0,u,Str(WindowMouseX(0))+"/"+Str(WindowMouseY(0))) : u+20
        DrawText(0,u,"Clic : "+Str(clic))
        StopDrawing()
    EndIf
       
    FlipBuffers()
    ClearScreen(RGB(200,200,200))

ForEver

FR (pour Fred ;)):
Il semblerait qu'il y ait un soucis si on utilise l'option RedimensionnementAuto lors de la création de l'écran.

Lorsqu'on agrandit l'écran avec l'option Redimensionnementauto (très utile pour la création de jeu ;)), la souris n'est plus bloquée dans l'écran zoomé comme cela devrait être le cas, mais elle est bloquée (capturée) dans la taille de l'écran initial.
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Screen with autoresizing and mouse

Post by Demivec »

I think that even though the screen is resized it still has the same number of pixels and that is respected by commands that use pixel references to draw on the screen.

I've added just 2 lines to your example to display a sprite cursor at the mouse position, it shows the difference between the screen mouse coordinates and the API cursor for the window.

IMHO that means there isn't a bug. If you are going to use API then maybe you would want to scale it so that it refers to the proper screen position.

Code: Select all

;{ init
If InitSprite() <> 0 : EndIf
If InitKeyboard() <> 0 : EndIf
If InitMouse() <> 0 : EndIf
;}

flag = #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget
If OpenWindow(0,0,0,1200,800, "Game, screen Autoresized", Flag) <> 0   
    If OpenWindowedScreen(WindowID(0), 0,0,600,400,1,0,0) <> 0       
        ScreenW = ScreenWidth() 
        ScreenH = ScreenHeight()
        WinW = WindowWidth(0)
        WinH = WindowHeight(0)
      EndIf
      If CreateSprite(0, 10, 10) And StartDrawing(SpriteOutput(0)): Box(0,0, 10, 10, RGB(0,255,0)): StopDrawing(): EndIf ;<<< create screen mouse pointer
    ShowCursor_(1)
 EndIf

Repeat
   
    Repeat       
        EventID  = WaitWindowEvent(1)       
        Select EventID               
            Case #PB_Event_CloseWindow
                End               
        EndSelect       
    Until event = 0
   
    If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Escape)
            End
        EndIf
    EndIf
      
     RatioX = WindowMouseX(0) / (WinW/ScreenW)
     RatioY = WindowMouseY(0) / (WinH/ScreenH)
        
    If ExamineMouse()
        mx = MouseX()
        my = MouseY()
        
        DisplaySprite(0, mx, my)      ; <<< show screen mouse pointer
        If MouseButton(#PB_MouseButton_Left)
            clic = 1
        Else
           clic = 0
        EndIf
              
    EndIf
  
    If StartDrawing(ScreenOutput())
        u=0
        DrawText(0,0,"Try to go over "+Str(ScreenW)+" x "+Str(ScreenH)) : u+20
        DrawText(0,u,Str(mx)+"/"+Str(my)) : u+20
        ; DrawText(0,u,Str(RatioX)+"/"+Str(RatioY)) : u+20
        DrawText(0,u,Str(WindowMouseX(0))+"/"+Str(WindowMouseY(0))) : u+20
        DrawText(0,u,"Clic : "+Str(clic))
        StopDrawing()
    EndIf
       
    FlipBuffers()
    ClearScreen(RGB(200,200,200))

ForEver
User avatar
[blendman]
Enthusiast
Enthusiast
Posts: 297
Joined: Thu Apr 07, 2011 1:14 pm
Location: 3 arks
Contact:

Re: Screen with autoresizing and mouse

Post by [blendman] »

Yes

There is a difference between MouseX()/MouseY() and WindowMouseX(0)/WindowMouseY(0), if the screen has "auto-resizing" option activated.

Your example means that if we want to use Autoresizing, we are forced to use a sprite mouse pointer, and we can use the "API" cursor (with showCursor_(1), for example).
But, with this example, there is still a little issue : when we are not inside the game (inscreen = 0), we have to set the cursor by ourself.
In windows, it's easy with (SetCursorPos_(x,y)), but what is the equivalent to linux and macos ?

it's not perfect, but it works (for windows) :

Code: Select all

;{ init
If InitSprite() <> 0 : EndIf
If InitKeyboard() <> 0 : EndIf
If InitMouse() <> 0 : EndIf
;}

flag = #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget
If OpenWindow(0,0,0,1200,800, "Game, screen Autoresized", Flag) <> 0   
    If OpenWindowedScreen(WindowID(0), 0,0,600,400,1,0,0) <> 0       
        ScreenW = ScreenWidth()
        ScreenH = ScreenHeight()
        WinW = WindowWidth(0)
        WinH = WindowHeight(0)
      EndIf
      If CreateSprite(0, 10, 10) And StartDrawing(SpriteOutput(0)): Box(0,0, 10, 10, RGB(0,255,0)): StopDrawing(): EndIf ;<<< create screen mouse pointer
    ;ShowCursor_(1)
 EndIf
 
Repeat
   
    Repeat       
        EventID  = WaitWindowEvent(1)       
        Select EventID               
            Case #PB_Event_SizeWindow
                ScreenW = ScreenWidth()
                ScreenH = ScreenHeight()
                WinW = WindowWidth(0)
                WinH = WindowHeight(0)
                
            Case #PB_Event_CloseWindow
                End               
        EndSelect       
    Until event = 0
   
    If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Escape)
            End
        EndIf
    EndIf
     
     RatioX = WindowMouseX(0) ;/ (WinW/ScreenW)
     RatioY = WindowMouseY(0) ;/ (WinH/ScreenH)
     
     If inscreen = 1
         If ExamineMouse()
             
             mx = MouseX()
             my = MouseY()
             
             If mx<=1 Or my<=1 Or mx>=screenW-2 Or my>=screenH-2
                 inscreen = 0
                 ReleaseMouse(1)
                 
                 If mx <=1
                     x = 0+WindowX(0)
                 ElseIf mx >=screenW-2
                     x = winW+2+WindowX(0)
                 Else
                     x = mx *(WinW/ScreenW)+WindowX(0)
                 EndIf
                 If my<=1
                     y = 0+WindowY(0)
                 ElseIf my>=screenH-2
                     y =winH+2+WindowY(0)                     
                 Else
                     y = my*(WinH/ScreenH)+WindowY(0)
                 EndIf                 
                 SetCursorPos_(mx *(WinW/ScreenW)+WindowX(0),my *(WinH/ScreenH)+WindowY(0)+25)
             EndIf
         EndIf 
     Else
         If RatioX>1 And RatioX<winw-2 And RatioY>1 And RatioY<winh-2
             inscreen = 1
             ReleaseMouse(0)
             rx.d = WinW/ScreenW
             ry.d = WinH/ScreenH
             MouseLocate(RatioX/rx,RatioY/ry)
         EndIf         
     EndIf
     
     
     DisplaySprite(0, mx, my)      ; <<< show screen mouse pointer
     If MouseButton(#PB_MouseButton_Left)
         clic = 1
     Else
         clic = 0
     EndIf
     
    
 
    If StartDrawing(ScreenOutput())
        u=0
        DrawText(0,0,"Try to go over "+Str(ScreenW)+" x "+Str(ScreenH)) : u+20
        DrawText(0,u,Str(mx)+"/"+Str(my)) : u+20
        ; DrawText(0,u,Str(RatioX)+"/"+Str(RatioY)) : u+20
        DrawText(0,u,Str(WindowMouseX(0))+"/"+Str(WindowMouseY(0))) : u+20
        DrawText(0,u,"Clic : "+Str(clic))
        StopDrawing()
    EndIf
       
    FlipBuffers()
    ClearScreen(RGB(200,200,200))

ForEver
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Screen with autoresizing and mouse

Post by dobro »

Click Left = move Screen
Click Right=resize Screen

Code: Select all

; Graph100

InitSprite()
Enumeration
   #win
   #container
   #screen
EndEnumeration
OpenWindow(#win, 0, 0, 800, 600, "Bouton Gauche pour déplacer, Bouton droit pour redimensionner ;)", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ContainerGadget(#container, 50, 50, 100,100) ; cree container Gadget
OpenWindowedScreen(GadgetID(#container), 0, 0, 100, 100, #True, 0, 0) ; ATTENTION , ici on assservi le Screen au Container
CloseGadgetList()
balle.POINT\x = 25
balle\y = 50
vitesse.POINT\x = 2
vitesse\y = 1
Repeat
   Repeat
      event = WindowEvent()
      
      balle\x + vitesse\x
      balle\y + vitesse\y
      If balle\x < 5 Or balle\x > 95 : vitesse\x = -vitesse\x : EndIf
      If balle\y < 5 Or balle\y > 95 : vitesse\y = -vitesse\y : EndIf
      
      If event = #WM_LBUTTONUP
         ResizeGadget(#container, WindowMouseX(#win), WindowMouseY(#win), #PB_Ignore, #PB_Ignore)
      EndIf
      If event = #WM_RBUTTONUP
         ResizeGadget(#container, #PB_Ignore, #PB_Ignore, WindowMouseX(#win) - GadgetX(#container), WindowMouseY(#win) - GadgetY(#container))
      EndIf
      
      ClearScreen(0)
      If StartDrawing(ScreenOutput())
         Circle(balle\x, balle\y, 5, #Red)
         StopDrawing()
      EndIf
      FlipBuffers()
   until event=0
Until event = #PB_Event_CloseWindow
;
; Epb


does it work on Mac and Linux ???
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Post Reply