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

changed color type from int to union #113

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
18 changes: 16 additions & 2 deletions include/cub3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 17 additions & 17 deletions src/enemies/enemy_draw.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include <cub3D.h>

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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parser_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/parser/validate_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/border.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <cub3D.h>

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);
}
20 changes: 0 additions & 20 deletions src/utils/colors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/utils/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
44 changes: 23 additions & 21 deletions src/walls/fog.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <cub3D.h>

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);
Expand Down Expand Up @@ -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);
}