← Back to blog
Wolfenstein · · DevDad

Wolfenstein GX in motion

Wolfenstein GX in animated clips: the splash screen, iron bars you can see through, dynamic lighting, rocket light, the boss fight and the death screen. The code is in the longer post.

showcase wolfenstein raylib gifs highlights

The technical breakdown of Wolfenstein GX is long. This one is short: the trailer, a few seconds of animation per feature. The longer post has the code for every effect. This one shows what the game looks like before you read 4,000 words about RGB lightmaps and BFS pathfinding.

All of it is one .gx file of about 6,200 lines, compiled through C, with raylib for graphics. It runs at 60 FPS natively and in the browser.


The opening

Splash screen

The splash screen is its own small render pass: an animated background, music, and “PRESS ANY KEY TO CONTINUE” placed with a percentage margin so it doesn’t get cut off in oddly scaled browser windows. Title screens are usually an afterthought. In a single-file game the title screen is one state of the same state machine that runs the game. STATE_MENU returns before the gameplay input handler, so the splash can use the digit keys (1/2/3 for quality) without clashing with the weapon keys later.


Light through an opening door

Door light

Watch the corridor when the door opens. It gets brighter, because the torch on the other side now reaches cells it could not reach before.

There is no tint or particle trick involved. The lightmap is an RGB value per cell, recomputed at the moment the door passes 50% of its opening animation. While the door is closed the light cannot see those cells; once it is open, it can. The check is one line:

var now_blocking = (g_door_timer[i] < 0.5)
if (was_blocking != now_blocking) { lightmap_dirty = true }

That is two recomputes per door cycle, one when it opens past half and one when it closes past half. The whole feature is about 20 lines, and the light changes at the moment your eye expects it to.


See-through iron bars

Iron bars

This is my favorite trick in the project. Iron bars are a door type that does not stop the ray. The DDA raycaster hits the bars, notes in a per-column buffer that there are bars there, and carries on to the wall behind. A separate pass then draws the bars on top with alpha-cut transparency.

So you can see through the bars into the alcove behind them. The goal sits there under its own torch, visible from across the level, which shows the player where to go without any special case in the engine. It costs five lines in cast_ray() and one overlay pass.


Soldiers that flank you

Combat

Soldiers run a 7-state machine (IDLE → TARGET → SHOOT → RUN → HURT → DYING → DEAD). When chasing, they pick one of two modes each frame:

  • If they can see the player, they zigzag straight at them.
  • If they lost sight, they run BFS over the 32×32 grid to the last known position, take the first step, and plan again every 0.5 s.

The whole AI (seeing, planning, moving, opening doors, getting unstuck) is about 80 lines of GX. It runs at 60 FPS with dozens of enemies because every cost has a limit. BFS is linear in the cells of a 1024-cell map, the 0.5 s timer spreads the replanning across enemies, and three i32[1024] scratch arrays hold all the working data without allocating.

The pathfinder calls enemy_try_open_door() on each waypoint, so the door is already opening when the soldier gets there, and the chase doesn’t stutter at every doorway.


Knife

Knife

There are three weapons, and the knife is the fun one. Press 2 to equip it and left-click to stab. You can also press V with any weapon: the game switches to the knife, plays the whole slash (idle, raise, stab, recoil), hits whatever is in front of you, and switches back to the weapon you had.

All of it goes through one start_knife_attack() helper. Equipping, switching and the V stab are the same code, with a different weapon to return to.


A rocket that lights the corridor

RPG light

A rocket flying down a dark corridor lights the walls as it passes. Nothing is precomputed. The rocket owns a slot in the dynamic light pool, with radius 2.5 and color (0.9, 0.85, 0.7), and moves that slot with it every frame:

if (g_rocket_active[i]) {
    if (g_rocket_dyn_id[i] < 0) { g_rocket_dyn_id[i] = dyn_light_acquire() }
    // write current x/y/intensity/color to that slot
} else if (g_rocket_dyn_id[i] >= 0) {
    dyn_light_release(g_rocket_dyn_id[i]); g_rocket_dyn_id[i] = -1
}

Acquire and release mirror each other and are safe to call twice, so a light cannot leak. Rockets, exploding barrels and the boss’s fire all use the same pool. Giving anything in the game its own moving light takes two function calls.


Explosions

Explosion

Barrels can be shot, and rockets explode on impact. The explosion sprite is drawn in plain WHITE, because it is its own light source and should not be darkened by the lightmap. What makes it hit hard is the dynamic light at the impact point: intensity flickering from 1 down to 0 over the explosion’s lifetime, color (1.0, 0.6, 0.2), radius 4.0.

For a moment the walls turn orange, the soldier in the doorway is lit from behind, and then it fades. It goes through the same sample_light() and the same lightmap as everything else. The explosion needed no renderer of its own; it registers a dynamic light and the existing pipeline does the rest.


The boss

Boss fight

The boss has a 9-state machine, including a death in three stages: the head explodes, the body staggers, then it collapses, each with its own timing and look. In its armored states (BOSS_PREPARE, BOSS_SHOOT) its sprite gets a tint that ignores the world lighting, so it pulses brightly through the shading.

When its health drops below a threshold, the boss registers a flickering fire at its feet, color (1.0, 0.4, 0.1), flickering at 0.5 + 0.1·sin(timer·6). The lighting around it turns orange while it dies. It is the only enemy you can notice through the lighting, so you can tell where it is before you turn the corner.


You died

You died

Every source of damage (bullets, explosions, fire, melee) goes through one function:

fn hit_player(damage: f32) {
    if (g_player_dead) { return }
    g_player_hp -= damage
    if (g_pain_cooldown <= 0.0) { play_player_pain(); g_pain_cooldown = 0.4 }
    if (g_player_hp <= 0) {
        g_player_dead = true; g_death_timer = 3.0
        show_hud_msg("YOU DIED")
    }
}

The 400 ms cooldown stops continuous damage, like standing in fire, from playing the pain sound every frame. Damage is an f32 so that small per-frame amounts add up instead of truncating to zero. One function owns the health math, the death transition and the sound. Moving everything into it fixed a real bug: fire damage used to be subtracted inline with no death check, and the player could keep burning below 0 HP forever.

The “YOU DIED” overlay fades in over the death timer, then the state machine goes back to the menu. No damage source needs special code.


Try it

The game runs in the browser on the Showcase page. Click the canvas to capture the mouse. WASD and Shift move, the mouse looks around, Esc releases the mouse, 1/2/3 switch weapons, V stabs, and F2/F3/F4 set the quality.

The source is projects/wolfenstein.gx in the GX repo, and Building Wolfenstein in 7,000 lines of GX goes through every system.

DOOM is next, and that post will be longer.