Overview
The game module provides the primary interface for managing the Cub3D game instance. It handles initialization of all game subsystems including entities, sprites, sounds, rendering threads, and the HUD.Functions
game_new_e()
Creates and initializes a new game instance with all required subsystems.The MLX window instance to render the game to
The parsed map data containing level layout and entity definitions
Pointer to the newly created game instance, or NULL on error
Implementation Details
The function performs initialization in three stages:- Basic Initialization - Creates hashmaps for sprites, sounds, fonts, and 3D sprites; initializes environment and minimap
- Entity Initialization - Spawns all entities from the map, initializes fonts, builds the walls matrix and billboards vector
- Finalization - Initializes HUD, window, FPS tracking, rendering threads, and background audio/sprites
Usage Example
clear_game()
Clears all dynamically allocated data within a game instance without freeing the game structure itself.Pointer to the game instance to clear (cast from t_game *)
What Gets Cleared
- All entities in the entities list
- Sprite, font, and sound hashmaps
- 3D sprites hashmap
- Camera rendering threads
- HUD stats states
- Walls matrix and billboards vector
ft_bzero() after cleanup.
This function is safe to call with NULL. It checks for NULL before proceeding.
free_game()
Completely frees a game instance including the structure itself.Pointer to the game instance to free (cast from t_game *)
Usage Example
render_game()
Renders the complete game view for a single character to a canvas.The game instance containing all render data
The image buffer to render to
The character whose perspective to render from
Rendering Pipeline
- Draws ceiling and floor colors from environment settings
- Renders the 3D camera view (raycasting, walls, entities)
- Overlays the HUD (minimap, stats, debug info, action prompts)
Usage Example
This function is typically called per-player in split-screen scenarios. Each player gets their own canvas.
render_players_game()
Renders all active players to the window with automatic split-screen layout.The game instance to render
The window to render to
Split-Screen Layouts
The function automatically handles different player counts:- 1 Player: Full screen
- 2 Players: Vertical split (left/right)
- 3 Players: Custom layout (2 top, 1 bottom center)
- 4 Players: Quad split (2x2 grid)
Dynamic Canvas Management
Player canvases are automatically:- Created on first use
- Resized when window size changes
- Repositioned when player count changes
- Updated with the current player ray count