Skip to content

Commit

Permalink
Merged main into branch. Issues with render_utils having been moved a…
Browse files Browse the repository at this point in the history
…nd subsequently deleted
  • Loading branch information
Jean Teissier authored and Jean Teissier committed Aug 29, 2024
2 parents f66cb30 + 560309f commit 92bab18
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 71 deletions.
5 changes: 3 additions & 2 deletions Makefile
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:02:08 by tsuchen #+# #+# #
# Updated: 2024/08/29 17:27:35 by jteissie ### ########.fr #
# Updated: 2024/08/29 17:44:16 by jteissie ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -15,7 +15,8 @@ NAME = cub3d
SRCS_M = main.c \
game_init.c \
cleanup.c \
color_utils.c
color_utils.c \
render_utils.c


SRCS_PS = parser.c \
Expand Down
33 changes: 30 additions & 3 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 17:33:28 by jteissie ### ########.fr */
/* Updated: 2024/08/29 17:42:57 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -48,10 +48,11 @@
# define WIDTH 1280
# define HEIGHT 720
# define FOV 90
# define ROT_STEP 1.0f
# define MOV_STEP 0.1f
# define KEY_PRESS 2
# define MOUSE_PRESS 4
# define MOUSE_MOVE 6
# define CLOSE_BUTTON 17
# define FILE_EXTENSION ".cub"

typedef enum e_texture t_texture;
Expand Down Expand Up @@ -80,6 +81,20 @@ typedef enum e_bool
FALSE,
TRUE,
} t_bool;

typedef enum e_rot
{
CLOCK,
CCLOCK,
} t_rot;

typedef enum e_move
{
FORWARD,
BACKWARD,
RIGHT,
LEFT,
} t_move;
typedef enum e_p_dir
{
NORTH = 'N',
Expand All @@ -88,6 +103,15 @@ typedef enum e_p_dir
EAST = 'E',
} t_p_dir;

typedef struct s_image
{
void *img;
char *addr;
int bpp;
int line_length;
int endian;
} t_image;

typedef struct s_data
{
char *map_path;
Expand All @@ -97,7 +121,7 @@ typedef struct s_data
char **map;
void *mlx;
void *window;
t_image *image;
t_image image;
t_textdata *textures;
t_wall wall_data;
t_p_dir p_dir_default;
Expand All @@ -117,5 +141,8 @@ int create_trgb(int t, int r, int g, int b);
int get_color(int trgb, char index);
int add_shade(double factor, int color);
int get_opposite(int color);
/* rendering utils*/
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);

