# ricky0625/so_long architecture

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

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

## Overview

##### Main Components

- Game Entry Point & Main Loop:
- File: srcs/so_long.c
- This is the starting point that initializes the game, loads the map, and kicks off the game loop.

- Initialization & Setup:
- Directory: srcs/init
- Functions such as add_entity.c, add_object.c, and vector_helper.c configure game entities and objects, and load assets.

- & Level Management:
- Directory: srcs/map
- Modules like map_aesthetic.c, map_format.c, map_pathfinder.c, and map_validator.c are responsible for loading, validating, and processing the map data (from .ber files in the maps folder).

- Rendering & Drawing Subsystem:
- This subsystem (draw_base.c, draw_map.c, draw_entity.c, update_frame.c, etc.) handles assembling the game frame using sprites (assets in various folders) and outputs via the MLX library functions (from the mlx folder).

- Game Mechanisms & Logic:
- Directory: srcs/mechanism
- Contains key gameplay logic like player and enemy (skeleton, ghost) behavior, collisions, and event handling via files such as player_mechanism.c and skeleton_mechanism.c.

- Resource Management & Cleanup:
- Directory: srcs/free
- Manages memory deallocation, freeing up objects upon game exit.

- External Libraries & Utilities:
- Directories: lib42 (including ft_printf, get_next_line, libft)
- These libraries provide generic utilities like string manipulation, formatted printing, and input functions which support the core game logic.

- Assets & Sprites:
- Directory: assets
- Organized into subfolders (player, ghost, skeleton, key, wall, etc.) containing XPM images used for game textures.

- Files:
- Directory: maps
- Contains playable maps and map test files with a .ber extension defining level layouts.

##### Relationships & Interactions

- Game Initialization:
- so_long.c calls initialization routines in srcs/init to set up game entities and load the map (from maps folder) and assets.
- The map is parsed and validated in srcs/map before gameplay starts.

- Game Loop & Rendering:

- Game Mechanics:
- Input events and timer events trigger game mechanics in srcs/mechanism, where the player’s movements and enemy behaviors are re-evaluated.
- The outcome of these mechanisms (e.g., collision detection, collectible gathering) directly affects the state updates in the game loop.

- External Libraries:
- The lib42 utilities (ft_printf for logging/debug printing, get_next_line for file reading, and libft for common C operations) underpin many of the routines across initialization, map loading, and gameplay.

- Resource Management:
- When the game ends or on error conditions, routines in srcs/free are invoked to clean up memory (including dynamic allocations related to game logic and asset management).

##### Architectural Patterns & Design Principles

- Modular Design:
- The project is clearly divided into modules (initialization, map processing, drawing, and game mechanics) each handling a defined piece of functionality.

- Separation of Concerns:
- Different directories focus on distinct responsibilities (e.g., map validation is separated from drawing functions).

- Utilization of External Libraries:
- The design leverages MLX for graphics and external lib42 components for helper functionality, keeping the game logic lean and modular.

- Triggers initialization, game loop, input event registrations.

- Sets up entities (player, ghosts, skeletons, collectibles) and configures game data structures.

- Reads and processes .ber files, validates structure and paths, integrates with assets.

- Represents the assets folder and how different sprite groups (player, walls, ghosts, keys, etc.) are organized and loaded into memory.

- Handles frame building with functions like draw_map, draw_entity, and update_frame.

- Manages player controls, enemy movements, collision detection, in-game interactions.

- Deals with memory cleanup and safe game termination.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Entry["Game Entry Point (so_long.c)"]:::core
    Init["Initialization Module (srcs/init)"]:::core
    MapLoader["Map Loader & Validator (srcs/map)"]:::core
    Assets["Asset Manager (assets)"]:::core
    MapFiles["Map Files (maps)"]:::asset
    Rendering["Rendering Engine (srcs/draw)"]:::core
    Mechanism["Game Mechanisms (srcs/mechanism)"]:::core
    Cleanup["Resource Management (srcs/free)"]:::core
    MLX["MLX (Minilibx)"]:::external
    Lib42["lib42"]:::external

    %% Main Flow
    Entry -->|"init_game"| Init
    Init -->|"load&validate_map"| MapLoader
    MapLoader -->|"load_assets"| Assets
    Assets -->|"build_frame"| Rendering

    %% Map Files Input
    MapFiles -->|"maps_input"| MapLoader

    %% Game Loop & Mechanisms
    Entry -->|"register_input_events"| Mechanism
    Mechanism -->|"update_state"| Rendering

    %% External Libraries Dependencies
    Init ---|"util_functions"| Lib42
    MapLoader ---|"util_functions"| Lib42
    Mechanism ---|"util_functions"| Lib42
    Rendering -->|"graphics_output"| MLX

    %% Cleanup Operations
    Entry -->|"on_exit"| Cleanup
    Rendering -->|"on_exit"| Cleanup

    %% Click Events
    click Entry "https://github.com/ricky0625/so_long/blob/main/srcs/so_long.c"
    click Init "https://github.com/ricky0625/so_long/tree/main/srcs/init"
    click MapLoader "https://github.com/ricky0625/so_long/tree/main/srcs/map"
    click Assets "https://github.com/ricky0625/so_long/tree/main/assets"
    click MapFiles "https://github.com/ricky0625/so_long/tree/main/maps"
    click Rendering "https://github.com/ricky0625/so_long/tree/main/srcs/draw"
    click Mechanism "https://github.com/ricky0625/so_long/tree/main/srcs/mechanism"
    click Cleanup "https://github.com/ricky0625/so_long/tree/main/srcs/free"
    click Lib42 "https://github.com/ricky0625/so_long/tree/main/lib42"
    click MLX "https://github.com/ricky0625/so_long/tree/main/mlx"

    %% Styles
    classDef core fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef external fill:#ccffcc,stroke:#008800,stroke-width:2px;
    classDef asset fill:#ccccff,stroke:#0000cc,stroke-width:2px;
```
