Imports XilonEngineII.NET

''' <summary>
''' Xilon Engine II .NET sample/test app
''' </summary>
''' <remarks>This code sample and engine design is still under development and can change over time creating incompatible issues.</remarks>
Module modMain

	Private DataDirectory As IO.DirectoryInfo = (New IO.DirectoryInfo(My.Application.Info.DirectoryPath).Parent)

	Private Engine As Xilon_EngineII
	'XEII class

	Private WithEvents frmTarget As Form
	'the target (graphics display) to render to

	Private mySprite As XEII_2DPhysicalSprite
	Private myFloor As XEII_2DPhysicalSprite

    Private myTexture As XEII_Texture

    Public Sub Main()
		Engine = New Xilon_EngineII
		'create new XEII instance

		Engine.Size = New Size(1024, 768)
		'Engine.Fullscreen = True

		Engine.Initialize()
		'initalize engine (create DirectX 9 device);
		'Settings should be changed before initializing engine; Default:
		'	Engine.Adapter: XEII_Adapter.Primary
		'	Engine.Behavior: XEII_EnumDeviceBehavior.DeviceBehavior_HardwareVertexProcessing
		'	Engine.ColorDepth: XEII_EnumColorDepth.ColorDepth_Adapterbpp
		'	Engine.Fullscreen: False
		'	Engine.RefreshRate: 60
		'	Engine.Size: Size(800, 600)
		'	Engine.Target: Form (Engine Creates Form) 
		'	Engine.Type: XEII_EnumDeviceType.DeviceType_Hardware
		'	Engine.VSync: False

		'Engine.Renderer.FramesPerSecondLimit = 30

		frmTarget = CType(Engine.Target, Form)
		'get the form the engine created
		frmTarget.BackColor = Color.Black
		'the backbuffer is black so make the form match

		Engine.Scene2D.Physics.Gravity = New PointF(0.0F, 200.0F)
		'apply a downward force (aka Gravity)

		myFloor = New XEII_2DPhysicalSprite("myFloor", (Engine.Size.Width * 0.5F), Engine.Size.Height, Engine.Size.Width * 2.0F, 32, 0.5F, 0.4F, 0.4F)
		myFloor.Mass = Single.PositiveInfinity
		myFloor.IgnoreGravity = True
		Engine.Scene2D.World.Add(myFloor)
		'create and add a floor for the boxes to land on

		myTexture = New XEII_Texture("myBoxes", DataDirectory.FullName & "TexturesBladeBox.jpg", Color.Empty, XEII_Texture.XEII_EnumTextureFilter.Filter_LinearBilinear, 8, XEII_Texture.XEII_EnumTextureFilter.Filter_LinearBilinear)
		Engine.Data.Textures.Add(myTexture)
		'create the box texture
		'myTexture.Load()

		mySprite = New XEII_2DPhysicalSprite("myCharacter", 0.5F, 0.4F, 0.4F)
		'create a new physical sprite
		mySprite.Size = New SizeF(64, 64)
		'set sprite to 64x64
		mySprite.Location = New PointF(Engine.Size.Width * 0.5F - 92, 32)
		'set sprite location to center of screen (off by 92 pixels)
		mySprite.Visible = True
		'sprite should be rendered
		mySprite.Mass = 20
        'set sprite mass

        mySprite.SetTexture(myTexture)
		'link the sprite to the texture

		'Engine.Scene2D.Characters.Add(mySprite)
		'add sprite to characters category

		Dim x As Integer, y As Integer
		Dim sprTemp As XEII_2DPhysicalSprite
		For y = 0 To 9
			For x = -6 To 6
				sprTemp = New XEII_2DPhysicalSprite("mySprite_" & x.ToString() & "x" & y.ToString(), 0.5F, 0.4F, 0.4F)
				sprTemp.Size = New SizeF(64, 64)
				sprTemp.Location = New PointF(Engine.Size.Width * 0.5F - (x * 72), -(y * 72))
				sprTemp.Visible = True
				sprTemp.Mass = 10

				sprTemp.SetTexture(myTexture)

				Engine.Scene2D.Props.Add(sprTemp)
				If y = 0 Then XEII_2DPhysics.ApplyTorque(sprTemp, 950000)
			Next x
		Next y

		Engine.Renderer.Start()
		'start the engine renderer (the renderer is started in a different thread)
		'myTexture.Load(True)

		Engine.Scene2D.Physics.Start()

		Application.Run()
	End Sub

	Private Sub frmTarget_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles frmTarget.Click

	End Sub

	Private Sub frmTarget_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles frmTarget.DoubleClick
		frmTarget.Close()
		'double click equals end application
	End Sub

	Private Sub frmTarget_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles frmTarget.FormClosing
		Engine = Nothing
		'the engine handles destorying all objects in the pools

		Application.Exit()
	End Sub

	Private Sub frmTarget_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles frmTarget.KeyDown
		If Not mySprite Is Nothing Then
			If e.KeyCode = Keys.Right Then
				XEII_2DPhysics.ApplyTorque(mySprite, 1000000)
			ElseIf e.KeyCode = Keys.Left Then
				XEII_2DPhysics.ApplyTorque(mySprite, -1000000)
            ElseIf e.KeyCode = Keys.Up Then
            End If
		End If
	End Sub
End Module