'
' *   EZ GL2D Fonts Example
' *   Source is provided "as is" without express or implied warranty of any kind.
' * 
'

Imports System
Imports System.Collections.Generic
Imports System.Threading
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Imports EZGL2D

Namespace EZGL2DexampleFont
	Public Class GL2Dexample ' inherit the GameWindow class
		Inherits GameWindow

		Private Const SCREEN_WIDTH As Integer = 800 ' screen dimension
		Private Const SCREEN_HEIGHT As Integer = 600

		Private cubeDisplayList As Integer ' out display list

		Private frame As Integer

		Private fontProto As New FontGL()
		Private fontBig As New FontGL()
		Private fontDigi As New FontGL()

		Private texture As GL2D.SpriteGL ' texture for our cube

		' Init screen
		Public Sub New()
			MyBase.New(SCREEN_WIDTH, SCREEN_HEIGHT)

		End Sub

		' this is where you would initialize everything
		' like loading textures, setting up OpenGL screen, etc.
		Protected Overrides Sub OnLoad(ByVal e As EventArgs)
			MyBase.OnLoad(e)

			VSync = VSyncMode.On ' force vertical synching
			Title = "EZGL2D is too easy to use it hurts!" ' set the title
			'WindowBorder = WindowBorder.Hidden;             // hide the window

			initCubeDisplayList() ' initialize our display list

			' Load our fonts
			fontProto.loadFont("data/EZGL2DexampleFont/protofont.png", 32, 32, CInt(TextureMinFilter.Nearest))
			fontBig.loadFont("data/EZGL2DexampleFont/bigfont.png", 64, 64, CInt(TextureMinFilter.Linear))
			fontDigi.loadFont("data/EZGL2DexampleFont/digifont.png", 16, 16, CInt(TextureMinFilter.Nearest))

			' Loar our texture
			texture = GL2D.LoadSprite("data/EZGL2DexampleFont/cubetexture.bmp", CInt(TextureMinFilter.Linear))

		End Sub

		Protected Overrides Sub OnResize(ByVal e As EventArgs)
			MyBase.OnResize(e)
			GL2D.Screen2D(Width, Height) ' reset the screen every resize
		End Sub

		' this is where all your game logic
		' should be done. (Input, Entity movement, collisions, etc)
		Protected Overrides Sub OnUpdateFrame(ByVal e As FrameEventArgs)
			MyBase.OnUpdateFrame(e)

			frame += 1

			' get input
			If Keyboard(OpenTK.Input.Key.Escape) Then
				GL.DeleteLists(cubeDisplayList, 1)
				Me.Exit()
				Return
			End If
		End Sub

		' This is where all rendering/drawing should be done
		Protected Overrides Sub OnRenderFrame(ByVal e As FrameEventArgs)
			MyBase.OnRenderFrame(e)


			GL2D.ClearScreen() ' clear the screen

			' Draw the single moving textured cube
			GL.Color4(Color.White)
			GL.Enable(EnableCap.DepthTest) ' unless you're doing translucent effects enable this
			GL.BindTexture(TextureTarget.Texture2D, texture.textureID) ' yes, we can use out sprite as a texture :)
			GL.PushMatrix()
				GL.Translate(Math.Sin(frame * 0.02), Math.Cos(frame * 0.07), Math.Cos(frame* 0.002) + Math.Sin(frame * 0.01))
				GL.Rotate(frame * 2, 1, 0, 0)
				GL.Rotate(frame * 2, 0, 1, 0)
				GL.Rotate(frame * 2, 0, 0, 1)
				DrawCube()
			GL.PopMatrix()

			GL2D.Begin2D() ' start 2D drawing mode
				fontBig.printCenter(10, 100, "EZ GL2D", Width)
				fontProto.printCenterSine(10, 230, "http://rel.phatcode.net", Width, 20, 2, frame*3) ' Wavy font
				GL.Color4(Color.Blue)
				fontProto.printCenterSine(10, 270, "http://rel.phatcode.net", Width, 20, 2, frame * 3) ' wavy font in blue
				GL.Color4(Color.Red)
				fontBig.printCenterSine(10, 400, "--2D + 3D--", Width, 30, 1, frame * -13) ' red big fonts

				' moving a text dynamically
				Dim dx As Double = Math.Sin(frame * 0.005) + Math.Cos(frame * 0.005) + Math.Sin(frame * 0.032)
				Dim dy As Double = Math.Cos(frame * 0.005) + Math.Sin(frame * 0.005) + Math.Cos(frame * 0.032)
				Dim [me] As String = " RELMINATOR "
				GL.Color4(Color.Azure)
				fontDigi.print(Width \ 2 + CInt(Math.Truncate(dx * 200)) - [me].Length, Height \ 2 + CInt(Math.Truncate(dy * 100)), " RELMINATOR ")
			GL2D.End2D()

			Me.SwapBuffers() ' copy what we draw to screen

			Thread.Sleep(1) ' sleep so that we don't hog CPU

		End Sub

		' Draws a single cube
		Private Sub DrawCube()
			GL.CallList(cubeDisplayList)
		End Sub

		' initializes our display list
		Private Sub initCubeDisplayList()
			cubeDisplayList = GL.GenLists(1)
			GL.NewList(cubeDisplayList, ListMode.Compile)

			GL.Begin(PrimitiveType.Quads)

			' Front Face
			GL.TexCoord2(0, 0)
			GL.Vertex3(-1, -1, 1)
			GL.TexCoord2(1, 0)
			GL.Vertex3(1, -1, 1)
			GL.TexCoord2(1, 1)
			GL.Vertex3(1, 1, 1)
			GL.TexCoord2(0, 1)
			GL.Vertex3(-1, 1, 1)
			' Back Face
			GL.TexCoord2(1, 0)
			GL.Vertex3(-1, -1, -1)
			GL.TexCoord2(1, 1)
			GL.Vertex3(-1, 1, -1)
			GL.TexCoord2(0, 1)
			GL.Vertex3(1, 1, -1)
			GL.TexCoord2(0, 0)
			GL.Vertex3(1, -1, -1)
			' Top Face
			GL.TexCoord2(0, 1)
			GL.Vertex3(-1, 1, -1)
			GL.TexCoord2(0, 0)
			GL.Vertex3(-1, 1, 1)
			GL.TexCoord2(1, 0)
			GL.Vertex3(1, 1, 1)
			GL.TexCoord2(1, 1)
			GL.Vertex3(1, 1, -1)
			' Bottom Face
			GL.TexCoord2(1, 1)
			GL.Vertex3(-1, -1, -1)
			GL.TexCoord2(0, 1)
			GL.Vertex3(1, -1, -1)
			GL.TexCoord2(0, 0)
			GL.Vertex3(1, -1, 1)
			GL.TexCoord2(1, 0)
			GL.Vertex3(-1, -1, 1)
			' Right face
			GL.TexCoord2(1, 0)
			GL.Vertex3(1, -1, -1)
			GL.TexCoord2(1, 1)
			GL.Vertex3(1, 1, -1)
			GL.TexCoord2(0, 1)
			GL.Vertex3(1, 1, 1)
			GL.TexCoord2(0, 0)
			GL.Vertex3(1, -1, 1)
			' Left Face
			GL.TexCoord2(0, 0)
			GL.Vertex3(-1, -1, -1)
			GL.TexCoord2(1, 0)
			GL.Vertex3(-1, -1, 1)
			GL.TexCoord2(1, 1)
			GL.Vertex3(-1, 1, 1)
			GL.TexCoord2(0, 1)
			GL.Vertex3(-1, 1, -1)

			GL.End()

			GL.EndList()

		End Sub

        <STAThread()> _
  Public Shared Sub Main()
            Using example As New GL2Dexample()
                example.Run(60) ' run the game at 60 frames per second
            End Using
        End Sub

	End Class
End Namespace
