Dump SSDL and get a tutorial on SDL. You’ll see a lot that you recognize. Many SSDL functions are SDL functions with an “S” stuck on the front (as in, SDL_PollEvent became SSDL_PollEvent). Usually SDL functions need one more initial argument, often of type SDL_Window* or SDL_Renderer*, two types you’ll learn right away. You can usually guess what’ll be needed (hint: functions with “Render” in the name probably need SDL_Renderer*).
Or, keep SSDL, but extend with more SDL features – say, joystick support.
Either way it’ll be useful to look behind SSDL to what it’s been hiding from you. Let’s start with initialization and cleanup code.
A simple SDL program

In SSDL, the initialization code in Example 29-1 is done by the constructors for SSDL_Display and SSDL_SoundSystem. Example 29-1 has simplifications. One biggie is, we can’t throw an SSDL_Exception without SSDL (duh), so instead we deal with failure to launch with return -1;.
See what it does: initializes SDL (this must be done first); creates the window (good!); creates a “renderer,” needed to draw or paste images; initializes SDL_Image and SDL_TTF, needed for image and fonts. If anything goes wrong, we give up, because you can’t really go on without these things.
It also supports sound by initializing SDL_Mixer if it can.
The cleanup code shuts down the helper libraries, kills the window and renderer, and finally shuts down SDL.
I obviously prefer my way, for organization, neatness, and not having to type all that code in every new program; but since we’re looking at the messy guts of it all, I guess we’ll leave it in main as game programmers often do. At least I’m not using global variables.
Writing code
- Many SSDL types represents SDL types, usually pointers.
SSDL_Color is essentially an SDL_Color.
SSDL_Display is essentially an SDL_Renderer * and an SDL_Window *. (If you care how these get passed into SDL functions that want them, see the SSDL_Display class definition, specifically two user-defined conversions or cast operators.)
SSDL_Font is a TTF_Font *.
SSDL_Image is an SDL_Texture *.
SSDL_Music is a Mix_Music *.
SSDL_Sound is a Mix_Chunk * and an int (for channel).
SSDL_Sprite is an SDL_Texture* plus a lot of fields, to be sent to SDL_RenderCopyEx in a complicated call (see SSDL_RenderSprite).
These classes exist mostly to protect beginners from pointers and everyone from having to do his/her own dynamic allocation and cleanup.
Besides RGB, SDL_Color and SSDL_Color have an “alpha” member which also ranges from 0 to 255. 0 means completely transparent and 255 means completely opaque. To use it you’ll need SDL functions with “blend” in the name.
Forget ssin and sout; you’ll use TTF_RenderText_Solid (see SSDL_Display::RenderTextLine).
SDL is always using dynamic memory, but you can’t use new and delete. SDL and its helpers provide their own allocation and deallocation functions, for example, SDL_CreateTexture and SDL_DestroyTexture; TTF_OpenFont and TTF_CloseFont. You have to use them.
OK, so let’s do something, cheating by seeing how SSDL did it. I’ll put an image on the screen and wait for someone to press a key. Hoo-ah!
Displaying an image in SDL
Waiting for a key…I read SSDL_WaitKey, then the thing that it calls, then the things it calls, and eventually can construct the monstrosity in Example 29-3. It goes right after the image display code in Example 29-2.
Waiting for a keystroke in SDL
Well, what do you know, it works. And it only took me 100 lines!
Game programs are notorious for bad practices: long functions like this one, global variables, and pointers out the wazoo. As you start programming SDL, you can show everyone how to do it right.
Antibugging
Sometimes I get a crash at program’s end: SDL or a helper library fails on part of its cleanup. I’m probably not supposed to, but I admit, I comment out cleanup code. It won’t matter after the program ends anyway.
Compiling
In Unix or MinGW, take an SSDL-capable Makefile for your platform (MinGW or Unix) and remove all references to SSDL.
C/C++ ➤ General ➤ Additional Include Directories: Take out the path to SSDL includes.
Linker ➤ General ➤ Additional Library Directories: Take out the path to SSDL libraries.
Linker ➤ Input ➤ Additional Dependencies: Take out ssdl<whatever it is>.lib.
Then compile, and run, as you would an SSDL project.
Further resources
I think the best reference is libsdl.org. Documentation on SDL_Image, and others, is there; you just have to find it (I do a web search for what I want and it’ll take me there). And it’s hard to beat Lazy Foo’ (lazyfoo.net) for tutorials.