norminette

This commit is contained in:
mcolonna 2024-10-15 16:03:18 +02:00
parent ba87f4a37c
commit 800ddb97ce
5 changed files with 229 additions and 227 deletions

57
algo.c
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/15 14:10:11 by mcolonna ### ########.fr */ /* Updated: 2024/10/15 16:00:41 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,8 +16,7 @@ void *g_mlx = NULL;
void *g_win = NULL; void *g_win = NULL;
t_ray g_ray; t_ray g_ray;
int worldMap[mapWidth][mapHeight] = int g_world_map[MAP_WIDTH][MAP_HEIGHT] = {
{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
@ -44,26 +43,32 @@ int worldMap[mapWidth][mapHeight] =
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}; };
static int initalgo()
{
// TODO from map // TODO from map
g_ray.posX = 14;
g_ray.posY = 6.5; static int initalgo(void)
{
g_ray.pos.x = 14;
g_ray.pos.y = 6.5;
g_ray.rot = 0; g_ray.rot = 0;
return (0); return (0);
} }
void draw_screen(void) { // TODO manage image format error better
void draw_screen(void)
{
int bpp; int bpp;
int size_line; int size_line;
int endian; int endian;
void *img_ptr;
u_int32_t *img_data;
void *img_ptr = mlx_new_image(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT); img_ptr = mlx_new_image(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT);
u_int32_t *img_data = (u_int32_t *)mlx_get_data_addr(img_ptr, &bpp, &size_line, &endian); img_data = (u_int32_t *)
mlx_get_data_addr(img_ptr, &bpp, &size_line, &endian);
if (bpp != 32 || endian != 0) if (bpp != 32 || endian != 0)
{ {
// TODO manage error better printf("image format error\n");
printf("image format error (got bpp == %i, endian == %i)\n", bpp, endian);
exit(1); exit(1);
} }
render(img_data); render(img_data);
@ -72,30 +77,26 @@ void draw_screen(void) {
void load_textures(t_tex *tex) void load_textures(t_tex *tex)
{ {
int width, height; int width;
// Charger les textures 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_north = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm",
tex->tex_east = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", &width, &height); &width, &height);
tex->tex_west = mlx_xpm_file_to_image(g_mlx, "textures/west.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);
} }
int main(void) int main(void)
{ {
// Initialisation
g_mlx = mlx_init(); g_mlx = mlx_init();
g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d"); g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d");
initalgo(); initalgo();
mlx_hook(g_win, KeyPress, KeyPressMask, keypress, NULL);
// hook keypress
mlx_hook(g_win, 2, 1L<<0, keypress, NULL);
// render the initial frame
draw_screen(); draw_screen();
// start the mlx loop
mlx_loop(g_mlx); mlx_loop(g_mlx);
return (0); return (0);
} }

34
algo.h
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
/* Updated: 2024/10/15 14:06:42 by mcolonna ### ########.fr */ /* Updated: 2024/10/15 15:48:45 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,13 +20,13 @@
# include "Minilibx/mlx_int.h" # include "Minilibx/mlx_int.h"
# include "Libft/libft.h" # include "Libft/libft.h"
#define mapWidth 24 // cases (TODO test) # define MAP_WIDTH 24 // cases (TODO test)
#define mapHeight 24 // cases (TODO test) # define MAP_HEIGHT 24 // cases (TODO test)
#define texWidth 64 # define TEX_WIDTH 64
#define texHeight 64 # define TEX_HEIGHT 64
# define MOVE_SPEED 0.1 // cases # define MOVE_SPEED 0.1 // cases
# define PI 3.1415926535 # define PI 3.1415926535
#define ROT_SPEED (PI / 16) // rad # define ROT_SPEED_DIVIDE_PI 16 // pi/n rad
# define COLOR_WALL 0xFF0000 // 0xRRGGBB # define COLOR_WALL 0xFF0000 // 0xRRGGBB
# define COLOR_CEILING 0x29f8ff // 0xRRGGBB # define COLOR_CEILING 0x29f8ff // 0xRRGGBB
# define COLOR_FLOOR 0xFF985C // 0xRRGGBB # define COLOR_FLOOR 0xFF985C // 0xRRGGBB
@ -34,13 +34,22 @@
# define SCREEN_HEIGHT 480 // px # define SCREEN_HEIGHT 480 // px
# define FOV 0.66 // ? TODO unit # define FOV 0.66 // ? TODO unit
typedef struct s_point_double
{
double x;
double y;
} t_point_double;
typedef struct s_point_int
{
int x;
int y;
} t_point_int;
typedef struct s_ray typedef struct s_ray
{ {
// pos player on map (cases) // pos player on map (cases)
double posX; t_point_double pos;
// pos player on map (cases)
double posY;
// player rotation (rad) // player rotation (rad)
double rot; double rot;
} t_ray; } t_ray;
@ -48,7 +57,7 @@ typedef struct s_ray
extern void *g_mlx; extern void *g_mlx;
extern void *g_win; extern void *g_win;
extern t_ray g_ray; extern t_ray g_ray;
extern int worldMap[mapWidth][mapHeight]; extern int g_world_map[MAP_WIDTH][MAP_HEIGHT];
typedef struct s_tex typedef struct s_tex
{ {
@ -60,8 +69,7 @@ typedef struct s_tex
} t_tex; } t_tex;
void vector_from_rotation( void vector_from_rotation(t_point_double *vec, double angle, double norm);
double *vecX, double *vecY, double angle, double norm);
int keypress(int keycode); int keypress(int keycode);

43
move.c
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */ /* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
/* Updated: 2024/10/15 13:27:10 by mcolonna ### ########.fr */ /* Updated: 2024/10/15 16:01:08 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,40 +14,50 @@
static int move_forward(void) static int move_forward(void)
{ {
double dirX, dirY; t_point_double dir;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
if (worldMap[(int)((g_ray.posX + dirX * MOVE_SPEED))][(int)(g_ray.posY)] != 1) vector_from_rotation(&dir, g_ray.rot, 1);
g_ray.posX += dirX * MOVE_SPEED; if (g_world_map[(int)((g_ray.pos.x + dir.x * MOVE_SPEED))]
if (worldMap[(int)(g_ray.posX)][(int)(g_ray.posY + dirY * MOVE_SPEED)] != 1) [(int)(g_ray.pos.y)] != 1
g_ray.posY += dirY * MOVE_SPEED; )
g_ray.pos.x += dir.x * MOVE_SPEED;
if (g_world_map[(int)(g_ray.pos.x)]
[(int)(g_ray.pos.y + dir.y * MOVE_SPEED)] != 1
)
g_ray.pos.y += dir.y * MOVE_SPEED;
return (0); return (0);
} }
static int move_backward(void) static int move_backward(void)
{ {
double dirX, dirY; t_point_double dir;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
if (worldMap[(int)(g_ray.posX - dirX * MOVE_SPEED)][(int)(g_ray.posY)] != 1) vector_from_rotation(&dir, g_ray.rot, 1);
g_ray.posX -= dirX * MOVE_SPEED; if (g_world_map[(int)(g_ray.pos.x - dir.x * MOVE_SPEED)]
if (worldMap[(int)(g_ray.posX)][(int)(g_ray.posY - dirY * MOVE_SPEED)] != 1) [(int)(g_ray.pos.y)] != 1
g_ray.posY -= dirY * MOVE_SPEED; )
g_ray.pos.x -= dir.x * MOVE_SPEED;
if (g_world_map[(int)(g_ray.pos.x)]
[(int)(g_ray.pos.y - dir.y * MOVE_SPEED)] != 1
)
g_ray.pos.y -= dir.y * MOVE_SPEED;
return (0); return (0);
} }
static int move_right(void) static int move_right(void)
{ {
g_ray.rot += ROT_SPEED; g_ray.rot += PI / ROT_SPEED_DIVIDE_PI;
return (0); return (0);
} }
static int move_left(void) static int move_left(void)
{ {
g_ray.rot -= ROT_SPEED; g_ray.rot -= PI / ROT_SPEED_DIVIDE_PI;
return (0); return (0);
} }
// TODO leaks when quit?
int keypress(int keycode) int keypress(int keycode)
{ {
if (keycode == XK_Up || keycode == XK_z || keycode == XK_w) if (keycode == XK_Up || keycode == XK_z || keycode == XK_w)
@ -61,9 +71,8 @@ int keypress(int keycode)
if (keycode == XK_Escape) if (keycode == XK_Escape)
{ {
mlx_destroy_window(g_mlx, g_win); mlx_destroy_window(g_mlx, g_win);
exit(0); // TODO leaks? exit(0);
} }
// render the updated frame after key pressd
draw_screen(); draw_screen();
return (0); return (0);
} }

154
render.c
View file

@ -6,59 +6,60 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 14:55:05 by greg #+# #+# */ /* Created: 2024/10/14 14:55:05 by greg #+# #+# */
/* Updated: 2024/10/15 13:52:35 by mcolonna ### ########.fr */ /* Updated: 2024/10/15 15:59:55 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "algo.h" #include "algo.h"
static void calculate_perpwalldist3(int x, static void calculate_perpwalldist3(int x,
double *raydirX, double *raydirY, t_point_double *raydir, t_point_double *deltadist)
double *deltadistX, double *deltadistY)
{ {
double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1; const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1;
double dirX; t_point_double plane;
double dirY; t_point_double dir;
double planeX;
double planeY; vector_from_rotation(&dir, g_ray.rot, 1);
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1); vector_from_rotation(&plane, g_ray.rot + PI / 2, 1);
vector_from_rotation(&planeX, &planeY, g_ray.rot + PI / 2, 1); raydir->x = dir.x + plane.x * ray_direction;
*raydirX = dirX + planeX * ray_direction; raydir->y = dir.y + plane.y * ray_direction;
*raydirY = dirY + planeY * ray_direction; deltadist->x = 1e30;
*deltadistX = (raydirX == 0) ? 1e30 : fabs(1 / *raydirX); if (raydir->x != 0)
*deltadistY = (raydirY == 0) ? 1e30 : fabs(1 / *raydirY); deltadist->x = fabs(1 / raydir->x);
deltadist->y = 1e30;
if (raydir->y != 0)
deltadist->y = fabs(1 / raydir->y);
} }
static void calculate_perpwalldist2(int x, static void calculate_perpwalldist2(int x,
double *sidedistX, double *sidedistY, t_point_double *sidedist,
double *deltadistX, double *deltadistY, t_point_double *deltadist,
int *stepX, int *stepY) t_point_int *step)
{ {
int mapX = (int)g_ray.posX; const int map_x = (int)g_ray.pos.x;
int mapY = (int)g_ray.posY; const int map_y = (int)g_ray.pos.y;
double raydirX; t_point_double raydir;
double raydirY;
calculate_perpwalldist3(x, &raydirX, &raydirY, deltadistX, deltadistY); calculate_perpwalldist3(x, &raydir, deltadist);
if (raydirX < 0) if (raydir.x < 0)
{ {
*stepX = -1; step->x = -1;
*sidedistX = (g_ray.posX - mapX) * *deltadistX; sidedist->x = (g_ray.pos.x - map_x) * deltadist->x;
} }
else else
{ {
*stepX = 1; step->x = 1;
*sidedistX = (mapX + 1.0 - g_ray.posX) * *deltadistX; sidedist->x = (map_x + 1.0 - g_ray.pos.x) * deltadist->x;
} }
if (raydirY < 0) if (raydir.y < 0)
{ {
*stepY = -1; step->y = -1;
*sidedistY = (g_ray.posY - mapY) * *deltadistY; sidedist->y = (g_ray.pos.y - map_y) * deltadist->y;
} }
else else
{ {
*stepY = 1; step->y = 1;
*sidedistY = (mapY + 1.0 - g_ray.posY) * *deltadistY; sidedist->y = (map_y + 1.0 - g_ray.pos.y) * deltadist->y;
} }
} }
@ -69,38 +70,31 @@ static void calculate_perpwalldist2(int x,
/// @return Result. /// @return Result.
static double calculate_perpwalldist(int x) static double calculate_perpwalldist(int x)
{ {
int mapX = (int)g_ray.posX; t_point_int map_pos;
int mapY = (int)g_ray.posY;
bool side; bool side;
double sidedistX; t_point_double sidedist;
double sidedistY; t_point_double deltadist;
double deltadistX; t_point_int step;
double deltadistY;
int stepX; map_pos.x = (int)g_ray.pos.x;
int stepY; map_pos.y = (int)g_ray.pos.y;
calculate_perpwalldist2(x, &sidedistX, &sidedistY, &deltadistX, &deltadistY, &stepX, &stepY); calculate_perpwalldist2(x, &sidedist, &deltadist, &step);
while(worldMap[mapX][mapY] != 1) while (g_world_map[map_pos.x][map_pos.y] != 1)
{ {
// jump to next map square, either in x-direction, or in y-direction if (sidedist.x < sidedist.y)
if(sidedistX < sidedistY)
{ {
sidedistX += deltadistX; sidedist.x += deltadist.x;
mapX += stepX; map_pos.x += step.x;
side = false; side = false;
continue ;
} }
else sidedist.y += deltadist.y;
{ map_pos.y += step.y;
sidedistY += deltadistY;
mapY += stepY;
side = true; side = true;
} }
}
double perpwalldist;
if (!side) if (!side)
perpwalldist = (sidedistX - deltadistX); return (sidedist.x - deltadist.x);
else return (sidedist.y - deltadist.y);
perpwalldist = (sidedistY - deltadistY);
return (perpwalldist);
} }
/// @brief Draw a vertical line according to the ray direction x. /// @brief Draw a vertical line according to the ray direction x.
@ -108,27 +102,26 @@ static double calculate_perpwalldist(int x)
/// @param x Ray direction x between 0 and 1. /// @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)
{ {
double perpwalldist = calculate_perpwalldist(x); const double perpwalldist = calculate_perpwalldist(x);
int lineHeight = (int)(SCREEN_HEIGHT / perpwalldist); const int line_height = (int)(SCREEN_HEIGHT / perpwalldist);
int drawStart = -lineHeight / 2 + SCREEN_HEIGHT / 2; int draw_start;
if(drawStart < 0) int draw_end;
drawStart = 0; int y;
int drawEnd = lineHeight / 2 + SCREEN_HEIGHT / 2;
if(drawEnd >= SCREEN_HEIGHT) draw_start = -line_height / 2 + SCREEN_HEIGHT / 2;
drawEnd = SCREEN_HEIGHT - 1; if (draw_start < 0)
int y = 0; draw_start = 0;
while (y < drawStart) draw_end = line_height / 2 + SCREEN_HEIGHT / 2;
{ if (draw_end >= SCREEN_HEIGHT)
img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING; draw_end = SCREEN_HEIGHT - 1;
y++; y = 0;
}
while (y < drawEnd)
{
img_data[y * SCREEN_WIDTH + x] = COLOR_WALL;
y++;
}
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;
else
img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR; img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR;
y++; y++;
} }
@ -136,15 +129,7 @@ static void draw_vertical_line(int x, u_int32_t *img_data)
int render(u_int32_t *img_data) int render(u_int32_t *img_data)
{ {
int x; // px int x;
// clear image data
ft_bzero(img_data, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(int)); // TODO why
// 1
double dirX, dirY, planeX, planeY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
vector_from_rotation(&planeX, &planeY, g_ray.rot + PI/2, FOV);
x = 0; x = 0;
while (x < SCREEN_WIDTH) while (x < SCREEN_WIDTH)
@ -152,6 +137,5 @@ int render(u_int32_t *img_data)
draw_vertical_line(x, img_data); draw_vertical_line(x, img_data);
x++; x++;
} }
return (0); return (0);
} }

View file

@ -6,14 +6,14 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */ /* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */
/* Updated: 2024/10/15 12:39:15 by mcolonna ### ########.fr */ /* Updated: 2024/10/15 15:18:43 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "algo.h" #include "algo.h"
void vector_from_rotation(double *vecX, double *vecY, double angle, double norm) void vector_from_rotation(t_point_double *vec, double angle, double norm)
{ {
*vecX = -cos(angle) * norm; vec->x = -cos(angle) * norm;
*vecY = sin(angle) * norm; vec->y = sin(angle) * norm;
} }