Originally I wrote the demo using OpenGL – why not? It runs on Windows, Mac, Android, iOS (and more). But then I considered Monogame. It’s a clone of Microsoft’s abandoned XNA, but rather than targeting the devices that XNA supports (Windows Desktop, Xbox 360 and Windows Phone 7) it fills in the (wide) gaps that XNA doesnt support (MacOS, Android, iOS, Windows 8, Windows Phone 8, Ouya, PSP Vita). It’s a nice friendly API and is under active development.

So I’m going to write a short series of posts on how to make Monogame run using F# on various platforms. It’s not a Monogame/XNA tutorial per-se, but a jump start into how to get the 1st window on-screen without having to bootstrap the app using C#.

Back in Xamarin Studio wait a moment for the solution to reload and you’ll probably now find you cannot run the app! You’ll get a nice error like “No build configuration set for Debug”. You can remedy this by simply opening the project options and closing them again.

Next, let’s write a little code:

Replace the contents of Program.fs with

open System
//Simple version of the XNA Game class.
//This is the minimal you'll need to see something onscreen
type Game() as game =
inherit Microsoft.Xna.Framework.Game()
let manager = new Microsoft.Xna.Framework.GraphicsDeviceManager(game)
[<EntryPoint>]
let main args =
let game = new Game()
game.Run()
0

Run this and **bang** –

MonoMac.AppKit.AppKitThreadAccessException: AppKit Consistency error: you are calling a method that can only be invoked from the UI thread.
at MonoMac.AppKit.NSApplication.EnsureUIThread () [0x00000] in :0
at MonoMac.AppKit.NSWindow..ctor (RectangleF contentRect, NSWindowStyle aStyle, NSBackingStore bufferingType, Boolean deferCreation) [0x00000] in :0
at Microsoft.Xna.Framework.MacGameNSWindow..ctor (RectangleF rect, NSWindowStyle style, NSBackingStore backing, Boolean defer) [0x00000] in :0
at Microsoft.Xna.Framework.MacGamePlatform.InitializeMainWindow () [0x00000] in :0
at Microsoft.Xna.Framework.MacGamePlatform..ctor (Microsoft.Xna.Framework.Game game) [0x00000] in :0
at Microsoft.Xna.Framework.GamePlatform.Create (Microsoft.Xna.Framework.Game game) [0x00000] in :0
at Microsoft.Xna.Framework.Game..ctor () [0x00000] in :0
at Program+Game..ctor () [0x0000a] in /Users/.../.../TestApp/Program.fs:5
at Program.main (System.String[] args) [0x00000] in /.../.../TestApp/TestApp/Program.fs:10

Ouch – this is the rough equivalent to the “You’re not calling UI code on the UI thread you fool” Exception on Windows (UnauthorizedAccessException I beleive). The solution is to write a small amount of Cocoa code. Luckily for us, this is nicely trivial in F# and Mono.