# zenix-s/so_long architecture

> Architecture diagram of the GitHub repository zenix-s/so_long, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zenix-s/so_long
- Repository: https://github.com/zenix-s/so_long

## Overview

##### Project Type & Purpose
- It’s a game where a player navigates a map to collect items and exit the level.
- The project manages game state (player, map, collectibles, exit), rendering (via MLX42), input (player movement and hooks), and validation (map validation and path checking).

##### Analyzing the File Structure
- Top-Level Directories:
- Contains header files for different components, such as collectibles, exit, map, player, map_render, and shared utilities.
- lib: Contains external libraries (MLX42 for graphics and libft for common helper functions).
- maps: Contains sample map files with a .ber extension along with several error scenario maps.
- src: Contains the source code organized into subdirectories that mirror the modular structure (collectibles, exit, map, map_render, player, shared, etc.).
- textures: Likely stores image assets to be rendered.
- Key elements to note:
- There are distinct directories for initialization (init_game.c, init_mlx.c), error handling (ft_error.c), rendering (render_map_render.c, render_player.c, etc.), and input (player_hook.c).
- Validation routines are clearly separated under src/map_validation and src/map/path_validation, reflecting careful input and state verification.

##### Breaking Down the Architecture
- Main Entry Point:
- so_long.c: This is the central entry that brings together game initialization, the main loop, and cleanup.
- Game Initialization and Setup:
- Initialization functions (init_game.c, init_mlx.c, and individual component initializations within their respective directories).
- Loading from the maps directory and validation routines ensure the game level is correctly set up.
- Components:
- Module: Manages the game level layout, including reading the map file and validating walls, borders, and allowed characters.
- Player Module: Manages the player's state, movement, hooks for user input, and rendering.
- Collectibles Module: Responsible for initializing, tracking, and rendering collectible items.
- Exit Module: Manages exit placement, state (open/closed), and rendering.
- Shared Utilities:
- There are several “shared” subdirectories that contain utilities such as error handling (ft_error.c), shared definitions (collectibles_shared.h, map_shared.h), and other helper functions.
- External Dependencies:
- MLX42 is used for all graphical rendering (initializing the MLX window, rendering images, managing events).
- libft provides additional functions that support game logic.

##### Be Detailed Yet Concise
- The goal is to identify all the major elements and their interactions. Do not omit the smaller but critical shared utilities as they play a central role in error handling and common functions.

## Diagram (Mermaid)

