' 
' Written by Paul Grunewald (aka Stormy)
' Date: 07.28.2006
' Mail: Paul.Grunewald@gmail.com
' Description: Tool for testing purposes with ogl's functions
' 

' Einbindung von OpenGL
#include "inc/2d_opengl.bi"

' Festlegung der Konstanten, die für den Bildschirm wichtig sind
const scrnX = 800
const scrnY = 600
const depth = 32
const fullscreen = &h0           ' Vollbildmodus ( &h0 = aus, &h1 = an )

Init_ogl2D(scrnX, scrnY, depth, fullscreen)

glEnable GL_BLEND

glEnable GL_TEXTURE_2D           ' activate textures

DIM MyFont AS FontSystem
MyFont.Spacing = 2
FontLoad(MyFont, "resources/blending/font/standard.tga","resources/blending/font/standard.dat")

DIM SHARED AS GLenum BlendModes(0 TO 10)
DIM SHARED AS STRING BlendModesStr(0 TO 10)

DIM SHARED AS GLenum DepthModes(0 TO 7)
DIM SHARED AS STRING DepthModesStr(0 TO 7)

BlendModes(0) = GL_ZERO 
BlendModes(1) = GL_ONE
BlendModes(2) = GL_DST_COLOR
BlendModes(3) = GL_SRC_COLOR
BlendModes(4) = GL_ONE_MINUS_DST_COLOR
BlendModes(5) = GL_ONE_MINUS_SRC_COLOR
BlendModes(6) = GL_SRC_ALPHA
BlendModes(7) = GL_ONE_MINUS_SRC_ALPHA
BlendModes(8) = GL_DST_ALPHA
BlendModes(9) = GL_ONE_MINUS_DST_ALPHA
BlendModes(10) = GL_SRC_ALPHA_SATURATE

BlendModesStr(0) = "GL_ZERO"
BlendModesStr(1) = "GL_ONE"
BlendModesStr(2) = "GL_DST_COLOR"
BlendModesStr(3) = "GL_SRC_COLOR"
BlendModesStr(4) = "GL_ONE_MINUS_DST_COLOR"
BlendModesStr(5) = "GL_ONE_MINUS_SRC_COLOR"
BlendModesStr(6) = "GL_SRC_ALPHA"
BlendModesStr(7) = "GL_ONE_MINUS_SRC_ALPHA"
BlendModesStr(8) = "GL_DST_ALPHA"
BlendModesStr(9) = "GL_ONE_MINUS_DST_ALPHA"
BlendModesStr(10) = "GL_SRC_ALPHA_SATURATE"

DepthModes(0) = GL_NEVER
DepthModes(1) = GL_LESS
DepthModes(2) = GL_EQUAL
DepthModes(3) = GL_LEQUAL
DepthModes(4) = GL_GREATER
DepthModes(5) = GL_NOTEQUAL
DepthModes(6) = GL_GEQUAL
DepthModes(7) = GL_ALWAYS

DepthModesStr(0) = "GL_NEVER"
DepthModesStr(1) = "GL_LESS"
DepthModesStr(2) = "GL_EQUAL"
DepthModesStr(3) = "GL_LEQUAL"
DepthModesStr(4) = "GL_GREATER"
DepthModesStr(5) = "GL_NOTEQUAL"
DepthModesStr(6) = "GL_GEQUAL"
DepthModesStr(7) = "GL_ALWAYS"

TYPE Tile
 Show AS INTEGER
 X AS INTEGER
 Y AS INTEGER
 Z AS INTEGER
 tex AS GFXType
 BlendMode1 AS INTEGER
 BlendMode2 AS INTEGER
 DepthMode AS INTEGER
 AS INTEGER R, G, B, A
 fR AS INTEGER
 fG AS INTEGER
 fB AS INTEGER
END TYPE

DIM Tiles(3) AS Tile
DIM background AS GFXType
DIM AS INTEGER R,G,B, Current, f, x, y, e
DIM AS INTEGER CurItem(3), EnterItem(20), EnterMode = 0, EnterRow = 0
DIM AS STRING a, title, version
DIM AS INTEGER switch_blend, switch_depth

Title = "Stormy's OGL-Demo"
Version = "Version: 0.1"
WindowTitle Title + " (" + Version + ")"
load_texture ("resources/blending/background.tga", background)
CurItem(1) = 1: CurItem(2) = 1: CurItem(3) = 1
Current = 1
' Setting up Tiles

With Tiles(1)
 .Show = 1
 .X = 125
 .Y = 10
 .Z = 10
 load_texture ("resources/blending/texture1.tga", .tex)
 .BlendMode1 = 6
 .BlendMode2 = 7
 .DepthMode = 1
 .R = 255: .G = 255: .B = 255: .A = 255
 .fR = 255: .fG = 0: .fB = 0
End With

With Tiles(2)
 .Show = 1
 .X = 125+128
 .Y = 10+64
 .Z = 20
 load_texture ("resources/blending/texture2.tga", .tex)
 .BlendMode1 = 6
 .BlendMode2 = 7
 .DepthMode = 1
 .R = 255: .G = 255: .B = 255: .A = 255
 .fR = 0: .fG = 255: .fB = 0
End With

With Tiles(3)
 .Show = 1
 .X = 125+128+128
 .Y = 10+64+64
 .Z = 30
 load_texture ("resources/blending/texture3.tga", .tex)
 .BlendMode1 = 6
 .BlendMode2 = 7
 .DepthMode = 1
 .R = 255: .G = 255: .B = 255: .A = 255
 .fR = 0: .fG = 0: .fB = 255
End With



do
 glCls  
 glColor4f 1,1,1,1
 glBlendfunc GL_ONE, GL_ONE
  
 'Sub FontPrintEx (ByRef Font AS FontSystem, ByVal text AS STRING, ByVAL x AS INTEGER, BYVAL y AS INTEGER, ByVal R AS INTEGER, ByVal G AS INTEGER, ByVal B AS INTEGER, ByVal A AS INTEGER = 1) ' 
  ' !! Hier kommen die Zeichenbefehle rein !!
'glBlendfunc GL_SRC_ALPHA, GL_SRC_ALPHA
glColor4f 1,1,1,1
glBlendfunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
 FOR x = 1 TO 4
  FOR y = 1 TO 2
   PutTexture (background, (background.width*(x-1)), (background.height*(y-1)),0)   
  NEXT y 
 NEXT e 

glBindTexture GL_TEXTURE_2D, 0
glColor4f 0,0,0,1
PutBox (0, 400, 800, 600, 1)

glBindTexture GL_TEXTURE_2D, 0
glColor4f 0,0,0,.5
PutBox (800-FontGetWidth(MyFont,STR$(version))-10, 0, 800, 16+5, 12)

GLPrint (MyFont, version, 800-FontGetWidth(MyFont,STR$(version))-5, 0, 125, 255,255,255)
  
a = INKEY$
FOR e = 1 TO 3
With Tiles(e)
 IF .Show = 1 THEN
 
  IF switch_blend = 1 THEN glDisable GL_BLEND ELSE glEnable GL_BLEND
  glBlendfunc BlendModes(.BlendMode1), BlendModes(.BlendMode2)
  
  IF switch_depth = 1 THEN glDisable GL_DEPTH_TEST ELSE glEnable GL_DEPTH_TEST  
  glDepthFunc DepthModes(.DepthMode)
  
  'glBlendfunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
  glColor4ub .R, .G, .B, .A
  PutTexture(.tex, .X, .Y, .Z)
    
  glDepthFunc GL_LESS
  GLPrint (MyFont, STR$(e), .X+.tex.width-FontGetWidth(MyFont,STR$(e))-5-1, .Y-1, 124, 128,128,128)
  GLPrint (MyFont, STR$(e), .X+.tex.width-FontGetWidth(MyFont,STR$(e))-5, .Y, 125, .fR,.fG,.fB)
  
 END IF
IF Current = e THEN R = 255: G = 255: B = 255 ELSE R = 128: G = 128: B = 128
GLPrint (MyFont, "Texture #" + STR$(e), 100+266*(e-1), 405, 3, R, G,B)
f=0

GLPrint (MyFont, "Position:", 10+266*(e-1), 405+16, 3, 128,128,128)
f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "X="+STR$(.X), 75+10+266*(e-1), 405+16, 3, R, G,B)
f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "Y="+STR$(.Y), 135+10+266*(e-1), 405+16, 3, R, G,B)
f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "Z="+STR$(.Z), 195+10+266*(e-1), 405+16, 3, R, G,B)
GLPrint (MyFont, "Blendfunc:", 10+266*(e-1), 405+32, 3, 128,128,128)
f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "1st: " + STR$(BlendModesStr(.BlendMode1)), 10+266*(e-1), 405+48, 3, R, G,B)
f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "2nd: " + STR$(BlendModesStr(.BlendMode2)), 10+266*(e-1), 405+64, 3, R, G,B)

GLPrint (MyFont, "Colors:", 10+266*(e-1), 405+80, 3, 128,128,128)

f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "R: " + STR$(.R), 10+266*(e-1), 405+96, 3, R, G,B)

f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "G: " + STR$(.G), 10+266*(e-1)+60, 405+96, 3, R, G,B)

f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "B: " + STR$(.B), 10+266*(e-1)+120, 405+96, 3, R, G,B)

f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, "A: " + STR$(.A), 10+266*(e-1)+180, 405+96, 3, R, G,B)

GLPrint (MyFont, "Depthfunc:", 10+266*(e-1), 405+112, 3, 128,128,128)
f+=1: IF CurItem(e) = f THEN R = 255: G = 224: B = 26: IF EnterItem(f) = 1 AND EnterMode = e THEN R = 255: G = 0: B = 0 ELSE ELSE R = 200: G = 200: B = 200
GLPrint (MyFont, DepthModesStr(.DepthMode), 95+266*(e-1), 405+112, 3, R, G,B)

End With 
NEXT e

GLPrint (MyFont, "Keys: 1,2,3 - Switch texture, B - Blending on/off, D - Depthtest on/off, Enter = Change value", 10, 550, 3, 167,226,255)

