The same can be done for XNA. First, you need to get the code sample that shows how to embed XNA into a Windows Forms control. It's available in the education catalog on the App hub.
Then, you need to wrap the control into a form, and add some functionality to easily couple plug in an F# function.
#r @"D:\Documents\WinForms\WinFormsGraphicsDevice\bin\Release\WinFormsGraphicsDevice.exe"
#I @"C:\Program Files\Microsoft XNA\XNA Game Studio\v4.0\References\Windows\x86"
#r "Microsoft.Xna.Framework.Graphics.dll"
#r "Microsoft.Xna.Framework.dll"
#r "Microsoft.Xna.Framework.Game.dll"
open System.Windows.Forms
open Microsoft.Xna.Framework
type XnaControl() =
inherit WinFormsGraphicsDevice.GraphicsDeviceControl()
let mutable drawer = fun (dt : GameTime) -> ()
let watch = new System.Diagnostics.Stopwatch()
let mutable last_time = watch.Elapsed
member this.Drawer
with get () = drawer
and set (v) = drawer <- v
override this.Initialize() =
watch.Start()
last_time <- watch.Elapsed
override this.Draw() =
let diff = watch.Elapsed - last_time
last_time <- watch.Elapsed
GameTime(diff, watch.Elapsed)
|> drawer
type XnaForm() =
inherit Form()
let ctrl = new XnaControl()
let animationHandler = new System.EventHandler(fun _ _ -> ctrl.Invalidate())
do
ctrl.Dock <- DockStyle.Fill
base.Controls.Add(ctrl)
member this.XnaControl = ctrl
member this.EnableAnimation() =
Application.Idle.AddHandler(animationHandler)
member this.DisableAnimation() =
Application.Idle.RemoveHandler(animationHandler)
This is how I use it:
let form = new XnaForm()
form.Show()
let content = new Content.ContentManager(form.XnaControl.Services)
content.RootDirectory <- @"D:\Documents\WorldConquest\ContentLibrary\bin\x86\Debug\Content"
let units : Graphics.Texture2D = content.Load("units")
let batch = new Graphics.SpriteBatch(form.XnaControl.GraphicsDevice)
let draw _ =
try
batch.Begin()
batch.Draw(units, Vector2.Zero, Color.White)
finally
batch.End()
form.XnaControl.Drawer <- draw
You'll probably want to load some content (fonts, textures...) otherwise you won't be able to draw much. I don't have any nice way to build content interactively yet. For now, this is what I have to do:
1. Add a C# XNA game library
2. Add an XNA content project
3. Reference the content project from the library created in step 1, and configure the library to use the "Reach" API. This is done from the project's properties.