diff --git a/.vscode/settings.json b/.vscode/settings.json index 81a0a9e..a7b0f2c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,7 +5,8 @@ "screen.h": "c", "mlx42.h": "c", "glfw3.h": "c", - "vec_math.h": "c" + "vec_math.h": "c", + "texture.h": "c" }, "cmake.ignoreCMakeListsMissing": true } \ No newline at end of file diff --git a/assets/bricks_cyan_x64.png b/assets/bricks_cyan_x64.png new file mode 100644 index 0000000..b6526b1 Binary files /dev/null and b/assets/bricks_cyan_x64.png differ diff --git a/assets/bricks_green_x64.png b/assets/bricks_green_x64.png new file mode 100644 index 0000000..1515c17 Binary files /dev/null and b/assets/bricks_green_x64.png differ diff --git a/assets/bricks_orange_x64.png b/assets/bricks_orange_x64.png new file mode 100644 index 0000000..d808c52 Binary files /dev/null and b/assets/bricks_orange_x64.png differ diff --git a/assets/bricksx64.png b/assets/bricksx64.png new file mode 100644 index 0000000..71d18c9 Binary files /dev/null and b/assets/bricksx64.png differ diff --git a/assets/bricksx64_east.png b/assets/bricksx64_east.png new file mode 100644 index 0000000..1b3ef0c Binary files /dev/null and b/assets/bricksx64_east.png differ diff --git a/assets/bricksx64_north.png b/assets/bricksx64_north.png new file mode 100644 index 0000000..4687423 Binary files /dev/null and b/assets/bricksx64_north.png differ diff --git a/assets/bricksx64_south.png b/assets/bricksx64_south.png new file mode 100644 index 0000000..7a46337 Binary files /dev/null and b/assets/bricksx64_south.png differ diff --git a/assets/bricksx64_west.png b/assets/bricksx64_west.png new file mode 100644 index 0000000..63dbc4d Binary files /dev/null and b/assets/bricksx64_west.png differ diff --git a/inc/cub3d.h b/inc/cub3d.h index 26aa2be..e5edc8c 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* cub3d.h :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */ -/* Updated: 2025/05/04 16:46:00 by whaffman ######## odam.nl */ +/* ::: :::::::: */ +/* cub3d.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */ +/* Updated: 2025/05/06 15:56:57 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,7 @@ # include "player.h" # include "collision.h" # include "parser.h" +# include "texture.h" int initialize_cub3d(t_game **game, const char *mapfile); diff --git a/inc/texture.h b/inc/texture.h new file mode 100644 index 0000000..b30eb6a --- /dev/null +++ b/inc/texture.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* texture.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/06 15:46:20 by qmennen #+# #+# */ +/* Updated: 2025/05/06 15:59:52 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TEXTURE_H +# define TEXTURE_H + +# include "cub3d.h" + +int texture_load(t_game *game); + +#endif \ No newline at end of file diff --git a/inc/types.h b/inc/types.h index 44ef1c9..cfa3129 100644 --- a/inc/types.h +++ b/inc/types.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ -/* Updated: 2025/05/06 15:20:37 by qmennen ### ########.fr */ +/* Updated: 2025/05/06 18:04:13 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,6 +60,7 @@ typedef struct s_map char *SO_texture; char *WE_texture; char *EA_texture; + mlx_texture_t *textures[4]; unsigned int floor_color; unsigned int ceiling_color; } t_map; diff --git a/src/map/map_create.c b/src/map/map_create.c index 1c970c3..581d604 100644 --- a/src/map/map_create.c +++ b/src/map/map_create.c @@ -1,16 +1,15 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* map_create.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */ -/* Updated: 2025/04/25 11:36:18 by whaffman ######## odam.nl */ +/* ::: :::::::: */ +/* map_create.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */ +/* Updated: 2025/05/06 16:00:10 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ -#include #include "cub3d.h" @@ -26,6 +25,7 @@ int map_create(t_game **game, const char *mapfile) perror("Error allocating memory for (*game)->map"); return (FAILURE); } + ft_memset((*game)->map->textures, 0, sizeof((*game)->map->textures)); parse_args(mapfile, (*game)); grid = copy_map((*game)->map->grid, (*game)->map->width, (*game)->map->height); if (!grid) diff --git a/src/render/DDAscratch.c b/src/render/DDAscratch.c index 9828ef1..0a9709d 100644 --- a/src/render/DDAscratch.c +++ b/src/render/DDAscratch.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */ -/* Updated: 2025/05/06 15:21:22 by qmennen ### ########.fr */ +/* Updated: 2025/05/06 18:17:55 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -95,16 +95,16 @@ t_side get_side(t_vec2 ray_dir, double perp_dist) if (perp_dist > 0) { if (ray_dir.x > 0) - return (SIDE_EAST); - else return (SIDE_WEST); + else + return (SIDE_EAST); } else { if (ray_dir.y > 0) - return (SIDE_SOUTH); - else return (SIDE_NORTH); + else + return (SIDE_SOUTH); } } @@ -122,7 +122,10 @@ t_render cast_ray(t_game *game, int x) perp_dist = dda(ray_dir, pos, game->map); render.perp_dist = fabs(perp_dist); render.side = get_side(ray_dir, perp_dist); - render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist) + (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist); + if (perp_dist < 0) + render.wall_x = pos.x + ray_dir.x * render.perp_dist; + else + render.wall_x = pos.y + ray_dir.y * render.perp_dist; render.wall_x -= floor(render.wall_x); return (render); } @@ -140,22 +143,39 @@ unsigned int get_color(t_render render) return (color[render.side] << 8 | (int)(1.0 / dist * 255)); } +unsigned int get_texture_color(mlx_texture_t *texture, t_render render, int tex_x, int tex_y) +{ + int index; + double dist; + + if (render.side == SIDE_NORTH || render.side == SIDE_EAST) + tex_x = texture->width - tex_x - 1; + index = (tex_x + tex_y * texture->height) * texture->bytes_per_pixel; + dist = (render.perp_dist == 0) * 1e30 + (render.perp_dist > 1) * render.perp_dist + (render.perp_dist <= 1) * 1; + return texture->pixels[index] << 24 | texture->pixels[index + 1] << 16 | texture->pixels[index + 2] << 8 | (int)(1.0 / dist * 255); +} + void draw_line(t_game *game, t_render render, int x) { int y; int color; int lineHeight; int drawStart; + int texDrawStart; int drawEnd; + int tex_x; + int tex_y; y = 0; lineHeight = (int)(game->screen->height / render.perp_dist); drawStart = -lineHeight / 2 + game->screen->height / 2; + texDrawStart = drawStart; if (drawStart < 0) drawStart = 0; - drawEnd = lineHeight / 2 + game->screen->height / 2; + drawEnd = lineHeight / 2 + game->screen->height / 2; if (drawEnd >= game->screen->height) drawEnd = game->screen->height - 1; + tex_x = (int)(render.wall_x * (double)game->map->textures[render.side]->width); while (y < game->screen->height) { if (y < drawStart) @@ -163,9 +183,16 @@ void draw_line(t_game *game, t_render render, int x) else if (y > drawEnd) color = game->map->floor_color << 8 | (int)fabs(2 * y * 0xFF / (double)game->screen->height - 0xFF); else - color = get_color(render); + { + + tex_y = (int)(((double) game->map->textures[render.side]->height / (double) lineHeight) * (y - texDrawStart)); + color = get_texture_color(game->map->textures[render.side], render, tex_x, tex_y); + } + if (x < 0 || x >= game->screen->width || y < 0 || y >= game->screen->height) + break; mlx_put_pixel(game->screen->img, x, y, color); y++; + } } diff --git a/src/texture/texutre_load.c b/src/texture/texutre_load.c new file mode 100644 index 0000000..2f8dadd --- /dev/null +++ b/src/texture/texutre_load.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* texutre_load.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/06 15:45:58 by qmennen #+# #+# */ +/* Updated: 2025/05/06 15:55:30 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "texture.h" + +void texture_delete(t_game *game) +{ + int i; + + i = 0; + while (i < 4) + { + if (game->map->textures[i]) + mlx_delete_texture(game->map->textures[i]); + i++; + } +} + +int texture_load(t_game *game) +{ + game->map->textures[SIDE_NORTH] = mlx_load_png(game->map->NO_texture); + game->map->textures[SIDE_EAST] = mlx_load_png(game->map->EA_texture); + game->map->textures[SIDE_SOUTH] = mlx_load_png(game->map->SO_texture); + game->map->textures[SIDE_WEST] = mlx_load_png(game->map->WE_texture); + if (!game->map->textures[SIDE_NORTH] || !game->map->textures[SIDE_EAST] + || !game->map->textures[SIDE_SOUTH] || !game->map->textures[SIDE_WEST]) + { + texture_delete(game); + return (FAILURE); + } + return (SUCCESS); +} diff --git a/src/util/initialize.c b/src/util/initialize.c index 0793566..fd5eff4 100644 --- a/src/util/initialize.c +++ b/src/util/initialize.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* initialize.c :+: :+: */ -/* +:+ */ -/* By: qmennen +#+ */ -/* +#+ */ -/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */ -/* Updated: 2025/04/25 11:20:49 by whaffman ######## odam.nl */ +/* ::: :::::::: */ +/* initialize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */ +/* Updated: 2025/05/06 16:18:44 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,8 @@ int initialize_cub3d(t_game **game, const char *mapfile) return (FAILURE); if (!map_create(game, mapfile)) return (FAILURE); + if (!texture_load(*game)) + return (FAILURE); if (!screen_create(game)) return (FAILURE); if (!keyboard_create(game)) diff --git a/test.cub b/test.cub index bbd05dd..e0de358 100644 --- a/test.cub +++ b/test.cub @@ -1,12 +1,11 @@ -NO ./path/file.png +NO ./assets/bricksx64_north.png -WE ./path/file.png +WE ./assets/bricksx64_west.png -SO ./path/file.png - -EA ./path/file.png +SO ./assets/bricksx64_south.png +EA ./assets/bricksx64_east.png F 90,30,30 @@ -16,7 +15,7 @@ C 100,100,200 1111111 111 1111 111111 1111111111 111111 1000001110111001 100001 1000001001 10001 -1000000000000001 100001 100000110111100011 +1001000000000001 100001 100000110111100011 1000001110000001110011111111 110000001000000001 1000001 10001000000010000001 100000001000000001 1000001110001000000000000001111100000001000000001