Pages

Thursday, June 15, 2017

SDL2 + C# The Quick Start Guide

The primary goal of this Tutorial is to introduce C# developers to SDL2 development. The secondary goal is to provide existing SDL and SDL2 developers with a SDL2+C# quick start guide. I recognize that this is pretty niche, which is why this might be the only tutorial on the subject that doesn’t require some fat middleware framework.

What does this tutorial demonstrate?

Using SDL2 with C# obviously, but, when it’s all over, you will have a window that can display images, play audio and accept input, and this is a great combination for game development.

What development tools am I using?

I am using Windows 7 with Visual Studio 2013, because that’s what I use, but don’t worry, my projects run on MonoDevelop and the mono runtime without modification.

What is SDL2?
“Simple DirectMedia Layer is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.”

*Important:Make sure that you force your application to run either 32bit or 64bit depending on the dll packages you download, otherwise you could get the following error: "System.BadImageFormatException".

Get Coding:

Once I do this, I don’t want to ever do it again, so I’ll be making an object that manages everything for me.

Now run your application, you should see your image being rendered in the top left corner. If not and there aren’t any errors, there are some causes, but most likely you made a typo in your image loading filepath.

Run your application, weird crash, something about vs host. Weird.
Go to your bin directory and run the compiled exe. Works fine. Weirder.
To fix this, go to your project settings, go to the Debug tab, and select “Enable native code debugging”, now your project will launch from the editor without error.

14. Now we have images and sound effects, but we need a way to input instructions. This can be done a lot of good and bad ways. We will do it the simplest way, well, sort of, it’s the flattest way. Writing a complete input system is way out of the scope of this tutorial, so I’ll demo simple gamepad input.

Inside of your core class, after you initialize Mix_OpenAudio, add the following.

SDL.SDL_InitSubSystem(SDL.SDL_INIT_GAMECONTROLLER);

NOTE* For those of you who have used SDL2before, I’m initializing the game controller as a subsystem rather than inside of the SDL_Init arguments, because adding the game controller mysteriously crashes SDL with the C# bindings and I’m not super interested in figuring out way, I’m satisfied with the work around.

Back inside your Program.cs’s main function.

After you loaded the sound, lets set the controller with the following.

IntPtr gc = SDL2.SDL.SDL_GameControllerOpen(0);

This function will use whatever controller you have at index 0, in my case, index 0 is the only controller I have plugged in.

If everything worked correctly, every time you press A, your ears are blasted with noise. Way to go.

-----

In conclusion, you now have a very compact C# SDL2 system capable of displaying images, playing audio and capturing gamepad input. If there are any problems, please feel free to post a comment.

... And if you scrolled to the bottom because you don’t like to read. I have attached a zipped project file. And, if that’s not enough, I have also attached a my LudumDare 38 Compo entry which would have seriously benefited from this tutorial, because for some crazy reason I decided to learn how to do all of this during a 48 hour game jam.