we got textures

This commit is contained in:
Quinten Mennen 2025-05-06 18:18:33 +02:00
parent 06156e5436
commit b0a0cfe722
17 changed files with 130 additions and 38 deletions

View File

@ -5,7 +5,8 @@
"screen.h": "c", "screen.h": "c",
"mlx42.h": "c", "mlx42.h": "c",
"glfw3.h": "c", "glfw3.h": "c",
"vec_math.h": "c" "vec_math.h": "c",
"texture.h": "c"
}, },
"cmake.ignoreCMakeListsMissing": true "cmake.ignoreCMakeListsMissing": true
} }

BIN
assets/bricks_cyan_x64.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
assets/bricks_green_x64.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
assets/bricksx64.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

BIN
assets/bricksx64_east.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
assets/bricksx64_north.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
assets/bricksx64_south.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
assets/bricksx64_west.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* cub3d.h :+: :+: */ /* cub3d.h :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */ /* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/05/04 16:46:00 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 15:56:57 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,6 +48,7 @@
# include "player.h" # include "player.h"
# include "collision.h" # include "collision.h"
# include "parser.h" # include "parser.h"
# include "texture.h"
int initialize_cub3d(t_game **game, const char *mapfile); int initialize_cub3d(t_game **game, const char *mapfile);

20
inc/texture.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:52:44 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 *SO_texture;
char *WE_texture; char *WE_texture;
char *EA_texture; char *EA_texture;
mlx_texture_t *textures[4];
unsigned int floor_color; unsigned int floor_color;
unsigned int ceiling_color; unsigned int ceiling_color;
} t_map; } t_map;

View File

@ -1,16 +1,15 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* map_create.c :+: :+: */ /* map_create.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */ /* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */
/* Updated: 2025/04/25 11:36:18 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 16:00:10 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdlib.h>
#include "cub3d.h" #include "cub3d.h"
@ -26,6 +25,7 @@ int map_create(t_game **game, const char *mapfile)
perror("Error allocating memory for (*game)->map"); perror("Error allocating memory for (*game)->map");
return (FAILURE); return (FAILURE);
} }
ft_memset((*game)->map->textures, 0, sizeof((*game)->map->textures));
parse_args(mapfile, (*game)); parse_args(mapfile, (*game));
grid = copy_map((*game)->map->grid, (*game)->map->width, (*game)->map->height); grid = copy_map((*game)->map->grid, (*game)->map->width, (*game)->map->height);
if (!grid) if (!grid)

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */ /* 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 (perp_dist > 0)
{ {
if (ray_dir.x > 0) if (ray_dir.x > 0)
return (SIDE_EAST);
else
return (SIDE_WEST); return (SIDE_WEST);
else
return (SIDE_EAST);
} }
else else
{ {
if (ray_dir.y > 0) if (ray_dir.y > 0)
return (SIDE_SOUTH);
else
return (SIDE_NORTH); 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); perp_dist = dda(ray_dir, pos, game->map);
render.perp_dist = fabs(perp_dist); render.perp_dist = fabs(perp_dist);
render.side = get_side(ray_dir, 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); render.wall_x -= floor(render.wall_x);
return (render); return (render);
} }
@ -140,22 +143,39 @@ unsigned int get_color(t_render render)
return (color[render.side] << 8 | (int)(1.0 / dist * 255)); 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) void draw_line(t_game *game, t_render render, int x)
{ {
int y; int y;
int color; int color;
int lineHeight; int lineHeight;
int drawStart; int drawStart;
int texDrawStart;
int drawEnd; int drawEnd;
int tex_x;
int tex_y;
y = 0; y = 0;
lineHeight = (int)(game->screen->height / render.perp_dist); lineHeight = (int)(game->screen->height / render.perp_dist);
drawStart = -lineHeight / 2 + game->screen->height / 2; drawStart = -lineHeight / 2 + game->screen->height / 2;
texDrawStart = drawStart;
if (drawStart < 0) if (drawStart < 0)
drawStart = 0; drawStart = 0;
drawEnd = lineHeight / 2 + game->screen->height / 2; drawEnd = lineHeight / 2 + game->screen->height / 2;
if (drawEnd >= game->screen->height) if (drawEnd >= game->screen->height)
drawEnd = game->screen->height - 1; drawEnd = game->screen->height - 1;
tex_x = (int)(render.wall_x * (double)game->map->textures[render.side]->width);
while (y < game->screen->height) while (y < game->screen->height)
{ {
if (y < drawStart) if (y < drawStart)
@ -163,9 +183,16 @@ void draw_line(t_game *game, t_render render, int x)
else if (y > drawEnd) else if (y > drawEnd)
color = game->map->floor_color << 8 | (int)fabs(2 * y * 0xFF / (double)game->screen->height - 0xFF); color = game->map->floor_color << 8 | (int)fabs(2 * y * 0xFF / (double)game->screen->height - 0xFF);
else 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); mlx_put_pixel(game->screen->img, x, y, color);
y++; y++;
} }
} }

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texutre_load.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* initialize.c :+: :+: */ /* initialize.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */ /* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
/* Updated: 2025/04/25 11:20:49 by whaffman ######## odam.nl */ /* 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); return (FAILURE);
if (!map_create(game, mapfile)) if (!map_create(game, mapfile))
return (FAILURE); return (FAILURE);
if (!texture_load(*game))
return (FAILURE);
if (!screen_create(game)) if (!screen_create(game))
return (FAILURE); return (FAILURE);
if (!keyboard_create(game)) if (!keyboard_create(game))

View File

@ -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 SO ./assets/bricksx64_south.png
EA ./assets/bricksx64_east.png
EA ./path/file.png
F 90,30,30 F 90,30,30
@ -16,7 +15,7 @@ C 100,100,200
1111111 111 1111 111111 1111111111 111111 1111111 111 1111 111111 1111111111 111111
1000001110111001 100001 1000001001 10001 1000001110111001 100001 1000001001 10001
1000000000000001 100001 100000110111100011 1001000000000001 100001 100000110111100011
1000001110000001110011111111 110000001000000001 1000001110000001110011111111 110000001000000001
1000001 10001000000010000001 100000001000000001 1000001 10001000000010000001 100000001000000001
1000001110001000000000000001111100000001000000001 1000001110001000000000000001111100000001000000001