Rotating Triangle in F#

Sunday, January 4, 2009 by gradbot

For my first project in F# I ported a simple C# XNA project that draws an rotating triangle. The original C# article can be found here.

I'm using Microsoft F#, September 2008 CTP (Community Technology Preview) and Microsoft XNA 3.0 CTP

You can download the complete solution here.




#I @"C:\Program Files\Microsoft XNA\XNA Game Studio\v3.0\References\Windows\x86";;

#r @"Microsoft.Xna.Framework.dll";;
#r @"Microsoft.Xna.Framework.Game.dll";;

open System
open System.Collections.Generic

open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Audio
open Microsoft.Xna.Framework.Content
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input
open Microsoft.Xna.Framework.Storage

type MyGame =
inherit Game

val mutable graphics : GraphicsDeviceManager
val mutable content : ContentManager
val mutable effect : Effect
val mutable world : Matrix
val mutable view : Matrix
val mutable projection : Matrix

new() as this =
{
graphics = null
content = null
effect = null
world = Matrix.Identity
view = Matrix.Identity
projection = Matrix.Identity
}
then
this.graphics <- new GraphicsDeviceManager(this)
this.content <- new ContentManager(this.Services)
this.view <-
Matrix.CreateLookAt(
new Vector3(0.0f, 0.0f, 3.0f),
Vector3.Zero,
new Vector3(0.0f, 1.0f, 0.0f))
this.projection <-
Matrix.CreatePerspectiveFieldOfView(
MathHelper.Pi / 4.0f,
(float32)this.Window.ClientBounds.Height / (float32)this.Window.ClientBounds.Width * 3.0f / 2.0f,
0.01f,
1000.0f)

override this.Initialize() =
base.Initialize()

override this.LoadContent() =
this.effect <- this.content.Load("colorfill")

override this.Draw(gameTime) =
let gd = this.graphics.GraphicsDevice
gd.Clear(Color.Green)

let vertex = [|
new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.0f), Color.Red);
new VertexPositionColor(new Vector3(0.0f, 0.5f, 0.0f), Color.Blue);
new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.0f), Color.Yellow);
|]

gd.RenderState.CullMode <- CullMode.None
gd.VertexDeclaration <- new VertexDeclaration(gd, VertexPositionColor.VertexElements)

this.effect.Begin()

for pass in this.effect.CurrentTechnique.Passes do
pass.Begin()
gd.DrawUserPrimitives(PrimitiveType.TriangleList, vertex, 0, 1)
pass.End()

this.effect.End()

override this.Update(gameTime) =
if (GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed) then
this.Exit()

this.world <- this.world * Matrix.CreateRotationY(MathHelper.TwoPi / 360.0f)
this.effect.Parameters.Item("WorldViewProj").SetValue(this.world * this.view * this.projection)
base.Update(gameTime)

let game = new MyGame()
game.Run()

Filed under , , having  

0 comments: