so_long
This project introduces 2D game development using the MiniLibX library. The goal is to create a simple game where a character moves through a map, collects items, and reaches an exit to win. Below is the structure and key components of the project.
Project Overview
Aspect | Description |
---|---|
Objective | Create a 2D game where the player collects items and reaches an exit. |
Map | Represented by a .ber file with elements like walls (1 ), empty space (0 ), player (P ), collectibles (C ), and exit (E ). |
Interactions | The player moves the character using the keyboard to collect items and reach the exit. |
Requirements | Map validation, MiniLibX for graphics, and event handling. |
Learning Outcomes | 2D graphics, event management, data validation, and C project organization. |
Project Structure
File Organization
File | Description |
---|---|
so_long.c | Main program function. |
map_validation.c | Contains map validation functions (rectangular shape, walled borders, etc.). |
graphics.c | Handles graphics rendering using MiniLibX. |
events.c | Manages events like key presses and window closing. |
player.c | Controls character movement and interactions. |
utils.c | Helper functions for memory management, error handling, etc. |
MiniLibX: Graphics Library
MiniLibX is a lightweight library for creating 2D graphics and handling events in C. Below are its main components:
1. Initialization
Function | Description |
---|---|
mlx_init() | Initializes MiniLibX and returns a pointer to the graphics connection. |
mlx_new_window() | Creates a new graphics window with specified size and title. |
Example:
void *mlx_ptr = mlx_init();
void *win_ptr = mlx_new_window(mlx_ptr, 800, 600, "so_long");
2. Colors
Format | Description |
---|---|
0xRRGGBB | Hexadecimal format for defining colors (8 bits per channel). |
Examples | 0xFFFFFF (White), 0xFF0000 (Red), 0x00FF00 (Green), 0x0000FF (Blue). |
Function:
mlx_pixel_put(mlx_ptr, win_ptr, 100, 100, 0x00FF00); // Draws a green pixel at (100, 100)
3. Graphics Drawing
Function | Description |
---|---|
mlx_new_image() | Creates an in-memory image for efficient pixel drawing. |
mlx_get_data_addr() | Gets a pointer to image data for pixel manipulation. |
mlx_put_image_to_window() | Displays the image in the window. |
Example:
void *img = mlx_new_image(mlx_ptr, 800, 600);
char *data = mlx_get_data_addr(img, &bpp, &size_line, &endian);
// Draw a red pixel at (100, 50)
int x = 100, y = 50;
int color = 0xFF0000;
*(int *)(data + (y * size_line + x * (bpp / 8))) = color;
mlx_put_image_to_window(mlx_ptr, win_ptr, img, 0, 0);
4. Image Loading
Function | Description |
---|---|
mlx_xpm_file_to_image() | Loads an .xpm image from file. |
mlx_put_image_to_window() | Displays the image in the window. |
Example:
void *img = mlx_xpm_file_to_image(mlx_ptr, "sprite.xpm", &width, &height);
mlx_put_image_to_window(mlx_ptr, win_ptr, img, 100, 100);
5. Event Handling
Function | Description |
---|---|
mlx_key_hook() | Handles keyboard events. |
mlx_mouse_hook() | Handles mouse events. |
mlx_hook() | Handles general events (window closing, etc.). |
mlx_loop() | Starts the main loop to keep the window open and process events. |
Example:
int handle_keypress(int keycode, void *param)
{
if (keycode == 53) // ESC key on macOS
exit(0);
return (0);
}
mlx_key_hook(win_ptr, handle_keypress, NULL);
mlx_loop(mlx_ptr);
6. Clean Exit
Function | Description |
---|---|
mlx_destroy_window() | Closes and frees window memory. |
mlx_destroy_image() | Frees memory associated with an image. |
Example:
mlx_destroy_window(mlx_ptr, win_ptr);
mlx_destroy_image(mlx_ptr, img_ptr);
7. Text Rendering
Function | Description |
---|---|
mlx_string_put() | Draws text on the window. |
Example:
mlx_string_put(mlx_ptr, win_ptr, 50, 50, 0xFFFFFF, "Hello, so_long!");
Map Validation
The map must follow these rules:
- Rectangular format: All rows must have equal length.
- Walled perimeter: Map borders must consist of walls (
1
). - Required elements:
- One player (
P
). - One exit (
E
). - At least one collectible (
C
).
- One player (
- Valid path: There must be a path from the player to the exit passing through all collectibles.
Example Map
1111111111
100100E001
101100C001
10P0010001
1111111111
Execution Flow
- Load map: Read the
.ber
file and validate its structure. - Initialize graphics: Create the window and load images (sprites, backgrounds, etc.).
- Handle events: Capture key presses and move the character.
- Update game: Check if the player has collected all items and reached the exit.
- Close game: Free resources and exit properly.