IF switch_blend = 0 THEN
 GLPrint (MyFont, "on", 10 + FontGetWidth(MyFont, "Keys: 1,2,3 - Switch texture, B - Blending "), 550, 4, 255,224,26)
ELSE
 GLPrint (MyFont, "off", 10 + FontGetWidth(MyFont, "Keys: 1,2,3 - Switch texture, B - Blending on/"), 550, 4, 255,224,26)
END IF

IF switch_depth = 0 THEN
 GLPrint (MyFont, "on", 10 + FontGetWidth(MyFont, "Keys: 1,2,3 - Switch texture, B - Blending on/off, D - Depthtest "), 550, 4, 255,224,26)
ELSE
 GLPrint (MyFont, "off", 10 + FontGetWidth(MyFont, "Keys: 1,2,3 - Switch texture, B - Blending on/off, D - Depthtest on/"), 550, 4, 255,224,26)
END IF


IF EnterMode = 0 THEN
 IF a = CHR$(255) + "H" OR a = CHR$(255) + "K" THEN IF CurItem(Current) > 1 THEN CurItem(Current) -= 1
 IF a = CHR$(255) + "P" OR a = CHR$(255) + "M" THEN IF CurItem(Current) < f THEN CurItem(Current) += 1

 IF a = "1" THEN Current = 1
 IF a = "2" THEN Current = 2
 IF a = "3" THEN Current = 3
 IF UCASE$(a) = "B" THEN switch_blend xor= 1
 IF UCASE$(a) = "D" THEN switch_depth xor= 1
 IF a = CHR$(13) THEN EnterItem(CurItem(Current)) = 1: EnterMode = Current
 
ELSE

 IF a = CHR$(13) THEN EnterItem(CurItem(Current)) = 0: EnterMode = 0

SELECT CASE CurItem(Current)
CASE 1
 IF a = CHR$(255) + "P" OR a = CHR$(255) + "K" THEN Tiles(Current).X -= 1
 IF a = CHR$(255) + "H" OR a = CHR$(255) + "M" THEN Tiles(Current).X += 1
CASE 2
 IF a = CHR$(255) + "P" OR a = CHR$(255) + "K"  THEN Tiles(Current).Y -= 1
 IF a = CHR$(255) + "H" OR a = CHR$(255) + "M"  THEN Tiles(Current).Y += 1
CASE 3
 IF a = CHR$(255) + "P" OR a = CHR$(255) + "K"  THEN Tiles(Current).Z -= 1
 IF a = CHR$(255) + "H" OR a = CHR$(255) + "M"  THEN Tiles(Current).Z += 1
CASE 4
 IF a = CHR$(255) + "P" OR a = CHR$(255) + "K" THEN IF Tiles(Current).BlendMode1 > LBOUND(BlendModes) THEN Tiles(Current).BlendMode1 -= 1
 IF a = CHR$(255) + "H" OR a = CHR$(255) + "M" THEN IF Tiles(Current).BlendMode1 < UBOUND(BlendModes) THEN  Tiles(Current).BlendMode1 += 1
CASE 5
 IF a = CHR$(255) + "P" OR a = CHR$(255) + "K" THEN IF Tiles(Current).BlendMode2 > LBOUND(BlendModes) THEN Tiles(Current).BlendMode2 -= 1
 IF a = CHR$(255) + "H" OR a = CHR$(255) + "M" THEN IF Tiles(Current).BlendMode2 < UBOUND(BlendModes) THEN  Tiles(Current).BlendMode2 += 1
CASE 6
 IF a = CHR$(255) + "P" AND Tiles(Current).R > 0 THEN Tiles(Current).R -= 1
 IF a = CHR$(255) + "H" AND Tiles(Current).R < 255 THEN Tiles(Current).R += 1
CASE 7
 IF a = CHR$(255) + "P" AND Tiles(Current).G > 0 THEN Tiles(Current).G -= 1
 IF a = CHR$(255) + "H" AND Tiles(Current).G < 255 THEN Tiles(Current).G += 1
CASE 8
 IF a = CHR$(255) + "P" AND Tiles(Current).B > 0 THEN Tiles(Current).B -= 1
 IF a = CHR$(255) + "H" AND Tiles(Current).B < 255 THEN Tiles(Current).B += 1
CASE 9
 IF a = CHR$(255) + "P" AND Tiles(Current).A > 0 THEN Tiles(Current).A -= 1
 IF a = CHR$(255) + "H" AND Tiles(Current).A < 255 THEN Tiles(Current).A += 1

CASE 10
 IF a = CHR$(255) + "P" OR a = CHR$(255) + "K" THEN IF Tiles(Current).DepthMode > LBOUND(DepthModes) THEN Tiles(Current).DepthMode -= 1
 IF a = CHR$(255) + "H" OR a = CHR$(255) + "M" THEN IF Tiles(Current).DepthMode < UBOUND(DepthModes) THEN  Tiles(Current).DepthMode += 1
 
 
END SELECT

END IF

  ' 
  glFlush ' Verarbeitung der Befehle
  flip 
  screensync
loop until multikey(&h01) ' Verlasse die Schleife sobald Escape gedrückt wird

DestroyFont(MyFont)