From 3415ff9a2175bd982403bef2c9da444eb5c5b41d Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 10:09:16 +0200 Subject: [PATCH 01/10] added union --- include/cub3D.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/cub3D.h b/include/cub3D.h index 5335a1a..b730e16 100644 --- a/include/cub3D.h +++ b/include/cub3D.h @@ -30,6 +30,18 @@ enum e_parser_error invalid_map }; +typedef union u_rgba +{ + u_int32_t bytes; + struct s_color + { + u_int8_t red; + u_int8_t blue; + u_int8_t green; + u_int8_t alpha; + } t_color; +} t_rgba; + typedef struct s_parser_state { bool map_parsed; From 4cba6e2bc42836b222358b423685e61451687113 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 10:28:08 +0200 Subject: [PATCH 02/10] implemented union color for walls --- include/cub3D.h | 5 ++--- src/walls/fog.c | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/include/cub3D.h b/include/cub3D.h index b730e16..32919a7 100644 --- a/include/cub3D.h +++ b/include/cub3D.h @@ -29,16 +29,15 @@ enum e_parser_error missing_option, invalid_map }; - typedef union u_rgba { u_int32_t bytes; struct s_color { - u_int8_t red; + u_int8_t alpha; u_int8_t blue; u_int8_t green; - u_int8_t alpha; + u_int8_t red; } t_color; } t_rgba; diff --git a/src/walls/fog.c b/src/walls/fog.c index 6cbff46..f2f1ef7 100644 --- a/src/walls/fog.c +++ b/src/walls/fog.c @@ -1,12 +1,12 @@ #include -static void put_pixel_floor(mlx_image_t *img, t_vector_int pix_pos, - int base_color, int fog); -static int get_rgba_from_tex(const mlx_texture_t *tex, - t_vector_int pix_pos, double dis, int fog); +static void put_pixel_floor(mlx_image_t *img, t_vector_int pix_pos, + int base_color, int fog); +static u_int32_t get_rgba_from_tex(const mlx_texture_t *tex, + t_vector_int pix_pos, double dis, int fog); void draw_vertical_line(t_window *window, t_vector *target, - int p_x, t_direction direction) + int p_x, t_direction direction) { const double dis = distance_perpendicular(window->player->pos, window->player->dir, *target); @@ -35,21 +35,21 @@ void draw_vertical_line(t_window *window, t_vector *target, } } -static int get_rgba_from_tex(const mlx_texture_t *tex, +static u_int32_t get_rgba_from_tex(const mlx_texture_t *tex, t_vector_int pix_pos, double dis, int fog) { - int color; + t_rgba color; const int pos = (pix_pos.y * tex->width + pix_pos.x) * tex->bytes_per_pixel; const double brightness = max(1.0 - (dis / fog), 0); if (dis > fog) return (0x000000ff); - color = (int)(tex->pixels[pos] * brightness) << 24 - | (int)(tex->pixels[pos + 1] * brightness) << 16 - | (int)(tex->pixels[pos + 2] * brightness) << 8 - | (int)tex->pixels[pos + 3]; - return (color); + color.t_color.red = tex->pixels[pos] * brightness; + color.t_color.green = tex->pixels[pos + 1] * brightness; + color.t_color.blue = tex->pixels[pos + 2] * brightness; + color.t_color.alpha = tex->pixels[pos + 3]; + return (color.bytes); } static void put_pixel_floor(mlx_image_t *img, t_vector_int pix_pos, From f56b1f339a1c8be5d699390760c6a69c19e1a5ec Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 10:32:15 +0200 Subject: [PATCH 03/10] added t_rgba for enemies --- src/enemies/enemy_draw.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/enemies/enemy_draw.c b/src/enemies/enemy_draw.c index a94ff02..79751c9 100644 --- a/src/enemies/enemy_draw.c +++ b/src/enemies/enemy_draw.c @@ -3,17 +3,17 @@ static unsigned int enemy_get_pix(double scale_x, double scale_y, mlx_texture_t *tex, t_enemy *enemy) { - int pos; - unsigned int color; - const int x = tex->width * fabs(scale_x); - const int y = tex->height * fabs(scale_y); + int pos; + t_rgba color; + const int x = tex->width * fabs(scale_x); + const int y = tex->height * fabs(scale_y); pos = (y * tex->width + x) * 4; - color = get_rgba(tex->pixels[pos] * enemy->brightness, - tex->pixels[pos + 1] * enemy->brightness, - tex->pixels[pos + 2] * enemy->brightness, - tex->pixels[pos + 3]); - return (color); + color.t_color.red = tex->pixels[pos] * enemy->brightness; + color.t_color.green = tex->pixels[pos + 1] * enemy->brightness; + color.t_color.blue = tex->pixels[pos + 2] * enemy->brightness; + color.t_color.alpha = tex->pixels[pos + 3]; + return (color.bytes); } static mlx_texture_t *enemy_get_texture(t_enemy *enemy, int frame_count) From fb0c8baa10cada46f4e276ed474b3a5fde98142d Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 10:32:51 +0200 Subject: [PATCH 04/10] updated function type --- src/enemies/enemy_draw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/enemies/enemy_draw.c b/src/enemies/enemy_draw.c index 79751c9..6ae050b 100644 --- a/src/enemies/enemy_draw.c +++ b/src/enemies/enemy_draw.c @@ -1,6 +1,6 @@ #include -static unsigned int enemy_get_pix(double scale_x, double scale_y, +static u_int32_t enemy_get_pix(double scale_x, double scale_y, mlx_texture_t *tex, t_enemy *enemy) { int pos; From 06a5c0ecb65d246d000843c2ad72c9db4b4ddf76 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 10:39:51 +0200 Subject: [PATCH 05/10] added border function --- Makefile | 2 +- include/cub3D.h | 3 +++ src/utils/border.c | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/utils/border.c diff --git a/Makefile b/Makefile index 3bd5a9a..71e5480 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ HUD_FILES = hud.c minimap.c minimap_fov_border.c minimap_enemies.c UTILS = $(addprefix $(UTILS_DIR), $(UTILS_FILES)) UTILS_DIR = src/utils/ -UTILS_FILES = vector.c utils.c colors.c map.c +UTILS_FILES = vector.c utils.c colors.c map.c border.c MATH = $(addprefix $(MATH_DIR), $(MATH_FILES)) MATH_DIR = src/math/ diff --git a/include/cub3D.h b/include/cub3D.h index 32919a7..c187835 100644 --- a/include/cub3D.h +++ b/include/cub3D.h @@ -324,6 +324,9 @@ double max(double a, double b); bool is_on_map(double x, double y, t_map *map); bool is_on_screen(int x, int y); +//utils/border.c +double border(double lower_bound, double value, double upper_bound) + //hud.c t_hud *setup_hud(mlx_t *mlx); void draw_hud(void *arg); diff --git a/src/utils/border.c b/src/utils/border.c new file mode 100644 index 0000000..476c33f --- /dev/null +++ b/src/utils/border.c @@ -0,0 +1,10 @@ +#include + +double border(double lower_bound, double value, double upper_bound) +{ + if (value > upper_bound) + return (upper_bound); + if (value < lower_bound) + return (lower_bound); + return (value); +} From 67df306c10959714401e50f8856773dd9b5484cb Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 10:44:50 +0200 Subject: [PATCH 06/10] added missing semicolon --- include/cub3D.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cub3D.h b/include/cub3D.h index c187835..f8a2233 100644 --- a/include/cub3D.h +++ b/include/cub3D.h @@ -325,7 +325,7 @@ bool is_on_map(double x, double y, t_map *map); bool is_on_screen(int x, int y); //utils/border.c -double border(double lower_bound, double value, double upper_bound) +double border(double lower_bound, double value, double upper_bound); //hud.c t_hud *setup_hud(mlx_t *mlx); From d926aca8565b5f57853a8005cf779f8ec2624f58 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 10:52:24 +0200 Subject: [PATCH 07/10] implemented rgba untion for floor --- src/walls/fog.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/walls/fog.c b/src/walls/fog.c index f2f1ef7..7b3a78c 100644 --- a/src/walls/fog.c +++ b/src/walls/fog.c @@ -55,13 +55,15 @@ static u_int32_t get_rgba_from_tex(const mlx_texture_t *tex, static void put_pixel_floor(mlx_image_t *img, t_vector_int pix_pos, int base_color, int fog) { - const double brightness = max(abs(HEIGHT / 2 - pix_pos.y) - * fog / 4500.0, 0); - const uint8_t alpha = base_color & 0xff; - const uint8_t red = min(((base_color >> 24) & 0xff) * brightness, 255); - const uint8_t green = min(((base_color >> 16) & 0xff) * brightness, 255); - const uint8_t blue = min(((base_color >> 8) & 0xff) * brightness, 255); + t_rgba color; + const double brightness = border(0, + abs(HEIGHT / 2 - pix_pos.y) * fog / 4500.0, + 1); - base_color = (red << 24) | (green << 16) | (blue << 8) | alpha; - mlx_put_pixel(img, pix_pos.x, pix_pos.y, base_color); + color.bytes = base_color; + color.t_color.red = (color.t_color.red * brightness); + color.t_color.green = (color.t_color.green * brightness); + color.t_color.blue = (color.t_color.blue * brightness); + color.t_color.alpha = color.t_color.alpha; + mlx_put_pixel(img, pix_pos.x, pix_pos.y, color.bytes); } From 2a02dacc1a2d4ea311475ed11547b2d96f779e87 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 11:06:29 +0200 Subject: [PATCH 08/10] added rgba union to map struct --- include/cub3D.h | 4 ++-- src/parser/parser_init.c | 4 ++-- src/parser/validate_options.c | 6 +++--- src/utils/map.c | 4 ++-- src/walls/fog.c | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/cub3D.h b/include/cub3D.h index f8a2233..02a6637 100644 --- a/include/cub3D.h +++ b/include/cub3D.h @@ -185,8 +185,8 @@ typedef struct s_map mlx_texture_t *textures[4]; t_vector start_pos; t_vector start_dir; - int floor_color; - int ceiling_color; + t_rgba floor_color; + t_rgba ceiling_color; bool has_spawn; mlx_texture_t *door_tex; t_list *enemy_list; diff --git a/src/parser/parser_init.c b/src/parser/parser_init.c index 87e6e2c..90d8eaa 100644 --- a/src/parser/parser_init.c +++ b/src/parser/parser_init.c @@ -35,8 +35,8 @@ t_map *init_map(void) map->door_tex = NULL; map->enemy_list = NULL; map->has_spawn = false; - map->floor_color = get_rgba(0, 0, 0, 0); - map->ceiling_color = get_rgba(0, 0, 0, 0); + map->floor_color.bytes = 0x0; + map->ceiling_color.bytes = 0x0; map->state = init_state(); if (!map->state) return (NULL); diff --git a/src/parser/validate_options.c b/src/parser/validate_options.c index 36fb3e2..92c6578 100644 --- a/src/parser/validate_options.c +++ b/src/parser/validate_options.c @@ -2,9 +2,9 @@ bool options_are_valid(t_map *map) { - if (!map->floor_color || !map->ceiling_color - || map->floor_color == get_rgba(0, 0, 0, 0) - || map->ceiling_color == get_rgba(0, 0, 0, 0)) + if (!map->floor_color.bytes || !map->ceiling_color.bytes + || map->floor_color.bytes == 0x0 + || map->ceiling_color.bytes == 0x0) { map->state->error_type = missing_option; return (false); diff --git a/src/utils/map.c b/src/utils/map.c index 404f2e4..3fe025f 100644 --- a/src/utils/map.c +++ b/src/utils/map.c @@ -7,7 +7,7 @@ void set_floor_color(t_map *map, int color) { if (!map->state->f_parsed) { - map->floor_color = color; + map->floor_color.bytes = color; map->state->f_parsed = true; } else @@ -18,7 +18,7 @@ void set_ceiling_color(t_map *map, int color) { if (!map->state->c_parsed) { - map->ceiling_color = color; + map->ceiling_color.bytes = color; map->state->c_parsed = true; } else diff --git a/src/walls/fog.c b/src/walls/fog.c index 7b3a78c..9864895 100644 --- a/src/walls/fog.c +++ b/src/walls/fog.c @@ -1,7 +1,7 @@ #include static void put_pixel_floor(mlx_image_t *img, t_vector_int pix_pos, - int base_color, int fog); + t_rgba base_color, int fog); static u_int32_t get_rgba_from_tex(const mlx_texture_t *tex, t_vector_int pix_pos, double dis, int fog); @@ -53,14 +53,14 @@ static u_int32_t get_rgba_from_tex(const mlx_texture_t *tex, } static void put_pixel_floor(mlx_image_t *img, t_vector_int pix_pos, - int base_color, int fog) + t_rgba base_color, int fog) { t_rgba color; const double brightness = border(0, abs(HEIGHT / 2 - pix_pos.y) * fog / 4500.0, 1); - color.bytes = base_color; + color.bytes = base_color.bytes; color.t_color.red = (color.t_color.red * brightness); color.t_color.green = (color.t_color.green * brightness); color.t_color.blue = (color.t_color.blue * brightness); From 49baa2eaadb3d398da41a619f02afe24b49fb7b8 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 11:19:13 +0200 Subject: [PATCH 09/10] cleaner implementation of t_rgba into enemy --- src/enemies/enemy_draw.c | 20 ++++++++++---------- src/utils/colors.c | 20 -------------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/src/enemies/enemy_draw.c b/src/enemies/enemy_draw.c index 6ae050b..361642e 100644 --- a/src/enemies/enemy_draw.c +++ b/src/enemies/enemy_draw.c @@ -1,19 +1,19 @@ #include -static u_int32_t enemy_get_pix(double scale_x, double scale_y, - mlx_texture_t *tex, t_enemy *enemy) +static t_rgba enemy_get_pix(t_vector_int iter, t_vector lim, + mlx_texture_t *tex, t_enemy *enemy) { int pos; t_rgba color; - const int x = tex->width * fabs(scale_x); - const int y = tex->height * fabs(scale_y); + const int x = tex->width * (lim.x + iter.x) / lim.x / 2; + const int y = tex->height * (iter.y + lim.y) / lim.y / 2; pos = (y * tex->width + x) * 4; color.t_color.red = tex->pixels[pos] * enemy->brightness; color.t_color.green = tex->pixels[pos + 1] * enemy->brightness; color.t_color.blue = tex->pixels[pos + 2] * enemy->brightness; color.t_color.alpha = tex->pixels[pos + 3]; - return (color.bytes); + return (color); } static mlx_texture_t *enemy_get_texture(t_enemy *enemy, int frame_count) @@ -52,7 +52,7 @@ static int enemy_adjust_frame_count(t_enemy *enemy, double delta_time) static void draw_single_enemy(t_window *window, t_enemy *enemy) { - unsigned int color; + t_rgba color; t_vector_int iter; const t_vector lim = (t_vector){ENEMY_WIDTH / enemy->dis, ENEMY_HEIGHT / enemy->dis}; @@ -69,12 +69,12 @@ static void draw_single_enemy(t_window *window, t_enemy *enemy) if (!is_on_screen(enemy->x_on_screen + iter.x, HEIGHT / 2 + iter.y + ENEMY_Y_OFFSET / enemy->dis)) continue ; - color = enemy_get_pix((lim.x + iter.x) / lim.x / 2, - (iter.y + lim.y) / 2 / lim.y, + color = enemy_get_pix(iter, lim, enemy_get_texture(enemy, frame_count), enemy); - if (get_alpha(color) != 0) + if (color.t_color.alpha != 0) mlx_put_pixel(window->img, enemy->x_on_screen + iter.x, - HEIGHT / 2 + iter.y + ENEMY_Y_OFFSET / enemy->dis, color); + HEIGHT / 2 + iter.y + ENEMY_Y_OFFSET / enemy->dis, + color.bytes); } } } diff --git a/src/utils/colors.c b/src/utils/colors.c index c3a89c4..1cdc696 100644 --- a/src/utils/colors.c +++ b/src/utils/colors.c @@ -4,23 +4,3 @@ int get_rgba(int r, int g, int b, int a) { return (r << 24 | g << 16 | b << 8 | a); } - -int get_red(int rgba) -{ - return ((rgba >> 24) & 0xFF); -} - -int get_green(int rgba) -{ - return ((rgba >> 16) & 0xFF); -} - -int get_blue(int rgba) -{ - return ((rgba >> 8) & 0xFF); -} - -int get_alpha(int rgba) -{ - return (rgba & 0xFF); -} From 125f4d043d67796e9da7c9b5d8d421ad91b60fb2 Mon Sep 17 00:00:00 2001 From: Florian Sandel Date: Thu, 6 Jul 2023 11:22:45 +0200 Subject: [PATCH 10/10] removed unnecesarry comparison in emey --- src/enemies/enemy_draw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/enemies/enemy_draw.c b/src/enemies/enemy_draw.c index 361642e..d642a91 100644 --- a/src/enemies/enemy_draw.c +++ b/src/enemies/enemy_draw.c @@ -54,7 +54,7 @@ static void draw_single_enemy(t_window *window, t_enemy *enemy) { t_rgba color; t_vector_int iter; - const t_vector lim = (t_vector){ENEMY_WIDTH / enemy->dis, + const t_vector lim = (t_vector){min(ENEMY_WIDTH / enemy->dis, 500), ENEMY_HEIGHT / enemy->dis}; const int frame_count = enemy_adjust_frame_count(enemy, window->mlx->delta_time); @@ -64,7 +64,7 @@ static void draw_single_enemy(t_window *window, t_enemy *enemy) && iter.y < HEIGHT / 2 - ENEMY_Y_OFFSET / enemy->dis) { iter.x = max(-lim.x, -500); - while (++iter.x < lim.x && iter.x < 500) + while (++iter.x < lim.x) { if (!is_on_screen(enemy->x_on_screen + iter.x, HEIGHT / 2 + iter.y + ENEMY_Y_OFFSET / enemy->dis))