photos writings music about

<- prev | next ->

DWGL


''=============================================================================
'' Danny Wilfong's OpenGL for FreeBasic
'' Coming from Easy GL2D by Relminator (Richard Eric M. Lope)
'' Modified and rebuilt starting April 04, 2011
''=============================================================================
'' DWGLcolor and color conversion in each drawing function
'' may be simplified. Don't understand it fully yet. Think it's working okay though.
''
'' Old notes: DWGLColor should use DW_RGBA() ?     

#Include Once "DWGL.bi"

Namespace GL

'' Some globals to make life easier
'' Kinda safe because access is limited to this
'' module and cannot be accessed outside of this
'' module even with a namespace qualifier

Dim Shared As GLuint current_texture = 0
Dim Shared As GLuint font_list_base = 0
Dim Shared As GLuint font_textureID = 0

''=============================================================================
''
''      synchronizes the drawing after vblank
''
''      got this from FB's example file
''      added Landeel's stuff for linux
''     
''=============================================================================
Function vsync_on() As Integer
     
      Dim swapinterval As Function(ByVal interval As Integer) As Integer
      Dim extensions As String
     
      '' setup opengl and retrieve supported extensions
      ScreenControl FB.GET_GL_EXTENSIONS, extensions
     
      If (InStr(extensions, "WGL_EXT_swap_control") <> 0) Then
            '' extension supported, retrieve proc address
            swapinterval = ScreenGLProc("wglSwapIntervalEXT")
      Else
            swapinterval = ScreenGLProc("glXSwapIntervalSGI")
      End If

      If (swapinterval <> 0) Then
            '' ok, we got it. set opengl to wait for vertical sync on buffer swaps
            swapinterval(1)
      End If
     
      Return swapinterval
End Function


''=============================================================================
''
''      Starts the 2d drawing
''
''=============================================================================
Sub start_2d(ByVal wid As Integer, ByVal hei As Integer)
     
      glMatrixMode(GL_PROJECTION)     
      glPushMatrix()
      glLoadIdentity()
      glOrtho(0, wid, hei, 0, -1, 1)
      glMatrixMode(GL_MODELVIEW)
      glPushMatrix()
      glLoadIdentity()
      'If SCR_S = 1 Then glTranslatef(0.375, 0.375, 0)      '' Alignment of pixels.
     
End Sub

''=============================================================================
''
''      Ends ortho drawing for if you want to do a 2d-3d combo engine
''
''=============================================================================
Sub end_2d()
     
      glMatrixMode(GL_PROJECTION)
      glPopMatrix()
      glMatrixMode(GL_MODELVIEW)
      glPopMatrix()
     
End Sub

''=============================================================================
''
''      private!! sets up the fontsystem
''
''=============================================================================
Private Sub font_init()

     
      Dim As Any Ptr image = ImageCreate(128,128,RGBA(255,0,255,0))
     
      For x As Integer = 0 To 15
            For y As Integer = 0 To 15
                  Draw String image, (x * 8, y * 8), Chr( x + y * 16)
            Next
      Next

     
      font_textureID = load_image_24bit_alpha( image )
     

      ImageDestroy( image )


      font_list_base = glGenLists(256)
      Dim As Single scale = 1/128
      Dim As Single w = 8 * scale
      Dim As Single h = 8 * scale

      glPushMatrix()
      glLoadIdentity()
      For font_loop As Integer = 0 To 255
           
            Dim As Single x = (font_loop Mod 16) * w
            Dim As Single y = Fix(font_loop / 16) * h
           
            glNewList( font_list_base + font_loop, GL_COMPILE )
            glBegin( GL_QUADS )
                        glTexCoord2f( x, y + h)
                        glVertex2i( 0, 8 )
                      
                        glTexCoord2f( x + w, y + h )
                        glVertex2i( 8, 8 )
                      
                        glTexCoord2f( x + w,y )
                        glVertex2i( 8, 0 )
                      
                        glTexCoord2f( x, y )
                        glVertex2i( 0, 0 )
            glEnd()
           
            glTranslatef( 8,0,0 )
            glEndList()
           
      Next font_loop

      glPopMatrix()
     
End Sub

''=============================================================================
''
''      Sets up OpenGL for 2d mode
''
''=============================================================================
Sub screen_init(ByVal screen_wid As Integer, ByVal screen_hei As Integer, scale As Single = 1.0, ByVal flags As Integer = 0)
     
      If flags Then
            ScreenRes screen_wid, screen_hei, 32, 2, FB.GFX_OPENGL Or flags
      Else
            ScreenRes screen_wid, screen_hei, 32, 2, FB.GFX_OPENGL
      EndIf
     
      'screen information
      Dim w As Integer, h As Integer
      'OpenGL params for gluerspective
      Dim FOVy As Double                  'Field of view angle in Y
      Dim Aspect As Double                 'Aspect of screen
      Dim znear As Double                 'z-near clip distance
      Dim zfar As Double                  'z-far clip distance

      glViewport(0, 0, screen_wid, screen_hei)
      'glViewport(0, 0, scr_w, scr_h)
      'glViewport(1024\2-800\2, 768\2-600\2, 1024\2+800\2-1, 768\2+600\2-1)
'      glViewport(res_w\2-960, -res_h\2+540, scr_w/3, scr_h/3)
      'glViewport(100, 50, 320*5, 180*5)
     
      'Set current Mode to projection(ie: 3d)
      glMatrixMode(GL_PROJECTION)
     
      'Load identity matrix to projection matrix
      glLoadIdentity()

      'Set gluPerspective params
      FOVy = 90/2                                                      '45 deg fovy
      Aspect = screen_wid / screen_hei
      znear = 1                                                           'Near clip
      zfar = 500                                                           'far clip
     
      'use glu Perspective to set our 3d frustum dimension up
      gluPerspective(FOVy, aspect, znear, zfar)
     
      'Modelview mode
      'ie. Matrix that does things to anything we draw
      'as in lines, points, tris, etc.
      glMatrixMode(GL_MODELVIEW)
      'load identity(clean) matrix to modelview
      glLoadIdentity()
     
      glShadeModel(GL_FLAT)                        'set shading (GL_FLAT, GL_SMOOTH)
      glClearColor(0.0, 0.0, 0.0, 1.0)            'set Clear color to BLACK
      glClearDepth(1.0)                                   'Set Depth buffer to 1(z-Buffer)
      glDisable(GL_DEPTH_TEST)                        'Disable Depth Testing so that our z-buffer works
     
      'compare each incoming pixel z value with the z value present in the depth buffer
      'LEQUAL means than pixel is drawn if the incoming z value is less than
      'or equal to the stored z value
      glDepthFunc(GL_LEQUAL)
     
      'have one or more material parameters track the current color
      'Material is your 3d model
      glEnable(GL_COLOR_MATERIAL)

      'Enable Texturing
      glEnable(GL_TEXTURE_2D)
     
            'Tell openGL that we want the best possible perspective transform
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

      'Disable Backface culling
      glDisable (GL_CULL_FACE)
      glPolygonMode(GL_FRONT, GL_FILL)
     
     
      '' enable blending for transparency
      glEnable(GL_BLEND)                 
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
     
      glDisable( GL_DEPTH_TEST )
     
      glEnable( GL_ALPHA_TEST )
      glAlphaFunc(GL_GREATER, 0)

      glDisable(GL_STENCIL_TEST)
      glDisable(GL_TEXTURE_1D)
      glDisable(GL_LIGHTING)
      glDisable(GL_LOGIC_OP)
      glDisable(GL_DITHER)
      glDisable(GL_FOG)

      glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST)
      glHint(GL_LINE_SMOOTH_HINT , GL_FASTEST)

      glPointSize( SCR_S )
      glLineWidth( SCR_S )
     
      '' set up the font system
      font_init()

      'start_2d(320, 180)
      start_2d(screen_wid\scale, screen_hei\scale)
     
End Sub


''=============================================================================
''
''      Sets blend mode
''      E_TRANS = normal transparent
''      E_SOLID = solid with no transparency
''      E_BLENDED = 1:1 blending
''      E_GLOW = ADDITIVE blending
''
''=============================================================================
Sub set_blend_mode(ByVal blend_mode As E_BLEND_MODE)
     
      Select Case blend_mode
            Case E_TRANS
                  glDisable(GL_BLEND)                 
                  glEnable(GL_ALPHA_TEST)
            Case E_SOLID
                  glDisable(GL_BLEND)
                  glDisable(GL_ALPHA_TEST)                 
            Case E_BLENDED
                  glEnable(GL_BLEND)
                  glEnable(GL_ALPHA_TEST)                 
                  'glBlendFunc(GL_SRC_ALPHA, GL_ONE)
                  'glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA)
                  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
            Case E_GLOW
                  glEnable(GL_BLEND)
                  glEnable(GL_ALPHA_TEST)
                  glBlendFunc(GL_SRC_ALPHA, GL_ONE)                  '' Additive preserving alpha
                  'glBlendFunc(GL_ONE, GL_ONE)                        '' Additive without alpha (alpha appears black)
            Case E_BLACK
                  glEnable(GL_BLEND)
                  glEnable(GL_ALPHA_TEST)           
                  glBlendFunc(GL_ZERO,GL_ONE_MINUS_SRC_ALPHA)
            Case Else
                  glDisable(GL_BLEND)                 
                  glEnable(GL_ALPHA_TEST)
      End Select
     
