mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 09:58:09 +01:00
feat: made 3d... for the cub3d
This commit is contained in:
parent
077659cd25
commit
5281fe6abd
8 changed files with 185 additions and 5 deletions
3
Makefile
3
Makefile
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
12
src/main.c
12
src/main.c
|
|
@ -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:21:26 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,7 +144,8 @@ int game_loop(t_cub3d_data *data)
|
|||
// ray_angle += fraction;
|
||||
// }
|
||||
|
||||
draw_map(data->map, &data->player, data->img_data);
|
||||
//draw_map(data->map, &data->player, data->img_data);
|
||||
matrix_to_image(data);
|
||||
mlx_put_image_to_window(data->mlx, data->mlx_win,
|
||||
data->img_data->img, 0, 0);
|
||||
mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT);
|
||||
|
|
|
|||
|
|
@ -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++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
22
src/raycast/raycaster.h
Normal file
22
src/raycast/raycaster.h
Normal 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
|
||||
67
src/raycast/render.c
Normal file
67
src/raycast/render.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* render.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/07/30 12:50:10 by tchampio #+# #+# */
|
||||
/* Updated: 2025/07/30 16:22:42 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;
|
||||
static int texture_size = 64; // to be changed in favor of a const
|
||||
|
||||
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; // cc complains otherwise
|
||||
(void)pos; // cc complains otherwise
|
||||
data->screen_matrix[ray->draw_start][x] = color;
|
||||
ray->draw_start++;
|
||||
}
|
||||
}
|
||||
54
src/renderer/render.c
Normal file
54
src/renderer/render.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* render.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/07/30 14:53:39 by tchampio #+# #+# */
|
||||
/* Updated: 2025/07/30 16:21:18 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
21
src/renderer/render.h
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue