-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skeleton code done, need to review and test ig
- Loading branch information
Jean Teissier
authored and
Jean Teissier
committed
Aug 29, 2024
1 parent
7ef7a32
commit 4478627
Showing
6 changed files
with
79 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ | ||
/* Updated: 2024/08/29 15:53:50 by jteissie ### ########.fr */ | ||
/* Updated: 2024/08/29 16:23:21 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -57,7 +57,7 @@ | |
typedef enum e_texture t_texture; | ||
typedef struct s_textdata t_textdata; | ||
typedef struct s_image t_image; | ||
|
||
typedef struct s_wall t_wall; | ||
typedef enum e_keys | ||
{ | ||
ARROW_RIGHT = 65363, | ||
|
@@ -94,11 +94,13 @@ typedef struct s_data | |
void *window; | ||
t_image *image; | ||
t_textdata *textures; | ||
t_wall wall_data; | ||
t_p_dir p_dir_default; | ||
t_vec p_pos; | ||
t_vec p_dir; | ||
t_vec p_cam; | ||
t_vec ray_dir; | ||
int side; | ||
t_p_dir spawn_dir; | ||
} t_data; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,24 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/08/27 17:03:43 by jteissie #+# #+# */ | ||
/* Updated: 2024/08/29 15:48:24 by jteissie ### ########.fr */ | ||
/* Updated: 2024/08/29 17:02:50 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "cub3d.h" | ||
|
||
static void get_text_addr(t_textdata *t) | ||
{ | ||
int pixel_bits; | ||
int line_bytes; | ||
int endian; | ||
|
||
t->text_addr[N] = mlx_get_data_addr(t->text_img[N], &pixel_bits, &line_bytes, &endian); | ||
t->text_addr[S] = mlx_get_data_addr(t->text_img[S], &pixel_bits, &line_bytes, &endian); | ||
t->text_addr[W] = mlx_get_data_addr(t->text_img[W], &pixel_bits, &line_bytes, &endian); | ||
t->text_addr[E] = mlx_get_data_addr(t->text_img[E], &pixel_bits, &line_bytes, &endian); | ||
} | ||
|
||
static int load_assets(t_data *data) | ||
{ | ||
t_textdata *t; | ||
|
@@ -24,6 +36,7 @@ static int load_assets(t_data *data) | |
if (!t->text_img[N] || !t->text_img[S] | ||
|| !t->text_img[W] || !t->text_img[E]) | ||
return (PANIC); | ||
get_text_addr(t); | ||
return (SUCCESS); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/08/15 12:13:34 by tsuchen #+# #+# */ | ||
/* Updated: 2024/08/29 16:05:30 by jteissie ### ########.fr */ | ||
/* Updated: 2024/08/29 16:07:07 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -51,19 +51,18 @@ double rc_raydist(t_vec *ray, t_data *data) | |
t_vec ray_pos; | ||
t_vec unit_dist; | ||
t_vec dist_ray; | ||
int side; | ||
|
||
vec_init(&ray_pos, (int)data->p_pos.x, (int)data->p_pos.y); | ||
unit_dist.x = sqrt(1 + ((ray->y * ray->y) / (ray->x * ray->x))); | ||
unit_dist.y = sqrt(1 + ((ray->x * ray->x) / (ray->y * ray->y))); | ||
rc_ray_init(&dist_ray, &ray_pos, ray, &unit_dist); | ||
while (1) | ||
{ | ||
side = rc_dda(&dist_ray, &unit_dist, &ray_pos, ray); | ||
data->side = rc_dda(&dist_ray, &unit_dist, &ray_pos, ray); | ||
if (data->map[(int)ray_pos.x][(int)ray_pos.y] == '1') | ||
break ; | ||
} | ||
if (side == 1) | ||
if (data->side == 1) | ||
return ((dist_ray.y - unit_dist.y) * vec_cos(ray, &data->p_dir)); | ||
else | ||
return ((dist_ray.x - unit_dist.x) * vec_cos(ray, &data->p_dir)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/08/29 12:12:17 by tsuchen #+# #+# */ | ||
/* Updated: 2024/08/29 15:49:14 by jteissie ### ########.fr */ | ||
/* Updated: 2024/08/29 16:52:12 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -20,36 +20,6 @@ void rc_mlx_pixel_put(t_image *image, int x, int y, int color) | |
*(unsigned int *)dst = color; | ||
} | ||
|
||
static void get_tex_coordinates(int *tex_coordinates, t_data *data, int wall_height, double ray_dist) | ||
{ | ||
double wall_x; | ||
|
||
if (data->side == 0) | ||
wall_x = data->p_pos.y + ray_dist * data->ray_dir.y; | ||
else | ||
wall_x = data->p_pos.x + ray_dist * data->ray.dir.x; | ||
wall_x -= floor(wall_x); | ||
tex_coordinates[0] = (int)(wall_x * (double)data->textures->text_width) | ||
if (tex_coordinates[0] >= data->textures->text_width) | ||
tex_coordinates[0] = data->tex_width - 1; | ||
} | ||
|
||
void mlx_render_wall(t_data *data, int x, int y, double ray_dist) | ||
{ | ||
int wall_height; | ||
int draw_bounds[2]; | ||
int tex_coordinates[2]; | ||
|
||
wall_height = (int)(HEIGHT / ray_dist); | ||
draw_bounds[0] = (HEIGHT - wall_height) / 2; | ||
if (draw_bounds[0] < 0) | ||
draw_bounds[0] = 0; | ||
draw_bounds[1] = (HEIGHT + wall_height) / 2; | ||
if (draw_bounds[1] >= HEIGHT) | ||
draw_bounds[1] = HEIGHT - 1; | ||
get_tex_coordinates(tex_coordinates, data, wall_height, ray_dist) | ||
} | ||
|
||
void rc_stripe_pixel_put(t_data *data, int x, double ray_dist) | ||
{ | ||
int y; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,57 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/08/29 14:22:08 by jteissie #+# #+# */ | ||
/* Updated: 2024/08/29 14:56:35 by jteissie ### ########.fr */ | ||
/* Updated: 2024/08/29 17:08:54 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "cub3d.h" | ||
|
||
static t_texture get_current_dir(t_data *data) | ||
{ | ||
if (data->ray_dir.y < 0) | ||
return (N) | ||
else if (data->ray_dir.x < 0) | ||
return (W) | ||
else if (data->ray_dir.y >= 0) | ||
return (S) | ||
else if (data->ray_dir.x >= 0) | ||
return (E) | ||
} | ||
|
||
static void get_tex_coordinates(int *tex_coordinates, t_data *data, int wall_height, double ray_dist) | ||
{ | ||
double wall_x; | ||
|
||
if (data->side == 0) | ||
wall_x = data->p_pos.y + ray_dist * data->ray_dir.y; | ||
else | ||
wall_x = data->p_pos.x + ray_dist * data->ray.dir.x; | ||
wall_x -= floor(wall_x); | ||
tex_coordinates[0] = (int)(wall_x * (double)data->textures->text_width) | ||
if (tex_coordinates[0] >= data->textures->text_width) | ||
tex_coordinates[0] = data->tex_width - 1; | ||
tex_coordinates[1] = ((y - data->wall_data.draw_bounds[0]) * data->textures->height) / wall_height; | ||
if (tex_coordinates[1] >= data->textures->text_height) | ||
tex_coordinates[1] = data->textures->text_height - 1; | ||
} | ||
|
||
void mlx_render_wall(t_data *data, int x, int y, double ray_dist) | ||
{ | ||
t_wall wall_data; | ||
int color; | ||
int dir; | ||
|
||
dir = get_curr_dir(data); | ||
wall_data = data->wall_data; | ||
wall_data.height = (int)(HEIGHT / ray_dist); | ||
wall_data.draw_bounds[0] = (HEIGHT - wall_data.height) / 2; | ||
if (wall_data.draw_bounds[0] < 0) | ||
wall_data.draw_bounds[0] = 0; | ||
wall_data.draw_bounds[1] = (HEIGHT + wall_data.wall_height) / 2; | ||
if (wall_data.draw_bounds[1] >= HEIGHT) | ||
wall_data.draw_bounds[1] = HEIGHT - 1; | ||
get_tex_coordinates(wall_data.tex_coordinates, data, wall_data.height, ray_dist) | ||
color = data->textures->text_addr[dir][wall_data.tex_coordinates[1] * data->textures->width + wall_data.tex_coordinates[0]]; | ||
rc_mlx_pixel_put(data->image, x, y, color); | ||
} |