End Sub

''=============================================================================
''
''      Enables or disables AA depending on the switch
''
''=============================================================================
Sub enable_antialias(ByVal switch As Integer = 1)
     
      If switch Then
            glEnable( GL_POINT_SMOOTH )
            glEnable( GL_LINE_SMOOTH )
      Else
            glDisable( GL_POINT_SMOOTH )
            glDisable( GL_LINE_SMOOTH )
      EndIf
     
End Sub

''=============================================================================
''
''      Clears the buffer
''
''=============================================================================
Sub clear_screen()
     
      glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)

End Sub

Sub pixel( ByVal x As Integer, ByVal y As Integer, ByVal DWGLcolor As GLuint )
     
      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )

      glTranslatef(0.5, 0.5, 0) '' Remove centeredness of drawing. Lines up with sprite pixels.
      glBegin( GL_POINTS )
            glVertex2i( x, y )
      glEnd()
      glEnable( GL_TEXTURE_2D )
      glTranslatef(-0.5, -0.5, 0) '' Return translation to normal
      glColor4ub(255,255,255,255)
     
End Sub

Sub line( ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal DWGLcolor As GLuint )

      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )
     
      x2 += 1

      glTranslatef(0.0, 0.5, 0) '' Remove centeredness of drawing. Lines up with sprite pixels.
     
      glBegin( GL_LINES )
            glVertex2i( x1, y1 )
            glVertex2i( x2, y2 )
      glEnd()
      glEnable( GL_TEXTURE_2D )
     
      glTranslatef(0.0, -0.5, 0) '' Return translation to normal

      glColor4ub(255,255,255,255)
     
End Sub

Sub quad( ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal x3 As Integer, ByVal y3 As Integer, ByVal x4 As Integer, ByVal y4 As Integer, ByVal DWGLcolor As GLuint )

      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )
     
      glTranslatef(0.0, 0.5, 0) '' Remove centeredness of drawing. Lines up with sprite pixels.
     
      glBegin( GL_LINE_STRIP )
            glVertex2i( x1, y1 )
            glVertex2i( x2, y2 )
            glVertex2i( x3, y3 )
            glVertex2i( x4, y4 )
            glVertex2i( x1, y1 )
      glEnd()
      glEnable( GL_TEXTURE_2D )
     
      glTranslatef(0.0, -0.5, 0) '' Return translation to normal

      glColor4ub(255,255,255,255)
     
End Sub

Sub box( ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal DWGLcolor As GLuint)

      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )
     
      glTranslatef(0.5, 0.5, 0) '' Remove centeredness of drawing. Lines up with sprite pixels.
     
      glBegin( GL_LINE_STRIP )
            glVertex2i(x1, y1)
            glVertex2i(x2, y1)
            glVertex2i(x2, y2)
            glVertex2i(x1, y2)
            glVertex2i(x1, y1)
      glEnd()
     
      glTranslatef(-0.5, -0.5, 0) '' Return translation to normal
     
      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)
     
End Sub

Sub box_filled( ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal DWGLcolor As GLuint )

      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )
     
      '' Adjust for correct drawing of Quads box:
      '' This fixes 1-pixel offset even if drawn from right to left or bottom to top.
      If x1 > x2 Then x1+=1 Else x2+=1
      If y1 > y2 Then y1+=1 Else y2+=1
     
      glBegin(GL_QUADS)
           
            glVertex2i      (x1,y1)
            glVertex2i      (x1,y2)
            glVertex2i      (x2,y2)
            glVertex2i      (x2,y1)
           
      glEnd()
     
      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)
     
End Sub

Sub box_filled_gradient( ByVal x1 As Integer, ByVal y1 As Integer,_
                                    ByVal x2 As Integer, ByVal y2 As Integer,_
                                    ByVal DWGLcolor1 As GLuint,_
                                    ByVal DWGLcolor2 As GLuint,_
                                    ByVal DWGLcolor3 As GLuint,_
                                    ByVal DWGLcolor4 As GLuint )

      glDisable( GL_TEXTURE_2D )

      DWGLcolor1 = RGBA(RGBA_B(DWGLcolor1),RGBA_G(DWGLcolor1),RGBA_R(DWGLcolor1),RGBA_A(DWGLcolor1))
      DWGLcolor2 = RGBA(RGBA_B(DWGLcolor2),RGBA_G(DWGLcolor2),RGBA_R(DWGLcolor2),RGBA_A(DWGLcolor2))
      DWGLcolor3 = RGBA(RGBA_B(DWGLcolor3),RGBA_G(DWGLcolor3),RGBA_R(DWGLcolor3),RGBA_A(DWGLcolor3))
      DWGLcolor4 = RGBA(RGBA_B(DWGLcolor4),RGBA_G(DWGLcolor4),RGBA_R(DWGLcolor4),RGBA_A(DWGLcolor4))
                             
      x2 += 1
      y2 += 1
     
      glBegin(GL_QUADS)
            glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor1 ) )
            glVertex2i      (x1,y1)
            glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor2 ) )
            glVertex2i      (x1,y2)
            glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor3 ) )
            glVertex2i      (x2,y2)
            glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor4 ) )
            glVertex2i      (x2,y1)           
      glEnd()
     
      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)
     
End Sub

Sub triangle( ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer,_
                       ByVal x3 As Integer, ByVal y3 As Integer, ByVal DWGLcolor As GLuint )

      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )

      glTranslatef(0, 0.5, 0)
     
      glBegin( GL_LINE_STRIP )
            glVertex2i( x1, y1 )
            glVertex2i( x2, y2 )
            glVertex2i( x3, y3 )
            glVertex2i( x1, y1 )
      glEnd()
     
      glTranslatef(0, -0.5, 0)
     
      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)
     
End Sub

Sub triangle_filled( ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer,_
                              ByVal x3 As Integer, ByVal y3 As Integer, ByVal DWGLcolor As GLuint )

      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )

      glBegin( GL_TRIANGLES )
            glVertex2i( x1, y1 )
            glVertex2i( x2, y2 )
            glVertex2i( x3, y3 )
      glEnd()
     
      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)
     
End Sub

Sub triangle_filled_gradient( ByVal x1 As Integer, ByVal y1 As Integer,_
                                               ByVal x2 As Integer, ByVal y2 As Integer,_
                                               ByVal x3 As Integer, ByVal y3 As Integer,_
                                               ByVal DWGLcolor1 As GLuint, ByVal DWGLcolor2 As GLuint, ByVal DWGLcolor3 As GLuint )

      glDisable( GL_TEXTURE_2D )

      DWGLcolor1 = RGBA(RGBA_B(DWGLcolor1),RGBA_G(DWGLcolor1),RGBA_R(DWGLcolor1),RGBA_A(DWGLcolor1))
      DWGLcolor2 = RGBA(RGBA_B(DWGLcolor2),RGBA_G(DWGLcolor2),RGBA_R(DWGLcolor2),RGBA_A(DWGLcolor2))
      DWGLcolor3 = RGBA(RGBA_B(DWGLcolor3),RGBA_G(DWGLcolor3),RGBA_R(DWGLcolor3),RGBA_A(DWGLcolor3))

      glBegin( GL_TRIANGLES )
            glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor1 ) )
            glVertex2i( x1, y1 )
            glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor2 ) )
            glVertex2i( x2, y2 )
            glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor3 ) )
            glVertex2i( x3, y3 )
      glEnd()
     
      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)
     
End Sub

Sub circle_2d(ByVal x As Integer, ByVal y As Integer, ByVal radius As Integer, ByVal DWGLcolor As GLuint)

      Dim As Integer stepsize = 45 * 3.141593/radius
      If stepsize < 1 Then stepsize = 1
      If stepsize > 5 Then stepsize = 5
     
      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )
     
      glTranslatef(0.5, 0.5, 0) '' Remove centeredness of drawing. Lines up with sprite pixels.
     
      glBegin(GL_LINE_LOOP)
      For i As Integer = 0 To 359 Step stepsize
            Dim As Single angle = i * 3.141593/180
            glVertex2i(x + Cos(angle)* radius, y + Sin(angle)* radius)
      Next i
      glEnd()

      glTranslatef(-0.5, -0.5, 0) '' Return translation to normal

      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)
     
End Sub

Sub circle_2d_filled(ByVal x As Integer, ByVal y As Integer, ByVal radius As Integer, ByVal DWGLcolor As GLuint, stepsize As Integer = 1)

      '' Gave user control over step size, which effects smoothness of edges.
      'Dim As Integer stepsize = 360 * 3.141593/radius
      'If stepsize < 1 Then stepsize = 1
      'If stepsize > 6 Then stepsize = 6
     
      glDisable( GL_TEXTURE_2D )
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )
     
      glBegin(GL_TRIANGLE_FAN)
      For i As Integer = 0 To 359 Step stepsize
            Dim As Single angle = i * 3.141593/180
            glVertex2i(x + Cos(angle)* radius, y + Sin(angle)* radius)
      Next i
      glEnd()

      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)
     
