Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created February 11, 2026 21:26
Show Gist options
  • Select an option

  • Save peterhellberg/62b32eebdda5336a4f40ef1c7cc410b8 to your computer and use it in GitHub Desktop.

Select an option

Save peterhellberg/62b32eebdda5336a4f40ef1c7cc410b8 to your computer and use it in GitHub Desktop.
CC ?= zig cc
CFLAGS ?= -std=c11 -Wall -Wextra `sdl2-config --cflags`
LDFLAGS ?= `sdl2-config --libs`
TARGET = sdl-minimal
all: $(TARGET)
$(TARGET): sdl-minimal.c
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
clean:
rm -f $(TARGET)
#include <SDL2/SDL.h>
#include <SDL2/SDL_hints.h>
#include <SDL2/SDL_video.h>
#include <stdbool.h>
const int WIDTH = 96 * 3;
const int HEIGHT = 54 * 3;
const int radius = 20;
const int speed = 6;
int x = WIDTH / 2;
int y = HEIGHT / 2;
bool running = true;
SDL_Event e;
static void draw_circle(SDL_Renderer *r, int cx, int cy, int radius) {
for (int y = -radius; y <= radius; y++) {
for (int x = -radius; x <= radius; x++) {
if (x * x + y * y <= radius * radius) {
SDL_RenderDrawPoint(r, cx + x, cy + y);
}
}
}
}
static void draw(SDL_Renderer *r) {
int cx = x;
int cy = y;
SDL_SetRenderDrawColor(r, 0, 0, 0, 255);
SDL_RenderClear(r);
SDL_SetRenderDrawColor(r, 255, 100, 0, 255);
SDL_RenderDrawLine(r, cx, cy, 0, 0);
SDL_RenderDrawLine(r, cx, cy, 0, HEIGHT);
SDL_RenderDrawLine(r, cx, cy, WIDTH, 0);
SDL_RenderDrawLine(r, cx, cy, WIDTH, HEIGHT);
SDL_SetRenderDrawColor(r, 255, 255, 255, 255);
draw_circle(r, cx, cy, radius);
}
void update() {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
running = false;
} else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_z || e.key.keysym.sym == SDLK_ESCAPE) {
running = false;
}
}
}
const Uint8 *keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_LEFT])
x -= speed;
if (keys[SDL_SCANCODE_RIGHT])
x += speed;
if (keys[SDL_SCANCODE_UP])
y -= speed;
if (keys[SDL_SCANCODE_DOWN])
y += speed;
}
int main(void) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("SDL minimal", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT,
SDL_WINDOW_FULLSCREEN);
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_RenderSetLogicalSize(renderer, WIDTH, HEIGHT);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
while (running) {
update();
draw(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(16);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment