Using SDL with c#

In order to make my level editor I decided to give a try to DSL. After performing some researches, it seems there’s only one valid option to do SDL with c# : SdlDotNet

Sadly support is not good, and questions remain unanswered on forums, but still, it works enough for me to use it and make a little beginner guide.

SDL

Simple DirectMedia Layer (SDL) is a cross-platform, free and open source multimedia library written in C that presents a simple interface to various platforms’ graphics, sound, and input devices.

(http://en.wikipedia.org/wiki/Simple_DirectMedia_Layer)

For the level editor I am using this library to load, display, and manipulate thousands of sprites (tiles). Currently I am only using the 2D display part of this library.

SdlDotNet

Sdl.net is the wrapper for .net platform, it supports Microsoft .net framework and mono (linux, mac, windows), and give an object oriented approach to SDL (SDL being written in C, it’s not an object oriented library).

In order to use Sdl.net in your project, simply :

  • download the runtime
  • create a reference to the two provided dlls in your project
  • copy dependencies to the release directory of your project

Executable should work now under Microsoft .net framework and mono

Simple example

For the level editor I needed to use a control to display objects in the program itself, SdlDotNet provides a SurfaceControl for this, simply add the control to your toolbox, and insert it your main app.

When this is done, you have access to the most important function here, SurfaceControl.Blit that will allow you to insert a Surface object to display. The Surface object is just like an image that you can manipulate :

  • Rotate
  • Duplicate
  • Extract elements
  • Insert other surfaces/sprites

Surface can be created empty, with an external bitmap, or by transforming another Surface. For our example I will create a first empty surface, then load an image, insert it twice in the first surface and display everything in my SurfaceControl :

Surface surface = new Surface(100, 100);
Surface item = new Surface((Bitmap)Bitmap.FromFile(“example.png”));
surface.Blit(item, new Point(0, 0));
surface.Blit(item, new Point(20, 20));
this.surfaceControl.Blit(surface);

This is all the basic that you need to begin, then I suggest you to go through the guides and api references on official website.

Troubleshoot

I came into some error when playing with SDL.net, mainly because PNG support is poor because of the Alpha Mask, you will sometimes end up with black images instead of what you were looking for. In order to help you I would suggest that you insert SdlDotNet project sources into your own project, and debug it yourself : usually you just need to change Alpha Mask to its default value (0) for things to work.

As I don’t know the side effects of this modification, I won’t commit any changes to official project.

Comments are closed.