return;

Getting started with C programming - Example game Windows -

Here is a very simple shooter game made in C using minimal libraries. This is quite a big of a jump from the last example, but everything is commented, just go to the main function at the bottom and read along, one part at a time.

This page is for Windows Windows. I don't know how to do this on Linux Linux at this time, but you can check the Linux SDL version.


Adding sound and images

Most image and audio files are very complicated so it is highly recommended to use libraries to load them. Audio playback can also be quite involved because audio APIs in operating systems have always been a big mess. Luckily there's simple "single-file header" libraries to help.

Just download the source code (from the green button that says "code") and extract those into the same place as your project.

That's it. Unlike normal libraries, single-file libraries do not require any additional setup. The only thing you typically need to do is #define something before #including (otherwise you only get the type declarations without the code), here's how to properly include them:

#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio-master/miniaudio.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb-master/stb_image.h"

Note: loading .bmp image files and .wav audio files by yourself is pretty easy, and I recommend trying it some time as a learning exercise. Those file formats have a very large file size though and the goal of this example game is to do something more practical, so I chose to use libraries instead.


The code

I won't put the code on this webpage since there's about 900 lines of it, instead you can download the code and assets (not including the libraries mentioned above).

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.


Note about software rendering and performance

This project renders it's graphics by simply writing pixels into memory (this is called "software rendering"). This works fine in a small window and in programs that don't need to animate graphics much (like FilenameWrangler), but it's still VERY slow compared to how fast a GPU can draw graphics. It's not ideal to do this for full-screen graphical programs like games. You can speed up the rendering by adding -O3 into the compiler command, this tells the compiler to try as hard as it can to optimize your program (it significantly slows down the compile-time though).

I don't exactly know what to recommend for proper graphics, everything has it's own problems.

Drawing text

There's 3 approaches to drawing text: