The level system provides controlled progression across predefined bonus maps
while preserving the main runtime context: MLX display, window, frame loop,
hooks, retro buffers, HUD assets, and sprite infrastructure.
It allows the game to switch from one .cub level to the next without
restarting the process.
Levels / progression
Sequential bonus map reload without restarting the process
The level system loads the next predefined bonus map in place while preserving the active MLX window, hooks, frame loop, and global bonus UI context.
F4 progression simulation
Current path list is sequential: `maps/bonus/level1.cub` through `level8.cub`.
Moved resources are nulled in the temporary context to avoid double free.
// after transferring next -> app
next.map.grid = NULL;
next.tex_no = NULL;
next.tex[0].img_ptr = NULL;
// if parse/load fails before transfer:
free_app(&next);
// app remains the current valid level
03
t_bonus_levels
Progression state is small and explicit.
paths[] // ordered level files
count // registered level count
current // active level index
active // progression enabled
start_path // original CLI map path
✓
transition invariants
The system is deterministic, bounded, and safe to defend.
window and loop stay alivefailed next load keeps current levelmoved fields are nulledfree_app remains safe after transitions
At startup, bonus_levels_init(&app, argv[1]) compares the CLI map path against
the registered level paths. If the input path matches, progression mode is
activated and parsing starts from bonus_level_current_path(&app).
Path matching supports normalized relative paths and suffix matching, so inputs
such as ./maps/bonus/level1.cub still resolve correctly.
If the next level exists and load succeeds, the runtime switches level in place.
If there is no next level, or if loading fails, the current level remains valid.
bonus_load_next_level(app) builds the next level in a temporary t_app
context, preloads level-specific resources with the existing MLX context, then
transfers ownership into the live app only after the new level is ready.
This design avoids restarting the process and keeps window/hook infrastructure
intact while replacing level-owned data such as parsed config, map grid, door
state, pickup state, and wall textures.