I’ll start with a simple application skeleton, which will be the basis of the examples in the rest of this guide. If you are already familiar with .NET you can probably just skim the code (find the link at the bottom of the article) and skip ahead to the next page. For those of you new to .NET I’ll just give a quick overview.
The skeleton consists of two files: Program.cs which contains the program logic, and Osx.cs which contains the bindings for the MacOS X types, enumerations, and functions.
The program starts by creating an application object. Since this is just a skeleton it doesn’t actually do anything with the application except immediately shut it down again. Later on I will add the event loop here.
using (new OsxGameDevApplication())
{
// event loop goes here
}
In case you aren’t familiar with C#, you can read more about the using keyword and IDisposable interface. But in a nutshell, the code above amounts to this:
OsxGameDevApplication _obj;
try
{
_obj = new OsxGameDevApplication();
// event loop goes here
}
finally
{
_obj.Dispose();
}
So it creates the application object at the start, and then makes sure that the Dispose() method is called no matter what happens inside the block, even if an exception occurs.
In the application object constructor I call into MacOS X to get the current screen resolution (this isn’t really part of the skeleton, I just wanted to demonstrate the technique). The screen resolution is written to the console so I can make sure that everything is working correctly.
IntPtr mainDisplayId = Osx.CGMainDisplayID();
CGRect bounds = Osx.CGDisplayBounds(mainDisplayId);
Console.WriteLine("Resolution is " + bounds.Width + "x" + bounds.Height);
The bindings to the MacOS X methods, as well as the CGRect data type, are defined in Osx.cs.
[StructLayout(LayoutKind.Sequential)]
public struct CGRect
{
public float X, Y;
public float Width, Height;
}
public class Osx
{
[DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
public static extern CGRect CGDisplayBounds(IntPtr displayID);
[DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
public static extern IntPtr CGMainDisplayID();
}
Program cleanup code should go into the Dispose() method at the point indicated by the comments.
public void Dispose()
{
Console.WriteLine("Game is finishing");
// game cleanup code goes here
Console.WriteLine("Game is finished");
}
The source code for this example is available from the link below. The following sequence of commands, entered in Terminal, will compile and run it. You should see the status messages as they are written to the console.
$ cd OsxGameDev
$ mcs -out:Game.exe *.cs
$ mono Game.exe
And that should cover everything. With that out of the way, it is time to create a window!
| Attachment | Size |
|---|---|
| OsxGameDev_Skeleton.tar.gz | 840 bytes |