1
0

🏗️」 wip: DDA setup finished and loop started

This commit is contained in:
2024-10-07 11:25:31 +02:00
parent e11c60b808
commit c7fd02590a
14 changed files with 258 additions and 56 deletions

18
includes/game/constant.h Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* constant.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 16:39:48 by adjoly #+# #+# */
/* Updated: 2024/10/04 16:40:10 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CONSTANT_H
# define CONSTANT_H
# define WHITE 0xFFFFFFFF
#endif

26
includes/game/dda.h Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dda.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 14:04:10 by adjoly #+# #+# */
/* Updated: 2024/10/06 18:48:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DDA_H
# define DDA_H
# include "typedef.h"
/**
* @brief Function used to get all the ray angle
*
* @param player The address of the t_player struct
* @param dda The address of the t_dda struct
*/
void get_ray_angle(t_player *player, t_dda (*dda)[800]);
#endif

View File

@ -6,55 +6,24 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */ /* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */
/* Updated: 2024/09/29 16:47:32 by adjoly ### ########.fr */ /* Updated: 2024/10/06 18:48:33 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef GAME_H #ifndef GAME_H
# define GAME_H # define GAME_H
# include "dda.h"
# include "settings.h"
# include "parsing.h" # include "parsing.h"
# include "typedef.h"
# include <stdint.h> # include <stdint.h>
# include <stdlib.h>
# include <math.h> # include <math.h>
# include <stdbool.h> # include <stdbool.h>
# include <unistd.h> # include <unistd.h>
# define ESCAPE_KEY 41
# define W_KEY 26
# define S_KEY 22
# define A_KEY 4
# define D_KEY 7
# define WHITE 0xFFFFFFFF
# define WINDOW_Y 900
# define WINDOW_X 1600
# define PLAYER_ROT_SPEED (2 * M_PI) / 8
# define PLAYER_SPEED 3
# define MAP_CHUNK_SIZE 64
# define RAY_SIZE 2
# define FOV 60 * (M_PI / 180)
typedef struct s_ray_dir
{
float x;
float y;
} t_ray_dir;
typedef struct s_player
{
t_coord coords;
double direction;
t_ray_dir ray_dir[WINDOW_X / RAY_SIZE];
} t_player;
typedef struct s_cub
{
void *mlx;
void *win;
void *sprites;
void *img;
char **map;
t_player player;
} t_cub;
/** /**
* @brief This function is used to handle keypress * @brief This function is used to handle keypress
@ -105,4 +74,5 @@ void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color);
* @param cub The address of the t_cub struct * @param cub The address of the t_cub struct
*/ */
void print_map(t_cub *cub); void print_map(t_cub *cub);
#endif #endif

