| PSHOT-ENG — Build Your Own 3D Engine in ISO C89 | Contents | Chapter 02 | Going Further |
Build portable fixed-width types, isolate SDL 1.2 behind a thin platform boundary, upscale a 320x240 pixel buffer cleanly, and prove the loop with an animated XOR test pattern.
| Chapter: | 01 Duration: 45 min |
| Files: | src/types.h, src/platform.h, src/platform.c, src/main.c |
| Concepts: | Compile-time size checks, 0x00RRGGBB packing, SDL_Surface pitch & bit shifts, delta ticks |
| On this page: | Fixed-Width Types · Platform Contract · SDL 1.2 Backend · Test Loop · Build & Verify · Recap |
ISO C89 predates <stdint.h>. We define u8 through s32 manually and guard them with negative-array-size compile-time assertions. Our software renderer always draws into a 320x240 buffer of 0x00RRGGBB pixels, upscaled 3x to 960x720 on modern monitors.
| src/types.h (c) |
#ifndef TYPES_H
#define TYPES_H
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
typedef char assert_u8_size [sizeof(u8) == 1 ? 1 : -1];
typedef char assert_u16_size [sizeof(u16) == 2 ? 1 : -1];
typedef char assert_u32_size [sizeof(u32) == 4 ? 1 : -1];
#define FB_WIDTH 320
#define FB_HEIGHT 240
#define WIN_SCALE 3
#define PACK_RGB(r, g, b) \
((((u32)(r) & 0xFFu) << 16) | (((u32)(g) & 0xFFu) << 8) | ((u32)(b) & 0xFFu))
typedef struct {
int width;
int height;
u32 *pixels;
} Framebuffer;
typedef enum {
KEY_UP = 0,
KEY_DOWN,
KEY_LEFT,
KEY_RIGHT,
KEY_W,
KEY_A,
KEY_S,
KEY_D,
KEY_SPACE,
KEY_SHIFT,
KEY_ESC,
KEY_COUNT
} KeyCode;
typedef struct {
u8 keys[KEY_COUNT];
int mouse_dx;
int mouse_dy;
int quit;
} InputState;
#endif |
| Pitfall: Trailing Semicolons on #define Never put a semicolon at the end of a #define constant like #define WIN_SCALE 3. The C preprocessor performs blind text substitution, so a trailing semicolon expands into a syntax error inside function calls. |
Every other file in pshot talks only to platform.h. It knows nothing about SDL events, surfaces, or window managers.
| src/platform.h (c) |
#ifndef PLATFORM_H
#define PLATFORM_H
#include "types.h"
int plat_init(const char *title,
int fb_w,
int fb_h,
int scale);
void plat_shutdown(void);
void plat_poll_input(InputState *in);
void plat_present(const Framebuffer *fb);
u32 plat_get_ticks(void);
void plat_delay(u32 ms);
void plat_set_mouse_grab(int enabled);
#endif |
Inside plat_present, we inspect g_screen -> format for host RGB shifts. If the display natively uses R=16, G=8, B=0, we take a fast copy path; otherwise we repack each pixel cleanly for BGR or unusual video modes. Always advance rows using g_screen -> pitch (in bytes) rather than assuming width * 4.
| src/platform.c (c) |
#include "SDL.h"
#include "types.h"
#include <stdio.h>
#include <stdlib.h>
#include "platform.h"
static SDL_Surface *g_screen = NULL;
static int g_scale = 1;
static int map_sdl_key(SDLKey sym) {
switch (sym) {
case SDLK_UP: return KEY_UP;
case SDLK_DOWN: return KEY_DOWN;
case SDLK_LEFT: return KEY_LEFT;
case SDLK_RIGHT: return KEY_RIGHT;
case SDLK_w: return KEY_W;
case SDLK_a: return KEY_A;
case SDLK_s: return KEY_S;
case SDLK_d: return KEY_D;
case SDLK_SPACE: return KEY_SPACE;
case SDLK_LSHIFT:
case SDLK_RSHIFT: return KEY_SHIFT;
case SDLK_ESCAPE: return KEY_ESC;
default: return -1;
}
}
int plat_init(const char *title, int fb_w, int fb_h, int scale) {
int win_w;
int win_h;
if (scale < 1) {
scale = 1;
}
g_scale = scale;
win_w = fb_w * g_scale;
win_h = fb_h * g_scale;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr,
"SDL_Init FAILED...: %s\n",
SDL_GetError());
return 0;
}
g_screen = SDL_SetVideoMode(win_w, win_h, 32, SDL_SWSURFACE);
if (!g_screen) {
fprintf(stderr,
"SDL_SetVideoMode FAILED...: %s\n",
SDL_GetError());
SDL_Quit();
return 0;
}
SDL_WM_SetCaption(title, NULL);
return 1;
}
void plat_shutdown(void) {
if (g_screen) {
SDL_WM_GrabInput(SDL_GRAB_OFF);
SDL_ShowCursor(SDL_ENABLE);
g_screen = NULL;
}
SDL_Quit();
}
void plat_poll_input(InputState *in) {
SDL_Event ev;
int map;
in -> mouse_dx = 0;
in -> mouse_dy = 0;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_QUIT:
in -> quit = 1;
break;
case SDL_KEYDOWN:
map = map_sdl_key(ev.key.keysym.sym);
if (map >= 0) {
in -> keys[map] = 1;
}
if (ev.key.keysym.sym == SDLK_ESCAPE) {
in -> quit = 1;
}
break;
case SDL_KEYUP:
map = map_sdl_key(ev.key.keysym.sym);
if (map >= 0) {
in -> keys[map] = 0;
}
break;
case SDL_MOUSEMOTION:
in -> mouse_dx += ev.motion.xrel;
in -> mouse_dy += ev.motion.yrel;
break;
default:
break;
}
}
}
void plat_present(const Framebuffer *fb) {
SDL_PixelFormat *fmt;
u8 *dst;
int y;
int x;
int sy;
int sx;
int rgb;
u8 rshift;
u8 gshift;
u8 bshift;
if (!g_screen || !fb || !fb -> pixels) { return; }
if (SDL_MUSTLOCK(g_screen)) {
if (SDL_LockSurface(g_screen) < 0) { return; }
}
fmt = g_screen -> format;
rshift = fmt -> Rshift;
gshift = fmt -> Gshift;
bshift = fmt -> Bshift;
rgb = (rshift == 16 &&
gshift == 8 &&
bshift == 0);
dst = (u8 *)g_screen -> pixels;
for (y = 0; y < fb -> height; ++y) {
const u32 *src_row = fb -> pixels + (y * fb -> width);
for (sy = 0; sy < g_scale; ++sy) {
u32 *dst_row = (u32 *)(dst + ((y * g_scale + sy) * g_screen -> pitch));
if (rgb) {
for (x = 0; x < fb -> width; ++x) {
u32 px = src_row[x];
for (sx = 0; sx < g_scale; ++sx) { *dst_row++ = px; }
}
} else {
for (x = 0; x < fb -> width; ++x) {
u32 px = src_row[x];
u32 converted = (((px >> 16) & 0xFFu) << rshift) |
(((px >> 8) & 0xFFu) << gshift) |
(( px & 0xFFu) << bshift);
for (sx = 0; sx < g_scale; ++sx) {
*dst_row++ = converted;
}
}
}
}
}
if (SDL_MUSTLOCK(g_screen)) {
SDL_UnlockSurface(g_screen);
}
SDL_Flip(g_screen);
}
u32 plat_get_ticks(void) {
return (u32)SDL_GetTicks();
}
void plat_delay(u32 ms) {
SDL_Delay(ms);
}
void plat_set_mouse_grab(int enabled) {
if (enabled) {
SDL_WM_GrabInput(SDL_GRAB_ON);
SDL_ShowCursor(SDL_DISABLE);
} else {
SDL_WM_GrabInput(SDL_GRAB_OFF);
SDL_ShowCursor(SDL_ENABLE);
}
} |
Notice int main(int argc, char *argv[]). On Windows, SDL 1.2 redefines main to SDL_main; writing int main(void) will fail to link when we cross-compile in Chapter 06.
| src/main.c (c) |
#include "types.h"
#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void render_test_pattern(Framebuffer *fb, u32 ticks) {
int y;
int x;
int shift;
shift = (int)(ticks / 16u);
for (y = 0; y < fb -> height; ++y) {
u32 *row = fb -> pixels + (y * fb -> width);
for (x = 0; x < fb -> width; ++x) {
u8 r = (u8)((x + shift) & 0xFF);
u8 g = (u8)((y + shift) & 0xFF);
u8 b = (u8)(((x ^ y) + shift) & 0xFF);
row[x] = PACK_RGB(r, g, b);
}
}
}
int main(int argc, char *argv[]) {
Framebuffer fb;
InputState input;
u32 last_ticks;
u32 fps_timer;
u32 frames;
(void)argc;
(void)argv;
fb.width = FB_WIDTH;
fb.height = FB_HEIGHT;
fb.pixels = (u32 *)malloc((size_t)(fb.width * fb.height) * sizeof(u32));
if (!fb.pixels) {
fprintf(stderr, "OUT OF MEMORY ALLOCATING FRAME BUFFER...\n");
return 1;
}
if (!plat_init("PSHOT-ENG -- CH 01", fb.width, fb.height, WIN_SCALE)) {
free(fb.pixels);
return 1;
}
memset(&input, 0, sizeof(input));
last_ticks = plat_get_ticks();
fps_timer = last_ticks;
frames = 0;
while (!input.quit) {
u32 now = plat_get_ticks();
u32 dt_ms = now - last_ticks;
last_ticks = now;
(void)dt_ms;
plat_poll_input(&input);
render_test_pattern(&fb, now);
plat_present(&fb);
++frames;
if (now - fps_timer >= 1000u) {
printf("FPS: %u\n", frames);
frames = 0;
fps_timer = now;
}
}
plat_shutdown();
free(fb.pixels);
return 0;
} |
| Terminal (bash) |
make run |
You should see a crisp, chunky 960x720 window scrolling an RGB XOR pattern, with live FPS: numbers printing in the terminal once per second. Pressing Esc exits cleanly.
0x00RRGGBB array; only plat_present converts to the OS surface format.SDL_PollEvent drains every queued input event each frame and accumulates relative mouse motion into mouse_dx / mouse_dy.| « Ch 00: Setup | Table of Contents | Ch 02: 3D Math & Wireframe » |