Page 1 of 1

OpenGl and ExamineKeyboard() = error

Posted: Sat Dec 14, 2024 5:16 pm
by SPH
Hi all,

Here is a code using polygons (my current passion).
I have a problem with the keyboard because I don't use openscreen.
I use OpenWindow and OpenGLGadget.

So the ExamineKeyboard() function makes an error.

I guess there is an OpenGL solution?

PS: Remove the "Goto no"

I would like a zero latency effect when pressing a key. You know: when you press a key (and leave it pressed), the character is displayed then there is a pause, then a repetition of characters. I don't want this wait after the first character.

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
InitKeyboard()
InitSprite()

Global chemin$,sph_cmb,Dim sph_xx(1),Dim sph_yy(1),attente,attente_ok
Global numero,echelle_xf.f,echelle_yf.f,Resx,Resy,categorie$
Global Dim speedx.w(1,1),Dim speedy.w(1,1),pas
Global perso__x, perso__y, marcher__f.f,  marcher__zoom.f


;EnableExplicit

;-CONSTANTS
Enumeration
  #MainWindow
  #OpenGLGadget
EndEnumeration


ExamineDesktops()
ddw=DesktopWidth(0)
ddh=DesktopHeight(0)
mix=ddw/2
miy=ddh/2

;********************* DPI *********************
echelle_xf.f=(1920/ddw)*DesktopResolutionX()
echelle_yf.f=(1080/ddh)*DesktopResolutionY()
;**********************************************

;-STRUCTURES
Structure Integer2
  X.i
  Y.i
EndStructure
Global.Integer2 WindowDim
WindowDim\X = ddw
WindowDim\Y = ddh


;-DEFINES
Define.i Event
Define.i WindowFlags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget


;-DECLARES
Declare Render()
Declare Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
Declare SetupOpenGL()
Declare SetupGLTexture(ImageHandle.i)


