Structures Overview - 1Fr3aK2/Cub3d GitHub Wiki

🗂️ Structures Overview

This section defines constants and structures used to represent the 2D map, textures, player information, MLX (MiniLibX) graphics context, and the main game data management system.


⚙️ Constants

Constant Type Description
WIN_W int Default window width (800).
WIN_H int Default window height (600).
MOVE_SPEED float Player movement speed (0.1).
ROT_SPEED float Player rotation speed (0.05).
VALID_CHARS char* String containing all valid characters in the map: walls ('1'), empty spaces ('0'), and player orientations ('N', 'S', 'E', 'W').
WALL char Character representing a wall ('1').
SPACE char Character representing empty space ('0').
ORIENTATION char* String containing all possible player orientations ("NSEW").
NORTH char Character representing north orientation ('N').
SOUTH char Character representing south orientation ('S').
WEST char Character representing west orientation ('W').
EAST char Character representing east orientation ('E').
PLAYER char* String containing all player orientation characters ("NSWE").
PI float Value of Pi ('3.1415926535').

🧩 Structures

🗃️ t_file

Stores file-related information, primarily used to manage file descriptors for map input.

Field Type Description
fd int File descriptor used to read the map or configuration files.

🖼️ t_img

Represents an image resource in the MLX graphics system, including its pointer and dimensions.

Field Type Description
img void* Pointer to the MLX image resource.
width int Image width in pixels.
height int Image height in pixels.

💻 t_mlx

Encapsulates all MLX (MiniLibX) graphical context elements, such as the MLX instance, window, and main image buffer.

Field Type Description
mlx void* Pointer to the MLX instance (connection to the X11 display).
win void* Pointer to the main game window.
img t_img Structure containing image data (used for rendering).

🗺️ t_map

Represents the game map and its associated texture paths.

Field Type Description
buffer char** Temporary buffer storing the raw map lines read from the file.
map char** Final processed map as a 2D array of characters.
temp_map char** Temporary storage used during map validation or conversion.
north char* Path to the north wall texture.
south char* Path to the south wall texture.
west char* Path to the west wall texture.
east char* Path to the east wall texture.
height int Number of rows in the map grid.
rgb_ceiling t_rgb Rgb color of the ceiling.
rgb_floor t_rgb Rgb color of the floor.

🎮 t_player

Stores the player’s position, movement direction, and camera plane for raycasting and rendering.

Field Type Description
x float Player’s horizontal position in the map (centered in the tile).
y float Player’s vertical position in the map (centered in the tile).
dir_x float X component of the direction vector indicating where the player is facing.
dir_y float Y component of the direction vector indicating where the player is facing.
plane_x float X component of the camera plane vector, defining the field of view.

The direction vector (dir_x, dir_y) determines the player's facing direction, while the plane vector (plane_x) defines the camera projection used for rendering.


🧱 t_data

The main structure that aggregates all core game data — including MLX context, player, map, and file information — for centralized management.

Field Type Description
img t_img Image buffer used for rendering.
mlx t_mlx MLX structure managing the window and display connection.
player t_player Player structure containing position and direction data.
map t_map Map structure holding layout and texture information.
file t_file File structure managing the map file descriptor.

This structure simplifies function calls by grouping all major components (graphics, player, map, and file) into a single entity.


📖 Description

These constants and structures form the foundation of the 2D rendering engine:

  • 🧱 t_map defines the layout, textures, and structure of the world.
  • 🎮 t_player defines the player’s position, direction, and camera projection for raycasting.
  • 💻 t_mlx manages the MLX connection, window, and image buffer for rendering.
  • 🗃️ t_file handles file operations for loading map data.
  • 🧩 t_data unifies all subsystems into a single, centralized structure for efficient access and management.

💡 Example Usage

t_file file;
file.fd = open("map.cub", O_RDONLY);

t_map map = {0};
map.north = map.south = map.west = map.east = NULL;
map.floor = map.ceiling = NULL;

t_player player = {0};
player.x = 5.5;
player.y = 3.5;
player.dir_x = 0;
player.dir_y = -1;
player.plane_x = 0.66;

t_mlx mlx = {0};
mlx.mlx = mlx_init();
mlx.win = mlx_new_window(mlx.mlx, WIN_W, WIN_H, "Cub3D");

t_data data = {0};
data.file = file;
data.map = map;
data.player = player;
data.mlx = mlx;

printf("Window size: %dx%d\n", WIN_W, WIN_H);
printf("Player position: (%.2f, %.2f)\n", data.player.x, data.player.y);