PureBasic recalcule le screen entier à chaque frame, on ne peut pas ajouter des choses en cours de traitement, à moins d'utiliser le buffer avec DrawingBuffer(), qui donne accès directement à la mémoire vidéo, mais son utilisation réclame de solides compétences.
Tu peux t'inspirer de ce code que je viens d'écrire à l'arrache mais qui marche.
On peut, capturer/relacher la souris et le clavier dans le screen, mettre en pause, et ouvrir/fermer une fenêtre "Enregistrer le jeu".
Code : Tout sélectionner
EnableExplicit
Global direction = 1
Global playerX = 1
Global playerY = 1
; Start with released input
Global InputReleased = 1
Global x,Event,Quit,Gadget, pausejeu,Touche$,Touche2$,Fenetre
Declare go()
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageRequester("Error", "Can't open the sprite system", 0)
End
EndIf
OpenWindow(1,0,0,200,100,"Enregistrer jeu",#PB_Window_ScreenCentered|#PB_Window_Invisible)
ButtonGadget( 10, 50,50,50,25, "Fermer")
If OpenWindow(0, 0, 0, 360, 285, "Jeu: ESC, F1, F5", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(1, 10, 10, 120, 25, "Capture Souris clavier")
ButtonGadget(2, 140, 10, 100, 25, "Pause")
ButtonGadget(3, 250, 10, 100, 25, "ok")
TextGadget (4, 10, 40, 300, 30, "Capturer Souris et clavier")
If OpenWindowedScreen(WindowID(0), 10, 70, 340, 200, 0, 0, 0)
LoadSprite(0, #PB_Compiler_Home + "examples/sources/Data/PureBasicLogo.bmp")
CreateSprite(1,16,16)
StartDrawing(SpriteOutput(1))
DrawingMode(#PB_2DDrawing_Default )
Circle(8, 8, 8)
StopDrawing()
ReleaseMouse(#True)
pausejeu=0
Repeat
Repeat
; Always process all the events to flush the queue at every frame
Event = WindowEvent()
Fenetre = EventWindow()
Select Event
Case #PB_Event_CloseWindow
; If Fenetre=1
; HideWindow(1,#True)
; Else
Quit = 1
; EndIf
Case #PB_Event_Gadget
; Do the normal application management here
Gadget = EventGadget()
Select Gadget
Case 1
InputReleased = 0
ReleaseMouse(#False)
SetGadgetText(4, "'F1' pour relacher clavier souris")
Case 2
SetGadgetText(4, "Bouton "+Str(Gadget)+" pressé.")
If pausejeu=0
pausejeu=1
SetGadgetText(2,"go")
Else
pausejeu=0
SetGadgetText(2,"pause")
EndIf
Case 10
ReleaseMouse(#False)
InputReleased = 0
SetGadgetText(4, "clavier souris relachés");
HideWindow(1,#True)
pausejeu=0
EndSelect
EndSelect
Until Event = 0 ; Quit the event loop only when no more events are available
If pausejeu=0
go()
EndIf
Until Quit Or KeyboardPushed(#PB_Key_Escape)
Else
MessageRequester("Error", "Can't open windowed screen!", 0)
EndIf
EndIf
Procedure go()
ExamineKeyboard()
If InputReleased = 0
ExamineMouse()
; Debug MouseX()
; sprite et screen recalculés à chaque 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, "clavier souris relachés ")
pausejeu=1
EndIf
If KeyboardPushed(#PB_Key_F5)
ReleaseMouse(#True)
InputReleased = 1
SetGadgetText(4, "clavier souris relachés");
HideWindow(1,#False)
pausejeu=1
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
If x < 0 : direction = 1 : EndIf
DisplaySprite(1, MouseX(),MouseY())
Touche$=KeyboardInkey()
If Touche$
Touche2$=Touche$
EndIf
StartDrawing(ScreenOutput())
DrawText( 10,10,Touche2$)
StopDrawing()
FlipBuffers()
EndProcedure
M.