SDL2-C

My efforts to make coding with pure C and SDL2 more clear and fun.

A Collection of SDL2 C Tutorials and Demos, 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.


Why C?
Because C is seeing! =)

Foreword

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!

Coverage

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.

Procedural Sound Annotation

I spent quite some time on that topic and decided that it’s not worth going further. The Hardware isn’t there yet. The focus of the past few decades was to improve GPU’s. Common audio chips are simpler than ever, if one compares the complexity of a GPU and an audio chip it is clear that the audio chip doesn’t reveal a lot functionality.

Requirements

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

Entry Level

SDL2-Surfaces Repo

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.

SDL-Renderer

An Introduction to the Rendering Subsystem.
SDL2-Renderer Repo

  • 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.

SDL-Timers

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

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

Put it together

SDL2-SpriteAnim Repo

Create Spritesheet from gif and render it timed.

SDL2-GUI-Button Repo
Responsive Button.

SDL2-Mousegrab Repo

Let rect follow the mouse.

SDL2-Slider Repo

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

Drag and Drop

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

Audio

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

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

Put it together

SDL2_simple_music_player Repo
Use of SDL-Mixer and GUI-Elements.

SDL2-Parallax-02 Repo

Rapid Parallax implementation with 6 Layers.

SDL2-Titlebar Repo
Roll your own titlebar.

Fonts

SDL2-TTF Repo

This is a series about the SDL-Extension SDL_ttf 2.0

Non-Linear Animations

In the surfaces-series I did some naive linear animations and scaling. This is a demo series about non linear animations. To get more control I am gonna use floats here, now I need some non linear functions. Math provides tons of those functions. In computer graphics shaping functions are well known and used to create procedural graphics, too. But, one can use those functions also to shape the time-steps, positions or colors.

Impulse

Repository with Python 3 mathplotlib sources included.

Procedural/algorithmic Textures

SDL2-Textures Repo

(Game-)Physics

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

Artificial intelligence (AI)

SDL2-Game-AI Repo

Curves

SDL2-Curves Repo

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

More Textures

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

SDL2-TextureColorMod Repo

This small demo shows how to use TextureColorMod.

A simple Game

Pong Repo

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.

Intermediate

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

Strings/Input/Text

SDL2-Text

This is a small SDL2 text API demo

A GIF-Viewer

SDL2-Gif Repo

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

SDL and OpenGL

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

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

Shade it! Repo

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.

Misc Demos

SDL2-Demo-01 Repo

Demo using SDL2 Image, SDL2 TTF, SDL2 Mixer.

Byte_Drawer Repo

Flipping Bits on a real char and visualize that.

Random Stuff

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