texture ajouté avec des incohérence a réglé, Il manque: Norme et leak

This commit is contained in:
Etienne Rey-bethbeder 2023-05-09 14:46:49 +02:00
parent d22ab7e610
commit 8533722131
8 changed files with 94 additions and 55 deletions

View File

@ -9,7 +9,7 @@ SRCS := ${SRCS_MAP} \
OBJS := ${SRCS:.c=.o} OBJS := ${SRCS:.c=.o}
CC := clang CC := clang
LIBS := libftx/libftx.a MLX42/build/libmlx42.a -ldl -lglfw -lm 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 NAME := cub3D
all: ${NAME} all: ${NAME}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 631 B

After

Width:  |  Height:  |  Size: 182 KiB

BIN
assets/red_.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 15:49:00 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); return (r << 24 | g << 16 | b << 8 | a);
} }
void draw_line(t_game *game, t_dda *dda, unsigned int color, int x)
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; int y;
y = 0; ply = &game->ply;
while (y < HEIGHT) 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) tex.y = (int)tex.pos & (tex.texture->height - 1);
{ tex.pos += tex.step;
mlx_put_pixel(game->window, x, y, game->map.color_bot); mlx_put_pixel(game->window, x, y, get_pixel_color(tex.texture, tex.x, tex.y));
}
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++;
} }
} }
void draw(t_game *game, t_dda *dda, int x)
void draw_dda(t_game *game, t_dda *dda, int x)
{ {
unsigned int color; int y;
if (dda->step_x < 0 && dda->side == 0) // NORTH y = -1;
color = get_rgba(0, 0, 200, 255); while (++y < dda->draw_start)
else if (dda->step_x > 0 && dda->side == 0) // SOUTH mlx_put_pixel(game->window, x, y, game->map.color_bot);
color = get_rgba(200, 0, 0, 255); draw_texture(game, dda, x);
else if (dda->step_y < 0 && dda->side == 1) // EAST y = dda->draw_end - 1;
color = get_rgba(0, 200, 0, 255); while (++y < HEIGHT)
else if (dda->step_y > 0 && dda->side == 1) // WEST mlx_put_pixel(game->window, x, y, game->map.color_top);
color = get_rgba(50, 100, 50, 255);
else
color = get_rgba(0, 0, 0, 255);
draw_line(game, dda, color, x);
} }
void dda(t_game *game, int x) void dda(t_game *game, int x)
@ -154,5 +182,5 @@ void dda(t_game *game, int x)
init_dda(game, &dda); init_dda(game, &dda);
loop_dda(game, &dda); loop_dda(game, &dda);
ray_dda(&dda); ray_dda(&dda);
draw_dda(game, &dda, x); draw(game, &dda, x);
} }

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:59:53 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; t_game game;
init(map, &game); if (init(map, &game))
if (!game.mlx)
return (1); return (1);
game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT); game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT);
if (!game.window) if (!game.window)

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 15:29:34 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; int draw_end;
} t_dda; } 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 typedef struct s_game
{ {
mlx_t *mlx; mlx_t *mlx;
mlx_image_t *window; mlx_image_t *window;
mlx_image_t textures[4]; mlx_texture_t *textures[4];
t_ply ply; t_ply ply;
t_map map; t_map map;
t_ray ray; t_ray ray;
} t_game; } t_game;
/* INIT */ /* INIT */
void init(t_map map, t_game *game); int init(t_map map, t_game *game);
/* MANAGE */ /* MANAGE */
void manage(mlx_key_data_t keydata, void *param); void manage(mlx_key_data_t keydata, void *param);

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:50:22 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; 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]) if (!textures[0])
return (1); return (1);
(*textures)[1] = mlx_load_png(map.img_path[1]); textures[1] = mlx_load_png(map.img_path[1]);
if (!textures[1]) if (!textures[1])
return (1); return (1);
(*textures)[2] = mlx_load_png(map.img_path[2]); textures[2] = mlx_load_png(map.img_path[2]);
if (!textures[2]) if (!textures[2])
return (1); return (1);
(*textures)[3] = mlx_load_png(map.img_path[3]); textures[3] = mlx_load_png(map.img_path[3]);
if (!textures[3]) if (!textures[3])
return (1); return (1);
return (0); 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); game->mlx = mlx_init(WIDTH, HEIGHT, "jan lili", true);
if (!game->mlx) if (!game->mlx)
return ; return (1);
game->map = map; game->map = map;
init_ply(map, &game->ply); init_ply(map, &game->ply);
init_ray(&game->ray); init_ray(&game->ray);
init_textures(map, &game->textures); if (init_textures(map, game->textures))
return (1);
return (0);
} }

View File

@ -1,7 +1,7 @@
NO ./path_to_the_north_texture NO ./assets/red.png
SO ./path_to_the_south_texture SO ./assets/blue.png
WE ./path_to_the_west_texture WE ./assets/green.png
EA ./path_to_the_east_texture EA ./assets/purple.png
F 200,100,0 F 200,100,0
C 225,30,0 C 225,30,0