Chapter 06: SDL Audio Mixer & Windows Builds

Wire up a low-latency SDL 1.2 software PCM audio callback for sound effects, then cross-compile a portable 32-bit Windows pshot.exe right from Linux Mint.

Chapter:06    Duration: 1 hr
Files:src/audio.h, src/audio.c, Makefile
Concepts:SDL_OpenAudio callback thread, SDL_LockAudio synchronization, 16-bit signed PCM synth, MinGW-w64 i686 Win32 cross-compilation
On this page:SDL 1.2 Audio Callback · Cross-Compiling for Windows · Testing the .exe · Exercises · Recap

1. Minimal SDL 1.2 Audio Callback

SDL 1.2 runs your audio callback on a background thread whenever the sound card asks for another chunk of 16-bit PCM samples. Because the main game thread and audio thread share g_freq and g_samples, always bracket main-thread writes with SDL_LockAudio() and SDL_UnlockAudio().

src/audio.h (c)
#ifndef     AUDIO_H
#define     AUDIO_H

int     audio_init(void);
void    audio_shutdown(void);
void    audio_play_beep(float freq_hz, int ms);

#endif
src/audio.c (c)
#include    "audio.h"
#include    "SDL.h"
#include    <math.h>
#include    <string.h>

#define     SAMPLE_RATE     22050
#define     TWO_PI          6.28318530717958647692f

static float    g_phase     = 0.0f;
static float    g_freq      = 440.0f;
static int      g_samples   = 0;
static int      g_ready     = 0;

static void audio_cb(void *userdata, Uint8 *stream, int len) {

    Sint16     *out;
    int         count;
    int         i;

    (void)userdata;

    out   = (Sint16 *)stream;
    count = len / 2;

    for (i = 0; i < count; ++i) {

        if (g_samples > 0) {

            float   s = (float)sin((double)g_phase);

            out[i]    = (Sint16)(s * 5000.0f);
            g_phase  += (TWO_PI * g_freq) / (float)SAMPLE_RATE;

            if (g_phase >= TWO_PI) { g_phase -= TWO_PI; }

            --g_samples;

        } else {

            out[i] = 0;

        }

    }

}

int audio_init(void) {

    SDL_AudioSpec   want;

    if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { return 0; }

    memset(&want, 0, sizeof(want));

    want.freq     = SAMPLE_RATE;
    want.format   = AUDIO_S16SYS;
    want.channels = 1;
    want.samples  = 512;
    want.callback = audio_cb;

    if (SDL_OpenAudio(&want, NULL) < 0) { return 0; }

    g_ready = 1;
    SDL_PauseAudio(0);

    return 1;

}

void audio_shutdown(void) {

    if (!g_ready) { return; }

    SDL_CloseAudio();
    g_ready = 0;

}

void audio_play_beep(float freq_hz, int ms) {

    if (!g_ready) { return; }

    SDL_LockAudio();

    g_freq    = freq_hz;
    g_samples = (SAMPLE_RATE * ms) / 1000;

    SDL_UnlockAudio();

}

Call audio_init() once at startup, audio_shutdown() before plat_shutdown(), and trigger audio_play_beep(180.0f, 70) whenever the player jumps or lands.

2. Cross-Compiling Portable Win32 Builds on Linux Mint

Because every engine module is strict ISO C89 and all OS hooks stay inside SDL 1.2, you do not need Windows or Visual Studio to ship a Windows executable that runs from Windows 98/XP all the way to Windows 11. Install the 32-bit MinGW-w64 cross-compiler on Linux Mint:

Terminal (bash)
sudo apt install mingw-w64

Download the official SDL-devel-1.2.15-mingw32.tar.gz archive from libsdl.org and unpack it into thirdparty/SDL-1.2.15-win32/ inside your project folder. Then append a win32 target to your Makefile:

Makefile (append win32 target) (makefile)
WIN_CC      = i686-w64-mingw32-gcc
WIN_SDL_DIR = thirdparty/SDL-1.2.15-win32
WIN_CFLAGS  = -std=c89 -Wall -Wextra -pedantic -Wno-long-long -O2 -Isrc -I$(WIN_SDL_DIR)/include/SDL
WIN_LDFLAGS = -L$(WIN_SDL_DIR)/lib -lmingw32 -lSDLmain -lSDL -lm -mwindows

win32:
	$(WIN_CC) $(WIN_CFLAGS) $(SRCS) -o pshot.exe $(WIN_LDFLAGS)
Pitfall: Link Order Matters on MinGW
On Windows MinGW builds, -lmingw32 -lSDLmain -lSDL must appear in that exact order, and -mwindows suppresses the extra blank console window behind your game. Copy SDL.dll from thirdparty/SDL-1.2.15-win32/bin/ next to pshot.exe when distributing.

3. Build & Test

Terminal (bash)
make win32
ls -lh pshot.exe

Drop pshot.exe, SDL.dll, and any .bmp assets into a folder and launch on any Windows box (or test locally under Wine with wine pshot.exe).

Exercises

Recap