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 EZGL2DexampleSprites
	Public Class GL2Dexample ' inherit the GameWindow class
		Inherits GameWindow

		Private Const SCREEN_WIDTH As Integer = 640 ' screen dimensions
		Private Const SCREEN_HEIGHT As Integer = 480

		Private plantSprite As GL2D.SpriteGL ' our single plant texture
		Private magicSprite As GL2D.SpriteGL ' our magic texture
		Private atlasSprites() As GL2D.SpriteGL ' sprites loaded via image atlas
		Private femaleSprites() As GL2D.SpriteGL ' sprites loaded without image atlas

		Private angle As Single = 0 ' angle of rotation of our magic sprite
		Private px As Single = 320 ' location of the movable plant sprite(use th keyboard)
		Private py As Single = 80
		Private speed As Single = 5 ' speed of movement
		Private frame As Integer = 0 ' standard frame counter
		Private megaFrame As Integer = 0 ' frame to animate the "megaman"
		Private chickFrame As Integer = 0 ' frame to animate the "female"

		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 = "EZ Sprites!" ' set the title
			'WindowBorder = WindowBorder.Hidden;             // hide the window

			GL2D.Screen2D(Width, Height) ' init our OpenGL screen

			plantSprite = GL2D.LoadSprite("data/EZGL2DexampleSprites/plant.png", CInt(TextureMinFilter.Linear)) ' load the texture
			magicSprite = GL2D.LoadSprite("data/EZGL2DexampleSprites/magic.png", CInt(TextureMinFilter.Linear)) ' load the texture
			atlasSprites = GL2D.LoadSpriteSet("data/EZGL2DexampleSprites/atlas.png", Atlas.atlasCoords.Length \ 4, Atlas.atlasCoords, CInt(TextureMinFilter.Linear)) ' load texture via sprite atlas
			femaleSprites = GL2D.LoadTileSet("data/EZGL2DexampleSprites/female.png", 32, 32, CInt(TextureMinFilter.Nearest)) ' Nearest for oldskool effect

		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
			angle += 5 ' increment angle

			' Animate sprites(female and megaman) every 8th frame
			If (frame And 7) = 0 Then
				megaFrame += 1
				If megaFrame > 7 Then
					megaFrame = 0
				End If
				chickFrame += 1
				If chickFrame > 2 Then
					chickFrame = 0
				End If
			End If

			' get input
			If Keyboard(OpenTK.Input.Key.Up) Then
				py -= speed
			End If
			If Keyboard(OpenTK.Input.Key.Down) Then
				py += speed
			End If
			If Keyboard(OpenTK.Input.Key.Left) Then
				px -= speed
			End If
			If Keyboard(OpenTK.Input.Key.Right) Then
				px += speed
			End If

			If Keyboard(OpenTK.Input.Key.Escape) Then
				Me.Exit()
				Return
			End If
		End Sub

		Protected Overrides Sub OnRenderFrame(ByVal e As FrameEventArgs)
			MyBase.OnRenderFrame(e)

			GL2D.ClearScreen() ' clear the screen

			GL2D.Begin2D() ' start 2D drawing mode


				' Draw a line of plants flipped
				GL2D.SetColor(1, 1, 1, 1)
				GL2D.BlendMode(GL2D.BLEND_MODE.GL2D_ALPHA)
				GL2D.Sprite(170 * 0, 0, GL2D.SpriteGL.FLIP_NONE, plantSprite)
				GL2D.Sprite(170 * 1, 0, GL2D.SpriteGL.FLIP_H, plantSprite)
				GL2D.Sprite(170 * 2, 0, GL2D.SpriteGL.FLIP_V, plantSprite)
				GL2D.Sprite(170 * 3, 0, GL2D.SpriteGL.FLIP_H Or GL2D.SpriteGL.FLIP_V, plantSprite)

				' Draw a line of plants flipped
				' but with different colors
				GL2D.BlendMode(GL2D.BLEND_MODE.GL2D_ALPHA)
				GL2D.SetColor(1, 0, 0, 1)
				GL2D.Sprite(170 * 0, 340, GL2D.SpriteGL.FLIP_NONE, plantSprite)
				GL2D.SetColor(0, 1, 0, 1)
				GL2D.Sprite(170 * 1, 340, GL2D.SpriteGL.FLIP_H, plantSprite)
				GL2D.SetColor(0, 0, 1, 1)
				GL2D.Sprite(170 * 2, 340, GL2D.SpriteGL.FLIP_V, plantSprite)
				GL2D.SetColor(0, 1, 1, 1)
				GL2D.Sprite(170 * 3, 340, GL2D.SpriteGL.FLIP_H Or GL2D.SpriteGL.FLIP_V, plantSprite)

				' show rotation and scaling
				GL2D.SetColor(1, 1, 1, 1)
				GL2D.BlendMode(GL2D.BLEND_MODE.GL2D_ALPHA)
            Dim scaleX As Single = CSng(Math.Sin(frame / 20.0)) * 2
            Dim scaleY As Single = CSng(Math.Sin(frame / 25.0)) * 2
				GL2D.SpriteRotateScale(100, 240, angle, scaleX, scaleY, GL2D.SpriteGL.FLIP_NONE, magicSprite)
				GL2D.SpriteRotateScale(540, 240, -angle, -scaleX, -scaleY, GL2D.SpriteGL.FLIP_NONE, magicSprite)

				' Glow lines!
				GL2D.BlendMode(GL2D.BLEND_MODE.GL2D_BLENDED)
				For i As Integer = frame To (360 + frame) - 1 Step 10
                Dim px1 As Double = Math.Sin(frame / 30.0) * 220 * Math.Cos((i * Math.PI) / 180.0)
                Dim py1 As Double = Math.Sin(frame / 80.0) * 120 * Math.Sin((i * Math.PI) / 180.0)
                Dim px2 As Double = Math.Sin(frame / 130.0) * 170 * Math.Cos(((i + 20) * Math.PI) / 180.0)
                Dim py2 As Double = Math.Sin(frame / 40.0) * 160 * Math.Sin(((i + 20) * Math.PI) / 180.0)
                Dim adder As Double = Math.Abs(4 * Math.Sin(frame / 20.0))
                GL2D.SetColor(CSng(Math.Abs(Math.Sin(frame / 10.0))), CSng(Math.Abs(Math.Sin(frame / 30.0))), CSng(Math.Abs(Math.Sin(frame / 40.0))), 1)
                For j As Integer = 0 To CInt(adder) - 1
                    GL2D.SpriteOnLine(320 + CInt(Math.Truncate(px1)), 240 + CInt(Math.Truncate(py1)), 320 + CInt(Math.Truncate(px2)), 240 + CInt(Math.Truncate(py2)), 15 - (CInt(Math.Truncate(adder)) * 2), atlasSprites(12))
                Next j
				Next i

				' animated megaman
				GL2D.BlendMode(GL2D.BLEND_MODE.GL2D_TRANS)
				GL2D.SetColor(1, 1, 1, 1)
				GL2D.SpriteRotateScale(320, 400, 0, 2, 2, GL2D.SpriteGL.FLIP_NONE, atlasSprites(1 + megaFrame))

				' animated chick
				GL2D.SpriteRotateScale(100 + 150 * 0, 90, 0, 2, 2, GL2D.SpriteGL.FLIP_NONE, femaleSprites(0 + chickFrame))
				GL2D.SpriteRotateScale(100 + 150 * 1, 90, 0, 2, 2, GL2D.SpriteGL.FLIP_NONE, femaleSprites(4 + chickFrame))
				GL2D.SpriteRotateScale(100 + 150 * 2, 90, 0, 2, 2, GL2D.SpriteGL.FLIP_NONE, femaleSprites(8 + chickFrame))
				GL2D.SpriteRotateScale(100 + 150 * 3, 90, 0, 2, 2, GL2D.SpriteGL.FLIP_NONE, femaleSprites(12 + chickFrame))

				' This is our "alpha blended" controllabe plant sprite
				GL2D.BlendMode(GL2D.BLEND_MODE.GL2D_BLENDED)
				GL2D.SpriteRotateScale(CInt(Math.Truncate(px)), CInt(Math.Truncate(py)), -angle, 1.0F, 1.0F, GL2D.SpriteGL.FLIP_NONE, plantSprite)

			GL2D.End2D() ' end 2D drawing mode

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

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


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

	End Class
End Namespace