```mermaid
flowchart TD 
    %% Global Components
    A["Game Core (so_long.c)"]:::core
    B["Game Initialization Module"]:::init
    A -->|"init"| B

    %% Map Module Subgraph
    subgraph "Map Module"
        direction TB
        C1["Map Source (init_map & free_map)"]:::module
        C2["Map Validation (map_validation & path_validation)"]:::module
        C3["Map Headers"]:::module
    end

    %% Player Module Subgraph
    subgraph "Player Module"
        direction TB
        D1["Player Source (init, movement, render, hook, free)"]:::module
        D2["Player Headers"]:::module
    end

    %% Collectibles Module Subgraph
    subgraph "Collectibles Module"
        direction TB
        E1["Collectibles Source (init, collect, render, free)"]:::module
        E2["Collectibles Headers"]:::module
    end

    %% Exit Module Subgraph
    subgraph "Exit Module"
        direction TB
        F1["Exit Source (init, open, render, free)"]:::module
        F2["Exit Headers"]:::module
    end

    %% Rendering Module Subgraph
    subgraph "Rendering Module"
        direction TB
        G1["Map Render Source (init, render, free)"]:::module
        G2["Map Render Header"]:::module
    end

    %% Shared Utilities Subgraph
    subgraph "Shared Utilities"
        direction TB
        H1["Shared Source (ft_error, all_collected, print_layout, update_text, gen_rand)"]:::util
        H2["Shared Headers"]:::util
    end

    %% External Libraries Subgraph
    subgraph "External Libraries"
        direction TB
        I1["MLX42"]:::external
        I2["libft"]:::external
    end

    %% Relationships from Game Initialization
    B -->|"load_map"| C1
    B -->|"init_player"| D1
    B -->|"init_collectibles"| E1
    B -->|"init_exit"| F1
    B -->|"start_game_loop"| G1
    B -->|"init_MLX"| I1
    B -->|"init_libft"| I2

    %% Map Module internal relationships
    C1 -->|"validate_map"| C2
    C1 -->|"header_link"| C3

    %% Player Module internal relationships
    D1 -->|"uses"| D2

    %% Collectibles Module internal relationships
    E1 -->|"uses"| E2

    %% Exit Module internal relationships
    F1 -->|"uses"| F2

    %% Modules feed into Rendering Module
    C1 -->|"render"| G1
    D1 -->|"render"| G1
    E1 -->|"render"| G1
    F1 -->|"render"| G1

    %% Rendering Module uses External Library MLX42 for drawing
    G1 -->|"draw_with"| I1

    %% Shared Utilities used by modules
    C1 -->|"error_util"| H1
    D1 -->|"error_util"| H1
    E1 -->|"error_util"| H1
    F1 -->|"error_util"| H1
    G1 -->|"error_util"| H1

    %% Click Events for Components
    click A "https://github.com/zenix-s/so_long/blob/main/src/so_long.c"
    click B "https://github.com/zenix-s/so_long/blob/main/src/init_game.c"
    click B "https://github.com/zenix-s/so_long/blob/main/src/init_mlx.c"
    click C1 "https://github.com/zenix-s/so_long/blob/main/src/map/init_map.c"
    click C1 "https://github.com/zenix-s/so_long/blob/main/src/map/free_map.c"
    click C2 "https://github.com/zenix-s/so_long/tree/main/src/map/map_validation"
    click C2 "https://github.com/zenix-s/so_long/blob/main/src/map/path_validation/validate_path.c"
    click C3 "https://github.com/zenix-s/so_long/blob/main/include/map/map.h"
    click C3 "https://github.com/zenix-s/so_long/blob/main/include/map/map_private.h"
    click D1 "https://github.com/zenix-s/so_long/blob/main/src/player/init_player.c"
    click D1 "https://github.com/zenix-s/so_long/blob/main/src/player/player_movement.c"
    click D1 "https://github.com/zenix-s/so_long/blob/main/src/player/render_player.c"
    click D1 "https://github.com/zenix-s/so_long/blob/main/src/player/hook/player_hook.c"
    click D1 "https://github.com/zenix-s/so_long/blob/main/src/player/free_player.c"
    click D2 "https://github.com/zenix-s/so_long/blob/main/include/player/player.h"
    click D2 "https://github.com/zenix-s/so_long/blob/main/include/player/player_private.h"
    click E1 "https://github.com/zenix-s/so_long/blob/main/src/collectibles/init_collectibles.c"
    click E1 "https://github.com/zenix-s/so_long/blob/main/src/collectibles/collect_collectible.c"
    click E1 "https://github.com/zenix-s/so_long/blob/main/src/collectibles/render_collectibles.c"
    click E1 "https://github.com/zenix-s/so_long/blob/main/src/collectibles/free_collectibles.c"
    click E2 "https://github.com/zenix-s/so_long/blob/main/include/collectibles/collectibles.h"
    click F1 "https://github.com/zenix-s/so_long/blob/main/src/exit/init_exit.c"
    click F1 "https://github.com/zenix-s/so_long/blob/main/src/exit/open_exit.c"
    click F1 "https://github.com/zenix-s/so_long/blob/main/src/exit/render_exit.c"
    click F1 "https://github.com/zenix-s/so_long/blob/main/src/exit/free_exit.c"
    click F2 "https://github.com/zenix-s/so_long/blob/main/include/exit/exit.h"
    click G1 "https://github.com/zenix-s/so_long/blob/main/src/map_render/init_map_render.c"
    click G1 "https://github.com/zenix-s/so_long/blob/main/src/map_render/render_map_render.c"
    click G1 "https://github.com/zenix-s/so_long/blob/main/src/map_render/free_map_render.c"
    click G2 "https://github.com/zenix-s/so_long/blob/main/include/map_reder/map_render.h"
    click H1 "https://github.com/zenix-s/so_long/blob/main/src/shared/ft_error.c"
    click H1 "https://github.com/zenix-s/so_long/blob/main/src/shared/collectibles/all_collected.c"
    click H1 "https://github.com/zenix-s/so_long/blob/main/src/shared/map/print_layout.c"
    click H1 "https://github.com/zenix-s/so_long/blob/main/src/shared/moves_text/update_text.c"
    click H1 "https://github.com/zenix-s/so_long/blob/main/src/shared/utils/gen_rand.c"
    click H2 "https://github.com/zenix-s/so_long/blob/main/include/shared.h"
    click H2 "https://github.com/zenix-s/so_long/blob/main/include/shared/collectibles_shared.h"
    click H2 "https://github.com/zenix-s/so_long/blob/main/include/shared/map_shared.h"
    click H2 "https://github.com/zenix-s/so_long/blob/main/include/utils/utils.h"
    click I1 "https://github.com/zenix-s/so_long/tree/main/lib/MLX42"
    click I2 "https://github.com/zenix-s/so_long/tree/main/lib/libft"

    %% Styles
    classDef core fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef init fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef module fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef util fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef external fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
```