32
includes/game/settings.h Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* settings.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 14:06:39 by adjoly #+# #+# */
/* Updated: 2024/10/04 16:38:10 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SETTINGS_H
# define SETTINGS_H
// Key code
# define ESCAPE_KEY 41
# define W_KEY 26
# define S_KEY 22
# define A_KEY 4
# define D_KEY 7
# define WHITE 0xFFFFFFFF
# define WINDOW_Y 900
# define WINDOW_X 1600
# define PLAYER_ROT_SPEED (2 * M_PI) / 8
# define PLAYER_SPEED 3
# define MAP_CHUNK_SIZE 64
# define RAY_SIZE 2
# define FOV 60 * (M_PI / 180)
#endif

64
includes/game/typedef.h Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* typedef.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 14:28:24 by adjoly #+# #+# */
/* Updated: 2024/10/06 19:03:26 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STRUCT_H
# define STRUCT_H
# include "parsing.h"
# include <stdbool.h>
typedef struct s_coord_f
{
float x;
float y;
} t_coord_f;
typedef struct s_step
{
char x;
char y;
} t_step;
typedef struct s_player
{
t_coord coords;
double direction;
} t_player;
typedef struct s_cub
{
void *mlx;
void *win;
void *sprites;
void *img;
char **map;
t_player player;
} t_cub;
typedef enum s_wall_side
{
HORIZONTAL,
VERTICAL
} t_wall_side;
typedef struct s_dda
{
t_coord_f ray_dir;
t_coord_f side_dist;
t_coord_f delta_dist;
t_step step;
t_wall_side wall_side;
bool wall_hit;
} t_dda;
#endif

View File

@ -6,12 +6,12 @@
/* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */ /* By: madumerg <madumerg@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */ /* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */
/* Updated: 2024/09/29 14:44:07 by adjoly ### ########.fr */ /* Updated: 2024/10/06 17:58:41 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
#include "game.h" #include "game/game.h"
#include "mlx.h" #include "mlx.h"
int main(int ac, char **av) int main(int ac, char **av)

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dda_while.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/06 18:46:56 by adjoly #+# #+# */
/* Updated: 2024/10/06 19:05:11 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/game.h"
void dda_while(t_dda *dda, char **map, t_coord map_coord)
{
dda->wall_hit = false;
while (!dda->wall_hit)
{
if (dda->side_dist.x < dda->side_dist.y)
{
dda->side_dist.x += dda->delta_dist.x;
map_coord.x += dda->step.x;
dda->wall_side = HORIZONTAL;
}
else
{
dda->side_dist.y += dda->delta_dist.y;
map_coord.y += dda->step.y;
dda->wall_side = VERTICAL;
}
if (map[map_coord.x][map_coord.y] == '1')
dda->wall_hit = true;
}
}

View File

@ -6,13 +6,12 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */ /* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */
/* Updated: 2024/09/29 14:44:28 by adjoly ### ########.fr */ /* Updated: 2024/10/05 14:30:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game/game.h"
#include "mlx.h" #include <stdio.h>
#include "stdio.h"
void change_direction(double speed, bool clockwise, t_player *player) void change_direction(double speed, bool clockwise, t_player *player)
{ {
@ -24,7 +23,6 @@ void change_direction(double speed, bool clockwise, t_player *player)
player->direction -= 2 * M_PI; player->direction -= 2 * M_PI;
else if (player->direction < 0) else if (player->direction < 0)
player->direction += 2 * M_PI; player->direction += 2 * M_PI;
printf("%f\n", player->direction);
} }
void get_player_image(t_cub *cub, uint8_t key_pressed) void get_player_image(t_cub *cub, uint8_t key_pressed)

View File

@ -6,13 +6,13 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/29 15:26:29 by adjoly #+# #+# */ /* Created: 2024/09/29 15:26:29 by adjoly #+# #+# */
/* Updated: 2024/09/29 16:46:44 by adjoly ### ########.fr */ /* Updated: 2024/10/06 17:58:04 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game/game.h"
void get_ray_angle(t_player *player) void get_ray_angle(t_player *player, t_dda (*dda)[800])
{ {
uint16_t x; uint16_t x;
float ray_angle; float ray_angle;
@ -21,8 +21,8 @@ void get_ray_angle(t_player *player)
while (x < WINDOW_X) while (x < WINDOW_X)
{ {
ray_angle = player->direction - (FOV / 2) + (x / WINDOW_X) * FOV; ray_angle = player->direction - (FOV / 2) + (x / WINDOW_X) * FOV;
player->ray_angle[x / RAY_SIZE].x = cos(ray_angle); dda[x / RAY_SIZE]->ray_dir.x = cos(ray_angle);
player->ray_angle[x / RAY_SIZE].y = sin(ray_angle); dda[x / RAY_SIZE]->ray_dir.y = sin(ray_angle);
x += RAY_SIZE; x += RAY_SIZE;
} }
} }

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 11:09:52 by adjoly #+# #+# */ /* Created: 2024/09/17 11:09:52 by adjoly #+# #+# */
/* Updated: 2024/09/23 14:05:54 by adjoly ### ########.fr */ /* Updated: 2024/10/06 15:41:55 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game/game.h"
#include "mlx.h" #include "mlx.h"
void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color) void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color)

View File

@ -6,13 +6,14 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */ /* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */
/* Updated: 2024/09/29 14:45:02 by adjoly ### ########.fr */ /* Updated: 2024/10/04 15:29:00 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h> #include <stdio.h>
#include "mlx.h" #include "mlx.h"
#include "game.h" #include "parsing.h"
#include "game/game.h"
int key_hook(int key, void *param) int key_hook(int key, void *param)
{ {

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/23 14:09:17 by adjoly #+# #+# */ /* Created: 2024/09/23 14:09:17 by adjoly #+# #+# */
/* Updated: 2024/09/23 14:41:51 by adjoly ### ########.fr */ /* Updated: 2024/10/06 17:58:25 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game/game.h"
#include "mlx.h" #include "mlx.h"
#include <stdio.h> #include <stdio.h>

View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* setup_dda.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 23:47:14 by adjoly #+# #+# */
/* Updated: 2024/10/06 19:02:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "game/game.h"
#include <stdbool.h>
#include <math.h>
void get_side_dist(t_dda *dda, t_coord incell_coords, t_player *player)
{
if (dda->ray_dir.x < 0)
{
dda->side_dist.x = incell_coords.x * dda->delta_dist.x;
dda->step.x = -MAP_CHUNK_SIZE;
}
else
{
dda->side_dist.x = (((int)player->coords.x + MAP_CHUNK_SIZE) - player->coords.x) * dda->delta_dist.x;
dda->step.x = MAP_CHUNK_SIZE;
}
if (dda->ray_dir.y < 0)
{
dda->side_dist.y = incell_coords.y * dda->delta_dist.y;
dda->step.y = -MAP_CHUNK_SIZE;
}
else
{
dda->side_dist.y = (((int)player->coords.y + MAP_CHUNK_SIZE) - player->coords.y) * dda->delta_dist.y;
dda->step.y = MAP_CHUNK_SIZE;
}
}
void setup_dda(t_player *player)
{
t_dda dda[WINDOW_X / RAY_SIZE];
size_t i;
t_coord incell_coords;
i = 0;
get_ray_angle(player, &dda);
incell_coords.x = player->coords.x % MAP_CHUNK_SIZE;
incell_coords.y = player->coords.y % MAP_CHUNK_SIZE;
while (i < WINDOW_X / RAY_SIZE)
{
dda[i].delta_dist.x = abs((int)(MAP_CHUNK_SIZE / dda[i].ray_dir.x));
dda[i].delta_dist.y = abs((int)(MAP_CHUNK_SIZE / dda[i].ray_dir.y));
get_side_dist(&dda[i], incell_coords, player);
i++;
}
}