'
' 2D OpenGL funtions by Stormy
'
' texture structure

#include once "GL/gl.bi"
#include once "GL/glu.bi"

TYPE GFXType
     Handle as gluint
     Width as integer
     Height as integer
     TextureFilter as integer
     imageData as ubyte ptr
     bpp as uinteger
     textype as uinteger
END TYPE

TYPE Point2D
	X AS INTEGER
	Y AS INTEGER
END TYPE

TYPE Point3D
	X AS INTEGER
	Y AS INTEGER
	Z AS INTEGER
END TYPE

#define TEX_MASKED      &h1
#define TEX_MIPMAP      &h2
#DEFINE TEX_NOFILTER    &h4
#DEFINE TEX_HASALPHA    &h8
#define TEX_LIGHTMAP    &h16

const FALSE = 0
const TRUE  = not FALSE

declare SUB WriteToConsole (ByVal Message AS string)

' Including loaders for specific file types

#include "ogl_load_bmp.bi"
#include "ogl_load_tga.bi"
#include "zlib.bi"
#include "ogl_load_png.bi"

' Generic loader for tga, bmp and png

function load_texture(filename AS STRING, byref texture AS GFXType, flags AS INTEGER = 0, killdata AS INTEGER = 0) AS INTEGER
DIM AS GLenum format, minfilter, magfilter
dim extention as string * 3
extention = right$(filename, 3)
IF extention = "tga" THEN
	if (not LoadTGA(@texture, filename)) then   '' Load The Texture
	        WriteToConsole "Error: Loading texture... " + filename
		return 1
	end if
ELSEIF extention = "bmp" THEN
	if (not LoadBMP(@texture, filename)) then   '' Load The Texture
		WriteToConsole "Error: Loading texture... " + filename
		return 1
	end if
ELSEIF extention = "png" THEN
	if (not LoadPNG(@texture, filename)) then   '' Load The Texture
		WriteToConsole "Error: Loading texture... " + filename
		return 1
	end if
END IF

glGenTextures (1, @texture.Handle)     '' Create The Texture ( CHANGE )
	glBindTexture (GL_TEXTURE_2D, texture.Handle)

		IF( flags AND TEX_NOFILTER ) THEN
			magfilter = GL_NEAREST
		ELSE
			magfilter = GL_LINEAR
		END IF

		IF( flags AND TEX_MIPMAP ) THEN
			gluBuild2DMipmaps GL_TEXTURE_2D, texture.bpp \ 8, texture.width, texture.height, texture.textype, GL_UNSIGNED_BYTE, texture.imageData

			IF( flags AND TEX_NOFILTER ) THEN
				minfilter = GL_NEAREST_MIPMAP_NEAREST
			ELSE
				minfilter = GL_LINEAR_MIPMAP_LINEAR
			END IF
		ELSE
			glTexImage2D (GL_TEXTURE_2D, 0, texture.bpp \ 8, texture.width, texture.height, 0, texture.textype, GL_UNSIGNED_BYTE, texture.imageData)
			minfilter = magfilter
		END IF
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter

	''  If Texture Image Exists ( CHANGE )
	'if texture.imageData and killdata = 1 then
	if killdata = 0 then
	deallocate (texture.imageData)    '' Free The Texture Image Memory ( CHANGE )
	end if

	return 0
end function


' Including font system
#include "fontsystem.bi"
' Including scancodes
#include "keys.bi"

sub PutTexture(byval texture as gfxtype, byval x as integer, byval y as integer, byval z as integer = 0)

    glBindTexture GL_TEXTURE_2D, texture.handle
    glTranslatef x,y,z
	glBegin GL_QUADS
		glTexCoord2f 0.0, 0.0 ' links unten
		glVertex3f   0, texture.width,  0.0
		glTexCoord2f 1.0, 0.0 ' rechts unten
		glVertex3f   texture.height,  texture.width,  0.0
		glTexCoord2f 1.0, 1.0 ' rechts oben
		glVertex3f   texture.height, 0,  0.0
		glTexCoord2f 0.0, 1.0 ' links oben
		glVertex3f   0, 0,  0.0
	glEnd
     glLoadIdentity
end sub

sub PutTextureVert(byval texture as gfxtype, byval x as integer, byval y as integer, byval z as integer = 0)

    glBindTexture GL_TEXTURE_2D, texture.handle
    glTranslatef x,y,z
	glBegin GL_QUADS
		glTexCoord2f 0.0, 1.0 ' links oben
		glVertex3f   0, texture.width,  0.0
		glTexCoord2f 1.0, 1.0 ' rechts oben
		glVertex3f   texture.height,  texture.width,  0.0
		glTexCoord2f 1.0, 0.0 ' rechts unten
		glVertex3f   texture.height, 0,  0.0
		glTexCoord2f 0.0, 0.0 ' links unten
		glVertex3f   0, 0,  0.0
	glEnd
     glLoadIdentity
end sub

sub PutClip (byval texture as gfxtype, x as integer, y as integer, z as integer = 0, offset_x1 as integer, offset_y1 as integer, offset_x2 as integer, offset_y2 as integer)
dim as integer quad_width, quad_height

   glEnable GL_TEXTURE_2D
   quad_width = abs(offset_x1 - offset_x2)
   quad_height = abs(offset_y1 - offset_y2)

    glBindTexture GL_TEXTURE_2D, texture.handle
    glTranslatef x,y,z
	glBegin GL_QUADS
		glTexCoord2f offset_x1/texture.width, 1-offset_y2/texture.height ' links unten
		 glVertex2f   0, quad_height
		glTexCoord2f  offset_x2/texture.width, 1-offset_y2/texture.height ' rechts unten
		 glVertex2f   quad_width,  quad_height
		glTexCoord2f offset_x2/texture.width, 1-offset_y1/texture.height ' rechts oben
		 glVertex2f   quad_width, 0
		glTexCoord2f offset_x1/texture.width, 1-offset_y1/texture.height ' links oben
		 glVertex2f   0, 0
	glEnd
     glLoadIdentity

end sub

sub PutClipArea (byval texture as gfxtype, x1 as integer, y1 as integer, x2 as integer, y2 as integer, z as integer = 0, offset_x1 as integer, offset_y1 as integer, offset_x2 as integer, offset_y2 as integer)
dim as integer quad_width, quad_height

   glEnable GL_TEXTURE_2D
   quad_width = abs(x1 - x2)
   quad_height = abs(y1 -y2)

    glBindTexture GL_TEXTURE_2D, texture.handle
    glTranslatef x1,y1,z

	glBegin GL_QUADS
		glTexCoord2f offset_x1/texture.width, 1-offset_y2/texture.height ' links unten
		 glVertex2f   0, quad_height
		glTexCoord2f  offset_x2/texture.width, 1-offset_y2/texture.height ' rechts unten
		 glVertex2f   quad_width,  quad_height
		glTexCoord2f offset_x2/texture.width, 1-offset_y1/texture.height ' rechts oben
		 glVertex2f   quad_width, 0
		glTexCoord2f offset_x1/texture.width, 1-offset_y1/texture.height ' links oben
		 glVertex2f   0, 0
	glEnd
     glLoadIdentity

end sub

' upgraded version
sub PutTile (Byval texture as GFXType, byval index as integer, w as integer, h as integer, x as integer, y as integer, z as integer = 0)

	dim as double cx, cy, rw, rh, horiz, vert
		index = index - 1
		horiz = fix(texture.width/w)
		vert = fix(texture.height/h)
		cx = (index mod horiz)/horiz
		cy = (index\vert)/vert
                rw = 1/horiz
		rh = 1/vert

    glBindTexture GL_TEXTURE_2D, texture.handle
    glTranslatef x,y,z

	glBegin GL_QUADS
		glTexCoord2f cx,1-cy-rh
		glVertex2i 0, h  		''  LINKS UNTEN
		glTexCoord2f cx+rw,1-cy-rh
		glVertex2i w, h			'' RECHTS UNTEN
		glTexCoord2f cx+rw,1-cy
		glVertex2i w, 0			'' RECHTS OBEN
		glTexCoord2f cx,1-cy
		glVertex2i 0, 0			'' LINKS OBEN
	glEnd

     glLoadIdentity

