Gadget OpenGL : Utilisation du clavier
Publié : mer. 20/mars/2019 14:15
J'explore les possibilités du gadget OpenGl et la première difficulté que je rencontre c'est l'interaction avec le clavier que je trouve lent.
Peut être que je n'ai pas la bonne méthode et je vous propose ce simple code pour avoir votre avis.
Utiliser les flèches du clavier pour déplacer un carré.je vous propose aussi le meme code 100% PureBasic pour que vous puissiez comparer le temps de réaction.Sur ce deuxième code on peut presser simultanément les flèches bas et haut pour un mouvement en diagonal ce qui n'est pas le cas avec le premier code.
Merci d'avance pour l'aide que vous pourrez m'apporter
Peut être que je n'ai pas la bonne méthode et je vous propose ce simple code pour avoir votre avis.
Utiliser les flèches du clavier pour déplacer un carré.
Code : Tout sélectionner
EnableExplicit
; Fenetre application, viewport et couleur arriere plan
Global window, ww = 800, wh = 600, viewport, wColor.f = RGB(107, 142, 35)
; Sprite
Global sx.f = 100, sy.f =100, sw = 64, sh = 64, sColor = RGB(255, 215, 0)
;Plan de l'application
Declare Start()
Declare GameEvent()
Declare GameRender()
Declare Exit()
Start()
Procedure Start()
window = OpenWindow(#PB_Any, 0, 0, ww, wh, "Test Opengl", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
; Fenetre Open GL
viewport = OpenGLGadget(#PB_Any, 0, 0, ww, wh, #PB_OpenGL_NoDepthBuffer | #PB_OpenGL_Keyboard)
SetActiveGadget(viewport)
; Modifier le contexte courant
SetGadgetAttribute(viewport, #PB_OpenGL_SetContext, #True)
; Couleur d'arrirer plan
glClearColor_(Red(wColor)/255, Green(wColor)/255, Blue(wColor)/255, 1.0)
; Volume de visualisation
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_();
gluOrtho2D_(0, ww, wh, 0)
;Triggers
BindGadgetEvent(viewport, @GameEvent(), #PB_EventType_KeyDown)
BindEvent(#PB_Event_CloseWindow, @Exit())
;Loop
While #True
Repeat : Until WindowEvent() = 0
GameRender()
Wend
EndProcedure
Procedure GameEvent()
Protected Speed.f = 8
Protected KeyPress = GetGadgetAttribute(viewport, #PB_OpenGL_Key)
If KeyPress= #PB_Shortcut_Left
sx - Speed
EndIf
If KeyPress = #PB_Shortcut_Right
sx + Speed
EndIf
If KeyPress = #PB_Shortcut_Up
sy - Speed
EndIf
If KeyPress = #PB_Shortcut_Down
sy + Speed
EndIf
If KeyPress = #PB_Shortcut_Escape
Exit()
EndIf
EndProcedure
Procedure GameRender()
; ClearScreen
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
; Affichage d'un carré
glMatrixMode_(#GL_MODELVIEW) ;Initialiser la matrice de transformation courante
glPushMatrix_()
glBegin_(#GL_POLYGON)
glColor3f_(Red(sColor)/255, Green(sColor)/255, Blue(sColor)/255)
glVertex2i_(sx, sy) ;Point haut gauche
glVertex2i_(sx + sw, sy) ;Point haut droit
glVertex2i_(sx + sw, sy + sh) ;Point bas droit
glVertex2i_(sx, sy + sh) ;Point bas gauche
glEnd_() ;Fin du polygone
glPopMatrix_();
; Flip buffer
SetGadgetAttribute(viewport, #PB_OpenGL_FlipBuffers, #True)
EndProcedure
Procedure Exit()
End
EndProcedure
Code : Tout sélectionner
; Fenetre application, viewport et couleur arriere plan
Global window, ww = 800, wh = 600, viewport, wColor.f = RGB(107, 142, 35)
; Sprite
Global sprite, sx.f = 100, sy.f =100, sw = 64, sh = 64, sColor = RGB(255, 215, 0)
;Plan de l'application
Declare Start()
Declare GameEvent()
Declare GameRender()
Declare Exit()
Start()
Procedure Start()
InitSprite()
InitKeyboard()
InitMouse()
window = OpenWindow(#PB_Any, 0, 0, ww, wh, "Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(window), 0, 0, ww, wh)
; Creation d'un sprite
sprite = CreateSprite(#PB_Any, sw, sh)
StartDrawing(SpriteOutput(sprite))
Box(0, 0, sw, sh, scolor)
StopDrawing()
; Trigger
BindEvent(#PB_Event_CloseWindow, @Exit())
;Loop
While #True
Repeat : Until WindowEvent() = 0
GameEvent()
FlipBuffers()
GameRender()
Wend
EndProcedure
Procedure GameEvent()
Protected Speed.f = 8
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
sx - Speed
EndIf
If KeyboardPushed(#PB_Key_Right)
sx + Speed
EndIf
If KeyboardPushed(#PB_Key_Up)
sy - Speed
EndIf
If KeyboardPushed(#PB_Key_Down)
sy + Speed
EndIf
If KeyboardPushed(#PB_Key_Escape)
Exit()
EndIf
EndProcedure
Procedure GameRender()
ClearScreen(wColor)
DisplaySprite(sprite, sx, sy)
EndProcedure
Procedure Exit()
End
EndProcedure
Merci d'avance pour l'aide que vous pourrez m'apporter