diff --git a/Makefile b/Makefile index 4d16625..220f432 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ SRCS := ${SRCS_MAP} \ OBJS := ${SRCS:.c=.o} CC := clang LIBS := libftx/libftx.a MLX42/build/libmlx42.a -ldl -lglfw -lm -CFLAGS := -g -Wall -Wextra -Werror -Wno-conversion -Ofast +CFLAGS := -g -Wall -Wextra -Werror -Wno-conversion #-Ofast NAME := cub3D all: ${NAME} diff --git a/assets/red.png b/assets/red.png index fd33fdb..7770d57 100644 Binary files a/assets/red.png and b/assets/red.png differ diff --git a/assets/red_.png b/assets/red_.png new file mode 100644 index 0000000..fd33fdb Binary files /dev/null and b/assets/red_.png differ diff --git a/game/dda.c b/game/dda.c index 296c096..f4b6813 100644 --- a/game/dda.c +++ b/game/dda.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */ -/* Updated: 2023/05/05 15:54:46 by erey-bet ### ########.fr */ +/* Updated: 2023/05/09 14:40:18 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -105,46 +105,74 @@ unsigned int get_rgba(int r, int g, int b, int a) return (r << 24 | g << 16 | b << 8 | a); } -void draw_line(t_game *game, t_dda *dda, unsigned int color, int x) -{ - int y; - y = 0; - while (y < HEIGHT) +static uint32_t switch_color_bytes(uint32_t bad) +{ + uint32_t good; + + good = ((bad & 0xff) << 24) | (((bad >> 8) & 0xff) << 16) + | (((bad >> 16) & 0xff) << 8) | bad >> 24; + return (good); +} + +uint32_t get_pixel_color(mlx_texture_t *texture, uint32_t x, uint32_t y) { + uint32_t *pixel_ptr = (uint32_t*)(texture->pixels + (y * texture->width + x) * texture->bytes_per_pixel); + return switch_color_bytes(*pixel_ptr); +} + +int get_texture_side(t_dda *dda) +{ + if (dda->side == 0 && dda->step_x < 0) + return (0); + if (dda->side == 0 && dda->step_x > 0) + return (1); + if (dda->side == 1 && dda->step_y < 0) + return (2); + if (dda->side == 1 && dda->step_y > 0) + return (3); + return (0); +} + +void draw_texture(t_game *game, t_dda *dda, double x) +{ + t_texture_draw tex; + t_ply *ply; + int y; + + ply = &game->ply; + tex.texture = game->textures[get_texture_side(dda)]; + if (dda->side == 0) + tex.wall_x = ply->pos_y + dda->perp_wall_dist * game->ray.dir_y; + else + tex.wall_x = ply->pos_x + dda->perp_wall_dist * game->ray.dir_x; + tex.wall_x -= floor(tex.wall_x); + tex.x = (int)(tex.wall_x * (double)tex.texture->width); + if ((dda->side == 0 && game->ray.dir_x > 0) + || (dda->side == 1 && game->ray.dir_y < 0)) + tex.x = tex.texture->width - tex.x - 1; + tex.step = 1.0 * tex.texture->height / dda->line_height; + tex.pos = (dda->draw_start - tex.texture->height / 2 + dda->line_height / 2) * tex.step; + y = dda->draw_start - 1; + printf("tex.x: %d\n", tex.x); + while (++y < dda->draw_end) { - if (y < dda->draw_start) - { - mlx_put_pixel(game->window, x, y, game->map.color_bot); - } - else if (y >= dda->draw_start && y <= dda->draw_end) - { - mlx_put_pixel(game->window, x, y, color); - } - else if (y > dda->draw_end) - { - mlx_put_pixel(game->window, x, y, game->map.color_top); - } - y++; + tex.y = (int)tex.pos & (tex.texture->height - 1); + tex.pos += tex.step; + mlx_put_pixel(game->window, x, y, get_pixel_color(tex.texture, tex.x, tex.y)); } } - -void draw_dda(t_game *game, t_dda *dda, int x) +void draw(t_game *game, t_dda *dda, int x) { - unsigned int color; + int y; - if (dda->step_x < 0 && dda->side == 0) // NORTH - color = get_rgba(0, 0, 200, 255); - else if (dda->step_x > 0 && dda->side == 0) // SOUTH - color = get_rgba(200, 0, 0, 255); - else if (dda->step_y < 0 && dda->side == 1) // EAST - color = get_rgba(0, 200, 0, 255); - else if (dda->step_y > 0 && dda->side == 1) // WEST - color = get_rgba(50, 100, 50, 255); - else - color = get_rgba(0, 0, 0, 255); - - draw_line(game, dda, color, x); + y = -1; + while (++y < dda->draw_start) + mlx_put_pixel(game->window, x, y, game->map.color_bot); + draw_texture(game, dda, x); + y = dda->draw_end - 1; + while (++y < HEIGHT) + mlx_put_pixel(game->window, x, y, game->map.color_top); } void dda(t_game *game, int x) @@ -154,5 +182,5 @@ void dda(t_game *game, int x) init_dda(game, &dda); loop_dda(game, &dda); ray_dda(&dda); - draw_dda(game, &dda, x); + draw(game, &dda, x); } diff --git a/game/game.c b/game/game.c index f61cfc5..b2bc825 100644 --- a/game/game.c +++ b/game/game.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */ -/* Updated: 2023/05/03 14:50:16 by erey-bet ### ########.fr */ +/* Updated: 2023/05/09 12:44:57 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,7 @@ int start_game(t_map map) { t_game game; - init(map, &game); - if (!game.mlx) + if (init(map, &game)) return (1); game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT); if (!game.window) diff --git a/game/game.h b/game/game.h index e1c70b0..2c554d1 100644 --- a/game/game.h +++ b/game/game.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */ -/* Updated: 2023/05/05 16:54:19 by erey-bet ### ########.fr */ +/* Updated: 2023/05/09 13:11:40 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,18 +56,28 @@ typedef struct s_dda int draw_end; } t_dda; +typedef struct s_texture_draw +{ + mlx_texture_t *texture; + double wall_x; + int x; + int y; + double pos; + double step; +} t_texture_draw; + typedef struct s_game { mlx_t *mlx; mlx_image_t *window; - mlx_image_t textures[4]; + mlx_texture_t *textures[4]; t_ply ply; t_map map; t_ray ray; } t_game; /* INIT */ -void init(t_map map, t_game *game); +int init(t_map map, t_game *game); /* MANAGE */ void manage(mlx_key_data_t keydata, void *param); diff --git a/game/init.c b/game/init.c index 1c22bd9..07f01f4 100644 --- a/game/init.c +++ b/game/init.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */ -/* Updated: 2023/05/05 17:05:49 by erey-bet ### ########.fr */ +/* Updated: 2023/05/09 12:44:43 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,30 +36,32 @@ void init_ray(t_ray *ray) ray->dir_y = 0; } -int init_textures(t_map map, mlx_image_t **textures) +int init_textures(t_map map, mlx_texture_t *textures[4]) { - (*textures)[0] = mlx_load_png(map.img_path[0]); + textures[0] = mlx_load_png(map.img_path[0]); if (!textures[0]) return (1); - (*textures)[1] = mlx_load_png(map.img_path[1]); + textures[1] = mlx_load_png(map.img_path[1]); if (!textures[1]) return (1); - (*textures)[2] = mlx_load_png(map.img_path[2]); + textures[2] = mlx_load_png(map.img_path[2]); if (!textures[2]) return (1); - (*textures)[3] = mlx_load_png(map.img_path[3]); + textures[3] = mlx_load_png(map.img_path[3]); if (!textures[3]) return (1); return (0); } -void init(t_map map, t_game *game) +int init(t_map map, t_game *game) { game->mlx = mlx_init(WIDTH, HEIGHT, "jan lili", true); if (!game->mlx) - return ; + return (1); game->map = map; init_ply(map, &game->ply); init_ray(&game->ray); - init_textures(map, &game->textures); + if (init_textures(map, game->textures)) + return (1); + return (0); } diff --git a/maps/test1.cub b/maps/test1.cub index 7a4b40a..83047f9 100644 --- a/maps/test1.cub +++ b/maps/test1.cub @@ -1,7 +1,7 @@ -NO ./path_to_the_north_texture -SO ./path_to_the_south_texture -WE ./path_to_the_west_texture -EA ./path_to_the_east_texture +NO ./assets/red.png +SO ./assets/blue.png +WE ./assets/green.png +EA ./assets/purple.png F 200,100,0 C 225,30,0