End Sub


''=============================================================================
''
''      Draws an ellipse
''      Contributed by Michael "h4tt3n" Nissen
''      syntax: ellipse(center x, center y, semimajor axis, semiminor axis, angle in radians, color)
''
''=============================================================================

Sub ellipse(ByVal x As Single, ByVal y As Single, ByVal a As Single, ByVal b As Single, ByVal angle As Single, ByVal DWGLcolor As GLuint )
          
          
      '' these constants decide the graphic quality of the ellipse
      Const As Single pi = 4*Atn(1)            ''            pi
      Const As Single twopi = 2*pi            ''            two pi (radians in a circle)
      Const As Integer face_length = 8      ''            approx. face length in pixels
      Const As Integer max_faces = 256           ''            maximum number of faces in ellipse
      Const As Integer min_faces = 16           ''            minimum number of faces in ellipse
     
      '' approx. ellipse circumference (hudson's method)
      Dim As Single h            = (a-b*a-b)/(a+b*a+b)
      Dim As Single circumference = 0.25*pi*(a+b)*(3*(1+h*0.25)+1/(1-h*0.25))
     
      '' number of faces in ellipse
      Dim As Integer num_faces = circumference\face_length
     
      '' clamp number of faces
      If num_faces > max_faces Then num_faces = max_faces
      If num_faces < min_faces Then num_faces = min_faces
     
      '' keep number of faces divisible by 4
      num_faces -= num_faces Mod 4
     
      '' precalc cosine theta
      Dim As Double s            = Sin(twopi/num_faces)
      Dim As Double c            = Cos(twopi/num_faces)
      Dim As Double xx            = 1
      Dim As Double yy            = 0
      Dim As Double xt            = 0
      Dim As Double ax = Cos(angle)
      Dim As Double ay = Sin(angle)
     
     
      '' draw ellipse
      glDisable( GL_TEXTURE_2D )     
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )

      glTranslatef(0.5, 0.5, 0) '' Remove centeredness of drawing. Lines up with sprite pixels.
     
      glBegin(GL_LINE_LOOP)
     
            For i As Integer = 1 To num_faces-1
                  xt = xx
                  xx = c * xx - s * yy
                  yy = s * xt + c * yy
                  glvertex2f(x+a*xx*ax-b*yy*ay, y+a*xx*ay+b*yy*ax)
            Next
            glVertex2f(x+a*ax, y+a*ay)
     
      glEnd()

      glTranslatef(-0.5, -0.5, 0) '' Return translation to normal
     
      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)

End Sub


''=============================================================================
''
''      Draws an ellipse
''      Contributed by Michael "h4tt3n" Nissen(I added a filled version ;*))
''      syntax: ellipse_filled(center x, center y, semimajor axis, semiminor axis, angle in radians, color)
''
''=============================================================================

Sub ellipse_filled(ByVal x As Single, ByVal y As Single, ByVal a As Single, ByVal b As Single, ByVal angle As Single, ByVal DWGLcolor As GLuint )
          
      '' these constants decide the graphic quality of the ellipse
      Const As Single pi = 4*Atn(1)            ''            pi
      Const As Single twopi = 2*pi            ''            two pi (radians in a circle)
      Const As Integer face_length = 8      ''            approx. face length in pixels
      Const As Integer max_faces = 256           ''            maximum number of faces in ellipse
      Const As Integer min_faces = 16           ''            minimum number of faces in ellipse
     
      '' approx. ellipse circumference (hudson's method)
      Dim As Single h            = (a-b*a-b)/(a+b*a+b)
      Dim As Single circumference = 0.25*pi*(a+b)*(3*(1+h*0.25)+1/(1-h*0.25))
     
      '' number of faces in ellipse
      Dim As Integer num_faces = circumference\face_length
     
      '' clamp number of faces
      If num_faces > max_faces Then num_faces = max_faces
      If num_faces < min_faces Then num_faces = min_faces
     
      '' keep number of faces divisible by 4
      num_faces -= num_faces Mod 4
     
      '' precalc cosine theta
      Dim As Double s            = Sin(twopi/num_faces)
      Dim As Double c            = Cos(twopi/num_faces)
      Dim As Double xx            = 1
      Dim As Double yy            = 0
      Dim As Double xt            = 0
      Dim As Double ax = Cos(angle)
      Dim As Double ay = Sin(angle)
     
     
      '' draw ellipse
      glDisable( GL_TEXTURE_2D )     
     
      DWGLcolor = RGBA(RGBA_B(DWGLcolor),RGBA_G(DWGLcolor),RGBA_R(DWGLcolor),RGBA_A(DWGLcolor))
      glColor4ubv( Cast( GLubyte Ptr, @DWGLcolor ) )

      glBegin(GL_TRIANGLE_FAN)
     
            For i As Integer = 1 To num_faces-1
                  xt = xx
                  xx = c * xx - s * yy
                  yy = s * xt + c * yy
                  glvertex2f(x+a*xx*ax-b*yy*ay, y+a*xx*ay+b*yy*ax)
            Next
            glVertex2f(x+a*ax, y+a*ay)
     
      glEnd()

      glEnable( GL_TEXTURE_2D )
     
      glColor4ub(255,255,255,255)

End Sub

Sub sprite_multi(ByVal x As Single, ByVal y As Single, ByVal scale_w As Single, ByVal scale_h As Single, ByVal flipmode As E_FLIP_MODE, ByVal spr As image Ptr)
     
      '' My version of sprite drawing, adding as many features as I can into one.
      '' Danny
     
      Dim As Integer x1 = 0
      Dim As Integer y1 = 0
      Dim As Integer x2 = (spr->Width)
      Dim As Integer y2 = (spr->height)
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
      glPushMatrix()
     
      glTranslatef(x, y, 0)
     
      glScalef(scale_w,scale_h,1.0)
     
      '' flip it according to mode:
      Dim As Single u1, v1, u2, v2
      Select Case flipmode
            Case E_NONE:     
                  u1 = u_off                  : v1 = v_off
                  u2 = u_off + u_width : v2 = v_off + v_height
            Case E_V:
                  u1 = u_off                  : v1 = v_off + v_height
                  u2 = u_off + u_width : v2 = v_off
            Case E_H:
                  u1 = u_off + u_width : v1 = v_off
                  u2 = u_off             : v2 = v_off + v_height
            Case E_VH:
                  u1 = u_off + u_width : v1 = v_off + v_height
                  u2 = u_off                  : v2 = v_off
            Case Else
                  u1 = u_off                  : v1 = v_off
                  u2 = u_off + u_width : v2 = v_off + v_height
      End Select
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u1, v1)
            glVertex2f      (x1,y1)
           
            glTexcoord2f(u1, v2)
            glVertex2f      (x1,y2)
           
            glTexcoord2f(u2, v2)
            glVertex2f      (x2,y2)
           
            glTexcoord2f(u2, v1)
            glVertex2f      (x2,y1)
           
      glEnd()
     
      glPopMatrix()
     
End Sub

''=============================================================================
''
''      Draws a 2d sprite
''
''=============================================================================
Sub sprite( ByVal x As Integer, ByVal y As Integer, ByVal spr As GL.IMAGE Ptr )
     
      Dim As Integer x1 = x
      Dim As Integer y1 = y
      Dim As Integer x2 = x + (spr->Width)
      Dim As Integer y2 = y + (spr->height)
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height

      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u_off, v_off)
            glVertex2i      (x1,y1)
           
            glTexcoord2f(u_off, v_off + v_height)
            glVertex2i      (x1,y2)
           
            glTexcoord2f(u_off + u_width, v_off + v_height)
            glVertex2i      (x2,y2)
           
            glTexcoord2f(u_off + u_width, v_off)
            glVertex2i      (x2,y1)
           
      glEnd()
           
End Sub


''=============================================================================
''
''      Draws a flipped 2d sprite
''
''=============================================================================
Sub sprite_flip( ByVal x As Integer, ByVal y As Integer, ByVal mode As E_FLIP_MODE, ByVal spr As image Ptr)
     
      Dim As Integer x1 = x
      Dim As Integer y1 = y
      Dim As Integer x2 = x + (spr->Width)
      Dim As Integer y2 = y + (spr->height)
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
      Dim As Single u1, v1, u2, v2
      Select Case mode
            Case E_NONE:     
                  u1 = u_off                  : v1 = v_off
                  u2 = u_off + u_width : v2 = v_off + v_height
            Case E_V:
                  u1 = u_off                  : v1 = v_off + v_height
                  u2 = u_off + u_width : v2 = v_off
            Case E_H:
                  u1 = u_off + u_width : v1 = v_off
                  u2 = u_off             : v2 = v_off + v_height
            Case E_VH:
                  u1 = u_off + u_width : v1 = v_off + v_height
                  u2 = u_off                  : v2 = v_off
            Case Else
                  u1 = u_off                  : v1 = v_off
                  u2 = u_off + u_width : v2 = v_off + v_height
      End Select
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u1, v1)
            glVertex2i      (x1,y1)
           
            glTexcoord2f(u1, v2)
            glVertex2i      (x1,y2)
           
            glTexcoord2f(u2, v2)
            glVertex2i      (x2,y2)
           
            glTexcoord2f(u2, v1)
            glVertex2i      (x2,y1)
           
      glEnd()
     