END SUB


sub PutTileVert (Byval texture as GFXType, byval index as integer, w as integer, h as integer, x as integer, y as integer, z as integer = 0)

	dim as double cx, cy, rw, rh, horiz, vert
		index = index - 1
		horiz = fix(texture.width/w)
		vert = fix(texture.height/h)
		cx = (index mod horiz)/horiz
		cy = (index\vert)/vert
                rw = 1/horiz
		rh = 1/vert

    glBindTexture GL_TEXTURE_2D, texture.handle
    glTranslatef x,y,z

	glBegin GL_QUADS
		glTexCoord2f cx,1-cy
		glVertex2i 0, h  		''  LINKS UNTEN
		glTexCoord2f cx+rw,1-cy
		glVertex2i w, h			'' RECHTS UNTEN
		glTexCoord2f cx+rw,1-cy-rh
		glVertex2i w, 0			'' RECHTS OBEN
		glTexCoord2f cx,1-cy-rh
		glVertex2i 0, 0			'' LINKS OBEN
	glEnd

     glLoadIdentity

END SUB

sub glCLS
glClear  GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
end sub

sub Init_ogl2D (w as integer, h as integer, bpp as integer, fullscreen as integer)

#if 1
screenres w,h,bpp,,&h2 OR fullscreen
' Konfiguration von OpenGL
glMatrixMode(GL_PROJECTION)      ' defining matrix
glLoadIdentity
glViewport(0,0,w,h)		 ' defining axes
glOrtho(0,w,h,0,-128,128)
glMatrixMode(GL_MODELVIEW)       ' deactivating background drawing
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glEnable GL_TEXTURE_2D           ' activate textures
glLoadIdentity
glEnable(GL_DEPTH_TEST)          ' depth test
glDepthFunc(GL_LESS)
glEnable(GL_ALPHA_TEST)          ' alpha test
glAlphaFunc(GL_GREATER, 0.0)
#endif
end sub

SUB WriteToConsole(ByVal Message AS string)
dim chCon as integer

 chCon = FreeFile
 open CONS for output as #chCon
 print #chCon, Message
 close #chCon

END SUB

sub PutBox (ByVal x1 as integer, ByVal y1 as integer, ByVal x2 as integer, ByVal y2 as integer, ByVal z as integer = 0)
dim as integer w,h
    w = abs(x2-x1)
    h = abs(y2-y1)
       glTranslatef x1,y1,z
	glBegin GL_QUADS
		glVertex2i   0, h
		glVertex2i   w, h
		glVertex2i   w, 0
		glVertex2i   0, 0
	glEnd
     glLoadIdentity
END SUB

sub DrawQuadMid (ByVal x AS INTEGER, ByVal y AS INTEGER, ByVal z AS INTEGER, ByVal w AS INTEGER, ByVal h AS INTEGER)

       glTranslatef x,y,z
	glBegin GL_QUADS
		glVertex2i   -w/2, h/2
		glVertex2i   w/2, h/2
		glVertex2i   w/2, -h/2
		glVertex2i   -w/2, -h/2
	glEnd
     glLoadIdentity
end sub

sub DrawTexQuad (ByVal x AS INTEGER, ByVal y AS INTEGER, ByVal z AS INTEGER, ByVal w AS INTEGER, ByVal h AS INTEGER)
       glTranslatef x,y,z
	glBegin GL_QUADS
		glTexCoord2f 0.0, 0.0
		glVertex2i   0, h
		glTexCoord2f 1.0, 0.0
		glVertex2i   w, h
		glTexCoord2f 1.0, 1.0
		glVertex2i   w, 0
		glTexCoord2f 0.0, 1.0
		glVertex2i   0, 0
	glEnd
     glLoadIdentity
end sub