It is currently Thu Jun 04, 2020 7:36 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [2D Game ] How can I make these algorithms work together?
PostPosted: Wed Jan 29, 2020 5:46 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Dec 04, 2015 9:26 pm
Posts: 287
Hello people .
I am still a layman in 2d games on Purebasic, and would like to know how can i unite these 2 algorithms in 1?

algorithm 1 > ( What does this algorithm do? When I click on the edge of the window the screen will not pause )
Code:
CompilerIf Not #PB_Compiler_OS = #PB_OS_Windows
  CompilerError "Only for Windows"
CompilerEndIf

CompilerIf Not #PB_Compiler_Thread
  CompilerError "Use Compiler Option Threadsafe"
CompilerEndIf

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

Global InputReleased = 1
Global Quit

Procedure thScreen(ID)
 
  Protected direction = 1
  Protected playerX = 1
  Protected playerY = 1
 
   
  Repeat
   
    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)
 
  Quit = 1
 
EndProcedure

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)
   
    ; Start with released input
    ReleaseMouse(#True)
    InputReleased = 1
   
    CreateThread(@thScreen(), 0)
   
    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 Quit ; Quit the event loop only when no more events are available
   
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
  EndIf
EndIf



algorithm 2 > ( What does this algorithm do? When hovering the mouse inside the screen area, the mouse will be locked, and when leaving the screen area the mouse will be released )

Code:
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


I am trying in every way to work with both algorithms in the same project, however, I am not managing to make both work together ...

NOTE : this is the code I'm making, however, it doesn't work ...

Code:
InitSprite()
InitMouse()
InitKeyboard()

ww=800
wh=600
style | #PB_Window_ScreenCentered
style | #PB_Window_SystemMenu
style | #PB_Window_MinimizeGadget

Global win.i
Global quit.i = 0

Procedure thScreen(ID)
 
  Protected direction = 1
  Protected playerX = 1
  Protected playerY = 1
 
 
  mouseCaptured = 1
   
  Repeat
   
  ExamineMouse()
  mx = MouseX()
  my = MouseY()
  mw = -MouseWheel()
 
  ; acceleration
;   mousespeed.f = 1.8
;   mx + MouseDeltaX()*mousespeed :: If mx<0 : mx=0 :: ElseIf mx>ww-1 : mx=ww-1 :: EndIf
;   my + MouseDeltaY()*mousespeed :: If my<0 : my=0 :: ElseIf my>wh-1 : my=wh-1 :: EndIf
;   MouseLocate(mx,my)
 

    If mouseCaptured
      If mx=0 Or mx=(ww-1) Or my=0 Or my=(wh-1)
;         Debug "----------------- releasing"
;         Debug "s x: " + mx
;         Debug "s y: " + my
;         Debug "w x: " + WindowMouseX(win)
;         Debug "w y: " + WindowMouseY(win)
       
        mouseCaptured=0
        ReleaseMouse(1)
      EndIf
    ElseIf Not mouseCaptured
      If WindowMouseX(win)>0 And WindowMouseX(win)<(ww-1) And WindowMouseY(win)>0 And WindowMouseY(win)<(wh-1)
         Debug "----------------- capturing"
         Debug "s x: " + mx
         Debug "s y: " + my
         Debug "w x: " + WindowMouseX(win)
         Debug "w y: " + WindowMouseY(win)
       
        mx=WindowMouseX(win)
        my=WindowMouseY(win)
        mouseCaptured=1
        ReleaseMouse(0)
        MouseLocate(mx,my)
      EndIf
    EndIf

 
  StartDrawing(ScreenOutput())
    DrawingMode(#PB_2DDrawing_Transparent)
    Circle(mx ,my, 1)
  StopDrawing()
 
  FlipBuffers()
  ClearScreen($333333)
   
  Until quit = 1
 
  Quit = 1
 
EndProcedure


If 01
  win=OpenWindow(#PB_Any, 50,100, ww,wh, "", style) :: AddKeyboardShortcut(win, #PB_Shortcut_Escape, 10)
  OpenWindowedScreen(WindowID(win), 0,0, ww,wh, 0,0,0)
  LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasicLogo.bmp")
  CreateThread(@thScreen(), 0)
Else
  OpenScreen(ww,wh,32,"", #PB_Screen_NoSynchronization)
  LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasicLogo.bmp")
  SetFrameRate(9999)
EndIf


Repeat
  ExamineKeyboard()
 
 
  If IsWindow(win) ;{
    Repeat
      event = WindowEvent()
      em = EventMenu()
      Select event
        Case #PB_Event_CloseWindow
          quit = #True
        Case #PB_Event_Menu
          Select em
          Case 10
            quit = #True
          EndSelect
      EndSelect
    Until Not event
    ;}
  EndIf
 
 
  If IsWindow(win)
    Delay(10)
  EndIf
Until quit Or KeyboardPushed(#PB_Key_Escape)


Top
 Profile  
Reply with quote  
 Post subject: Re: [2D Game ] How can I make these algorithms work together
PostPosted: Wed Jan 29, 2020 11:29 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 650
I wrote a mouse module some time ago, i always had some issue with PBs mouse functions.
Mby this helps: https://www.purebasic.fr/german/viewtopic.php?p=344843&sid=dbc6def154f81f2385048a80e8b7fb6e#p344843


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye