add map parsing and other things

This commit is contained in:
mcolonna 2024-10-04 15:32:12 +02:00
parent f04587cd62
commit 5bad24dcee
24 changed files with 974 additions and 15 deletions

30
include/color.h Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* color.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:25:50 by mcolonna #+# #+# */
/* Updated: 2024/10/02 15:41:43 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef COLOR_H
# define COLOR_H
# include <linux/types.h>
// TODO Must the transparency be 0 or 255?
/// @brief Represents an TRGB color in 0xTTRRGGBB format.
typedef __u32 t_color;
/// @brief Convert a color from each color value to a t_color.
///
/// @param red Level of red from 0 to 255.
/// @param green Level of green from 0 to 255.
/// @param blue Level of blue from 0 to 255.
/// @return The result.
t_color color_from_rgb(int red, int green, int blue);
#endif

View file

@ -6,13 +6,16 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 13:59:04 by mcolonna #+# #+# */
/* Updated: 2024/10/01 14:00:40 by mcolonna ### ########.fr */
/* Updated: 2024/10/04 13:13:04 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAP_H
# define MAP_H
# include <stdbool.h>
# include "color.h"
/// @brief The type of a case.
typedef enum e_map_wall
{
@ -26,14 +29,20 @@ typedef enum e_map_wall
typedef struct s_object
{
/// @brief Function called when creating the object.
///
/// @param data Address of the s_object.data pointer.
void (*init)(void **data);
/// @brief Function called when destroying the object (to free everything).
/// @brief Function called when destroying the object (to avoid leaks).
///
/// @param data Address of the s_object.data pointer.
void (*destroy)(void **data);
/// @brief Function called each tick.
///
/// @param data Address of the s_object.data pointer.
void (*tick)(void **data);
/// @brief Pointer the object can use to save data.
void *data;
} t_object;
@ -43,6 +52,7 @@ typedef struct s_map_case
{
/// @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;
@ -51,14 +61,46 @@ typedef struct s_map_case
/// @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 Path to the image file for the wall face north.
const char *texture_north;
/// @brief Path to the image file for the wall face south.
const char *texture_south;
/// @brief Path to the image file for the wall face west.
const char *texture_west;
/// @brief Path to the image file for the wall face east.
const char *texture_east;
/// @brief Width of the map.
int width;
unsigned int width;
/// @brief Height of the map.
int height;
unsigned int height;
/// @brief An 2D array of all the cases.
/// Syntax to get a case: cases[y][x]
///
/// Syntax to get a case: cases[y * width + x]
/// x is left to right, y is top to bottom.
t_map_case **cases;
t_map_case *cases;
} t_map;
#endif
/// @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);
#endif

31
include/map_mapping.h Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_mapping.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 12:00:07 by mcolonna #+# #+# */
/* Updated: 2024/10/04 15:24:14 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAP_MAPPING_H
# define MAP_MAPPING_H
# include "map.h"
/// @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[];
#endif

29
include/read_all_text.h Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_all_text.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 15:39:02 by mcolonna #+# #+# */
/* Updated: 2024/10/02 17:04:33 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef READ_ALL_TEXT_H
# define READ_ALL_TEXT_H
/// @brief Size of buffer used in read_all_text().
# define BUFFER_SIZE 1000
/// @brief Read all the text of the given file.
///
/// If the last line isn't ended by a '\\n', add one.
///
/// If error, returns NULL and set errno accordingly.
///
/// @param fd File descriptor of the file.
/// @return Alloc'd string of the file content. NULL if error.
char *read_all_text(int fd);
#endif

54
include/stream.h Normal file
View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stream.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 12:33:25 by mcolonna #+# #+# */
/* Updated: 2024/10/02 16:14:23 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STREAM_H
# define STREAM_H
# include <stdbool.h>
/// @brief Represents a string and an associated cursor.
typedef struct s_stream
{
/// @brief Index of the cursor.
int i;
/// @brief Pointer to the string.
const char *str;
} t_stream;
/// @brief Read a specific string, error if it isn't the expected string.
///
/// @param str Expected string.
/// @param stream Stream to use.
/// @param err Set to true if an error occured.
/// If already true, the function won't do anything.
void read_expected_string(const char *str, t_stream *stream, bool *err);
/// @brief Read an unsigned int (which fits the pattern /[0-9]+/).
///
/// @param dest Will be set to the value of the unsigned integer.
/// @param stream Stream to use.
/// @param err Set to true if an error occured.
/// If already true, the function won't do anything.
void read_unsigned(unsigned int *dest, t_stream *stream, bool *err);
/// @brief Read a string until limit char or \0.
///
/// @param c Limit char.
/// @param dest Will be set to an alloc'd pointer to the read string.
/// NULL if alloc error.
/// @param stream Stream to use.
/// @param err Set to true if an error occured.
/// If already true, the function won't do anything.
void read_until(char c, char **dest, t_stream *stream, bool *err);
#endif

24
include/util.h Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* util.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/03 14:19:03 by mc #+# #+# */
/* Updated: 2024/10/04 15:27:07 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef UTIL_H
# define UTIL_H
/// @brief Get the length of a string.
int getlen(const char *str);
/// @brief Write an error message on stderr.
///
/// @param str... All the strings to write. The last parameter MUST BE NULL.
void write_err(const char *str, ...);
#endif