Introduction
The Pacman game is built using C with SDL 1.2 for graphics rendering. The architecture follows a modular design with clear separation between graphics, game logic, sprite management, and input handling.Core modules
The codebase is organized into distinct functional modules:Main entry point
src/main.c - Application lifecycle management- Command-line argument parsing (
-ffor fullscreen,-wfor windowed) - Resolution backup and restoration using GLFW and xrandr
- Game initialization and cleanup orchestration
Graphics engine
src/gfx.c - SDL graphics subsystem- SDL initialization and video mode setup (320x200x32)
- Screen buffer operations and double-buffering
- Image loading and blitting
- Timer management for game loop synchronization
Sprite management
src/sprites.c - Sprite definitions and loading- Static sprite data for all game characters
- Sprite coordinate definitions from sprite sheet
- Number rendering sprites (0-9)
getsprites()- Loads all sprites from data/sprites.bmp- Array management for maze layout
- Structure initialization for game entities
Game logic
src/mov_fig.c - Character movement and rendering- Pacman movement with smooth interpolation
- Ghost rendering with state-based sprite selection
- Collision detection between Pacman and ghosts
- Ghost movement decision algorithm
- Target position calculation
- Pathfinding with randomization
- Raw sprite data for four ghost colors (red, blue, yellow, gray)
- Sprite data for scared and dead states
Input and menu
src/teclado.c / misc.c:teclado() - Keyboard input handling- Arrow key detection and processing
- Pacman direction control
- Game state updates based on input
- Options menu rendering
- Game start/restart logic
Data structures
The game uses two primary structures defined in src/include/misc.h:- struct pcman
- struct fantasmas
Data flow
The game follows a traditional initialization -> game loop -> cleanup pattern:Initialization phase
From src/main.c:160-196:Game loop
The main game loop is in misc.c:teclado() (lines 506-914): Key functions called per frame:- Input processing - SDL keyboard state polling
- Ghost AI -
optener_movimientos_fantasmas()calculates ghost targets - Movement -
mueve_figuras()updates positions and renders sprites - Collision detection - Checks Pacman vs Ghost collisions in mov_fig.c:191
- Rendering - Blits background buffer to screen surface
Cleanup phase
From src/main.c:193:atexit(SDL_Quit) handler registered in gfx.c:27 ensures SDL cleanup.
Key constants
Defined in src/include/defines.h:Screen width in pixels
Screen height in pixels
Color depth in bits per pixel
Maze array width (including borders)
Maze array height
Grid cell size in pixels
Number of ghosts in the game
Total dots to collect per level
Memory layout
Background buffer
From src/main.c:22:Maze array
From src/misc.c:17-45, the maze is a static 32x26 integer array representing:C(1) - Circuit/walkable path with dotN(2) - Nothing/walkable path without dotJ(3) - Path already walkedP(4) - Door (ghost house entrance)B(5) - Power pellet
The maze array uses value 0 for walls and non-walkable areas. The collision detection in movefant.c uses
truex() and truey() helper functions to check if a position is walkable.Performance considerations
Frame timing
The game uses a fixed 18 Hz timer (src/main.c:188) for game logic updates, while rendering runs at 50 FPS (FRAMES_PER_SEC in gfx.h:2).Smooth movement
Character movement uses incremental pixel offsets with velocity arrays:- velocidades[14] - Ghost speed per level (0.4 to 0.7, defined in misc.c:140-155)
- inc_velocidad_pc - Pacman speed (0.5, misc.c:173)
Sprite rendering
Theputico() function (gfx.c:142-152) uses memcpy() for fast sprite blitting:
Build system
The project uses a Makefile to compile all source files and link against SDL libraries:- SDL 1.2
- SDL_image
- GLFW3 (for monitor detection)
Compiler flags
Compiler flags
Typical compilation requires:
-lSDL-lSDL_image-lglfw-lm(math library)