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 5335a1a..02a6637 100644 --- a/include/cub3D.h +++ b/include/cub3D.h @@ -29,6 +29,17 @@ enum e_parser_error missing_option, invalid_map }; +typedef union u_rgba +{ + u_int32_t bytes; + struct s_color + { + u_int8_t alpha; + u_int8_t blue; + u_int8_t green; + u_int8_t red; + } t_color; +} t_rgba; typedef struct s_parser_state { @@ -174,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; @@ -313,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/enemies/enemy_draw.c b/src/enemies/enemy_draw.c index a94ff02..d642a91 100644 --- a/src/enemies/enemy_draw.c +++ b/src/enemies/enemy_draw.c @@ -1,18 +1,18 @@ #include -static unsigned int 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; - 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 * (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 = get_rgba(tex->pixels[pos] * enemy->brightness, - tex->pixels[pos + 1] * enemy->brightness, - tex->pixels[pos + 2] * enemy->brightness, - tex->pixels[pos + 3]); + 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); } @@ -52,9 +52,9 @@ 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, + 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,17 +64,17 @@ 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)) 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/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/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); +} 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); -} 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 6cbff46..9864895 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, + 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); 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,33 +35,35 @@ 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, - int base_color, int fog) + t_rgba 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.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); + color.t_color.alpha = color.t_color.alpha; + mlx_put_pixel(img, pix_pos.x, pix_pos.y, color.bytes); }