End Sub

''=============================================================================
''
''      Draws a scaled 2d sprite
''      having a scale of 1.0 gives you the original size
''
''=============================================================================
Sub sprite_scale( ByVal x As Integer, ByVal y As Integer, ByVal scale As Single, ByVal spr As image Ptr)
     
      Dim As Integer x1 = 0
      Dim As Integer y1 = 0
      Dim As Integer x2 = (spr->Width)
      Dim As Integer y2 = (spr->height)
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
      glPushMatrix()
     
      glTranslatef(x, y, 0)
     
      glScalef(scale,scale,1.0)
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u_off, v_off)
            glVertex2i      (x1,y1)
           
            glTexcoord2f(u_off, v_off + v_height)
            glVertex2i      (x1,y2)
           
            glTexcoord2f(u_off + u_width, v_off + v_height)
            glVertex2i      (x2,y2)
           
            glTexcoord2f(u_off + u_width, v_off)
            glVertex2i      (x2,y1)
           
      glEnd()
     
      glPopMatrix()
     
End Sub

''=============================================================================
''
''      Draws a scaled 2d sprite
''      having a scale of 1.0 gives you the original size
''
''=============================================================================
Sub sprite_scale_wh( ByVal x As Integer, ByVal y As Integer, ByVal scale_w As Single, ByVal scale_h As Single, ByVal spr As image Ptr)
     
      Dim As Integer x1 = 0
      Dim As Integer y1 = 0
      Dim As Integer x2 = (spr->Width)
      Dim As Integer y2 = (spr->height)
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
      glPushMatrix()
     
      glTranslatef(x, y, 0)
     
      glScalef(scale_w,scale_h,1.0)
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u_off, v_off)
            glVertex2i      (x1,y1)
           
            glTexcoord2f(u_off, v_off + v_height)
            glVertex2i      (x1,y2)
           
            glTexcoord2f(u_off + u_width, v_off + v_height)
            glVertex2i      (x2,y2)
           
            glTexcoord2f(u_off + u_width, v_off)
            glVertex2i      (x2,y1)
           
      glEnd()
     
      glPopMatrix()
     
End Sub

''=============================================================================
''
''      Draws a center-rotated 2d sprite
''
''=============================================================================
Sub sprite_rotate( ByVal x As Integer, ByVal y As Integer, ByVal angle As Integer, ByVal spr As image Ptr)
     
      Dim As Integer s_half_x = spr->Width\2
      Dim As Integer s_half_y = spr->height\2
     
      Dim As Integer x1 = -s_half_x
      Dim As Integer y1 = -s_half_y
     
      Dim As Integer x2 = s_half_x
      Dim As Integer y2 = s_half_y
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
     
     
     
      glPushMatrix()
     
      glTranslatef(x, y, 0)
      glRotatef(angle,0,0,1)
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u_off, v_off)
            glVertex2i      (x1,y1)
           
            glTexcoord2f(u_off, v_off + v_height)
            glVertex2i      (x1,y2)
           
            glTexcoord2f(u_off + u_width, v_off + v_height)
            glVertex2i      (x2,y2)
           
            glTexcoord2f(u_off + u_width, v_off)
            glVertex2i      (x2,y1)
           
      glEnd()
     
      glPopmatrix()
     
End Sub

''=============================================================================
''
''      Draws a center-rotated and scaled 2d sprite
''
''=============================================================================
Sub sprite_rotate_scale( ByVal x As Integer, ByVal y As Integer, ByVal angle As Integer, ByVal scale As Single, ByVal spr As image Ptr)
     
      Dim As Integer s_half_x = spr->Width\2
      Dim As Integer s_half_y = spr->height\2
     
      Dim As Integer x1 = -s_half_x
      Dim As Integer y1 = -s_half_y
     
      Dim As Integer x2 = s_half_x
      Dim As Integer y2 = s_half_y
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
     
     
     
      glPushMatrix()
     
      glTranslatef(x, y, 0)
      glScalef(scale,scale,1.0)
      glRotatef(angle,0,0,1)
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u_off, v_off)
            glVertex2i      (x1,y1)
           
            glTexcoord2f(u_off, v_off + v_height)
            glVertex2i      (x1,y2)
           
            glTexcoord2f(u_off + u_width, v_off + v_height)
            glVertex2i      (x2,y2)
           
            glTexcoord2f(u_off + u_width, v_off)
            glVertex2i      (x2,y1)
           
      glEnd()
     
      glPopmatrix()
     
End Sub

''=============================================================================
''
''      Draws a stretched 2d sprite
''
''=============================================================================
Sub sprite_stretch( ByVal x1 As Integer, ByVal y1 As Integer,_
                              ByVal x2 As Integer, ByVal y2 As Integer,_
                              ByVal spr As image Ptr)
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u_off, v_off)
            glVertex2i      (x1,y1)
           
            glTexcoord2f(u_off, v_off + v_height)
            glVertex2i      (x1,y2)
           
            glTexcoord2f(u_off + u_width, v_off + v_height)
            glVertex2i      (x2,y2)
           
            glTexcoord2f(u_off + u_width, v_off)
            glVertex2i      (x2,y1)
           
      glEnd()
     
End Sub


''=============================================================================
''
''      Draws a quad-stretched 2d sprite
''
''=============================================================================
Sub sprite_stretch_on_quad( ByVal x1 As Integer, ByVal y1 As Integer,_
                                          ByVal x2 As Integer, ByVal y2 As Integer,_
                                          ByVal x3 As Integer, ByVal y3 As Integer,_
                                          ByVal x4 As Integer, ByVal y4 As Integer,_
                                          ByVal spr As image Ptr)
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf
     
      glBegin(GL_QUADS)
           
            glTexcoord2f(u_off, v_off)
            glVertex2i      (x1,y1)
           
            glTexcoord2f(u_off, v_off + v_height)
            glVertex2i      (x2,y2)
           
            glTexcoord2f(u_off + u_width, v_off + v_height)
            glVertex2i      (x3,y3)
           
            glTexcoord2f(u_off + u_width, v_off)
            glVertex2i      (x4,y4)
           
      glEnd()
     
End Sub

Sub sprite_stretch_h( ByVal x As Integer, ByVal y As Integer, ByVal length As Integer, ByVal spr As image Ptr )
     
     
      Dim As Integer hw = (spr->Width\2)

      Dim As Integer x1 = x
      Dim As Integer y1 = y
      Dim As Integer x2 = x + length
      Dim As Integer y2 = y + spr->height
     
      Dim As Single u_off = spr->u_offset/spr->texture_width
      Dim As Single v_off = spr->v_offset/spr->texture_height
     
      Dim As Single u_width = spr->Width/spr->texture_width
      Dim As Single v_height = spr->height/spr->texture_height

      Dim As Single su = u_width/2
           
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( spr->textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, spr->textureID)
            current_texture = spr->textureID
      EndIf

      '' left
      Dim As Integer x2l = x1 + hw
      glBegin(GL_QUADS)
     
            glTexcoord2f(u_off, v_off)
            glVertex2i(x1,y1)
           
            glTexcoord2f(u_off, v_off + v_height)
            glVertex2i(x1,y2)
           
            glTexcoord2f(u_off + su, v_off + v_height)
            glVertex2i(x2l,y2)
           
            glTexcoord2f(u_off + su, v_off)
            glVertex2i(x2l,y1)
           
      glEnd()
     
      '' center
      Dim As Integer x1l = x + hw
      x2l = x2 - hw -1
      glBegin(GL_QUADS)
     
            glTexcoord2f(u_off + su, v_off)
            glVertex2i(x1l,y1)
                       
            glTexcoord2f(u_off + su, v_off + v_height)
            glVertex2i(x1l,y2)
           
            glTexcoord2f(u_off + su, v_off + v_height)
            glVertex2i(x2l,y2)
           
            glTexcoord2f(u_off + su, v_off)
            glVertex2i(x2l,y1)
           
      glEnd()
     
      '' right
      x1l = x2 - hw -1
      glBegin(GL_QUADS)
     
            glTexcoord2f(u_off + su, v_off)
            glVertex2i(x1l,y1)
           
            glTexcoord2f(u_off + su, v_off + v_height)
            glVertex2i(x1l,y2)
           
            glTexcoord2f(u_off + u_width, v_off + v_height)
            glVertex2i(x2,y2)
           
            glTexcoord2f(u_off + u_width, v_off)
            glVertex2i(x2,y1)
           
      glEnd()
     
End Sub


''=============================================================================
''
''      special function to get a particle texture
''
''=============================================================================
Private Function get_glow_image() As GLuint
     
      Static As GLuint textureID = 0
     
      If textureID = 0 Then
           
            Const IMAGE_WIDTH = 32, IMAGE_HEIGHT = 32, IMAGE_BITDEPTH = 24
            Dim As UInteger image_array(0 To 1031) => { _
            &H00000007, &H00000004, &H00000020, &H00000020, &H00000080, &H00000000, _
            &H00000000, &H00000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF010101, &HFF010101, &HFF020202, _
            &HFF020202, &HFF030303, &HFF030303, &HFF030303, &HFF020202, &HFF020202, _
            &HFF010101, &HFF010101, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF010101, &HFF020202, &HFF030303, _
            &HFF040404, &HFF050505, &HFF060606, &HFF060606, &HFF060606, &HFF060606, _
            &HFF060606, &HFF050505, &HFF040404, &HFF030303, &HFF020202, &HFF010101, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF010101, &HFF020202, &HFF040404, _
            &HFF050505, &HFF070707, &HFF080808, &HFF090909, &HFF0A0A0A, &HFF0B0B0B, _
            &HFF0B0B0B, &HFF0B0B0B, &HFF0A0A0A, &HFF090909, &HFF080808, &HFF070707, _
            &HFF050505, &HFF040404, &HFF020202, &HFF010101, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF010101, &HFF030303, _
            &HFF050505, &HFF070707, &HFF090909, &HFF0B0B0B, &HFF0C0C0C, &HFF0E0E0E, _
            &HFF0F0F0F, &HFF0F0F0F, &HFF101010, &HFF0F0F0F, &HFF0F0F0F, &HFF0E0E0E, _
            &HFF0C0C0C, &HFF0B0B0B, &HFF090909, &HFF070707, &HFF050505, &HFF030303, _
            &HFF010101, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF010101, _
            &HFF030303, &HFF060606, &HFF080808, &HFF0B0B0B, &HFF0D0D0D, &HFF0F0F0F, _
            &HFF111111, &HFF131313, &HFF141414, &HFF151515, &HFF151515, &HFF151515, _
            &HFF141414, &HFF131313, &HFF111111, &HFF0F0F0F, &HFF0D0D0D, &HFF0B0B0B, _
            &HFF080808, &HFF060606, &HFF030303, &HFF010101, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF010101, &HFF030303, &HFF060606, &HFF090909, &HFF0C0C0C, &HFF0E0E0E, _
            &HFF121212, &HFF141414, &HFF161616, &HFF181818, &HFF191919, &HFF1A1A1A, _
            &HFF1B1B1B, &HFF1A1A1A, &HFF191919, &HFF181818, &HFF161616, &HFF141414, _
            &HFF121212, &HFF0E0E0E, &HFF0C0C0C, &HFF090909, &HFF060606, &HFF030303, _
            &HFF010101, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF010101, &HFF030303, &HFF060606, &HFF090909, &HFF0C0C0C, _
            &HFF0F0F0F, &HFF131313, &HFF161616, &HFF191919, &HFF1B1B1B, &HFF1E1E1E, _
            &HFF1F1F1F, &HFF202020, &HFF212121, &HFF202020, &HFF1F1F1F, &HFF1E1E1E, _
            &HFF1B1B1B, &HFF191919, &HFF161616, &HFF131313, &HFF0F0F0F, &HFF0C0C0C, _
            &HFF090909, &HFF060606, &HFF030303, &HFF010101, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF020202, &HFF050505, &HFF080808, _
            &HFF0C0C0C, &HFF0F0F0F, &HFF131313, &HFF171717, &HFF1B1B1B, &HFF1E1E1E, _
            &HFF212121, &HFF232323, &HFF252525, &HFF262626, &HFF272727, &HFF262626, _
            &HFF252525, &HFF232323, &HFF212121, &HFF1E1E1E, &HFF1B1B1B, &HFF171717, _
            &HFF131313, &HFF0F0F0F, &HFF0C0C0C, &HFF080808, &HFF050505, &HFF020202, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF010101, &HFF040404, _
            &HFF070707, &HFF0B0B0B, &HFF0E0E0E, &HFF131313, &HFF171717, &HFF1B1B1B, _
            &HFF1F1F1F, &HFF232323, &HFF262626, &HFF292929, &HFF2B2B2B, &HFF2C2C2C, _
            &HFF2C2C2C, &HFF2C2C2C, &HFF2B2B2B, &HFF292929, &HFF262626, &HFF232323, _
            &HFF1F1F1F, &HFF1B1B1B, &HFF171717, &HFF131313, &HFF0E0E0E, &HFF0B0B0B, _
            &HFF070707, &HFF040404, &HFF010101, &HFF000000, &HFF000000, &HFF000000, _
            &HFF020202, &HFF050505, &HFF090909, &HFF0D0D0D, &HFF121212, &HFF161616, _
            &HFF1B1B1B, &HFF1F1F1F, &HFF242424, &HFF282828, &HFF2B2B2B, &HFF2E2E2E, _
            &HFF313131, &HFF323232, &HFF323232, &HFF323232, &HFF313131, &HFF2E2E2E, _
            &HFF2B2B2B, &HFF282828, &HFF242424, &HFF1F1F1F, &HFF1B1B1B, &HFF161616, _
            &HFF121212, &HFF0D0D0D, &HFF090909, &HFF050505, &HFF020202, &HFF000000, _
            &HFF000000, &HFF010101, &HFF030303, &HFF070707, &HFF0B0B0B, &HFF0F0F0F, _
            &HFF141414, &HFF191919, &HFF1E1E1E, &HFF232323, &HFF282828, &HFF2C2C2C, _
            &HFF303030, &HFF333333, &HFF363636, &HFF383838, &HFF383838, &HFF383838, _
            &HFF363636, &HFF333333, &HFF303030, &HFF2C2C2C, &HFF282828, &HFF232323, _
            &HFF1E1E1E, &HFF191919, &HFF141414, &HFF0F0F0F, &HFF0B0B0B, &HFF070707, _
            &HFF030303, &HFF010101, &HFF000000, &HFF010101, &HFF040404, &HFF080808, _
            &HFF0C0C0C, &HFF111111, &HFF161616, &HFF1B1B1B, &HFF212121, &HFF262626, _
            &HFF2B2B2B, &HFF303030, &HFF343434, &HFF383838, &HFF3B3B3B, &HFF3D3D3D, _
            &HFF3D3D3D, &HFF3D3D3D, &HFF3B3B3B, &HFF383838, &HFF343434, &HFF303030, _
            &HFF2B2B2B, &HFF262626, &HFF212121, &HFF1B1B1B, &HFF161616, &HFF111111, _
            &HFF0C0C0C, &HFF080808, &HFF040404, &HFF010101, &HFF000000, &HFF020202, _
            &HFF050505, &HFF090909, &HFF0E0E0E, &HFF131313, &HFF181818, &HFF1E1E1E, _
            &HFF232323, &HFF292929, &HFF2E2E2E, &HFF333333, &HFF383838, &HFF3C3C3C, _
            &HFF3F3F3F, &HFF424242, &HFF424242, &HFF424242, &HFF3F3F3F, &HFF3C3C3C, _
            &HFF383838, &HFF333333, &HFF2E2E2E, &HFF292929, &HFF232323, &HFF1E1E1E, _
            &HFF181818, &HFF131313, &HFF0E0E0E, &HFF090909, &HFF050505, &HFF020202, _
            &HFF000000, &HFF020202, &HFF060606, &HFF0A0A0A, &HFF0F0F0F, &HFF141414, _
            &HFF191919, &HFF1F1F1F, &HFF252525, &HFF2B2B2B, &HFF313131, &HFF363636, _
            &HFF3B3B3B, &HFF3F3F3F, &HFF434343, &HFF454545, &HFF474747, &HFF454545, _
            &HFF434343, &HFF3F3F3F, &HFF3B3B3B, &HFF363636, &HFF313131, &HFF2B2B2B, _
            &HFF252525, &HFF1F1F1F, &HFF191919, &HFF141414, &HFF0F0F0F, &HFF0A0A0A, _
            &HFF060606, &HFF020202, &HFF000000, &HFF030303, &HFF060606, &HFF0B0B0B, _
            &HFF0F0F0F, &HFF151515, &HFF1A1A1A, &HFF202020, &HFF262626, &HFF2C2C2C, _
            &HFF323232, &HFF383838, &HFF3D3D3D, &HFF424242, &HFF454545, &HFFAEAEAE, _
            &HFFD3D3D3, &HFF454545, &HFF454545, &HFF424242, &HFF3D3D3D, &HFF383838, _
            &HFF323232, &HFF2C2C2C, &HFF262626, &HFF202020, &HFF1A1A1A, &HFF151515, _
            &HFF0F0F0F, &HFF0B0B0B, &HFF060606, &HFF030303, &HFF000000, &HFF030303, _
            &HFF060606, &HFF0B0B0B, &HFF101010, &HFF151515, &HFF1B1B1B, &HFF212121, _
            &HFF272727, &HFF2C2C2C, &HFF323232, &HFF383838, &HFF3D3D3D, &HFF424242, _
            &HFF474747, &HFFD3D3D3, &HFFFFFFFF, &HFF454545, &HFF474747, &HFF424242, _
            &HFF3D3D3D, &HFF383838, &HFF323232, &HFF2C2C2C, &HFF272727, &HFF212121, _
            &HFF1B1B1B, &HFF151515, &HFF101010, &HFF0B0B0B, &HFF060606, &HFF030303, _
            &HFF000000, &HFF030303, &HFF060606, &HFF0B0B0B, &HFF0F0F0F, &HFF151515, _
            &HFF1A1A1A, &HFF202020, &HFF262626, &HFF2C2C2C, &HFF323232, &HFF383838, _
            &HFF3D3D3D, &HFF424242, &HFF454545, &HFF454545, &HFF454545, &HFF454545, _
            &HFF454545, &HFF424242, &HFF3D3D3D, &HFF383838, &HFF323232, &HFF2C2C2C, _
            &HFF262626, &HFF202020, &HFF1A1A1A, &HFF151515, &HFF0F0F0F, &HFF0B0B0B, _
            &HFF060606, &HFF030303, &HFF000000, &HFF020202, &HFF060606, &HFF0A0A0A, _
            &HFF0F0F0F, &HFF141414, &HFF191919, &HFF1F1F1F, &HFF252525, &HFF2B2B2B, _
            &HFF313131, &HFF363636, &HFF3B3B3B, &HFF3F3F3F, &HFF434343, &HFF454545, _
            &HFF474747, &HFF454545, &HFF434343, &HFF3F3F3F, &HFF3B3B3B, &HFF363636, _
            &HFF313131, &HFF2B2B2B, &HFF252525, &HFF1F1F1F, &HFF191919, &HFF141414, _
            &HFF0F0F0F, &HFF0A0A0A, &HFF060606, &HFF020202, &HFF000000, &HFF020202, _
            &HFF050505, &HFF090909, &HFF0E0E0E, &HFF131313, &HFF181818, &HFF1E1E1E, _
            &HFF232323, &HFF292929, &HFF2E2E2E, &HFF333333, &HFF383838, &HFF3C3C3C, _
            &HFF3F3F3F, &HFF424242, &HFF424242, &HFF424242, &HFF3F3F3F, &HFF3C3C3C, _
            &HFF383838, &HFF333333, &HFF2E2E2E, &HFF292929, &HFF232323, &HFF1E1E1E, _
            &HFF181818, &HFF131313, &HFF0E0E0E, &HFF090909, &HFF050505, &HFF020202, _
            &HFF000000, &HFF010101, &HFF040404, &HFF080808, &HFF0C0C0C, &HFF111111, _
            &HFF161616, &HFF1B1B1B, &HFF212121, &HFF262626, &HFF2B2B2B, &HFF303030, _
            &HFF343434, &HFF383838, &HFF3B3B3B, &HFF3D3D3D, &HFF3D3D3D, &HFF3D3D3D, _
            &HFF3B3B3B, &HFF383838, &HFF343434, &HFF303030, &HFF2B2B2B, &HFF262626, _
            &HFF212121, &HFF1B1B1B, &HFF161616, &HFF111111, &HFF0C0C0C, &HFF080808, _
            &HFF040404, &HFF010101, &HFF000000, &HFF010101, &HFF030303, &HFF070707, _
            &HFF0B0B0B, &HFF0F0F0F, &HFF141414, &HFF191919, &HFF1E1E1E, &HFF232323, _
            &HFF282828, &HFF2C2C2C, &HFF303030, &HFF333333, &HFF363636, &HFF383838, _
            &HFF383838, &HFF383838, &HFF363636, &HFF333333, &HFF303030, &HFF2C2C2C, _
            &HFF282828, &HFF232323, &HFF1E1E1E, &HFF191919, &HFF141414, &HFF0F0F0F, _
            &HFF0B0B0B, &HFF070707, &HFF030303, &HFF010101, &HFF000000, &HFF000000, _
            &HFF020202, &HFF050505, &HFF090909, &HFF0D0D0D, &HFF121212, &HFF161616, _
            &HFF1B1B1B, &HFF1F1F1F, &HFF242424, &HFF282828, &HFF2B2B2B, &HFF2E2E2E, _
            &HFF313131, &HFF323232, &HFF323232, &HFF323232, &HFF313131, &HFF2E2E2E, _
            &HFF2B2B2B, &HFF282828, &HFF242424, &HFF1F1F1F, &HFF1B1B1B, &HFF161616, _
            &HFF121212, &HFF0D0D0D, &HFF090909, &HFF050505, &HFF020202, &HFF000000, _
            &HFF000000, &HFF000000, &HFF010101, &HFF040404, &HFF070707, &HFF0B0B0B, _
            &HFF0E0E0E, &HFF131313, &HFF171717, &HFF1B1B1B, &HFF1F1F1F, &HFF232323, _
            &HFF262626, &HFF292929, &HFF2B2B2B, &HFF2C2C2C, &HFF2C2C2C, &HFF2C2C2C, _
            &HFF2B2B2B, &HFF292929, &HFF262626, &HFF232323, &HFF1F1F1F, &HFF1B1B1B, _
            &HFF171717, &HFF131313, &HFF0E0E0E, &HFF0B0B0B, &HFF070707, &HFF040404, _
            &HFF010101, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF020202, _
            &HFF050505, &HFF080808, &HFF0C0C0C, &HFF0F0F0F, &HFF131313, &HFF171717, _
            &HFF1B1B1B, &HFF1E1E1E, &HFF212121, &HFF232323, &HFF252525, &HFF262626, _
            &HFF272727, &HFF262626, &HFF252525, &HFF232323, &HFF212121, &HFF1E1E1E, _
            &HFF1B1B1B, &HFF171717, &HFF131313, &HFF0F0F0F, &HFF0C0C0C, &HFF080808, _
            &HFF050505, &HFF020202, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF010101, &HFF030303, &HFF060606, &HFF090909, &HFF0C0C0C, _
            &HFF0F0F0F, &HFF131313, &HFF161616, &HFF191919, &HFF1B1B1B, &HFF1E1E1E, _
            &HFF1F1F1F, &HFF202020, &HFF212121, &HFF202020, &HFF1F1F1F, &HFF1E1E1E, _
            &HFF1B1B1B, &HFF191919, &HFF161616, &HFF131313, &HFF0F0F0F, &HFF0C0C0C, _
            &HFF090909, &HFF060606, &HFF030303, &HFF010101, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF010101, &HFF030303, _
            &HFF060606, &HFF090909, &HFF0C0C0C, &HFF0E0E0E, &HFF121212, &HFF141414, _
            &HFF161616, &HFF181818, &HFF191919, &HFF1A1A1A, &HFF1B1B1B, &HFF1A1A1A, _
            &HFF191919, &HFF181818, &HFF161616, &HFF141414, &HFF121212, &HFF0E0E0E, _
            &HFF0C0C0C, &HFF090909, &HFF060606, &HFF030303, &HFF010101, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF010101, &HFF030303, &HFF060606, &HFF080808, &HFF0B0B0B, _
            &HFF0D0D0D, &HFF0F0F0F, &HFF111111, &HFF131313, &HFF141414, &HFF151515, _
            &HFF151515, &HFF151515, &HFF141414, &HFF131313, &HFF111111, &HFF0F0F0F, _
            &HFF0D0D0D, &HFF0B0B0B, &HFF080808, &HFF060606, &HFF030303, &HFF010101, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF010101, &HFF030303, _
            &HFF050505, &HFF070707, &HFF090909, &HFF0B0B0B, &HFF0C0C0C, &HFF0E0E0E, _
            &HFF0F0F0F, &HFF0F0F0F, &HFF101010, &HFF0F0F0F, &HFF0F0F0F, &HFF0E0E0E, _
            &HFF0C0C0C, &HFF0B0B0B, &HFF090909, &HFF070707, &HFF050505, &HFF030303, _
            &HFF010101, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF010101, &HFF020202, &HFF040404, &HFF050505, &HFF070707, _
            &HFF080808, &HFF090909, &HFF0A0A0A, &HFF0B0B0B, &HFF0B0B0B, &HFF0B0B0B, _
            &HFF0A0A0A, &HFF090909, &HFF080808, &HFF070707, &HFF050505, &HFF040404, _
            &HFF020202, &HFF010101, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF010101, _
            &HFF020202, &HFF030303, &HFF040404, &HFF050505, &HFF060606, &HFF060606, _
            &HFF060606, &HFF060606, &HFF060606, &HFF050505, &HFF040404, &HFF030303, _
            &HFF020202, &HFF010101, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF010101, &HFF010101, &HFF020202, _
            &HFF020202, &HFF030303, &HFF030303, &HFF030303, &HFF020202, &HFF020202, _
            &HFF010101, &HFF010101, &HFF000000, &HFF000000, &HFF000000, &HFF000000, _
            &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000, &HFF000000 }

     
            textureID = load_image( @image_array(0),GL_LINEAR )
     
      EndIf
     
      Return textureID

End Function

''=============================================================================
''
''      Draws a 2d glowing line
''      x1,y1,x2,y2 as start and end coordinate of the line
''      lwidth is the width of the line (how fat it is)
''      mycolor should use DW_RGBA()
''
''=============================================================================

Sub line_glow ( ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single,_
                        ByVal lwidth As Single, ByVal mycolor As GLuint)
     
     
      Dim As GLuint textureID = get_glow_image()
     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, textureID)
            current_texture = textureID
      EndIf

     
      Dim As Single nx,ny     
      nx = -(y2-y1)
      ny = (x2-x1)
     
      Dim leng As Single
      leng = Sqr(nx * nx + ny * ny )
      nx = nx / leng
      ny = ny / leng

      nx *= lwidth/2
      ny *= lwidth/2
           
      Dim As Single lx1, ly1, lx2, ly2, lx3, ly3, lx4, ly4
     
      lx1 = x2+nx
      ly1 = y2+ny
      lx2 = x2-nx
      ly2 = y2-ny                                         
      lx3 = x1-nx
      ly3 = y1-ny
      lx4 = x1+nx
      ly4 = y1+ny

      mycolor = RGBA(RGBA_B(mycolor),RGBA_G(mycolor),RGBA_R(mycolor),RGBA_A(mycolor))
      glColor4ubv (Cast(GLubyte Ptr, @mycolor))

      ''MAIN
      glbegin(GL_QUADS)
            glTexCoord2f( 0.5,0 )
            glVertex3f( lx1,ly1,0 )

            glTexCoord2f( 0.5,1 )
            glVertex3f( lx2,ly2,0 )

            glTexCoord2f( 0.5,1 )
            glVertex3f( lx3, ly3,0 )

            glTexCoord2f( 0.5,0 )
            glVertex3f( lx4,ly4,0 )
      glend()

      'RIGHT
      Dim As Single lx5, ly5,lx6,ly6,vx,vy
      vx = (x2-x1)
      vy = (y2-y1)
      leng = Sqr(vx * vx + vy * vy )
      vx = vx / leng
      vy = vy / leng
      vx *= lwidth/2
      vy *= lwidth/2
     
      lx5 = lx1 + vx
      ly5 = ly1 + vy
      lx6 = lx2 + vx
      ly6 = ly2 + vy
     
      glbegin(GL_QUADS)
            glTexCoord2f( 0.5,0 )
            glVertex3f( lx1,ly1,0 )

            glTexCoord2f( 1,0 )
            glVertex3f( lx5,ly5,0 )

            glTexCoord2f( 1,1 )
            glVertex3f( lx6, ly6,0 )

            glTexCoord2f( 0.5,1 )
            glVertex3f( lx2,ly2,0 )
      glend()

      'LEFT
      lx5 = lx4 -vx
      ly5 = ly4 -vy
      lx6 = lx3 -vx
      ly6 = ly3 -vy
      glbegin(GL_QUADS)
            glTexCoord2f( 0.5,0 )
            glVertex3f( lx4,ly4,0 )
           
            glTexCoord2f( 0.5,1 )
            glVertex3f( lx3,ly3,0 )

            glTexCoord2f( 1,1 )
            glVertex3f( lx6, ly6,0 )

            glTexCoord2f( 1,0 )
            glVertex3f( lx5,ly5,0 )
           
      glend()
     
