Getting started with C programming - Example game (with SDL) -
Here is a very simple shooter game made with SDL in C. This is quite a big of a jump from the last example, but everything is commented, just go to the main function and read along one part at a time. Read a C tutorial alongside this if you don't understand some parts of the C code.
This page is for Windows only for now.
Adding sound and images to SDL
SDL can't load file formats like PNG or play sounds well by default, to do it properly we need to add additional SDL modules:
- For images: SDL_image (SDL2_image-devel-X.X.X-mingw.zip). (alternate link)
- For audio: SDL_mixer (SDL2_mixer-devel-X.X.X-mingw.zip). (alternate link)
Extract those into the same place as your project. Just like SDL, you'll have to move the DLLs for both from
Now, I'll try my best not to go into an angry rant about why I personally avoid using third party libraries, but here's the new build file (remember to fix the paths if you have a different SDL version):
@ECHO OFF set libraries=-L"SDL2-2.26.3/x86_64-w64-mingw32/lib" -I"SDL2-2.26.3/x86_64-w64-mingw32/include/SDL2" -lSDL2 ^ -L"SDL2_mixer-2.6.3/x86_64-w64-mingw32/lib" -I"SDL2_mixer-2.6.3/x86_64-w64-mingw32/include/SDL2" -lSDL2_Mixer ^ -L"SDL2_image-2.6.3/x86_64-w64-mingw32/lib" -I"SDL2_image-2.6.3/x86_64-w64-mingw32/include/SDL2" -lSDL2_Image gcc main.c %libraries% -mwindows -o exampleshooter && ( start exampleshooter )
I won't put the code on this webpage since there's about 700 lines of it, instead you can download the code and assets. Just move them to your project folder (be careful about overwriting your old main.c file in case it has something important in it).
Use WASD or arrow keys to move, Space to shoot, Enter to restart, Esc to quit. The enemies will start spawning faster depending on how high your score is.
The project uses some free sounds and music from mixkit.