42_cub3d/include/map.h
mcolonna 424b9aa7b2 fixings and improvements
dev:
- add documentation
- main() is more readable

fix:
- unmanaged errors:
  - map parsing: error if color value is >255
  - map parsing: load_textures(): check return value of xml_xpm_file_to_image()
  - main.c: draw_screen(): check return value of mlx_new_image()

other details:
- dev: color_from_rgb(): change signature
- fix: nullize map in map_destroy
- change: change error message for read_all_text() errors
- change: change rotation speed
- change: add and use contant NOOBONUS_SPEED
2024-11-21 15:25:36 +01:00

141 lines
4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 13:59:04 by mcolonna #+# #+# */
/* Updated: 2024/11/19 13:25:42 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;
typedef struct s_player t_player;
/// @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 Initialize the player. NULL if there is no player on this case.
/// @param dest Will be set to the player.
/// @param pos Position of the case the player is on.
void (*init_player)(t_player *dest, t_point_int pos);
} 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.
/// If error, write an error text.
///
/// @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.
/// If error, write an error text.
///
/// @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.
/// If x or y are outside the map, undefined behaviour.
///
/// @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