#endif
18 changes: 1 addition & 17 deletions includes/raycasting.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,18 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 17:39:13 by tsuchen #+# #+# */
/* Updated: 2024/08/29 17:37:26 by jteissie ### ########.fr */
/* Updated: 2024/08/29 17:43:28 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef RAYCASTING_H
# define RAYCASTING_H

# define WIDTH 1280
# define HEIGHT 720

# include "vector.h"
# include "cub3d.h"

typedef struct s_vec t_vec;
typedef struct s_data t_data;

typedef struct s_image
{
void *img;
char *addr;
int bpp;
int line_length;
int endian;
} t_image;


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);
int rc_dda(t_vec *dist, t_vec *unit_dist, t_vec *pos, t_vec *dir);
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);
Expand Down
3 changes: 2 additions & 1 deletion includes/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 10:56:32 by tsuchen #+# #+# */
/* Updated: 2024/08/28 16:45:05 by tsuchen ### ########.fr */
/* Updated: 2024/08/29 16:43:25 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,5 +38,6 @@ void vec_mirror(t_vec *vec);
void vec_transp(t_vec *vec);
void vec_rotate(t_vec *vec, double angle);
double vec_cos(t_vec *vec_a, t_vec *vec_b);
void vec_copy_scale(t_vec *vec_a, const t_vec *vec_b, double scale);

#endif
8 changes: 4 additions & 4 deletions srcs/cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 17:04:32 by jteissie #+# #+# */
/* Updated: 2024/08/29 14:38:59 by jteissie ### ########.fr */
/* Updated: 2024/08/29 17:45:39 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,11 +37,11 @@ int cleanup(t_data *data)
{
if (data->textures)
cleanup_textures(data->textures, data->mlx);
if (data->image)
mlx_destroy_image(data->mlx, data->image->img);
if (data->image.img)
mlx_destroy_image(data->mlx, data->image.img);
if (data->window)
mlx_destroy_window(data->mlx, data->window);
mlx_destroy_display(data->mlx);
free(data->mlx);
return (SUCCESS);
exit(SUCCESS);
}
66 changes: 42 additions & 24 deletions srcs/events/key_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,72 @@
/* ::: :::::::: */
/* key_events.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 18:40:50 by jteissie #+# #+# */
/* Updated: 2024/08/28 15:08:21 by jteissie ### ########.fr */
/* Updated: 2024/08/29 16:52:06 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

#include "cub3d.h"

static void move_player(t_data *data, int value)
static void move_player(t_data *data, t_move dir)
{
(void)data;
(void)value;
t_vec step;

if (dir == FORWARD)
{
vec_copy_scale(&step, &data->p_dir, MOV_STEP);
vec_add(&data->p_pos, &step);
}
else if (dir == BACKWARD)
{
vec_copy_scale(&step, &data->p_dir, MOV_STEP);
vec_sub(&data->p_pos, &step);
}
else if (dir == RIGHT)
{
vec_copy_scale(&step, &data->p_cam, MOV_STEP);
vec_add(&data->p_pos, &step);
}
else if (dir == LEFT)
{
vec_copy_scale(&step, &data->p_cam, MOV_STEP);
vec_sub(&data->p_pos, &step);
}
return ;
}

static void move_camera(t_data *data, int value)
static void move_camera(t_data *data, t_rot dir)
{
(void)data;
(void)value;
if (dir == CLOCK)
{
vec_rotate(&data->p_cam, ROT_STEP * -1);
vec_rotate(&data->p_dir, ROT_STEP * -1);
}
else
{
vec_rotate(&data->p_cam, ROT_STEP);
vec_rotate(&data->p_dir, ROT_STEP);
}
return ;
}

int key_events(int keycode, t_data *data)
{
int value;

value = 0;
if (keycode == ARROW_RIGHT)
move_camera(data, value);
move_camera(data, CLOCK);
else if (keycode == ARROW_LEFT)
move_camera(data, value);
else if (keycode == ARROW_UP)
move_camera(data, value);
else if (keycode == ARROW_DOWN)
move_camera(data, value);
move_camera(data, CCLOCK);
else if (keycode == W_KEY)
move_player(data, value);
move_player(data, FORWARD);
else if (keycode == S_KEY)
move_player(data, value);
move_player(data, BACKWARD);
else if (keycode == A_KEY)
move_player(data, value);
move_player(data, LEFT);
else if (keycode == D_KEY)
move_player(data, value);
move_player(data, RIGHT);
else if (keycode == ESC_KEY)
{
cleanup(data);
exit(EXIT_SUCCESS);
}
return (SUCCESS);
}
7 changes: 5 additions & 2 deletions srcs/game_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 17:03:43 by jteissie #+# #+# */
/* Updated: 2024/08/29 17:29:02 by jteissie ### ########.fr */
/* Updated: 2024/08/29 17:44:55 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,7 +45,7 @@ int game_init(t_data *data)
data->mlx = mlx_init();
if (!data->mlx)
return (PANIC);
data->window = mlx_new_window(data->mlx, WIDTH, HEIGHT, "cub3d");
data->window = mlx_new_window(data->mlx, WIDTH, HEIGHT, P_NAME);
if (!data->window)
{
cleanup(data);
Expand All @@ -56,5 +56,8 @@ int game_init(t_data *data)
cleanup(data);
return (PANIC);
}
data->image.img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
data->image.addr = mlx_get_data_addr(data->image.img, &data->image.bpp,
&data->image.line_length, &data->image.endian);
return (SUCCESS);
}
14 changes: 5 additions & 9 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */
/* Updated: 2024/08/28 17:39:19 by jteissie ### ########.fr */
/* Updated: 2024/08/29 16:37:04 by tsuchen ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -58,15 +58,11 @@ static int init_data(t_data *data, char *map_file)
return (SUCCESS);
}

void init_hooks(t_data *data)
{
mlx_hook(data->window, KeyRelease, KeyReleaseMask, &key_events, data);
mlx_hook(data->window, DestroyNotify, StructureNotifyMask, &cleanup, data);
}

void start_game(t_data *data)
{
init_hooks(data);
mlx_loop_hook(data->mlx, rc_rendering, data);
mlx_hook(data->window, KeyPress, KeyPressMask, &key_events, data);
mlx_hook(data->window, DestroyNotify, StructureNotifyMask, &cleanup, data);
mlx_loop(data->mlx);
}

Expand Down
9 changes: 5 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 17:30:08 by jteissie ### ########.fr */
/* Updated: 2024/08/29 17:45:05 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -52,7 +52,7 @@ double rc_raydist(t_vec *ray, t_data *data)
t_vec unit_dist;
t_vec dist_ray;

vec_init(&ray_pos, (int)data->p_pos.x, (int)data->p_pos.y);
vec_init(&ray_pos, data->p_pos.x, 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);
Expand All @@ -68,7 +68,7 @@ double rc_raydist(t_vec *ray, t_data *data)
return ((dist_ray.x - unit_dist.x) * vec_cos(ray, &data->p_dir));
}

void rc_rendering(t_data *data)
int rc_rendering(t_data *data)
{
int x;
double cam_x;
Expand All @@ -84,5 +84,6 @@ void rc_rendering(t_data *data)
ray_dist = rc_raydist(&data->ray_dir, data);
rc_stripe_pixel_put(data, x, ray_dist);
}
mlx_put_image_to_window(data->mlx, data->window, data->image->img, 0, 0);
mlx_put_image_to_window(data->mlx, data->window, data->image.img, 0, 0);
return (0);
}
8 changes: 4 additions & 4 deletions srcs/raycasting/render_utils.c → srcs/render_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/29 12:12:17 by tsuchen #+# #+# */
/* Updated: 2024/08/29 16:52:12 by jteissie ### ########.fr */
/* Updated: 2024/08/29 17:47:57 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#include "raycasting.h"
#include "cub3d.h"

void rc_mlx_pixel_put(t_image *image, int x, int y, int color)
{
Expand All @@ -30,9 +30,9 @@ void rc_stripe_pixel_put(t_data *data, int x, double ray_dist)
while (++y < HEIGHT)
{
if (y < (HEIGHT - wall_height) / 2)
rc_mlx_pixel_put(data->image, x, y, data->textures->ceiling);
rc_mlx_pixel_put(&data->image, x, y, data->textures->ceiling);
else if (y > (HEIGHT + wall_height) / 2)
rc_mlx_pixel_put(data->image, x, y, data->textures->floor);
rc_mlx_pixel_put(&data->image, x, y, data->textures->floor);
else
mlx_render_wall(data, x, y, ray_dist);
}
Expand Down
Loading

0 comments on commit 92bab18

Please sign in to comment.