Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser with sprites #25

Merged
merged 21 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"files.associations": {
"cstdlib": "c",
"stdio.h": "c",
"meta.h": "c"
"meta.h": "c",
"array": "c",
"string_view": "c",
"initializer_list": "c",
"ranges": "c",
"span": "c",
"utility": "c"
}
}
17 changes: 13 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ RUN_CMD := ./$(NAME) test_maps/valid_tex.cub

# CFLAGS += -Wall -Wextra -Werror
# CFLAGS += -Wall -Wextra
CFLAGS += -g -fsanitize=address
# CFLAGS += -g
# CFLAGS += -Ofast -flto -march=native
# CFLAGS += -g -fsanitize=address
# # CFLAGS += -g
CFLAGS += -Ofast -flto -march=native

LIBFT := libft/build/libft.a
LIBMLX := MLX42/build/libmlx42.a
Expand All @@ -37,6 +37,7 @@ SRCS = parser/check_elements.c \
parser/check_colors.c \
parser/utils_one.c \
parser/utils_two.c \
parser/utils_three.c \
utils/error.c \
utils/free.c \
utils/colors.c \
Expand All @@ -56,7 +57,14 @@ SRCS = parser/check_elements.c \
test_utils.c \
timer.c \
set_textures.c \
game/floorcaster.c
game/floorcaster.c \
game/sprite.c \
game/sprite_utils.c \
parser/lexer.c \
parser/lexer_utils.c \
parser/map_lexer.c \
parser/flag_lexer.c \
parser/extra_lexer.c

HEADER_DIR := include
HEADERS := meta.h \
Expand Down Expand Up @@ -114,6 +122,7 @@ fclean: clean tclean
$(MAKE) -C MLX42/build clean
$(MAKE) -C libft fclean
rm -f $(NAME)
rm -rf $(NAME).dSYM

re: fclean all

Expand Down
135 changes: 104 additions & 31 deletions include/meta.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/* ************************************************************************** */
/**/
/* :::::::: */
/* meta.h :+: :+: */
/*+:+ */
/* By: jboeve <[email protected]>+#+ */
/* +#+ */
/* Created: 2023/11/01 20:07:37 by jboeve#+##+# */
/* Updated: 2024/01/20 01:14:53 by joppe ######## odam.nl */
/**/
/* */
/* :::::::: */
/* meta.h :+: :+: */
/* +:+ */
/* By: joppe <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/02/05 14:01:44 by joppe #+# #+# */
/* Updated: 2024/02/08 15:08:42 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */



#ifndef META_H
#define META_H

Expand Down Expand Up @@ -84,6 +86,8 @@

#define FOV 0.85

#define SPRITE_COUNT 2

typedef bool (t_ray_hitfunc) (const void *p, uint32_t x, uint32_t y);

typedef struct s_meta t_meta;
Expand All @@ -95,6 +99,14 @@ MAP_SPACE,
MAP_DOOR
} t_cell_type;

typedef enum e_element_type {
CEIL_FLOOR,
WALL,
SPRITE,
DOOR,
NON_VALID
} t_element_type;

typedef enum e_font_family {
FONT_DEJAVU_14,
FONT_MLX,
Expand Down Expand Up @@ -157,59 +169,77 @@ typedef struct s_player {
t_meta *meta;
t_ray rays[WINDOW_WIDTH];
t_vray vrays[WINDOW_HEIGHT];
bool should_render;
bool should_render;
t_vec2d cam_plane;
t_vec2d position;
t_vec2d direction;
// Sprite stuff.
int32_t *sprite_order; // malloc these based on the sprite count
double *sprite_dist;
double z_buffer[WINDOW_WIDTH];
} t_player;


typedef struct s_map {
char *map_element;
t_cell_type *level;
uint32_t width;
uint32_t height;
t_vec2u player_start;
char player_start_dir;
} t_map;

typedef struct s_flag
{
char *flag;
char *content;
struct s_flag *next;
} t_flag;

typedef struct s_tex {
char *tex_path;
mlx_texture_t *tex;
} t_tex;

typedef struct s_sprite {
t_vec2d pos;
t_tex tex;
} t_sprite;

typedef struct s_attr {
t_tex n; //add bit flag, if tex_path is missing then it means it is a color value
t_tex s;
t_tex e;
t_tex w;
t_tex f;
t_tex c;
t_tex c_alt;
t_rgba floor_c;
t_rgba ceiling_c;
t_tex n; //add bit flag, if tex_path is missing then it means it is a color value
t_tex s;
t_tex e;
t_tex w;
t_tex f;
t_tex c;
t_tex c_alt;
t_rgba floor_c;
t_rgba ceiling_c;
uint32_t sprite_count;
uint32_t sprite_arr_index;
t_sprite *sprites;
} t_attr;


typedef struct s_minimap {
mlx_image_t *minimap_image;
mlx_image_t *ppos_image;
mlx_image_t *fps_image;
mlx_image_t *info_image;
} t_minimap;
} t_minimap;

typedef struct s_meta {
mlx_t *mlx;
mlx_image_t *image;
t_timer update_timer;
t_minimap minimap;
t_timer fps_timer;
t_player player;
uint32_t fps;
t_map map;
t_attr attributes;
const char *scene_name;
char *map_element;
const char *scene_name;
t_flag *elements;
t_player player;
} t_meta;


Expand All @@ -230,13 +260,13 @@ void cursor_hook(double xpos, double ypos, void* param);
void keys_handle(t_meta *meta, double time_delta);

// render_minimap.c
void render_minimap(t_minimap *minimap, const t_map *map, const t_player *p);
void render_minimap(t_minimap *minimap, const t_map *map, const t_player *p);

// render_viewport.c
void render_viewport(mlx_image_t *image, t_player *p);

// minimap.c
void minimap_update(mlx_image_t *image, t_player *p);
void minimap_update(mlx_image_t *image, t_player *p);

// draw.c
void draw_rect(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color);
Expand All @@ -249,27 +279,70 @@ mlx_image_t *cube_put_string(mlx_image_t *image, const char *s, const t_font_a


// keys.c
void keys_update(mlx_key_data_t keydata, void *param);
void keys_update(mlx_key_data_t keydata, void *param);

// raycaster.c
t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *param);

// colors.c
int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a);
int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2i line_points, uint32_t h);
int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2i line_points, uint32_t h);
int32_t find_color(t_rgba rgba);

// free.c
void meta_free(t_meta *meta);
void free_t_flag_list(t_flag **list);

// set_textures.c
int set_textures(t_attr *attributes);
int set_textures(t_attr *attributes);

//pixel_picker.c
uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y);
void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_points, uint32_t h);
void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_points, uint32_t h);

// floorcaster.c
t_vray floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y);
t_vray floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y);

// sprite.c
int init_sprites(uint32_t sprite_count, int32_t **sprite_order, double **sprite_dist);
void sprite_calculate(t_player *p);

// test_utils.c REMOVE LATER

void print_double_array(char *msg, double *arr, uint32_t size, t_sprite *sp, int32_t *order);
void print_ints_array(char *msg, int32_t *arr, uint32_t size);
void print_sprites_array(t_sprite *arr, uint32_t size);
void print_attributes(t_attr *attributes);

// sprite_utils.c

void sprite_sort(double *sprite_dist, int32_t *sprite_order, uint32_t sprite_count);

// lexer.c
char *extract_file(char *map_file);
int lex(char *file, t_map *map, t_flag **elements);
int lexer(t_meta *meta, char *map_file);

// lexer_utils.c

void print_lexer_map(t_map *map);
void print_lexer_elements(t_flag *elements);

// map_lexer.c

bool nl_only_spaces(char *file);
int end_of_map(char *file);
void skip_map_element(char **file, int *skip);
int input_map_lexer(char *file, t_map *map);
int map_lex(char **file, t_map *map, int *skip, int mandatory);

