sdl2 clib

SDL2 multimedia — window, 2D hardware-accelerated renderer, textures, keyboard/mouse events, audio, and TrueType fonts.

ppm install sdl2
Requires SDL2 and its extension libraries:
sudo apt install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev libsdl2-mixer-dev

Overview

SDL2 is the standard cross-platform multimedia library for games and interactive applications. It provides a hardware-accelerated 2D renderer backed by OpenGL or Direct3D, an input event system, audio playback (via SDL_mixer), image loading (via SDL_image), and TrueType text (via SDL_ttf).

Game Loop

uses sdl2;

Init();
var win := CreateWindow('My Game', -1, -1, 800, 600, False, False, False);
var rnd := CreateRenderer(win, True);  { VSync on }

var tex    := LoadTexture(rnd, 'assets/player.png');
var font   := LoadFont('assets/font.ttf', 20);
var music  := LoadMusic('assets/bgm.ogg');
OpenAudio(44100, 2, 2048);
PlayMusic(music, -1);  { loop forever }

var running := True;
var px := 100;
var ev: TSDLEvent;

while running do
begin
  { Events }
  while PollEvent(ev) do
  begin
    if ev.EventType = SDL_QUIT then running := False;
    if (ev.EventType = SDL_KEYDOWN) and (ev.Scancode = SDL_SCANCODE_ESCAPE) then
      running := False;
  end;

  { Update }
  if IsKeyPressed(SDL_SCANCODE_RIGHT) then px := px + 3;
  if IsKeyPressed(SDL_SCANCODE_LEFT)  then px := px - 3;

  { Render }
  SetRenderDrawColor(rnd, 20, 20, 30, 255);
  RenderClear(rnd);

  var dst: TSDLRect;
  dst.X := px; dst.Y := 250; dst.W := 64; dst.H := 64;
  RenderCopyTexture(rnd, tex, dst);

  DrawText(rnd, font, 'Use arrow keys', 10, 10, 255, 255, 255);
  RenderPresent(rnd);
end;

HaltMusic();
FreeMusic(music);
CloseFont(font);
DestroyTexture(tex);
DestroyRenderer(rnd);
DestroyWindow(win);
Quit();

Drawing Primitives

{ Colored rectangle }
SetRenderDrawColor(rnd, 255, 100, 0, 255);
var r: TSDLRect;
r.X := 50; r.Y := 50; r.W := 200; r.H := 100;
RenderFillRect(rnd, r);

{ Border only }
SetRenderDrawColor(rnd, 255, 255, 0, 255);
RenderDrawRect(rnd, r);

{ Diagonal line }
SetRenderDrawColor(rnd, 0, 200, 255, 255);
RenderDrawLine(rnd, 0, 0, 800, 600);

{ Filled circle }
SetRenderDrawColor(rnd, 200, 50, 200, 255);
RenderFillCircle(rnd, 400, 300, 50);

Sprites with Rotation & Flip

var sprite := LoadTexture(rnd, 'ship.png');
SetTextureAlpha(sprite, 200);  { semi-transparent }

var src: TSDLRect; src.X := 0; src.Y := 0; src.W := 32; src.H := 32;
var dst2: TSDLRect; dst2.X := 300; dst2.Y := 200; dst2.W := 64; dst2.H := 64;

{ Rotate 45 degrees, no flip }
RenderCopyEx(rnd, sprite, src, dst2, 45.0, False, False);

{ Horizontal flip }
RenderCopyEx(rnd, sprite, src, dst2, 0, True, False);

Audio

OpenAudio(44100, 2, 2048);   { 44.1 kHz stereo }

var bgm   := LoadMusic('bgm.ogg');
var jump  := LoadSound('jump.wav');
var explosion := LoadSound('boom.wav');

SetMusicVolume(64);    { 50% volume }
PlayMusic(bgm, -1);   { loop forever }

PlaySound(jump, -1, 0);       { auto channel, no loop }
PlaySound(explosion, 2, 0);   { channel 2, no loop }
SetSoundVolume(-1, 96);      { set all channels to 75% }

PauseMusic();
ResumeMusic();
HaltMusic();
FreeMusic(bgm);
FreeSound(jump);
CloseAudio();

API Reference

Init & Window

FunctionDescription
InitInitialize SDL2 (video, audio, events).
QuitShutdown all SDL2 subsystems.
GetTicksMilliseconds elapsed since Init.
Delay(Ms)Sleep for Ms milliseconds.
CreateWindow(Title, X, Y, W, H, Fullscreen, Resizable, Borderless)Create a window. -1/-1 for centered position.
CreateWindowFullscreen(Title)Fullscreen at native resolution.
DestroyWindow(Win)Close and destroy window.
SetWindowTitle(Win, Title)Change window title.
GetWindowSize(Win, W, H)Get drawable size (accounts for high-DPI).

Renderer & Primitives

FunctionDescription
CreateRenderer(Win, VSync)Hardware-accelerated 2D renderer.
SetRenderDrawColor(Rnd, R,G,B,A)Set draw color (0–255).
RenderClear(Rnd)Fill screen with draw color.
RenderPresent(Rnd)Flip buffer to screen.
RenderDrawLine/Lines/Point/Rect/FillRect/FillCircleGeometric primitives.
SetRenderScale(Rnd, SX, SY)Virtual resolution scaling.

Textures

FunctionDescription
LoadTexture(Rnd, Path)Load PNG/JPG/BMP as GPU texture.
CreateTexture(Rnd, W, H)Blank texture (for render-to-texture).
SetTextureAlpha(Tex, Alpha)Transparency 0–255.
SetTextureColorMod(Tex, R,G,B)Tint/color multiply.
RenderCopyTexture(Rnd, Tex, Dst)Draw texture to rect.
RenderCopyEx(Rnd, Tex, Src, Dst, Angle, FlipH, FlipV)Draw with rotation and flip.
RenderCopyF(Rnd, Tex, Dst)Draw with floating-point rect.
DestroyTexture(Tex)Free texture.

Events & Input

FunctionDescription
PollEvent(Event)Get next event. Returns False if queue is empty.
WaitEvent(Event)Block until an event arrives.
IsKeyPressed(Scancode)Check if a key is currently held.
GetKeyboardStateSnapshot of all key states.
GetMouseState(X, Y, Buttons)Current mouse position and buttons.
WarpMouse(Win, X, Y)Move cursor to position.
ShowCursor(Visible)Show or hide the mouse cursor.
SetRelativeMouseMode(Enable)FPS-style relative mouse capture.

Fonts, Audio

FunctionDescription
LoadFont(Path, Size)Load TrueType font at point size.
DrawText(Rnd, Font, Text, X, Y, R,G,B)Render text directly to renderer.
RenderTextBlended(Rnd, Font, Text, R,G,B,A)Anti-aliased text as texture.
MeasureText(Font, Text, W, H)Get text pixel dimensions.
OpenAudio(Freq, Channels, ChunkSize)Init audio mixer.
LoadMusic(Path) / PlayMusic / PauseMusic / HaltMusicBackground music control.
LoadSound(Path) / PlaySound(Sound, Channel, Loops)Sound effect playback.
SetMusicVolume / SetSoundVolume(Ch, Vol)Volume 0–128.