


Declare Sub Init_GL_Window( Byref Display As GlfwVidMode )
Declare Sub Load_Textures( Texture() As GlUint )
Declare Sub Make_Font_DList( Byref DList As GlUINT, Byval Texture As GLUINT, Byval Wtiles As Single, Byval Htiles As Single, Byval t_Scale As Integer ) 
Declare Sub GlPrint(Byval Strng As String, Byval Texture As Gluint, Byval Text_List As GLUINT, Byval X As Single, Byval Y As Single, Byval R As Integer, Byval G As Integer, Byval B As Integer, Byref Display As GlfwVidMode)


Sub Init_GL_Window( Byref Display As GlfwVidMode )
    Display.Width       = 640
    Display.Height      = 480
    Display.RedBITS     = 8
    Display.GreenBITS   = 8
    Display.BlueBITS    = 8
    
    If glfwInit() Then
        'Successful!
    Else    
        Print "Failed to initialize GLFW!"
        Beep
        Sleep 3000
        End
    End If
    
    
    If  glfwOpenWindow( Display.Width, Display.Height, Display.RedBITS, Display.GreenBITS, Display.BlueBITS, 8, 32, 0, GLFW_FULLSCREEN  ) Then
        GlfwSwapInterval 1
    Else
        GlfwTerminate()
        Print "Failed to create OpenGL context... ending"
        Beep
        Sleep 3000
        End
    End If
    'To check, use... glfwGetWindowParam()
    
    
    ' OpenGL specific stuff...
    glViewport 0, 0, Display.Width, Display.Height
    Glmatrixmode GL_PROJECTION        
    Glloadidentity    
    Dim As dFloat FOVy = 45.0
    Dim As dFloat Aspect = Display.Width / Display.Height
    Dim As dFloat znear = .1
    Dim As dFloat zfar = 250
    gluPerspective FOVy, Aspect, zNear, zFar
    Glmatrixmode GL_MODELVIEW
    Glloadidentity    
    
    
    glShadeModel GL_SMOOTH
    glClearColor 0.0, 0.0, 0.0, 0.0
    glClearDepth 1.0
    glEnable GL_DEPTH_TEST
    glDepthFunc GL_LEQUAL   
    glEnable GL_COLOR_MATERIAL
    
    glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
    glPolygonMode GL_FRONT, GL_FILL
    GlFrontFace GL_CCW
    glEnable GL_CULL_FACE
    
End Sub

Sub Load_Textures( Texture() As GlUint )
    
    Dim FileNam As String, i As Integer
    For i= 1 To max_textures
        FileNam=""
        If i=1  Then Filenam = "Textures/Text.tga"
        If i=2  Then Filenam = "Textures/Wall.tga"
        If i=3  Then Filenam = "Textures/eightball.tga"
        If i=4  Then Filenam = "Textures/Wood2.tga"
        If i=5  Then Filenam = "Textures/Wood1.tga"
        If i=6  Then Filenam = "Textures/eightball.tga"
        If i=7  Then Filenam = "Textures/Wood1.tga"
        If i=8  Then Filenam = "Textures/backboard.tga"
        If i=9  Then Filenam = "Textures/Targ.tga"
        If i=10  Then Filenam = "Textures/Power.tga"
        
        If FileNam <> "" Then
            GlGenTextures 1, @Texture(i)
            GlEnable GL_TEXTURE_2D
            glBindTexture GL_Texture_2D, Texture(i)
            
            If GlfwLoadTexture2D( FileNam, GLFW_BUILD_MIPMAPS_BIT)  = 0 Then
                'GlfwTerminate()
                Print "Failed to load image, " + Filenam + "!!!"
                Beep
            End If
            
            glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT
            glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT
            glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR
            glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR
            If glfwExtensionSupported( ("GL_EXT_texture_filter_anisotropic")) Then
                Dim As GlFloat Largest_Texture_Filter
                glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @Largest_Texture_Filter)
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, Largest_Texture_Filter)
            End If
            glTexEnvf GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE
            
        End If     
    Next
    
End Sub


Sub GlPrint(Byval Strng As String, Byval Texture As Gluint, Byval Text_List As GLUINT, Byval X As Single, Byval Y As Single, Byval R As Integer, Byval G As Integer, Byval B As Integer, Byref Display As GlfwVidMode)
    Dim As Integer i, Length = Len(Strng)
    GlEnable GL_TEXTURE_2D
    GlBindTexture Gl_Texture_2D, Texture
    GlEnable GL_BLEND 
    GlBlendFunc Gl_Src_Alpha, Gl_One_Minus_Src_Alpha 
    GlPolygonMode Gl_Front, GL_Fill 
    Glmatrixmode Gl_Projection 
    Glpushmatrix 
    Glloadidentity 
    Glmatrixmode Gl_Modelview 
    Glpushmatrix 
    Glloadidentity
    GlOrtho 0,Display.Width, 0, Display.Height, -1, 1
    GLDisable GL_LIGHTING 
    GlDisable GL_DEPTH_TEST
    
    GlTranslateF X, Y, 0
    GlColor3F R,G,B
    GlListBase Text_List
    GlCallLists Length, GL_UNSIGNED_BYTE, @Strng
    
    Glmatrixmode Gl_Projection 
    Glpopmatrix
    Glmatrixmode Gl_Modelview 
    Glpopmatrix 
    GLEnable GL_LIGHTING 
    GlEnable GL_DEPTH_TEST
End Sub 

Sub Make_Font_DList( Byref DList As GlUINT, Byval Texture As GLUINT, Byval Wtiles As Single, Byval Htiles As Single, Byval t_Scale As Integer ) 
    
    Dim As Integer i 
    Dim As Single Tx, Ty, XStepp = 1/Wtiles, YStepp = 1/Htiles 
    Glpushmatrix()
    i=0
    For TY = Htiles-1 To 0 Step -1 
        For TX = 0 To Wtiles-1
            GlNewList DList+i, GL_COMPILE
            Glbegin GL_QUADS 
            
            GlTexCoord2D Tx*XStepp, (Ty+1)*YStepp 
            GlVertex2i 0, t_Scale 
            
            GlTexCoord2D Tx*XStepp, Ty*YStepp 
            GlVertex2i 0, 0 
            
            GlTexCoord2D (Tx+1)*XStepp, Ty*YStepp 
            GlVertex2i t_Scale, 0 
            
            GlTexCoord2D (Tx+1)*XStepp, (Ty+1)*YStepp 
            GlVertex2i t_Scale, t_Scale 
            
            Glend
            GlTranslateF t_Scale, 0, 0
            GlEndList 
            i+=1
        Next 
    Next 
    Glpopmatrix()
End Sub