Perhaps no other library has abstracted the creation and use of a GUI from the code like SDL2 Framework. It is available on Windows, macOS, and Linux. It is written in C, but interfaces well with C++.
Search for tutorials on using SDL2 and most likely there will be a short path to Lazy Foo. The self-hosted tutorial is 55 lessons long and starts with how to install SDL2 Framework on Windows, macOS (OS/X), and Linux. The Lazy Foo tutorials then cover how to create a window, using images, True Type Fonts (TTF), Sound, Events, and all the way to Touch. The one catch, all the code is in the ancient language of C++.
Creating a .Net Core 3 layer for SDL2 requires not only understanding how to interface C# code with SDL2 Framework, but also creating some tutorials to help others understand. Why not combine converting code with an already established and recognized SDL2 tutorial set? Enter the project to convert Lazy Foo to C#.
Great! Where to start? Getting things setup in Rider? Ok. What about Visual Studio? Don’t have. Arg. Very well. Let’s skip all that and go to the code. If you want the .Net Core3 layer for SDL2, visit Brainmuffin’s GitHub.
Got it? Let’s go.
The code for the first Lazy Foo tutorial is here.
Let’s get to converting. First, the includes.
The Lazy Foo tutorial starts with two very simple header files. Only two, if you can believe it:
#include <SDL.h> #include <stdio.h>
C# will need a bit more than that. There are the standard system items, the need for interop services, as well as, the calls from the .Net Core 3 layer.
using System;
using System.Runtime.InteropServices;
using static SDL2.NetCore3.SDL;
using static SDL2.NetCore3.SDL_error;
using static SDL2.NetCore3.SDL_pixels;
using static SDL2.NetCore3.SDL_timer;
using static SDL2.NetCore3.SDL_surface;
using static SDL2.NetCore3.SDL_video;
The globals will need to be moved into class variables. After all, the calling is coming from somewhere else. Using Rider on macOS or Linux means only command line programs.
As this is the first Lazy Foo tutorial, the class name will reflect it as L01_Hello_SDL. Also, being C#, the calls to SDL are unsafe, no matter which Operating System is being used. This is where the Native Interop Services is used. Call .Show from a command line program.
public static class L01_Hello_SDL
{
private const int SCREEN_WIDTH = 640;
private const int SCREEN_HEIGHT = 480;
public static void Show()
{
unsafe
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
throw new Exception($"SDL could not initialize! SDL_Error: {SDL_GetError()}");
var window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED_MASK, SDL_WINDOWPOS_UNDEFINED_MASK,
SCREEN_WIDTH, SCREEN_HEIGHT, (uint) SDL_WindowFlags.SDL_WINDOW_SHOWN);
if (window == IntPtr.Zero)
throw new Exception($"Window could not be created! SDL_Error: {SDL_GetError()}");
var screenSurface = SDL_GetWindowSurface( window );
var convertedScreen = Marshal.PtrToStructure<SDL_Surface>(screenSurface);
//Fill the surface white
SDL_FillRect( screenSurface, null, SDL_MapRGB( convertedScreen.format, 0xFF, 0xFF, 0xFF ) );
//Update the surface
SDL_UpdateWindowSurface( window );
//Wait two seconds
SDL_Delay( 2000 );
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
}
}
}