End Sub


''=============================================================================
''
''      Rudimentary font system
''
''=============================================================================
Sub Print(ByVal x As Integer, ByVal y As Integer, ByRef text As Const String, ByVal scale As Single = 1.0)

     
      '' Only change active texture when there is a need
      '' Speeds up the rendering by batching textures
      If ( font_textureID <> current_texture ) Then
            glBindTexture(GL_TEXTURE_2D, font_textureID)
            current_texture = font_textureID
      EndIf
           
      glPushMatrix()
      glLoadIdentity()
      glTranslatef( x, y, 0 )
      glScalef(scale, scale, 1)
      glListBase( font_list_base )
            For i As Integer = 0 To Len(text) - 1
                  glCallList( font_list_base + text[i] )
            Next i
      glPopMatrix()

End Sub

Sub gfxmap_load(f As String, spr() As GL.gfxmap_spr, set() As GL.gfxmap_set, SoftwareOnly As Integer = FALSE, RDim As Integer = TRUE, scale2x As Integer = FALSE)
     
      db_write("gfxMap_Load: "& f)
     
      Dim As Integer ff = FreeFile, nsprs, nsets
     
      If Open(f For Binary Access Read As #ff) Then
            db_write("      File read error# "& Err &", "& ExePath &"\"& f)
            db_write("      CurDir: "& CurDir)
            Beep
      EndIf
     
      Get #ff,,nsets
      Get #ff,,nsprs
     
      If RDim Then
            ReDim set(1 To nsets) As GL.gfxmap_set
            ReDim spr(1 To nsprs) As GL.gfxmap_spr
      EndIf
     
      Dim As Integer cb,x,y,w,h
      Dim As Integer reserved
      Dim As GLuint textureID
     
      db_write("      Found "& nsets &" sets and "& nsprs &" sprs.")
      If nsprs = 0 Then End
     
      For s As Integer = 1 To nsets
     
            Get #ff,,set(s).f
            Get #ff,,set(s).w
            Get #ff,,set(s).h
            Get #ff,,set(s).s2x
            Get #ff,,set(s).count
            For r As Integer = 1 To 10
                  Get #ff,,reserved
            Next
           
            Dim As String filename = dropFile(f) & Trim(set(s).f)
            db_write("      Set "& s &" = "& filename &", has "& set(s).count &" sprites, W="& set(s).w &", H="& set(s).h &", s2x="& strIf(set(s).s2x Or scale2x,"TRUE.","false."))
           
            If (set(s).s2x=TRUE Orelse scale2x=TRUE) Then
                  set(s).w *= 2
                  set(s).h *= 2
            EndIf
           
            Dim As GL.IMAGE Ptr setImage
           
            '' PNGs images must already be power of 2! (Cannot use the RoundUp function.) IE: ...64,128,256,512,1024,2048...
            If LCase(Right(set(s).f,4)) = ".png" Then
                 
                  If (set(s).s2x=TRUE OrElse scale2x=TRUE) Then
                        Dim As FB.IMAGE Ptr setImageOrig = png_load(filename, PNG_TARGET_FBNEW)
                        If setImageOrig = 0 Then Beep: db_write("      Error loading "& filename): Exit Sub
                        setImage = ImageCreate(set(s).w,set(s).h)
                        If setImage = 0 Then Beep: db_write("      Error using ImageCreate"): Exit Sub
                        ImageResizeX2_Smooth(setImage,setImageOrig)
                        ImageDestroy(setImageOrig)
                  Else
                        setImage = png_load(filename, PNG_TARGET_FBNEW)
                        If setImage = 0 Then Beep: db_write("      Error loading "& filename): Exit Sub
                  EndIf
                 
                  If SoftwareOnly = FALSE Then
                        textureID = GL.load_image(setImage)
                        db_write("      Loaded to VRAM")
                  Else : db_write("      Loaded to RAM only.")
                  EndIf
                                         
            Else
                  db_write("      Only PNG is supported by this engine.")
                  Beep : Exit Sub
            EndIf
                 
            For b As Integer = 1 To set(s).count
                  cb+=1
                  Get #ff,,x
                  Get #ff,,y
                  Get #ff,,w
                  Get #ff,,h
                  Get #ff,,spr(cb).id
                  Get #ff,,spr(cb).r
                  Get #ff,,spr(cb).ax
                  Get #ff,,spr(cb).ay
                  Get #ff,,spr(cb).delay
                  Get #ff,,spr(cb).transp

                  For r As Integer = 1 To 5
                        Get #ff,,reserved
                  Next
                 
                  spr(cb).set = @set(s)
                 
                  If (set(s).s2x=TRUE OrElse scale2x=TRUE) Then
                        x*=2 : y*=2 : w*=2 : h*=2
                  EndIf
                 
                  '' Right now we do create space in RAM for each set-image. This is mainly for creating a necessary header
                  '' and if the data is also loaded (Get) then we can do pixel-checking on the images (Point, etc.):
                  spr(cb).p = ImageCreate(w,h)
                  Get setImage,(x,y)-(x+(w-1),y+(h-1)),spr(cb).p
                 
                  '' Tell GL how to map this image from the texture:
                  spr(cb).p->textureID = textureID                  '' << This will be zero if SoftwareOnly = TRUE
                  spr(cb).p->u_offset = x
                  spr(cb).p->v_offset = y
                  spr(cb).p->texture_width = set(s).w
                  spr(cb).p->texture_height = set(s).h
                 
                  Flip
            Next
           
            Flip
           
            ImageDestroy setImage
           
            db_write("---- Done ----")

      Next
           
End Sub

Function spr_with_id(spr() As GL.gfxmap_spr, id As ZString Ptr, asInstr As Integer = FALSE) As Integer
      For n As Integer = 1 To UBound(spr)
            If asInstr = TRUE Then
                  If InStr(UCase(Trim(spr(n).id)),UCase(Trim(*id))) Then
                        Return n
                  EndIf
            Else
                  If UCase(Trim(spr(n).id)) = UCase(Trim(*id)) Then
                        Return n
                  EndIf
            EndIf
      Next
      Function = 0
End Function

Function spr_with_id(spr_ptr As GL.gfxmap_spr Ptr, id As ZString Ptr, asInstr As Integer = FALSE) As Integer
      'db_write("searching for: " & id)
      For n As Integer = 1 To spr_ptr[1].set->count
            If asInstr = TRUE Then
                  If InStr(UCase(Trim(spr_ptr[n].id)), UCase(Trim(*id))) Then
                        Return n
                  EndIf
            Else
                  'db_write("testing against: " & spr_ptr[n].id)
                  If UCase(Trim(spr_ptr[n].id)) = UCase(Trim(*id)) Then
                        Return n
                  EndIf
            EndIf
      Next
      Return 0
End Function


''=============================================================================
''
''      Loads a 32 bit buffer (BLOADed from a BMP image)
''
''=============================================================================
Function load_image( ByVal spr As Any Ptr , ByVal filter_mode As GLuint = GL_NEAREST) As GLuint
     
            Dim As GLuint TextureID
                  Dim As UByte r, g, b, a
                  Dim As FB.IMAGE Ptr temp = Cast(FB.IMAGE Ptr, spr)
                            
            glEnable( GL_TEXTURE_2D )
            glGenTextures(1, @TextureID)
            glBindTexture(GL_TEXTURE_2D, TextureID)
           
                  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_mode )
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_mode )
            glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE )
                      
            glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, temp->Width, temp->height,_
                                   0, GL_BGRA, GL_UNSIGNED_BYTE, spr + SizeOf(FB.IMAGE) )
            glBindTexture(GL_TEXTURE_2D, 0)
           
           
            Return TextureID
           
