Compare commits

...

4 commits

Author SHA1 Message Date
Theo Champion
fb57ef253a docs: documented ray struct 2025-07-30 16:46:31 +02:00
Theo Champion
f21b8dc448 fix: fixed norme 2025-07-30 16:40:33 +02:00
Theo Champion
e28cd1c85b feat: readded minimap 2025-07-30 16:35:39 +02:00
Theo Champion
5281fe6abd feat: made 3d... for the cub3d 2025-07-30 16:25:41 +02:00
10 changed files with 211 additions and 14 deletions

View file

@ -23,6 +23,9 @@ SOURCEFILES = \
src/player/angle.c \
src/player/player.c \
src/player/move.c \
src/raycast/ray.c \
src/raycast/render.c \
src/renderer/render.c \
OBJECTS = $(SOURCEFILES:.c=.o)
NAME = cub3d

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */
/* Updated: 2025/07/29 14:28:45 by tchampio ### ########.fr */
/* Updated: 2025/07/30 16:38:32 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,6 +32,7 @@ extern const float g_southeast;
# define MOVEMENT_SPEED 0.1
# define ROTATION_SPEED 0.1
# define PLANE_VALUE 0.66
# define TEXTURE_SIZE 64
# ifdef BONUS
# define COMPILED_TEXT "Compiled with bonuses"
# else

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
/* Updated: 2025/07/29 13:43:40 by tchampio ### ########.fr */
/* Updated: 2025/07/30 16:07:36 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@
# include "draw/img_data.h"
# include "player/player.h"
# include "utils/keypresses.h"
# include "consts.h"
typedef struct s_cub3d_data
{
@ -26,6 +27,7 @@ typedef struct s_cub3d_data
t_mapdata *map;
t_player player;
t_keypresses keypresses;
int screen_matrix[HEIGHT][WIDTH];
} t_cub3d_data;
#endif // CUB3D_DATA_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/07/29 14:00:36 by tchampio ### ########.fr */
/* Updated: 2025/07/30 16:30:11 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,9 @@
#include "draw/draw_map.h"
#include "map/mapdata.h"
#include "player/player.h"
#include "raycast/raycaster.h"
#include "renderer/render.h"
#include "raycast/ray.h"
#include "utils/hooks.h"
#include "utils/frees.h"
#include <stdbool.h>
@ -103,8 +106,12 @@ float coord_global_to_local(float coord)
int game_loop(t_cub3d_data *data)
{
t_ray ray;
mlx_destroy_image(data->mlx, data->img_data->img);
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
reset_matrix(data);
raycaster(data, &ray);
move_player(data);
// if (player_intercardinal_dir(data->player) == SOUTHEAST)
@ -137,6 +144,7 @@ int game_loop(t_cub3d_data *data)
// ray_angle += fraction;
// }
matrix_to_image(data);
draw_map(data->map, &data->player, data->img_data);
mlx_put_image_to_window(data->mlx, data->mlx_win,
data->img_data->img, 0, 0);

View file

@ -6,11 +6,12 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 12:10:17 by tchampio #+# #+# */
/* Updated: 2025/07/30 12:36:11 by tchampio ### ########.fr */
/* Updated: 2025/07/30 16:06:19 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "ray.h"
#include "raycaster.h"
#include "../player/player.h"
#include "../consts.h"
#include "../cub3d_data.h"
@ -84,7 +85,7 @@ void calculate_wall_height(t_ray *ray, t_player *player)
ray->draw_start = 0;
ray->draw_end = ray->wall_height / 2 + HEIGHT / 2;
if (ray->draw_end >= HEIGHT)
ray->draw_start = HEIGHT - 1;
ray->draw_end = HEIGHT - 1;
if (ray->side == 0)
ray->wall_x = player->y + ray->wall_dist * ray->dir_y;
else
@ -103,5 +104,7 @@ void raycaster(t_cub3d_data *data, t_ray *ray)
ray_calculate_step(ray, &data->player);
calculate_wall_dist(ray, data->map->map);
calculate_wall_height(ray, &data->player);
render_walls(data, ray, x);
x++;
}
}

View file

@ -6,13 +6,29 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 11:32:20 by tchampio #+# #+# */
/* Updated: 2025/07/29 11:35:17 by tchampio ### ########.fr */
/* Updated: 2025/07/30 16:46:05 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RAY_H
# define RAY_H
/*
* plane - plan de camera (vectoriel)
* dir_x - Direction (pour X)
* dir_y - Direction (pour Y)
* map_x - Position dans la map du rayon
* map_y - Position dans la map du rayon
* step_x - "pas" d'un rayon
* step_y
* side_dist_x - Distance horizontale avant la prochaine section de quadrillage
* side_dist_y - idem mais pour le vertical
* wall_dist - distance du mur touché
* side - de quel cote part le rayon
* wall_height - Hauteur du mur a dessiner
* draw_start - x boucle de la matrice de pixel pour dessiner a la bonne hauteur
* draw_end - fin du dessin du mur
*/
typedef struct s_ray
{
double plane;

22
src/raycast/raycaster.h Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycaster.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 15:16:04 by tchampio #+# #+# */
/* Updated: 2025/07/30 15:17:02 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RAYCASTER_H
# define RAYCASTER_H
# include "ray.h"
# include "../cub3d_data.h"
void raycaster(t_cub3d_data *data, t_ray *ray);
void render_walls(t_cub3d_data *data, t_ray *ray, int x);
#endif // RAYCASTER_H

66
src/raycast/render.c Normal file
View file

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 12:50:10 by tchampio #+# #+# */
/* Updated: 2025/07/30 16:38:59 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../consts.h"
#include "ray.h"
int get_cardinal(t_ray *ray)
{
if (ray->side == 0)
{
if (ray->dir_x < 0)
return (2);
else
return (3);
}
else
{
if (ray->dir_y > 0)
return (1);
else
return (0);
}
}
/*
* Dir values are:
* 0: North
* 1: South
* 2: West
* 3: East
*/
void render_walls(t_cub3d_data *data, t_ray *ray, int x)
{
int dir;
int tex_x;
unsigned int color;
double step;
double pos;
dir = get_cardinal(ray);
tex_x = (int)(ray->wall_x * TEXTURE_SIZE);
if ((ray->side == 0 && ray->dir_x < 0)
|| (ray->side == 1 && ray->dir_y > 0))
tex_x = TEXTURE_SIZE - tex_x - 1;
step = 1.0 * TEXTURE_SIZE / ray->wall_height;
pos = (ray->draw_start - HEIGHT / 2 + ray->wall_height / 2) * step;
while (ray->draw_start < ray->draw_end)
{
pos += step;
color = 0x0000ff;
(void)dir;
(void)pos;
data->screen_matrix[ray->draw_start][x] = color;
ray->draw_start++;
}
}

55
src/renderer/render.c Normal file
View file

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 14:53:39 by tchampio #+# #+# */
/* Updated: 2025/07/30 16:39:51 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "render.h"
#include "../consts.h"
#include "../cub3d_data.h"
#include "../draw/drawutils.h"
#include "../../libft/includes/libft.h"
void reset_matrix(t_cub3d_data *data)
{
int x;
int y;
y = 0;
while (y < HEIGHT)
{
x = 0;
while (x < WIDTH)
{
data->screen_matrix[y][x] = 0;
x++;
}
y++;
}
}
void matrix_to_image(t_cub3d_data *data)
{
int x;
int y;
y = 0;
while (y < HEIGHT)
{
x = 0;
while (x < WIDTH)
{
if (data->screen_matrix[y][x] > 0)
my_mlx_pixel_put(data->img_data,
x, y, data->screen_matrix[y][x]);
x++;
}
y++;
}
}

21
src/renderer/render.h Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 14:52:12 by tchampio #+# #+# */
/* Updated: 2025/07/30 16:20:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RENDER_H
# define RENDER_H
# include "../cub3d_data.h"
void reset_matrix(t_cub3d_data *data);
void matrix_to_image(t_cub3d_data *data);
#endif // RENDER_H