Archive for the ‘F#’ Category

F# With XNA Game Studio

Posted: April 5, 2011 in F#, Game Programming, Programming, XNA
Tags: ,


I’m currenly trying how to create XNA game with F# and the result is quite interesting :)  This experiment  was using Windows 7 x64, .NET 4.0, and XNA Game Studio 4.0

First step is creating F# Application using Visual Studio 2010

Then in your project expoler add references to XNA 4.0 libraries

Write code below in your Program.fs

open System
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input

type Game1() as this =
    inherit Game()
    let graphics = new GraphicsDeviceManager(this)

    let mutable spriteBatch = null
    let mutable texture = null

    override Game.LoadContent() =
        spriteBatch <- new SpriteBatch(this.GraphicsDevice)

    override Game.Update gameTime =
        base.Update gameTime

    override Game.Draw gameTime =
        base.Draw gameTime

let g = new Game1()
try g.Run()
finally g.Dispose()

Already finished? Here’s the result when you run the program :

And… the result is ugly weird blue window :) since code above is just basic of XNA implementation in F# :)

Getting Started with F# (Part I)

Posted: April 4, 2011 in F#

Hi, now i’m learning new programming language from .NET Framework, it’s F#. This new language is different from other .NET languages such as C# or VB. F# is designed to be simple yet powerful.

If we learn a new programming language, first step to do is how to print “hello world” :) and how to create it is quite simple.

Example below is code of hello world program. open System is similar to include in C#, open System is essential if we’re going to use function like Console.WriteLine or Console.ReadKey. To ending an expression, F# uses whitespace instead semicolon(;). To describe a function in F# we use keyword let and character “_’ is the name of main function in F#.

To print “hello world” message in console we use Console.WriteLine “hello world” and for preventing console window closing automatically we use Console.ReadKey().

open System

let _ =
 Console.WriteLine "hello world"

This is an example for creating function and simple addition. There’s function called add. In add function we have two parameters x and y. Function in F# don’t declare its parameters or return value  explicitly.

open System

let add x y =
    x + y

let _ =
    let a = add 1 2
    Console.WriteLine a

add function is similar to C# code below

int add (int x, int y)
    return x + y;

Now i’ll explain about currying. Look at code below:

let _ = 
    let x = add 1
    let a = x 2
    Console.WriteLine a

We call add function with just one parameter and store the result in x variable. Actually x is not value but another new function which can be called with an additional parameter.