Programming games in C# using SDL.NET Tutorial One

Setting Up the Game

One of the problems with open source is that projects sometimes seem to fall by the wayside or take confusing turns. Take SDL.NET. Ignoring the website for sale, a search on the web reveals a project that seems to have stopped in November 2010. I don't think it has stopped but just looks like it has.

Looking elsewhere I came across the Tao framework linked on the Mono website which seems to cover the same area and adding support for sound etc.

But looking on sourceforge (again!), it has been superseded by OpenTK but the focus there is OpenGL. However, it also includes OpenAL so installing the two (cs-sdl and OpenTK) seemed to be the way forward.

Part of the OpenTk install failed; the NS (shader) because I don't have VS 2008 installed! However, the rest of it was ok. I created a C# Console project and started playing with SDL.NET. The online documentation can be found here.

Looking back I can see that the OpenTK framework wasn't needed as such, that SDL.NET installed everything but that wasn't clear at the time. It still uses the Tao Framework even though development of that has been superseded by OpenTK. It's a little confusing and I hope the SDL.NET team will bring out an OpenTk compatible version in the future.

What Exactly is SDL.NET?

It's not, as I thought, just a thin wrapper round SDL, but adds considerable extra functionality.

There are a number of classes provided to provide the following:

  • Timers
  • Provides Sprites, including animation and Text
  • Provides surfaces for 2D and OpenGl
  • Provides support for Movie loading and playing
  • Provides support for Audio
  • Provides Bezier, polygon (and textures), square, circle, line, pie drawing
  • Provides particle support with emitters and sprites and manipulators.
  • Provides interfacing with Windows forms through a shared PictureBox with surface.


There are several things you have to do to get it setup. Here they are:

Locate the two SDL.NET dlls (SdlDotNet.dll and Tao.Sdl.dll) as well as the OpenTK dlls, and add them to the project references. After installation, the dlls are located in Program Files\SdlDotNet\bin (on a 32 bit Windows and Program Files (x86)\SdlDotNet\bin on 64 bit Windows. Right click on the References section in Solution Explorer then click Add Reference and select the Browse tab. That opens an Explorer dialog and after locating the dlls select then and click ok.

SDL.NET uses the SDL set of dlls and installs them under the lib folder. Don't delete them!

One last thing, click on the View\Properties so it opens up the Property pages and on the first tab (Application) Change Output type from Console Application to Windows Application. If you don't do this when the program first runs and opens up the SDL main Window it will open up a console Window as well.

We're now ready to start and I've created a short application below. This blits randomly sized and located rectangles and circles on the Window surface at 1,700 drawn per second at a frame rate of 50 frames per second.

That 1,700 comes from setting the number drawn per frame to 17 and displaying the frames per second in the Window caption using Video.WindowCaption. Each frame it draws 17 filled circles and rectangles, 17 x 2 x 50 = 1,700. This figure depends on the video card, CPU etc. It's an impressive speed.


// By David Bolton,
using System;
using System.Drawing;
using SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Graphics.Primitives;

public class ex1
    private const int wwidth = 1024;
    private const int wheight = 768;
    private static Surface Screen;
    private static Random r = new Random() ;

    public static void Main(string[] args)
        Screen = Video.SetVideoMode( wwidth, wheight, 32, false, false, false, true) ;
        Events.TargetFps = 50;
        Events.Quit += (QuitEventHandler) ;
        Events.Tick += (TickEventHandler) ;
        Events.Run() ;

    private static void QuitEventHandler(object sender, QuitEventArgs args)
        Events.QuitApplication() ;

    private static void TickEventHandler(object sender, TickEventArgs args)
        for (var i = 0; i < 17; i++)
            var rect = new Rectangle(new Point(r.Next(wwidth- 100),r.Next(wheight-100)),
                         new Size(10 + r.Next(wwidth - 90), 10 + r.Next(wheight - 90))) ;
            var Col = Color.FromArgb(r.Next(255),r.Next (255),r.Next(255)) ;
            var CircCol = Color.FromArgb(r.Next(255), r.Next (255), r.Next(255)) ;
            short radius = (short)(10 + r.Next(wheight - 90)) ;
            var Circ = new Circle(new Point(r.Next(wwidth- 100),r.Next(wheight-100)),radius) ;
            Screen.Fill(rect,Col) ;
            Circ.Draw(Screen, CircCol, false, true) ;
    Screen.Update() ;
    Video.WindowCaption = Events.Fps.ToString() ;


Object Oriented Development

SDL.NET is very Object Oriented and there are two predefined objects that are used in every SDL.NET application.

Video provides methods to set the video mode, create video surfaces, hide and show the mouse cursor, and interact with OpenGL. Not that we'll be doing OpenGL for a while.

The Events class contains events which can be attached to to read user input and other miscellaneous occurrences.

Here the Video object is used to set the size and resolution of the game Window (full screen is an option). The parameters for SetVideoMode let you change these and 13 overloads provide plenty of variety. There's a .chm file (Windows html help format) in the doc folder documenting all the classes and members.

The Events object has a Quit events handler that lets you add close down logic and you should call Events.QuitApplication() to make it respond to the user closing the application. The Events.Tick is possibly the most important event handler. It calls the specified event handler each frame. This is the model for all SDL.NET development.

You can set your desired frame rate and my reducing the loop to 5 and changing the Targetfps to 150 I got it running at 164 frames per second. TargetFps is a ballpark figure; it puts in delays to get you near that figure but the Events.Fps is what is delivered.


Like the original non Windowed version of SDL, the SDL.NET uses surfaces for rendering to the screen. A surface can be constructed from a graphics file. There are a large number of properties and methods that make it possible to read or write pixels as well as draw the graphics primitives, blit other surfaces, even dump a surface to a disk file for taking screenshots.

SDL>NET provides just about everything to let you create games. I'll be looking at the various features over the next few tutorials then move onto create games with it. Next time we'll look at sprites.