End Function

''=============================================================================
''
''      Loads a 32 bit buffer (BLOADed from a 24-bit BMP image)
''      Adds alpha transparency
''
''=============================================================================
Function load_image_24bit_alpha( ByVal spr As Any Ptr, ByVal filter_mode As GLuint = GL_NEAREST ) As GLuint
     
            Dim As GLuint TextureID
                  Dim As UByte r, g, b, a
                  Dim As FB.IMAGE Ptr temp = spr
                 
                  For y As Integer = 0 To temp->height-1
                  Dim As UInteger Ptr p = Cast(UInteger Ptr,(spr + SizeOf (FB.IMAGE)) + y * temp->pitch)          
                  For x As Integer = 0 To temp->Width-1
                        a = argb_a(p[x])
                        r = argb_r(p[x])
                        g = argb_g(p[x])
                        b = argb_b(p[x])
                        '' check for transparency
                        If ( g = 0 ) Then
                              If ( (r = 255) And (b = 255) ) Then
                                    p[x] = RGBA(r,g,b,0)
                              EndIf
                        EndIf
                  Next x
                  Next y
                 
                 
                  glEnable( GL_TEXTURE_2D )
            glGenTextures(1, @TextureID)
            glBindTexture(GL_TEXTURE_2D, TextureID)
           
                  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_mode )
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_mode )
            glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE )
                      
            glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, temp->Width, temp->height,_
                                   0, GL_BGRA, GL_UNSIGNED_BYTE, spr + SizeOf(FB.IMAGE) )
            glBindTexture(GL_TEXTURE_2D, 0)
           
           
            Return TextureID
           
End Function


''=============================================================================
''
''      Loads a 32 bit buffer (BLOADed from an 8-bit BMP image)
''      Adds alpha transparency
''
''=============================================================================
Function load_image_8bit_alpha( ByVal spr As Any Ptr, ByVal filter_mode As GLuint = GL_NEAREST ) As GLuint
     
            Dim As GLuint TextureID
                  Dim As UByte r, g, b, a
                  Dim As FB.IMAGE Ptr temp = spr
     
                       
                  For y As Integer = 0 To temp->height-1
                  Dim As UInteger Ptr p = Cast(UInteger Ptr,(spr + SizeOf(FB.IMAGE)) + y * temp->pitch)          
                  For x As Integer = 0 To temp->Width-1
                        a = argb_a(p[x])
                        r = argb_r(p[x])
                        g = argb_g(p[x])
                        b = argb_b(p[x])
                        '' check for transparency
                        If ( ( p[x] And &H00FFFFFF ) = 0 ) Then
                              p[x] = RGBA(r,g,b,0)
                        EndIf
                       
                  Next x
                  Next y
                 
                 
                  glEnable( GL_TEXTURE_2D )
            glGenTextures(1, @TextureID)
            glBindTexture(GL_TEXTURE_2D, TextureID)
           
                  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
            glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_mode )
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_mode )
            glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE )
                      
            glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, temp->Width, temp->height,_
                                   0, GL_BGRA, GL_UNSIGNED_BYTE, spr + SizeOf(FB.IMAGE) )
            glBindTexture(GL_TEXTURE_2D, 0)
           
           
            Return TextureID
           
End Function

''=============================================================================
''
''      Loads any type of BMP (Power of 2) into an OpenGL texture
''
''=============================================================================
Function load_BMP_to_texture(ByRef filename As String) As GLuint
     
            If filename = "" Then Return 0
           
            Dim As GLuint textureID
           
            Dim As Integer F = FreeFile
            Dim As Integer w, h            '' width and height
            Dim As UShort bpp            '' bits per pixel
           
            '' test header
            Open filename For Binary As #f
            Get #f, 19, w
            Get #f, 23, h
            Get #f, 29, bpp
            Close #f
          
            Dim As FB.IMAGE Ptr spr = ImageCreate(w,h)
            BLoad filename, spr
          
          
            Select Case bpp
                  Case 8
                        textureID = load_image_8bit_alpha(spr)
                  Case 24
                        textureID = load_image_24bit_alpha(spr)
                  Case Else
                        textureID = load_image(spr)
            End Select
           
            ImageDestroy spr
           
            Return textureID
           
End Function



''=============================================================================
''
''      Loads any type of BMP (Power of 2) into an OpenGL texture
''      results are stored in a glsprite datatype
''
''=============================================================================
Function load_BMP_to_glsprite(ByRef filename As String) As IMAGE Ptr
     
            If filename = "" Then Exit Function
           
            Dim As GLuint textureID
           
            Dim As Integer F = FreeFile
            Dim As Integer w, h            '' width and height
            Dim As UShort bpp            '' bits per pixel
           
            '' test header
            Open filename For Binary As #f
            Get #f, 19, w
            Get #f, 23, h
            Get #f, 29, bpp
            Close #f
          
            Dim As IMAGE Ptr spr = ImageCreate(w,h)
            BLoad filename, spr
          
          
            Select Case bpp
                  Case 8
                        textureID = load_image_8bit_alpha(spr)
                  Case 24
                        textureID = load_image_24bit_alpha(spr)
                  Case Else
                        textureID = load_image(spr)
            End Select
           
           
            spr->textureID = textureID
            spr->u_offset = 0
            spr->v_offset = 0
            spr->texture_width = spr->Width
            spr->texture_height = spr->height
           
           
            Return spr
           
End Function


Sub load_image_to_HW(ByVal spr As image Ptr)
           
            Dim As GLuint textureID
           
            Select Case spr->bpp
                  Case 1
                        textureID = load_image_8bit_alpha(spr)
                  Case 4
                        textureID = load_image_24bit_alpha(spr)
                  Case Else
                        textureID = load_image(spr)
            End Select           
           
            spr->textureID = textureID
            spr->u_offset = 0
            spr->v_offset = 0
            spr->texture_width = spr->Width
            spr->texture_height = spr->height
           
End Sub

''=============================================================================
''
''      call before ending program
''
''=============================================================================
Sub destroy()
     
      glDeleteLists( font_list_base, 256 )
      glDeleteTextures( 1, @font_textureID )
     
End Sub


''=============================================================================
''
''      use this to limit the FPS if vsync_ON does not work
''
''=============================================================================
Function fps_limit(ByVal max_FPS As Double) As Double
     
      Static As Integer frames_per_second = 0.0
      Static As Double last_time = 0.0
      Static As Double time_start = 0.0
     
      Static As Double GL_fps = 60
           
      frames_per_second += 1

      '' timeBeginPeriod(1) requests a resolution of 1ms for the periodic timers,
      '' instead of the default resolution of 10 ms in your case of Windows 7 64bit (15 ms for Windows XP).
      '' timeEndPeriod(1)' function cancels this modification. http://www.freebasic.net/forum/viewtopic.php?t=23338
     
      Do: timeBeginPeriod(1) : Sleep(1,1): timeEndPeriod(1) : Loop Until (Timer - time_start) >= (1/max_FPS)

      time_start = Timer
     
      If (time_start - last_time) > 1.0 Then
            last_time = time_start
            GL_fps = frames_per_second
            frames_per_second = 0
            WindowTitle "FPS: " + Str(GL_fps)
      End If
     
      Return GL_fps

End Function

End Namespace




<- back