// extra_lexer.c

char *get_title_val(char *file);
int save_extra_title(t_flag **extras, char **file);
bool is_valid_extra(char *file);
int lexer_input_extra(t_flag **extras, char *file, int *skip);



#endif
39 changes: 27 additions & 12 deletions include/parser.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/**/
/* :::::::: */
/* parser.h :+::+: */
/* parser.h :+: :+: */
/*+:+ */
/* By: jboeve <[email protected]>+#+ */
/* +#+ */
/* Created: 2023/11/01 20:07:37 by jboeve#+##+# */
/* Updated: 2024/01/02 19:05:30 by joppe ######## odam.nl */
/* Updated: 2024/01/25 15:27:28 by jboeve ######## odam.nl */
/**/
/* ************************************************************************** */

Expand All @@ -29,6 +29,11 @@
# define NO_MAP "Map element is missing\n"
# define INV_COLOR_CODE "Please check color codes for F and C elements\nRGB values should be between 0-255\n"
# define MLX_ERR "MLX error occured"
# define INVALID_ELEMENT "There are invalid elements in the input file\n"
# define SP_CONTENT_ERR "Please format the sprite element content as follows, SP path/to/texture XX.XX XX.XX\n"
# define DOUBLE_ERR "Please check that your doubles are formatted correctly\n"
# define SP_COORD_ERR "Sprite coordinates are out of bounds\n"

# include "meta.h"

typedef enum e_err {
Expand All @@ -48,33 +53,38 @@ MISSING_ELEMENTS,
MISSING_MAP,
COLOR_CODE_WRONG,
MLX_ERROR,
INV_ELE,
SP_CONTENT,
SP_DOUBLE_ERR,
SP_COORD
} t_err;


// error.c
int pr_err(t_err type);

// check_map.c
int valid_map_char(char c);
int player_pos_char(char c);
int valid_map_char(char c);
int player_pos_char(char c);
bool is_map_chars_valid(char *map);
int check_map(t_meta *meta, char *rect);
int find_index(t_meta *meta, uint32_t x, uint32_t y);


// parser.c
char *file_to_string(int fd);
char *read_file(int fd);
int map_ext(char *file);
int parser(t_meta *meta, char *map_file);
int map_extension(char *file);
int parser(t_meta *meta);

// parse_map.c
bool is_map_line(char *file);
int input_map(t_meta *meta, char *file);

// parse_elements.c
int input_texture(t_attr *attributes, char *file);
int input_colour(t_attr *attributes, char *file);
int input_texture_path(t_attr *attributes, char *flag, char *content);
int input_colour(t_attr *attributes, char *flag, char *content);
int save_elements(t_attr *attributes, char *file);
int parse_elements(t_meta *meta, char *file);
int parse_elements(t_meta *meta);

// check_colors.c
bool valid_rgb_value(char *file);
Expand All @@ -85,7 +95,6 @@ bool colors_valid(char *file);
bool is_valid_element(char *file);
bool only_spaces(char *file);
bool is_map_element(char *file);
bool elements_order(char *file);
bool check_missing(int *found);
bool is_missing(char *file);
bool is_duplicate(char *file);
Expand All @@ -103,7 +112,7 @@ bool is_wall(char *file);
bool is_floor_or_ceiling(char *file);

// utils_one.c
void skip_line(char **file);
void skip_line(char **file, int to_skip);
void skip_spaces(char **file);
void skip_digits(char **file);
int valid_map_char(char c);
Expand All @@ -114,6 +123,12 @@ uint32_t find_width(char *map);
uint32_t find_height(char *map);
char *make_rect(char *map, uint32_t w, uint32_t h);
bool is_path(char *str);
uint32_t count_sprites(t_flag *elements);

// utils_three.c

double ft_atod(char *s);
bool is_double(char *s);
bool valid_sprite_content(char *content);

#endif
Loading
Loading