How ricky0625/so_long works
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.