Skip to content

Commit

Permalink
Skeleton code done, need to review and test ig
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Teissier authored and Jean Teissier committed Aug 29, 2024
1 parent 7ef7a32 commit 4478627
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 42 deletions.
6 changes: 4 additions & 2 deletions includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down
12 changes: 9 additions & 3 deletions includes/raycasting.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* raycasting.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 17:39:13 by tsuchen #+# #+# */
/* Updated: 2024/08/29 12:10:56 by tsuchen ### ########.fr */
/* Updated: 2024/08/29 16:22:35 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,6 +30,12 @@ typedef struct s_image
int line_length;
int endian;
} t_image;
typedef struct s_wall
{
int height
int draw_bounds[2];
int tex_coordinates[2];
} t_wall;

void rc_mlx_pixel_put(t_image *image, int x, int y, int color);
void rc_stripe_pixel_put(t_data *data, int x, double ray_dist);
Expand All @@ -38,4 +44,4 @@ void rc_ray_init(t_vec *dist, t_vec *pos, t_vec *dir, t_vec *unit_dst);
double rc_raydist(t_vec *ray, t_data *data);
void rc_rendering(t_data *data);

#endif
#endif
15 changes: 14 additions & 1 deletion srcs/game_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
7 changes: 3 additions & 4 deletions srcs/raycasting/raycasting.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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));
Expand Down
32 changes: 1 addition & 31 deletions srcs/raycasting/render_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;
Expand Down
49 changes: 48 additions & 1 deletion srcs/raycasting/render_wall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 4478627

Please sign in to comment.