42_cub3d/include/map.h

160 lines
4.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 13:59:04 by mcolonna #+# #+# */
/* Updated: 2024/11/12 15:10:07 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAP_H
# define MAP_H
# include "include.h"
# include "utils.h"
/// @brief The type of a case.
typedef enum e_map_wall
{
/// @brief Empty case, for '0' and ' '.
EMPTY,
/// @brief A wall, for '1'.
WALL,
} t_map_wall;
/// @brief An object of the map (player, enemies?)
typedef struct s_object
{
/// @brief Function called when creating the object.
///
/// @param data Address of the s_object.data pointer.
/// @param pos Start position of the object.
void (*init)(void **data, t_point_int pos);
/// @brief Function called when destroying the object (to avoid leaks).
///
/// @param data Address of the s_object.data pointer.
/// @param pos Start position of the object.
void (*destroy)(void **data, t_point_int pos);
/// @brief Function called each tick.
///
/// @param data Address of the s_object.data pointer.
/// @param pos Start position of the object.
void (*tick)(void **data, t_point_int pos);
/// @brief Pointer the object can use to save data.
void *data;
} t_object;
/// @brief Represents a case of the map.
typedef struct s_map_case
{
/// @brief 'true' if the case is inside of the room (false with ' ' char)
bool inside;
/// @brief Is the case empty or a wall?
t_map_wall wall;
/// @brief The object that appears on this case. If there is none,
/// every pointer of the struct will be NULL.
t_object object;
} t_map_case;
/// @brief Each element of g_map_mapping. A case and its associated char.
typedef struct s_map_mapping_element
{
/// @brief char representing the case.
char name;
/// @brief associated case.
t_map_case value;
} t_map_mapping_element;
/// @brief List of each char and its according case.
/// Ended by an element with the name '\0'.
extern const t_map_mapping_element g_map_mapping[];
typedef struct s_player
{
// pos player on map (cases)
t_point_double pos;
// player rotation (rad) (0 rad is -y / pi/2 rad is +x)
double rot;
} t_player;
typedef enum e_direction
{
NORTH,
SOUTH,
EAST,
WEST,
} t_direction;
typedef struct s_texture
{
int width;
int height;
void *image;
} t_texture;
/// @brief Represents a map.
typedef struct s_map
{
/// @brief Color of the floor.
t_color color_floor;
/// @brief Color of the ceiling.
t_color color_ceiling;
/// @brief File path for each texture. Index is t_direction.
const char *texture_srcs[4];
/// @brief All textures. Index is t_direction.
t_texture textures[4];
/// @brief Width of the map.
unsigned int width;
/// @brief Height of the map.
unsigned int height;
/// @brief An 2D array of all the cases.
///
/// Syntax to get a case: cases[y * width + x]
/// x is west to east, y is north to south.
t_map_case *cases;
/// @brief Represents the player in the map.
t_player player;
} t_map;
/// @brief Create a t_map from the content of a .cub file.
///
/// @param dest Pointer to the t_map to set.
/// @param file .cub file to use to create the t_map.
/// It must be destroyed with mapDestroy to avoid leaks.
/// @return false if an error occured, otherwise true.
bool map_from_file(t_map *dest, const char *file);
/// @brief Destroy the map to avoid leaks.
void map_destroy(t_map *map);
/// @brief Return true if the map is valid. Write an error message on stderr.
///
/// @param map The map to check.
/// @return true if the map is valid, false if not.
bool check_map(const t_map *map);
/// @brief Get a case of the map from its coordinates.
///
/// @param map Map to get the case from.
/// @param x x position of the case to return.
/// @param y y position of the case to return.
/// @return Wanted case.
const t_map_case *map_get_case(const t_map *map, int x, int y);
#endif