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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
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, 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},
@ -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}
};
static int initalgo()
{
// 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;
return (0);
}
void draw_screen(void) {
// TODO manage image format error better
void draw_screen(void)
{
int bpp;
int size_line;
int endian;
void *img_ptr;
u_int32_t *img_data;
void *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_ptr = mlx_new_image(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT);
img_data = (u_int32_t *)
mlx_get_data_addr(img_ptr, &bpp, &size_line, &endian);
if (bpp != 32 || endian != 0)
{
// TODO manage error better
printf("image format error (got bpp == %i, endian == %i)\n", bpp, endian);
printf("image format error\n");
exit(1);
}
render(img_data);
@ -72,30 +77,26 @@ void draw_screen(void) {
void load_textures(t_tex *tex)
{
int width, height;
// Charger les textures
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);
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);
}
int main(void)
{
// Initialisation
g_mlx = mlx_init();
g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d");
initalgo();
// hook keypress
mlx_hook(g_win, 2, 1L<<0, keypress, NULL);
// render the initial frame
mlx_hook(g_win, KeyPress, KeyPressMask, keypress, NULL);
draw_screen();
// start the mlx loop
mlx_loop(g_mlx);
return (0);
}

34
algo.h
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 "Libft/libft.h"
#define mapWidth 24 // cases (TODO test)
#define mapHeight 24 // cases (TODO test)
#define texWidth 64
#define texHeight 64
# define MAP_WIDTH 24 // cases (TODO test)
# define MAP_HEIGHT 24 // cases (TODO test)
# define TEX_WIDTH 64
# define TEX_HEIGHT 64
# define MOVE_SPEED 0.1 // cases
# 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_CEILING 0x29f8ff // 0xRRGGBB
# define COLOR_FLOOR 0xFF985C // 0xRRGGBB
@ -34,13 +34,22 @@
# define SCREEN_HEIGHT 480 // px
# 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
{
// pos player on map (cases)
double posX;
// pos player on map (cases)
double posY;
t_point_double pos;
// player rotation (rad)
double rot;
} t_ray;
@ -48,7 +57,7 @@ typedef struct s_ray
extern void *g_mlx;
extern void *g_win;
extern t_ray g_ray;
extern int worldMap[mapWidth][mapHeight];
extern int g_world_map[MAP_WIDTH][MAP_HEIGHT];
typedef struct s_tex
{
@ -60,8 +69,7 @@ typedef struct s_tex
} t_tex;
void vector_from_rotation(
double *vecX, double *vecY, double angle, double norm);
void vector_from_rotation(t_point_double *vec, double angle, double norm);
int keypress(int keycode);

43
move.c
View file

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

154
render.c
View file

@ -6,59 +6,60 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
static void calculate_perpwalldist3(int x,
double *raydirX, double *raydirY,
double *deltadistX, double *deltadistY)
t_point_double *raydir, t_point_double *deltadist)
{
double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1;
double dirX;
double dirY;
double planeX;
double planeY;
vector_from_rotation(&dirX, &dirY, g_ray.rot, 1);
vector_from_rotation(&planeX, &planeY, g_ray.rot + PI / 2, 1);
*raydirX = dirX + planeX * ray_direction;
*raydirY = dirY + planeY * ray_direction;
*deltadistX = (raydirX == 0) ? 1e30 : fabs(1 / *raydirX);
*deltadistY = (raydirY == 0) ? 1e30 : fabs(1 / *raydirY);
const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1;
t_point_double plane;
t_point_double dir;
vector_from_rotation(&dir, g_ray.rot, 1);
vector_from_rotation(&plane, g_ray.rot + PI / 2, 1);
raydir->x = dir.x + plane.x * ray_direction;
raydir->y = dir.y + plane.y * ray_direction;
deltadist->x = 1e30;
if (raydir->x != 0)
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,
double *sidedistX, double *sidedistY,
double *deltadistX, double *deltadistY,
int *stepX, int *stepY)
t_point_double *sidedist,
t_point_double *deltadist,
t_point_int *step)
{
int mapX = (int)g_ray.posX;
int mapY = (int)g_ray.posY;
double raydirX;
double raydirY;
const int map_x = (int)g_ray.pos.x;
const int map_y = (int)g_ray.pos.y;
t_point_double raydir;
calculate_perpwalldist3(x, &raydirX, &raydirY, deltadistX, deltadistY);
if (raydirX < 0)
calculate_perpwalldist3(x, &raydir, deltadist);
if (raydir.x < 0)
{
*stepX = -1;
*sidedistX = (g_ray.posX - mapX) * *deltadistX;
step->x = -1;
sidedist->x = (g_ray.pos.x - map_x) * deltadist->x;
}
else
{
*stepX = 1;
*sidedistX = (mapX + 1.0 - g_ray.posX) * *deltadistX;
step->x = 1;
sidedist->x = (map_x + 1.0 - g_ray.pos.x) * deltadist->x;
}
if (raydirY < 0)
if (raydir.y < 0)
{
*stepY = -1;
*sidedistY = (g_ray.posY - mapY) * *deltadistY;
step->y = -1;
sidedist->y = (g_ray.pos.y - map_y) * deltadist->y;
}
else
{
*stepY = 1;
*sidedistY = (mapY + 1.0 - g_ray.posY) * *deltadistY;
step->y = 1;
sidedist->y = (map_y + 1.0 - g_ray.pos.y) * deltadist->y;
}
}
@ -69,38 +70,31 @@ static void calculate_perpwalldist2(int x,
/// @return Result.
static double calculate_perpwalldist(int x)
{
int mapX = (int)g_ray.posX;
int mapY = (int)g_ray.posY;
t_point_int map_pos;
bool side;
double sidedistX;
double sidedistY;
double deltadistX;
double deltadistY;
int stepX;
int stepY;
calculate_perpwalldist2(x, &sidedistX, &sidedistY, &deltadistX, &deltadistY, &stepX, &stepY);
while(worldMap[mapX][mapY] != 1)
t_point_double sidedist;
t_point_double deltadist;
t_point_int step;
map_pos.x = (int)g_ray.pos.x;
map_pos.y = (int)g_ray.pos.y;
calculate_perpwalldist2(x, &sidedist, &deltadist, &step);
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(sidedistX < sidedistY)
if (sidedist.x < sidedist.y)
{
sidedistX += deltadistX;
mapX += stepX;
sidedist.x += deltadist.x;
map_pos.x += step.x;
side = false;
continue ;
}
else
{
sidedistY += deltadistY;
mapY += stepY;
sidedist.y += deltadist.y;
map_pos.y += step.y;
side = true;
}
}
double perpwalldist;
if (!side)
perpwalldist = (sidedistX - deltadistX);
else
perpwalldist = (sidedistY - deltadistY);
return (perpwalldist);
return (sidedist.x - deltadist.x);
return (sidedist.y - deltadist.y);
}
/// @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.
static void draw_vertical_line(int x, u_int32_t *img_data)
{
double perpwalldist = calculate_perpwalldist(x);
int lineHeight = (int)(SCREEN_HEIGHT / perpwalldist);
int drawStart = -lineHeight / 2 + SCREEN_HEIGHT / 2;
if(drawStart < 0)
drawStart = 0;
int drawEnd = lineHeight / 2 + SCREEN_HEIGHT / 2;
if(drawEnd >= SCREEN_HEIGHT)
drawEnd = SCREEN_HEIGHT - 1;
int y = 0;
while (y < drawStart)
{
img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING;
y++;
}
while (y < drawEnd)
{
img_data[y * SCREEN_WIDTH + x] = COLOR_WALL;
y++;
}
const double perpwalldist = calculate_perpwalldist(x);
const int line_height = (int)(SCREEN_HEIGHT / perpwalldist);
int draw_start;
int draw_end;
int y;
draw_start = -line_height / 2 + SCREEN_HEIGHT / 2;
if (draw_start < 0)
draw_start = 0;
draw_end = line_height / 2 + SCREEN_HEIGHT / 2;
if (draw_end >= SCREEN_HEIGHT)
draw_end = SCREEN_HEIGHT - 1;
y = 0;
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;
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 x; // px
// 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);
int x;
x = 0;
while (x < SCREEN_WIDTH)
@ -152,6 +137,5 @@ int render(u_int32_t *img_data)
draw_vertical_line(x, img_data);
x++;
}
return (0);
}

View file

@ -6,14 +6,14 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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;
*vecY = sin(angle) * norm;
vec->x = -cos(angle) * norm;
vec->y = sin(angle) * norm;
}