merge everything

This commit is contained in:
mcolonna 2024-10-31 15:36:01 +01:00
commit b8e2065a90
9 changed files with 149 additions and 66 deletions

View file

@ -2,7 +2,7 @@ NAME = cub3d
CPP = gcc $(FLAGS)
LIBRARIES_DIR = Libft Minilibx
LIBRARIES_A = Libft/libft.a Minilibx/libmlx.a
CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx $(FLAGS_1)
CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx $(FLAGS_1) -g
CPP_2 = $(CPP) $(FLAGS_2)
SHARED = -lX11 -lXext -lm
C_FILES = $(wildcard **.c)

41
algo.c
View file

@ -3,50 +3,53 @@
/* ::: :::::::: */
/* algo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/29 14:10:10 by mcolonna ### ########.fr */
/* Updated: 2024/10/31 15:26:48 by mc ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
#include "input.h"
#include <stdio.h>
void *g_mlx = NULL;
void *g_win = NULL;
t_map g_map;
t_player g_player;
// t_tex g_tex;
static void load_textures(t_tex *tex)
{
tex->tex_width = 64;
tex->tex_height = 64;
tex->textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[2] = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[3] = mlx_xpm_file_to_image(g_mlx, "textures/west.xpm",
&tex->tex_width, &tex->tex_height);
}
void draw_screen(void)
{
void *img_ptr;
u_int32_t *img_data;
t_tex tex;
load_textures(&tex);
img_ptr = mlx_new_image(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT);
img_data = get_data_addr(img_ptr);
if (!img_data)
exit(1);
render(img_data);
render(img_data, &tex);
mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0);
mlx_destroy_image(g_mlx, img_ptr);
}
void load_textures(t_tex *tex)
{
int width;
int height;
tex->tex_north = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm",
&width, &height);
tex->tex_south = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm",
&width, &height);
tex->tex_east = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm",
&width, &height);
tex->tex_west = mlx_xpm_file_to_image(g_mlx, "textures/west.xpm",
&width, &height);
}
static void loop(void)
{
move();
@ -65,6 +68,8 @@ static int loop_hook(void *param)
return (0);
}
int main(int argc, char *argv[])
{
g_mlx = mlx_init();

43
algo.h
View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* algo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
/* Updated: 2024/10/17 15:07:14 by mcolonna ### ########.fr */
/* Updated: 2024/10/24 14:27:27 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,20 +41,33 @@
# define FOV 0.66 // ? TODO unit
# define HITBOX 0.25 // cases. should be more than MOVE_SPEED
typedef struct s_tex
{
void *textures[4];
void *current_tex;
int tex_height;
int tex_width;
int tex_dir;
int tex_x;
int tex_y;
int bpp;
int size_line;
int endian;
} t_tex;
typedef struct s_ray
{
// pos player on map (cases)
t_point_double dir;
bool side;
double wallx;
} t_ray;
extern void *g_mlx;
extern void *g_win;
extern t_player g_player;
extern t_map g_map;
typedef struct s_tex
{
void *tex_north;
void *tex_south;
void *tex_east;
void *tex_west;
void *current_tex;
} t_tex;
// extern t_tex g_tex;
/// @brief Write an error message on stderr.
///
@ -63,10 +76,14 @@ void write_err(const char *str, ...);
void vector_from_rotation(t_point_double *vec, double angle, double norm);
int render(u_int32_t *img_data);
int render(u_int32_t *img_data, t_tex *tex);
void draw_screen(void);
void move(void);
void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex);
void draw_wall(t_tex *tex, t_ray *ray, const int line_height, int x,
int y, u_int32_t *img_data);
#endif

View file

@ -3,17 +3,19 @@
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 14:55:05 by greg #+# #+# */
/* Updated: 2024/10/15 16:55:02 by mcolonna ### ########.fr */
/* Updated: 2024/10/24 14:27:52 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
#include <stdio.h>
static void calculate_perpwalldist3(int x,
t_point_double *raydir, t_point_double *deltadist)
t_point_double *deltadist,
t_ray *ray)
{
const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1;
t_point_double plane;
@ -21,27 +23,27 @@ static void calculate_perpwalldist3(int x,
vector_from_rotation(&dir, g_map.player.rot, 1);
vector_from_rotation(&plane, g_map.player.rot + PI / 2, 1);
raydir->x = dir.x + plane.x * ray_direction;
raydir->y = dir.y + plane.y * ray_direction;
ray->dir.x = dir.x + plane.x * ray_direction;
ray->dir.y = dir.y + plane.y * ray_direction;
deltadist->x = 1e30;
if (raydir->x != 0)
deltadist->x = fabs(1 / raydir->x);
if (ray->dir.x != 0)
deltadist->x = fabs(1 / ray->dir.x);
deltadist->y = 1e30;
if (raydir->y != 0)
deltadist->y = fabs(1 / raydir->y);
if (ray->dir.y != 0)
deltadist->y = fabs(1 / ray->dir.y);
}
static void calculate_perpwalldist2(int x,
t_point_double *sidedist,
t_point_double *deltadist,
t_point_int *step)
t_point_int *step,
t_ray *ray)
{
const int map_x = (int)g_map.player.pos.x;
const int map_y = (int)g_map.player.pos.y;
t_point_double raydir;
calculate_perpwalldist3(x, &raydir, deltadist);
if (raydir.x < 0)
calculate_perpwalldist3(x, deltadist, ray);
if (ray->dir.x < 0)
{
step->x = -1;
sidedist->x = (g_map.player.pos.x - map_x) * deltadist->x;
@ -51,7 +53,7 @@ static void calculate_perpwalldist2(int x,
step->x = 1;
sidedist->x = (map_x + 1.0 - g_map.player.pos.x) * deltadist->x;
}
if (raydir.y < 0)
if (ray->dir.y < 0)
{
step->y = -1;
sidedist->y = (g_map.player.pos.y - map_y) * deltadist->y;
@ -68,31 +70,30 @@ static void calculate_perpwalldist2(int x,
///
/// @param x Ray direction x between 0 and 1.
/// @return Result.
static double calculate_perpwalldist(int x)
static double calculate_perpwalldist(int x, t_ray *ray)
{
t_point_int map_pos;
bool side;
t_point_double sidedist;
t_point_double deltadist;
t_point_int step;
map_pos.x = (int)g_map.player.pos.x;
map_pos.y = (int)g_map.player.pos.y;
calculate_perpwalldist2(x, &sidedist, &deltadist, &step);
calculate_perpwalldist2(x, &sidedist, &deltadist, &step, ray);
while (map_get_case(&g_map, map_pos.x, map_pos.y)->wall == EMPTY)
{
if (sidedist.x < sidedist.y)
{
sidedist.x += deltadist.x;
map_pos.x += step.x;
side = false;
ray->side = false;
continue ;
}
sidedist.y += deltadist.y;
map_pos.y += step.y;
side = true;
ray->side = true;
}
if (!side)
if (!ray->side)
return (sidedist.x - deltadist.x);
return (sidedist.y - deltadist.y);
}
@ -100,14 +101,16 @@ static double calculate_perpwalldist(int x)
/// @brief Draw a vertical line according to the ray direction x.
///
/// @param x Ray direction x between 0 and 1.
static void draw_vertical_line(int x, u_int32_t *img_data)
static void draw_vertical_line(int x, u_int32_t *img_data,
t_ray *ray, t_tex *tex)
{
const double perpwalldist = calculate_perpwalldist(x);
const double perpwalldist = calculate_perpwalldist(x, ray);
const int line_height = (int)(SCREEN_HEIGHT / perpwalldist);
int draw_start;
int draw_end;
int y;
ft_init_texture(perpwalldist, ray, tex);
draw_start = -line_height / 2 + SCREEN_HEIGHT / 2;
if (draw_start < 0)
draw_start = 0;
@ -115,26 +118,28 @@ static void draw_vertical_line(int x, u_int32_t *img_data)
if (draw_end >= SCREEN_HEIGHT)
draw_end = SCREEN_HEIGHT - 1;
y = 0;
while (y < SCREEN_HEIGHT)
while (y++ < SCREEN_HEIGHT)
{
if (y < draw_start)
img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING;
else if (y < draw_end)
img_data[y * SCREEN_WIDTH + x] = COLOR_WALL;
{
draw_wall(tex, ray, line_height, x, y, img_data);
}
else
img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR;
y++;
}
}
int render(u_int32_t *img_data)
int render(u_int32_t *img_data, t_tex *tex)
{
int x;
int x;
t_ray ray;
x = 0;
while (x < SCREEN_WIDTH)
{
draw_vertical_line(x, img_data);
draw_vertical_line(x, img_data, &ray, tex);
x++;
}
return (0);

51
render2.c Normal file
View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/24 14:26:22 by grobledo #+# #+# */
/* Updated: 2024/10/24 14:27:37 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex)
{
if (ray->side == 0 && ray->dir.x < 0)
tex->tex_dir = 0;
if (ray->side == 0 && ray->dir.x >= 0)
tex->tex_dir = 1;
if (ray->side == 1 && ray->dir.y < 0)
tex->tex_dir = 2;
if (ray->side == 1 && ray->dir.y >= 0)
tex->tex_dir = 3;
if (ray->side == 0)
ray->wallx = g_map.player.pos.y + perpwalldist \
* ray->dir.y;
else
ray->wallx = g_map.player.pos.x + perpwalldist \
* ray->dir.x;
ray->wallx -= floor((ray->wallx));
}
void draw_wall(t_tex *tex, t_ray *ray, const int line_height, int x,
int y, u_int32_t *img_data)
{
u_int32_t color;
u_int32_t *texture_data;
tex->tex_y = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) \
* tex->tex_height) / line_height;
tex->tex_x = (int)(ray->wallx * (double)tex->tex_width);
if (ray->side == 0 && ray->dir.x > 0)
tex->tex_x = tex->tex_width - tex->tex_x - 1;
if (ray->side == 1 && ray->dir.y < 0)
tex->tex_x = tex->tex_height - tex->tex_x - 1;
texture_data = (u_int32_t *)mlx_get_data_addr(tex->textures[tex->tex_dir],
&tex->bpp, &tex->size_line, &tex->endian);
color = texture_data[tex->tex_x * tex->tex_width + tex->tex_y];
img_data[y * SCREEN_WIDTH + x] = color;
}

View file

@ -6,7 +6,7 @@ SO SOUTH!!!!!!1
WE weeeee
111111111
100000001
100000001
100010001
100000001
1000N0001
100000001

View file

@ -4,7 +4,7 @@ NO theimageforthenorthwall
C 0,2,67
SO SOUTH!!!!!!1
WE weeeee
111
111111111
111110111
10000N001
111110111

View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */
/* Updated: 2024/10/29 14:01:59 by mcolonna ### ########.fr */
/* Updated: 2024/10/17 16:36:14 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 16:56:47 by mcolonna #+# #+# */
/* Updated: 2024/10/29 14:13:28 by marvin ### ########.fr */
/* Updated: 2024/10/31 15:31:21 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,10 @@
# define UTILS_H
# include <linux/types.h>
# include <stdint.h>
# include <stddef.h>
typedef uint32_t u_int32_t;
typedef struct s_point_double
{
@ -27,6 +31,7 @@ typedef struct s_point_int
int y;
} t_point_int;
// TODO Must the transparency be 0 or 255?
/// @brief Represents an TRGB color in 0xTTRRGGBB format.
typedef __u32 t_color;