;-MAIN WINDOW
win=OpenWindow(#MainWindow, 0, 0,ddw,ddh, "Lecteur_polygons",#PB_Window_Maximize|#PB_Window_BorderLess)
If win=0
  ;   Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenWindow() impossible")
  End
EndIf

screenGL=OpenGLGadget(#OpenGLGadget,0,0,ddw,ddh)
If screenGL=0
  ;   Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenGLGadget() impossible")
  End
EndIf

dpix.f=DesktopResolutionX()
dpiy.f=DesktopResolutionY()

SetupOpenGL()

AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666) ; quitter


glOrtho_(0,ddw,ddh,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

;*********************************************************************************************************************************

;;;;;;;;;;;
SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
;{
Procedure.i OpenGLDrawText(Gadget.i, x.f, y.f, Text$, Textcolor=#White)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i, tmpy.f
  
  
  Image = CreateImage(#PB_Any, 1, 1)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_Transparent)
          DrawText(0, 0, Text$, Textcolor)
          
          tmpy = y - (1 / GadgetHeight(Gadget) * TextHeight * 2)
          
          glRasterPos2f_(x, tmpy)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Procedure Render()
  
  ;Clearing buffers and resetting clear color to remove old graphics from the last frame.
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  ;  glClearColor_(0.2, 0.2, 0.2, 1.0)
  glClearColor_(0,0,0,1)
  
  ;## DRAWING TEXTURES/IMAGES
  ;First enable the Texture system.
  glEnable_(#GL_TEXTURE_2D)
  
  ;This procedure will create a quad and apply a texture to it.
  ;The Texture variable contains the texture created earlier using SetupGLTexture().
  Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), -2)
  ; Render2DQuad(Texture2, 0, 0, ImageWidth(Image2), ImageHeight(Image2), -1)
  
  ;After all the textures have been displayed disable the texture system.
  ;Otherwise it will conflict with the non texture graphics.
  glDisable_(#GL_TEXTURE_2D)
  
EndProcedure
Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
  
  ;The texture is first bound which tells OpenGL to use this texture for any future rendering.
  glBindTexture_(#GL_TEXTURE_2D, OGLTexture)
  glBegin_(#GL_QUADS)
  glColor4f_   (1,1,1,1)
  glNormal3f_  (0,0,1.0)
  glTexCoord2f_(1.0,1.0)
  glVertex3f_  (StartX+Width,StartY,Z)
  glTexCoord2f_(0.0,1.0)
  glVertex3f_  (StartX,StartY,Z)
  glTexCoord2f_(0.0,0.0)
  glVertex3f_  (StartX,StartY+Height,Z)
  glTexCoord2f_(1.0,0.0)
  glVertex3f_  (StartX+Width,StartY+Height,Z)
  glEnd_()
  
EndProcedure
Procedure SetupOpenGL()
  
  glMatrixMode_(#GL_PROJECTION)
  
  glOrtho_(0.0, WindowDim\X, WindowDim\Y, 0.0, -1000.0, 1000.0)
  
  glMatrixMode_(#GL_MODELVIEW)
  
  ; glEnable_(#GL_DEPTH_TEST)
  
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
  
EndProcedure

Procedure load_poly_data(categorie$,pol) ; ne jamais appeller un polygon "0.pol"
  If attente_ok=0 Or attente_ok<>pol
    attente_ok=pol
    Dim sph_xx(574)
    Dim sph_yy(574)
    
    For i=1 To 574/2
      Read.w sph_xx(i-1)
      Read.w sph_yy(i-1)
    Next
  EndIf
  
EndProcedure




Procedure affiche_poly_old(xxx,yyy,angle.f,dkx,dky,zoom.f,alpha.f)
  sph_cmb=sph_yy(0)
  sph_la=0
  Repeat
    glBegin_(#GL_POLYGON)
    glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
    glColor4f_(sph_xx(sph_la+1)/255,sph_yy(sph_la+1)/255,sph_xx(sph_la+2)/255,(sph_yy(sph_la+2)/255)*alpha)
    ;;;;;;
    For i=3 To sph_cmb-1
      sph_xxx.f=(sph_xx(sph_la+i)/echelle_xf)-xxx
      sph_yyy.f=(sph_yy(sph_la+i)/echelle_yf)-yyy
      sq.f=Sqr(sph_xxx*sph_xxx+sph_yyy*sph_yyy)
      ff.f = ATan2(sph_xxx,sph_yyy)+angle ; par Nemerod (sans lui, je n'y serai pas arrivé)
      centre_x=xxx+Cos(ff)*sq*zoom
      centre_y=yyy+Sin(ff)*sq*zoom
      glVertex2f_(centre_x+dkx,centre_y+dky);
    Next
    ;;;;;;;
    glEnd_()                      ; 
    sph_cmb=sph_yy(sph_la+i)
    sph_la+i
  Until sph_yy(sph_la)=0
  ;;;;;;;;;;;;;;;
EndProcedure

Procedure gestion()
  Repeat
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
            Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
            ;;;;;;;         
            Select EventType()
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;             
            EndSelect
        EndSelect
      Case #PB_Event_Menu
        Select EventMenu()
          Case 666
            End
            
        EndSelect
    EndSelect
    
  Until Event = 0
EndProcedure
;}

;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
tourne.f=0
move.f=0.005
load_poly_data("monstre",1)
;;;
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
Repeat
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  gestion()
  
  ;;;;;;
  ;;;;;;
  ;;;;;;
  
  Goto no ; a supprimer
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Left)
    move-0.0001  
  EndIf
  If KeyboardPushed(#PB_Key_Right)
    move+0.0001  
  EndIf
  no:
  
  ;;;;;;
  ;;;;;;
  ;;;;;;
  
  affiche_poly_old(mix,miy,tourne,0,0,1,1)
  
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
  tourne+move
  
ForEver

End


DataSection
  Data.w 28,7,37,39,60,255,1919,0,1919,1079,0,1079,0,0,0,20,51,71,115,255
  Data.w 1908,589,1919,588,1919,813,1831,815,1772,833,1717,899,1528,946,1519,966,333,961,269,883
  Data.w 259,820,0,823,0,115,74,112,139,42,246,0,1869,0,0,10,51,71,115,255
  Data.w 0,801,0,963,67,964,213,886,211,847,267,814,157,775,0,19,61,82,141,255
  Data.w 236,0,155,544,162,598,300,818,343,825,348,643,380,646,713,881,1185,857,1267,816
  Data.w 1424,846,1577,814,1689,594,1695,540,1648,320,1629,0,0,9,61,82,141,255,700,861
  Data.w 723,900,485,900,427,844,398,838,381,672,0,7,61,82,141,255,616,816,381,674
  Data.w 379,618,611,775,0,22,64,110,239,255,380,0,256,255,235,333,257,496,363,646
  Data.w 380,648,412,705,606,750,630,710,646,711,695,734,1155,749,1211,720,1234,697,1384,661
  Data.w 1512,514,1509,354,1391,199,1344,0,0,6,64,110,239,255,1200,734,1179,768,1226,732
  Data.w 0,6,64,110,239,255,1519,641,1569,429,1590,426,0,7,64,110,239,255,1590,428
  Data.w 1542,271,1524,283,1567,437,0,7,64,110,239,255,1352,2,1407,123,1420,127,1362,0
  Data.w 0,7,64,110,239,255,1408,123,1523,285,1542,274,1422,129,0,14,0,138,255,255
  Data.w 388,0,307,205,290,355,342,524,556,625,1148,689,1352,602,1517,498,1504,342,1389,195
  Data.w 1345,0,0,17,0,0,4,255,528,958,448,1010,689,1010,643,706,600,607,648,161
  Data.w 551,233,506,354,436,415,420,496,390,516,382,552,406,618,412,700,0,7,0,0
  Data.w 4,255,0,1010,0,1079,1919,1071,1919,1009,0,10,0,0,4,255,680,980,725,951
  Data.w 936,887,913,956,924,989,927,1013,678,1031,0,18,0,0,4,255,932,889,1256,590
  Data.w 1425,402,1358,342,1305,237,1226,160,1169,41,1038,12,806,11,719,42,644,171,599,556
  Data.w 632,592,745,898,722,962,0,6,0,0,4,255,642,577,588,621,586,514,0,14
  Data.w 0,0,4,255,949,945,911,812,1256,571,1257,590,1154,742,1123,893,1123,948,1174,966
  Data.w 1189,1022,935,1033,931,964,0,17,0,0,4,255,1217,975,1161,1002,1178,1069,1412,1038
  Data.w 1405,1001,1368,976,1444,765,1466,602,1494,533,1483,491,1454,477,1423,400,1279,622,1229,768
  Data.w 0,7,0,0,4,255,1280,625,1249,577,1421,400,1425,407,0,7,0,0,4,255
  Data.w 109,1065,1919,1042,1919,1079,15,1079,0,7,188,148,130,255,943,514,937,579,945,579
  Data.w 959,512,0,7,188,148,130,255,907,664,898,705,913,705,914,663,0,7,188,148
  Data.w 130,255,847,688,830,688,837,641,844,643,0,7,188,148,130,255,817,503,831,539
  Data.w 837,538,833,502,0,7,108,25,0,255,789,437,795,453,810,456,806,437,0,7
  Data.w 108,25,0,255,966,423,961,439,979,440,985,425,0,0
EndDataSection
Thx for your regard :|

Re: OpenGl and ExamineKeyboard() = error

Posted: Sun Dec 15, 2024 1:36 am
by wombats
I'm not sure how to accomplish what you want, but I modified your example to show how to use the OpenGLGadget's events:

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
InitKeyboard()
InitSprite()

Global chemin$,sph_cmb,Dim sph_xx(1),Dim sph_yy(1),attente,attente_ok
Global numero,echelle_xf.f,echelle_yf.f,Resx,Resy,categorie$
Global Dim speedx.w(1,1),Dim speedy.w(1,1),pas
Global perso__x, perso__y, marcher__f.f,  marcher__zoom.f


;EnableExplicit

;-CONSTANTS
Enumeration
  #MainWindow
  #OpenGLGadget
EndEnumeration


ExamineDesktops()
ddw=DesktopWidth(0)
ddh=DesktopHeight(0)
mix=ddw/2
miy=ddh/2

;********************* DPI *********************
echelle_xf.f=(1920/ddw)*DesktopResolutionX()
echelle_yf.f=(1080/ddh)*DesktopResolutionY()
;**********************************************

;-STRUCTURES
Structure Integer2
  X.i
  Y.i
EndStructure
Global.Integer2 WindowDim
WindowDim\X = ddw
WindowDim\Y = ddh


;-DEFINES
Define.i Event
Define.i WindowFlags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget


;-DECLARES
Declare Render()
Declare Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
Declare SetupOpenGL()
Declare SetupGLTexture(ImageHandle.i)


;-MAIN WINDOW
win=OpenWindow(#MainWindow, 0, 0,ddw,ddh, "Lecteur_polygons",#PB_Window_Maximize|#PB_Window_BorderLess)
If win=0
  ;   Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenWindow() impossible")
  End
EndIf
screenGL=OpenGLGadget(#OpenGLGadget,0,0,ddw,ddh, #PB_OpenGL_Keyboard)
If screenGL=0
  ;   Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenGLGadget() impossible")
  End
EndIf

dpix.f=DesktopResolutionX()
dpiy.f=DesktopResolutionY()

SetupOpenGL()

AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666) ; quitter


glOrtho_(0,ddw,ddh,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

SetActiveGadget(#OpenGLGadget)

;*********************************************************************************************************************************

;;;;;;;;;;;
SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
;{
Procedure.i OpenGLDrawText(Gadget.i, x.f, y.f, Text$, Textcolor=#White)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i, tmpy.f
  
  
  Image = CreateImage(#PB_Any, 1, 1)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_Transparent)
          DrawText(0, 0, Text$, Textcolor)
          
          tmpy = y - (1 / GadgetHeight(Gadget) * TextHeight * 2)
          
          glRasterPos2f_(x, tmpy)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Procedure Render()
  
  ;Clearing buffers and resetting clear color to remove old graphics from the last frame.
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  ;  glClearColor_(0.2, 0.2, 0.2, 1.0)
  glClearColor_(0,0,0,1)
  
  ;## DRAWING TEXTURES/IMAGES
  ;First enable the Texture system.
  glEnable_(#GL_TEXTURE_2D)
  
  ;This procedure will create a quad and apply a texture to it.
  ;The Texture variable contains the texture created earlier using SetupGLTexture().
  Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), -2)
  ; Render2DQuad(Texture2, 0, 0, ImageWidth(Image2), ImageHeight(Image2), -1)
  
  ;After all the textures have been displayed disable the texture system.
  ;Otherwise it will conflict with the non texture graphics.
  glDisable_(#GL_TEXTURE_2D)
  
EndProcedure
Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
  
  ;The texture is first bound which tells OpenGL to use this texture for any future rendering.
  glBindTexture_(#GL_TEXTURE_2D, OGLTexture)
  glBegin_(#GL_QUADS)
  glColor4f_   (1,1,1,1)
  glNormal3f_  (0,0,1.0)
  glTexCoord2f_(1.0,1.0)
  glVertex3f_  (StartX+Width,StartY,Z)
  glTexCoord2f_(0.0,1.0)
  glVertex3f_  (StartX,StartY,Z)
  glTexCoord2f_(0.0,0.0)
  glVertex3f_  (StartX,StartY+Height,Z)
  glTexCoord2f_(1.0,0.0)
  glVertex3f_  (StartX+Width,StartY+Height,Z)
  glEnd_()
  
EndProcedure
Procedure SetupOpenGL()
  
  glMatrixMode_(#GL_PROJECTION)
  
  glOrtho_(0.0, WindowDim\X, WindowDim\Y, 0.0, -1000.0, 1000.0)
  
  glMatrixMode_(#GL_MODELVIEW)
  
  ; glEnable_(#GL_DEPTH_TEST)
  
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
  
EndProcedure

Procedure load_poly_data(categorie$,pol) ; ne jamais appeller un polygon "0.pol"
  If attente_ok=0 Or attente_ok<>pol
    attente_ok=pol
    Dim sph_xx(574)
    Dim sph_yy(574)
    
    For i=1 To 574/2
      Read.w sph_xx(i-1)
      Read.w sph_yy(i-1)
    Next
  EndIf
  
EndProcedure




Procedure affiche_poly_old(xxx,yyy,angle.f,dkx,dky,zoom.f,alpha.f)
  sph_cmb=sph_yy(0)
  sph_la=0
  Repeat
    glBegin_(#GL_POLYGON)
    glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
    glColor4f_(sph_xx(sph_la+1)/255,sph_yy(sph_la+1)/255,sph_xx(sph_la+2)/255,(sph_yy(sph_la+2)/255)*alpha)
    ;;;;;;
    For i=3 To sph_cmb-1
      sph_xxx.f=(sph_xx(sph_la+i)/echelle_xf)-xxx
      sph_yyy.f=(sph_yy(sph_la+i)/echelle_yf)-yyy
      sq.f=Sqr(sph_xxx*sph_xxx+sph_yyy*sph_yyy)
      ff.f = ATan2(sph_xxx,sph_yyy)+angle ; par Nemerod (sans lui, je n'y serai pas arrivé)
      centre_x=xxx+Cos(ff)*sq*zoom
      centre_y=yyy+Sin(ff)*sq*zoom
      glVertex2f_(centre_x+dkx,centre_y+dky);
    Next
    ;;;;;;;
    glEnd_()                      ; 
    sph_cmb=sph_yy(sph_la+i)
    sph_la+i
  Until sph_yy(sph_la)=0
  ;;;;;;;;;;;;;;;
EndProcedure

Procedure gestion()
  
EndProcedure
;}

;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
tourne.f=0
move.f=0.005
load_poly_data("monstre",1)
;;;
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
Repeat
  
  Repeat
    Event = WaitWindowEvent(10)
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #OpenGLGadget
            Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
            Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
            ;;;;;;;         
            Select EventType()
              Case #PB_EventType_KeyDown
                Select GetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_Key)
                  Case #PB_Shortcut_Left
                    Debug "left"
                    move-0.0001  
                  Case #PB_Shortcut_Right
                    Debug "right"
                    move+0.0001  
                EndSelect
            EndSelect
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;             
        EndSelect
      Case #PB_Event_Menu
        Select EventMenu()
          Case 666
            End
        EndSelect
    EndSelect
  Until Event = 0
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  gestion()
  affiche_poly_old(mix,miy,tourne,0,0,1,1)
  
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
  tourne+move
ForEver

End


DataSection
  Data.w 28,7,37,39,60,255,1919,0,1919,1079,0,1079,0,0,0,20,51,71,115,255
  Data.w 1908,589,1919,588,1919,813,1831,815,1772,833,1717,899,1528,946,1519,966,333,961,269,883
  Data.w 259,820,0,823,0,115,74,112,139,42,246,0,1869,0,0,10,51,71,115,255
  Data.w 0,801,0,963,67,964,213,886,211,847,267,814,157,775,0,19,61,82,141,255
  Data.w 236,0,155,544,162,598,300,818,343,825,348,643,380,646,713,881,1185,857,1267,816
  Data.w 1424,846,1577,814,1689,594,1695,540,1648,320,1629,0,0,9,61,82,141,255,700,861
  Data.w 723,900,485,900,427,844,398,838,381,672,0,7,61,82,141,255,616,816,381,674
  Data.w 379,618,611,775,0,22,64,110,239,255,380,0,256,255,235,333,257,496,363,646
  Data.w 380,648,412,705,606,750,630,710,646,711,695,734,1155,749,1211,720,1234,697,1384,661
  Data.w 1512,514,1509,354,1391,199,1344,0,0,6,64,110,239,255,1200,734,1179,768,1226,732
  Data.w 0,6,64,110,239,255,1519,641,1569,429,1590,426,0,7,64,110,239,255,1590,428
  Data.w 1542,271,1524,283,1567,437,0,7,64,110,239,255,1352,2,1407,123,1420,127,1362,0
  Data.w 0,7,64,110,239,255,1408,123,1523,285,1542,274,1422,129,0,14,0,138,255,255
  Data.w 388,0,307,205,290,355,342,524,556,625,1148,689,1352,602,1517,498,1504,342,1389,195
  Data.w 1345,0,0,17,0,0,4,255,528,958,448,1010,689,1010,643,706,600,607,648,161
  Data.w 551,233,506,354,436,415,420,496,390,516,382,552,406,618,412,700,0,7,0,0
  Data.w 4,255,0,1010,0,1079,1919,1071,1919,1009,0,10,0,0,4,255,680,980,725,951
  Data.w 936,887,913,956,924,989,927,1013,678,1031,0,18,0,0,4,255,932,889,1256,590
  Data.w 1425,402,1358,342,1305,237,1226,160,1169,41,1038,12,806,11,719,42,644,171,599,556
  Data.w 632,592,745,898,722,962,0,6,0,0,4,255,642,577,588,621,586,514,0,14
  Data.w 0,0,4,255,949,945,911,812,1256,571,1257,590,1154,742,1123,893,1123,948,1174,966
  Data.w 1189,1022,935,1033,931,964,0,17,0,0,4,255,1217,975,1161,1002,1178,1069,1412,1038
  Data.w 1405,1001,1368,976,1444,765,1466,602,1494,533,1483,491,1454,477,1423,400,1279,622,1229,768
  Data.w 0,7,0,0,4,255,1280,625,1249,577,1421,400,1425,407,0,7,0,0,4,255
  Data.w 109,1065,1919,1042,1919,1079,15,1079,0,7,188,148,130,255,943,514,937,579,945,579
  Data.w 959,512,0,7,188,148,130,255,907,664,898,705,913,705,914,663,0,7,188,148
  Data.w 130,255,847,688,830,688,837,641,844,643,0,7,188,148,130,255,817,503,831,539
  Data.w 837,538,833,502,0,7,108,25,0,255,789,437,795,453,810,456,806,437,0,7
  Data.w 108,25,0,255,966,423,961,439,979,440,985,425,0,0
EndDataSection

Re: OpenGl and ExamineKeyboard() = error

Posted: Sun Dec 15, 2024 7:20 am
by SPH
Thank you. But...

There is always this latency time that I want to avoid!
Imagine, you are on Notepad++: you hold down the A key. An A will be displayed, then a pause of a few milliseconds, then AAAAAAAAAAAAAAA.
Well, there, it is unfortunately the same. I would like a continuous A as soon as the key is pressed and without this word processing pause...

:arrow: :|

Re: OpenGl and ExamineKeyboard() = error

Posted: Sun Dec 15, 2024 12:42 pm
by breeze4me
You can change the system-wide keyboard repetition setting (not recommended), or you can use timers, as shown below.
You can also use GetAsyncKeyState_(), but this way is not appropriate for games. Because keystrokes can be interrupted by other programs.(It's a system-wide detection feature.)
In the code below, you'll need to assign as many timers as you need for each type of keystroke, because using just one timer won't handle multiple simultaneous keystrokes.

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
InitKeyboard()
InitSprite()

Global chemin$,sph_cmb,Dim sph_xx(1),Dim sph_yy(1),attente,attente_ok
Global numero,echelle_xf.f,echelle_yf.f,Resx,Resy,categorie$
Global Dim speedx.w(1,1),Dim speedy.w(1,1),pas
Global perso__x, perso__y, marcher__f.f,  marcher__zoom.f


;EnableExplicit

;-CONSTANTS
Enumeration
  #MainWindow
  #OpenGLGadget
EndEnumeration


ExamineDesktops()
ddw=600;DesktopWidth(0)
ddh=400;DesktopHeight(0)
mix=ddw/2
miy=ddh/2

;********************* DPI *********************
echelle_xf.f=(1920/ddw)*DesktopResolutionX()
echelle_yf.f=(1080/ddh)*DesktopResolutionY()
;**********************************************

;-STRUCTURES
Structure Integer2
  X.i
  Y.i
EndStructure
Global.Integer2 WindowDim
WindowDim\X = ddw
WindowDim\Y = ddh


;-DEFINES
Define.i Event
Define.i WindowFlags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget


;-DECLARES
Declare Render()
Declare Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
Declare SetupOpenGL()
Declare SetupGLTexture(ImageHandle.i)


;-MAIN WINDOW
win=OpenWindow(#MainWindow, 0, 0,ddw,ddh, "Lecteur_polygons",#PB_Window_BorderLess)
If win=0
  ;   Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenWindow() impossible")
  End
EndIf

screenGL=OpenGLGadget(#OpenGLGadget,0,0,ddw,ddh, #PB_OpenGL_Keyboard)
If screenGL=0
  ;   Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenGLGadget() impossible")
  End
EndIf

dpix.f=DesktopResolutionX()
dpiy.f=DesktopResolutionY()

SetupOpenGL()

AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666) ; quitter


glOrtho_(0,ddw,ddh,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

;*********************************************************************************************************************************

;;;;;;;;;;;
SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
;{
Procedure.i OpenGLDrawText(Gadget.i, x.f, y.f, Text$, Textcolor=#White)
  
  Protected Image.i, TextWidth.i, TextHeight.i, Result.i, tmpy.f
  
  
  Image = CreateImage(#PB_Any, 1, 1)
  If Image
    If StartDrawing(ImageOutput(Image))
      TextWidth = TextWidth(Text$)
      TextHeight = TextHeight(Text$)
      StopDrawing()
      
      If ResizeImage(Image, TextWidth, TextHeight)
        If StartDrawing(ImageOutput(Image))
          DrawingMode(#PB_2DDrawing_Transparent)
          DrawText(0, 0, Text$, Textcolor)
          
          tmpy = y - (1 / GadgetHeight(Gadget) * TextHeight * 2)
          
          glRasterPos2f_(x, tmpy)
          glDrawPixels_(TextWidth, TextHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
          
          Result = #True
          
          StopDrawing()
        EndIf
      EndIf
      
    EndIf
    FreeImage(Image)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Procedure Render()
  
  ;Clearing buffers and resetting clear color to remove old graphics from the last frame.
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  ;  glClearColor_(0.2, 0.2, 0.2, 1.0)
  glClearColor_(0,0,0,1)
  
  ;## DRAWING TEXTURES/IMAGES
  ;First enable the Texture system.
  glEnable_(#GL_TEXTURE_2D)
  
  ;This procedure will create a quad and apply a texture to it.
  ;The Texture variable contains the texture created earlier using SetupGLTexture().
  Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), -2)
  ; Render2DQuad(Texture2, 0, 0, ImageWidth(Image2), ImageHeight(Image2), -1)
  
  ;After all the textures have been displayed disable the texture system.
  ;Otherwise it will conflict with the non texture graphics.
  glDisable_(#GL_TEXTURE_2D)
  
EndProcedure
Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
  
  ;The texture is first bound which tells OpenGL to use this texture for any future rendering.
  glBindTexture_(#GL_TEXTURE_2D, OGLTexture)
  glBegin_(#GL_QUADS)
  glColor4f_   (1,1,1,1)
  glNormal3f_  (0,0,1.0)
  glTexCoord2f_(1.0,1.0)
  glVertex3f_  (StartX+Width,StartY,Z)
  glTexCoord2f_(0.0,1.0)
  glVertex3f_  (StartX,StartY,Z)
  glTexCoord2f_(0.0,0.0)
  glVertex3f_  (StartX,StartY+Height,Z)
  glTexCoord2f_(1.0,0.0)
  glVertex3f_  (StartX+Width,StartY+Height,Z)
  glEnd_()
  
EndProcedure
Procedure SetupOpenGL()
  
  glMatrixMode_(#GL_PROJECTION)
  
  glOrtho_(0.0, WindowDim\X, WindowDim\Y, 0.0, -1000.0, 1000.0)
  
  glMatrixMode_(#GL_MODELVIEW)
  
  ; glEnable_(#GL_DEPTH_TEST)
  
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
  
EndProcedure

Procedure load_poly_data(categorie$,pol) ; ne jamais appeller un polygon "0.pol"
  If attente_ok=0 Or attente_ok<>pol
    attente_ok=pol
    Dim sph_xx(574)
    Dim sph_yy(574)
    
    For i=1 To 574/2
      Read.w sph_xx(i-1)
      Read.w sph_yy(i-1)
    Next
  EndIf
  
EndProcedure




Procedure affiche_poly_old(xxx,yyy,angle.f,dkx,dky,zoom.f,alpha.f)
  sph_cmb=sph_yy(0)
  sph_la=0
  Repeat
    glBegin_(#GL_POLYGON)
    glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
    glColor4f_(sph_xx(sph_la+1)/255,sph_yy(sph_la+1)/255,sph_xx(sph_la+2)/255,(sph_yy(sph_la+2)/255)*alpha)
    ;;;;;;
    For i=3 To sph_cmb-1
      sph_xxx.f=(sph_xx(sph_la+i)/echelle_xf)-xxx
      sph_yyy.f=(sph_yy(sph_la+i)/echelle_yf)-yyy
      sq.f=Sqr(sph_xxx*sph_xxx+sph_yyy*sph_yyy)
      ff.f = ATan2(sph_xxx,sph_yyy)+angle ; par Nemerod (sans lui, je n'y serai pas arrivé)
      centre_x=xxx+Cos(ff)*sq*zoom
      centre_y=yyy+Sin(ff)*sq*zoom
      glVertex2f_(centre_x+dkx,centre_y+dky);
    Next
    ;;;;;;;
    glEnd_()                      ; 
    sph_cmb=sph_yy(sph_la+i)
    sph_la+i
  Until sph_yy(sph_la)=0
  ;;;;;;;;;;;;;;;
EndProcedure




Enumeration KeyRepeatTimer
  #Timer_Left
  #Timer_Right
  
  #Timer_MAX
EndEnumeration

Global Dim KeyRepeatTimerActive.l(#Timer_MAX - 1)


Procedure gestion()
  Shared move.f
  
  Repeat
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
            Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
            ;;;;;;;         
            Select EventType()
              Case #PB_EventType_LostFocus
                
                For tm = 0 To #Timer_MAX - 1
                  If KeyRepeatTimerActive(tm)
                    KeyRepeatTimerActive(tm) = 0
                    RemoveWindowTimer(0, tm)
                  EndIf
                Next
                
              Case #PB_EventType_KeyUp
                k = GetGadgetAttribute(1, #PB_OpenGL_Key)
                If k = #PB_Shortcut_Left
                  tm = #Timer_Left
                  If KeyRepeatTimerActive(tm)
                    KeyRepeatTimerActive(tm) = 0
                    RemoveWindowTimer(0, tm)
                    Debug "Timer_Left removed"
                  EndIf
                ElseIf k = #PB_Shortcut_Right
                  tm = #Timer_Right
                  If KeyRepeatTimerActive(tm)
                    KeyRepeatTimerActive(tm) = 0
                    RemoveWindowTimer(0, tm)
                    Debug "Timer_Right removed"
                  EndIf
                EndIf
                
              Case #PB_EventType_KeyDown
                k = GetGadgetAttribute(1, #PB_OpenGL_Key)
                If k = #PB_Shortcut_Left
                  tm = #Timer_Left
                  If KeyRepeatTimerActive(tm) = 0
                    move-0.0001
                    
                    Debug "left_KeyDown"
                    
                    KeyRepeatTimerActive(tm) = 1
                    AddWindowTimer(0, tm, 20)
                  EndIf
                  
                ElseIf k = #PB_Shortcut_Right
                  tm = #Timer_Right
                  If KeyRepeatTimerActive(tm) = 0
                    move+0.0001
                    
                    Debug "right_KeyDown"
                    KeyRepeatTimerActive(tm) = 1
                    AddWindowTimer(0, tm, 20)
                  EndIf
                EndIf
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;             
            EndSelect
        EndSelect
        
      Case #PB_Event_Timer
        Select EventTimer()
          Case #Timer_Left
            Debug "left"
            move-0.0001
            
          Case #Timer_Right
            Debug "right"
            move+0.0001
            
        EndSelect
        
      Case #PB_Event_Menu
        Select EventMenu()
          Case 666
            End
            
        EndSelect
    EndSelect
    
  Until Event = 0
EndProcedure
;}

;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
tourne.f=0
move.f=0.005
load_poly_data("monstre",1)
SetActiveGadget(#OpenGLGadget)
;;;
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
Repeat
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  gestion()
  
  ;;;;;;
  ;;;;;;
  ;;;;;;
  
  
  ;;;;;;
  ;;;;;;
  ;;;;;;
  
  affiche_poly_old(mix,miy,tourne,0,0,1,1)
  
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
  tourne+move
  
ForEver

End


DataSection
  Data.w 28,7,37,39,60,255,1919,0,1919,1079,0,1079,0,0,0,20,51,71,115,255
  Data.w 1908,589,1919,588,1919,813,1831,815,1772,833,1717,899,1528,946,1519,966,333,961,269,883
  Data.w 259,820,0,823,0,115,74,112,139,42,246,0,1869,0,0,10,51,71,115,255
  Data.w 0,801,0,963,67,964,213,886,211,847,267,814,157,775,0,19,61,82,141,255
  Data.w 236,0,155,544,162,598,300,818,343,825,348,643,380,646,713,881,1185,857,1267,816
  Data.w 1424,846,1577,814,1689,594,1695,540,1648,320,1629,0,0,9,61,82,141,255,700,861
  Data.w 723,900,485,900,427,844,398,838,381,672,0,7,61,82,141,255,616,816,381,674
  Data.w 379,618,611,775,0,22,64,110,239,255,380,0,256,255,235,333,257,496,363,646
  Data.w 380,648,412,705,606,750,630,710,646,711,695,734,1155,749,1211,720,1234,697,1384,661
  Data.w 1512,514,1509,354,1391,199,1344,0,0,6,64,110,239,255,1200,734,1179,768,1226,732
  Data.w 0,6,64,110,239,255,1519,641,1569,429,1590,426,0,7,64,110,239,255,1590,428
  Data.w 1542,271,1524,283,1567,437,0,7,64,110,239,255,1352,2,1407,123,1420,127,1362,0
  Data.w 0,7,64,110,239,255,1408,123,1523,285,1542,274,1422,129,0,14,0,138,255,255
  Data.w 388,0,307,205,290,355,342,524,556,625,1148,689,1352,602,1517,498,1504,342,1389,195
  Data.w 1345,0,0,17,0,0,4,255,528,958,448,1010,689,1010,643,706,600,607,648,161
  Data.w 551,233,506,354,436,415,420,496,390,516,382,552,406,618,412,700,0,7,0,0
  Data.w 4,255,0,1010,0,1079,1919,1071,1919,1009,0,10,0,0,4,255,680,980,725,951
  Data.w 936,887,913,956,924,989,927,1013,678,1031,0,18,0,0,4,255,932,889,1256,590
  Data.w 1425,402,1358,342,1305,237,1226,160,1169,41,1038,12,806,11,719,42,644,171,599,556
  Data.w 632,592,745,898,722,962,0,6,0,0,4,255,642,577,588,621,586,514,0,14
  Data.w 0,0,4,255,949,945,911,812,1256,571,1257,590,1154,742,1123,893,1123,948,1174,966
  Data.w 1189,1022,935,1033,931,964,0,17,0,0,4,255,1217,975,1161,1002,1178,1069,1412,1038
  Data.w 1405,1001,1368,976,1444,765,1466,602,1494,533,1483,491,1454,477,1423,400,1279,622,1229,768
  Data.w 0,7,0,0,4,255,1280,625,1249,577,1421,400,1425,407,0,7,0,0,4,255
  Data.w 109,1065,1919,1042,1919,1079,15,1079,0,7,188,148,130,255,943,514,937,579,945,579
  Data.w 959,512,0,7,188,148,130,255,907,664,898,705,913,705,914,663,0,7,188,148
  Data.w 130,255,847,688,830,688,837,641,844,643,0,7,188,148,130,255,817,503,831,539
  Data.w 837,538,833,502,0,7,108,25,0,255,789,437,795,453,810,456,806,437,0,7
  Data.w 108,25,0,255,966,423,961,439,979,440,985,425,0,0
EndDataSection

Re: OpenGl and ExamineKeyboard() = error

Posted: Sun Dec 15, 2024 1:21 pm
by SPH
Thanks a lot guy!