SDL2 C Tutorials, Examples, Demos and more

Comprehensive Collection of SDL2 C Tutorials, SDL2 with OpenGL/Vulkan and other APIs. Procedural Sounds and Graphics, plus GLSL Shaders for Game-Programming and general purpose. All done in pure C.

View on GitHub

Welcome!
This is my effort to make coding with pure C and SDL2 more clear and fun.
Why C? Cause C is seeing! =).
You should have a working C-Environment and SDL2 Development Libs installed; preferable some of the extensions to SDL, like Mixer, Image and TTF.

If you struggle with getting things up and running, refer to one of those pages:
Will Usher or Lazy Foo.
For Mac OS: Setting Up An SDL2 Project in Xcode 9 or without Xcode.
And if you wish to use SDL2 with CMake: Trenki’s Dev Blog

I am trying to cover the SDL2-API as needed, OpenGL, Vulkan and a couple of other common API’s like curl or jannson, tho this is pure C and mainly Linux orientated, doesn’t mean I don’t cross-compile for Windows or Android. If you need a german translation I could make that happen. SDL-Audio Series and Joystick/Gamecontroller Series are nearly finished, just polishing it. At the same time I am building up my C-Snippets, but I am pretty behind on that. SDL_net 2.0 isn’t covered yet.

Feel free to contact me for criticism, Demo Requests or whatever!
Oh, you can use the code as you wish, but don’t blame me!
Happy hacking!

C-Code:

Shade it!

Using SDL2 to show Shadertoy GLSL Fragment Shaders. Features “drag and drop”, Auto-Screenshot with shadername, editing with Auto-Compile.
WIP: Finishing SDL2-Audio right now, to move on to FFT and finally implement visualization of GLSL shaders with low latency.

Entry Level

SDL2-Surfaces

This is a set of small introductory programs.
It’s about the window, the window icon, a custom cursor, surfaces,
Pixel Manipulation and how to save a screenshot.

SDL2-Renderer
This is a series about the SDL2-Renderer.
Create a texture from surface, render it flipped, mirrored and rotated.
Take Screenshot with RenderReadPixels.
Render and animate a colorful gradient like a new shadertoy one.
Shows a bit how glsl works and why we want shaders.

SDL2-Timer-01
Records the duration how long left MouseButton is pressed.

SDL2-Timer-02
Program quits after a certain amount of time.

SDL2-SpriteAnim

Create Spritesheet from gif and render it timed.

SDL2-GUI-Button
Responsive Button.

SDL2-Mousegrab

Let rect follow the mouse.

SDL2-DnD
Drag and Drop. Receiving Text and Files via SDL_DropEvent.

Play music with SDL2
Use of SDL-Mixer for Music.

SDL2-Mixer-play_mp3
Use of SDL-Mixer for Sound.

Basic-Level:

SDL2_simple_music_player
Use of SDL-Mixer and GUI-Elements.

SDL2-Parallax-02

Rapid Parallax implementation with 6 Layers.

SDL2-Titlebar
Roll your own titlebar.

SDL2-TTF

This is a series about the SDL-Extension SDL_ttf 2.0

SDL2-Textures
Procedural/algorithmic Textures.

SDL2-Game-Physics
This is a series about physics - SI-Units, Velocity, Acceleration, Gravity, Friction etc.

SDL2-Game-AI
This is an introductory series about Artificial intelligence (AI).

SDL2-Slider

It is a follow up from Mousegrab. Shows how to make a Slider.

SDL2-Curves

How to apply De Casteljau’s algorithm on a quadratic Bézier curve.

SDL2-create_tex_part_of_win
Re-Render a part of the screen using SetRenderTarget and SDL_TEXTUREACCESS_TARGET.

SDL2-TextureColorMod

This small demo shows how to use TextureColorMod.

Pong

My scaleable Pong implementation. You can take over two player control anytime, the AI will take over if you go afk. A mingw Windows 64 build is in the Makefile.

Advanced in C

You need to fully understand pointers, dynamic memory allocation and bit operations.

SDL2-Text

This is a small SDL2 text API demo

SDL2-Gif

Plays an animated gif - pauseable and scaleable.
uses: SDL_gifwrap
by Jonathan Dearborn

SDL2-OpenGL
This is a starter series about GLSL Shaders using SDL2 as OpenGL Helper and a bit Shader-Language for C-Coders.

NanoVG-SDL2 starter
Startup Code to use SDL2/OpenGL/NanoVG

SDL2-Demo-01

Demo using SDL2 Image, SDL2 TTF, SDL2 Mixer.

Byte_Drawer

Flipping Bits on a real char and visualize that.

C&C++ mixed Code:

Classic Pong and Breakout stuff
Ressources covering Atari’s classic game.

C++ game engine skeleton
with GNU-Makefile

Misc

My C Code
C-Idioms, C-Examples, C-Tutorials, C-Snippets

Light Template
SDL2 Template for KDevelop

Heavy Template
SDL2 Template for KDevelop - with TTF & Mixer

Simple-state-pattern
High Level Game State Management - Code Flow diagram.

Game Loots
This guide is intended to get the Idea of random drops and as introduction to random distribution or weighted distribution.

External ressources

C

Stephen Meier’s SDL2 Tutorials

SDL Mixer:
change Frequency during runtime

C++

http://gigi.nullneuron.net/gigilabs/writing/sdl2-tutorials/

Emscripten

Emscripten and SDL 2 Tutorial

GameDev

Amit’s Game Programming Information
Game Engines Collection
Newcastle University

CG

Paul Bourke’s on geometry Philip Rideout’s blog
Lode’s Computer Graphics Tutorial
Íñigo Quílez
Jon Macey’s NGL Demos

Audio

DrPetter’s homepage