diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 2fcbe4a..a699fc0 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -62,8 +62,8 @@ int is_wall(t_game *game, int cell_x, int cell_y); // SRC/DRAW // ------------------------------- // draw.c -void draw_pixel(t_game *game, int x, int y, int color); -void draw_line(t_game *game, t_vec vec, int color); +void draw_pixel(t_img *img, int x, int y, int color); +void draw_line(t_img *img, t_vec *vec, int color); void draw(t_game *game); // raycast.c void raycast(t_game *game, t_rcast *rcast); diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 3ebeffb..81cf47d 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -35,7 +35,9 @@ typedef struct s_rcast t_vec screen_dist; t_vec screen_size; t_vec ray; - int screen_def; + t_vec wall; + int screen_width; + int screen_height; int cell; int cell_x; int cell_y; @@ -53,7 +55,6 @@ typedef struct s_rcast int ray_step_y; int is_x; int ray_nb; - int wall_height; double ratio; } t_rcast; @@ -73,8 +74,9 @@ typedef struct s_img void *ptr; char *data; int bpp; - int sizel; - int endian; + int szl; + int ndn; + int height; } t_img; /* diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index 62f4e72..08a0245 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -1,40 +1,40 @@ #include "cube3d.h" -static int pxl_out_limits(t_game *game, int x, int y) +static int pxl_out_limits(t_img *img, int x, int y) { int xmax; int ymax; - xmax = game->map_img.sizel / (game->map_img.bpp / 8); - ymax = game->map_win.size_y; + xmax = img->szl / (img->bpp / 8); + ymax = img->height; if (x < 0 || y < 0 || x > xmax || y > ymax) return (1); return (0); } -void draw_pixel(t_game *game, int x, int y, int color) +void draw_pixel(t_img *img, int x, int y, int color) { unsigned int position; - if (pxl_out_limits(game, x, y)) + if (pxl_out_limits(img, x, y)) return ; - position = y * game->map_img.sizel + x * (game->map_img.bpp / 8); - *(unsigned int*)(game->map_img.data + position) = color; + position = y * img->szl + x * (img->bpp / 8); + *(unsigned int*)(img->data + position) = color; } -void draw_line(t_game *game, t_vec vec, int color) +void draw_line(t_img *img, t_vec *vec, int color) { t_coord dist; int i; int j; - dist.x = vec.end.x - vec.start.x; - dist.y = vec.end.y - vec.start.y; + dist.x = (*vec).end.x - (*vec).start.x; + dist.y = (*vec).end.y - (*vec).start.y; i = 0; j = 0; while (ft_abs(i) <= ft_abs(dist.x) && ft_abs(j) <= ft_abs(dist.y)) { - draw_pixel(game, vec.start.x + i, vec.start.y + j, color); + draw_pixel(img, (*vec).start.x + i, (*vec).start.y + j, color); if (!ft_abs(dist.x) || ft_abs(j) < ft_abs(i * dist.y / dist.x)) j += ft_sign(dist.y); else @@ -60,11 +60,11 @@ void draw_line(t_game *game, t_vec vec, int color) if (rotation) rotate(&(game->plr), &(new)); if (!i || i == size - 1) - draw_pixel(game, new.x, new.y, border); + draw_pixel(&game->map_img, new.x, new.y, border); else if (!j || j == size - 1) - draw_pixel(game, new.x, new.y, border); + draw_pixel(&game->map_img, new.x, new.y, border); else - draw_pixel(game, new.x, new.y, fill); + draw_pixel(&game->map_img, new.x, new.y, fill); j++; } i++; @@ -113,14 +113,14 @@ void draw_line(t_game *game, t_vec vec, int color) screen.end.x = rcast->screen_size.end.x + game->plr.pos.x; screen.end.y = rcast->screen_size.end.y + game->plr.pos.y; rotate(&(game->plr), &(screen.end)); - draw_line(game, screen, 0x00FFFFFF); + draw_line(&game->map_img, &screen, 0x00FFFFFF); // draw screen dist screen.start.x = rcast->screen_dist.start.x + game->plr.pos.x; screen.start.y = rcast->screen_dist.start.y + game->plr.pos.y; screen.end.x = rcast->screen_dist.end.x + game->plr.pos.x; screen.end.y = rcast->screen_dist.end.y + game->plr.pos.y; rotate(&(game->plr), &(screen.end)); - draw_line(game, screen, 0x00FFFFFF); + draw_line(&game->map_img, &screen, 0x00FFFFFF); } // tmp end @@ -137,5 +137,5 @@ void draw(t_game *game) mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, SCREEN_HEIGHT); // tmp end -// mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0); + mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0); } diff --git a/srcs/draw/ray_intersect.c b/srcs/draw/ray_intersect.c index decb5a5..163160e 100644 --- a/srcs/draw/ray_intersect.c +++ b/srcs/draw/ray_intersect.c @@ -3,7 +3,6 @@ static void init_raycast(t_rcast *rcast, t_vec *ray) { rcast->is_x = 0; - rcast->wall_height = 0; // ray sign rcast->ray_sign_x = 1; if (ray->start.x < ray->end.x) diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 7fbe603..2115a99 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -30,11 +30,44 @@ } // tmp end -void draw_column(t_game *game, t_rcast *rcast, t_vec *ray) +static void calcul_wall(t_rcast *rcast) { - (void)game; - (void)rcast; - (void)ray; + int height; + + rcast->wall.start.x = rcast->ray_nb; + rcast->wall.end.x = rcast->ray_nb; + if (rcast->is_x == 1 && rcast->slope_y != 0) + height = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); + if (rcast->is_x == 0 && rcast->slope_x != 0) + height = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); + if (height > rcast->screen_height) + height = rcast->screen_height; + rcast->wall.start.y = rcast->screen_height / 2 + height / 2; + rcast->wall.end.y = rcast->screen_height / 2 - height / 2; +} + +void draw_column(t_game *game, t_rcast *rcast) +{ + t_vec floor; + t_vec ceilling; + + if (rcast->wall.start.y > 0) + { + floor.start.x = rcast->ray_nb; + floor.end.x = rcast->ray_nb; + floor.start.y = rcast->screen_height; + floor.end.y = rcast->wall.start.y; + draw_line(&game->img, &floor, 0x00FF0000); + } + if (rcast->wall.start.y < rcast->screen_height) + { + ceilling.start.x = rcast->ray_nb; + ceilling.end.x = rcast->ray_nb; + ceilling.start.y = rcast->wall.end.y; + ceilling.end.y = 0; + draw_line(&game->img, &ceilling, 0x00FF0000); + } + draw_line(&game->img, &rcast->wall, 0x00FF00FF); } void raycast(t_game *game, t_rcast *rcast) @@ -42,16 +75,17 @@ void raycast(t_game *game, t_rcast *rcast) t_vec ray; rcast->ray_nb = 0; - while (rcast->ray_nb <= rcast->screen_def) + while (rcast->ray_nb <= rcast->screen_width) { ray_intersect(game, rcast, &ray); // tmp, to draw the map calcul_ray_end(rcast, &ray); - draw_line(game, ray, 0x00FF00FF); + draw_line(&game->map_img, &ray, 0x00FF00FF); // tmp end - draw_column(game, rcast, &ray); + calcul_wall(rcast); + draw_column(game, rcast); (rcast->ray_nb)++; } } diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index d2d465e..d18d01f 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -1,11 +1,10 @@ #include "cube3d.h" -static void init_img(t_img *img, t_game *game) +static void init_img(t_img *img, void *mlx_ptr, t_win *win) { - img->ptr = mlx_new_image(game->mlx_ptr, game->map_win.size_x, - game->map_win.size_y); - img->data = mlx_get_data_addr(img->ptr, &(img->bpp), - &(img->sizel), &(img->endian)); + img->ptr = mlx_new_image(mlx_ptr, win->size_x, win->size_y); + img->data = mlx_get_data_addr(img->ptr, &img->bpp, &img->szl, &img->ndn); + img->height = win->size_y; } static void init_map(t_map *map) @@ -69,7 +68,8 @@ static void init_raycast(t_rcast *rcast) dist = (SCREEN_FOCAL / 2) * M_PI / 180; dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist)); - rcast->screen_def = SCREEN_WIDTH; + rcast->screen_width = SCREEN_WIDTH; + rcast->screen_height = SCREEN_HEIGHT; // screen dist rcast->screen_dist.start.x = 0; rcast->screen_dist.start.y = 0; @@ -116,21 +116,17 @@ t_game *init_game(void) mb_add(game->mlx_ptr); // tmp draw map - // size window map game->map_win.size_x = game->map.size_x * CELL; game->map_win.size_y = game->map.size_y * CELL + SCREEN_HEIGHT; - // create the window - game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x, - game->map_win.size_y, "map"); - // create image and get its data address - init_img(&(game->map_img), game); + game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x, game->map_win.size_y, "map"); + init_img(&(game->map_img), game->mlx_ptr, &(game->map_win)); // tmp end game->win.size_x = SCREEN_WIDTH; game->win.size_y = SCREEN_HEIGHT; game->win.ptr = mlx_new_window(game->mlx_ptr, game->win.size_x, game->win.size_y, "cub3d"); - init_img(&(game->img), game); + init_img(&(game->img), game->mlx_ptr, &(game->win)); // k(ey)_hook is the array containing the values of key press events ft_bzero(&game->k_hook, sizeof(game->k_hook)); // raycasting diff --git a/srcs/player/player_limits.c b/srcs/player/player_limits.c index 9890d6d..41e83c5 100644 --- a/srcs/player/player_limits.c +++ b/srcs/player/player_limits.c @@ -18,7 +18,7 @@ int plr_out_limits(t_game *game, int x, int y) int cell_x; int cell_y; - xmax = game->map_img.sizel / (game->map_img.bpp / 8); + xmax = game->map_img.szl / (game->map_img.bpp / 8); ymax = game->map_win.size_y; cell_x = x / game->map.cell; cell_y = y / game->map.cell;