possibility to choose angle of view
This commit is contained in:
@@ -1,13 +1,25 @@
|
|||||||
#ifndef CUBE3D_MACRO_H
|
#ifndef CUBE3D_MACRO_H
|
||||||
# define CUBE3D_MACRO_H
|
# define CUBE3D_MACRO_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* game params
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* nbr pixel player move */
|
||||||
|
# define PLR_MV 1
|
||||||
|
/* nbr key you can press at the same time */
|
||||||
# define MAX_NB_KEY 3
|
# define MAX_NB_KEY 3
|
||||||
# define SCREEN_DIST 50
|
/* screen definition */
|
||||||
# define SCREEN_SIZE 100
|
# define SCREEN_DEF 100
|
||||||
# define SCREEN_DEF 50
|
/* screen focal (in degree) */
|
||||||
# define PLR_MV 5
|
# define SCREEN_FOCAL 90
|
||||||
|
/* size of a cell on the map */
|
||||||
# define CELL 20
|
# define CELL 20
|
||||||
|
|
||||||
|
/*
|
||||||
|
* keys macro
|
||||||
|
*/
|
||||||
|
|
||||||
# define ARROW_LEFT 65361
|
# define ARROW_LEFT 65361
|
||||||
# define ARROW_UP 65362
|
# define ARROW_UP 65362
|
||||||
# define ARROW_RIGHT 65363
|
# define ARROW_RIGHT 65363
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ typedef struct s_rcast
|
|||||||
t_vec screen_dist;
|
t_vec screen_dist;
|
||||||
t_vec screen_size;
|
t_vec screen_size;
|
||||||
t_vec ray;
|
t_vec ray;
|
||||||
|
int screen_def;
|
||||||
|
int cell;
|
||||||
int cell_x;
|
int cell_x;
|
||||||
int cell_y;
|
int cell_y;
|
||||||
int slope_x;
|
int slope_x;
|
||||||
@@ -84,6 +86,7 @@ typedef struct s_map
|
|||||||
char **content;
|
char **content;
|
||||||
int size_x;
|
int size_x;
|
||||||
int size_y;
|
int size_y;
|
||||||
|
int cell;
|
||||||
} t_map;
|
} t_map;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ int main(int ac, char **av)
|
|||||||
// infinite loop that waits for events to occurs
|
// infinite loop that waits for events to occurs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// mlx_loop_hook(game->mlx_ptr, hook_action, game);
|
mlx_loop_hook(game->mlx_ptr, hook_action, game);
|
||||||
mlx_loop(game->mlx_ptr);
|
mlx_loop(game->mlx_ptr);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,9 @@ void draw_line(t_game *game, t_vec vec, int color)
|
|||||||
{
|
{
|
||||||
t_coord incr;
|
t_coord incr;
|
||||||
t_coord pos;
|
t_coord pos;
|
||||||
|
int cell;
|
||||||
|
|
||||||
|
cell = game->map.cell;
|
||||||
incr.x = 0;
|
incr.x = 0;
|
||||||
pos.x = 0;
|
pos.x = 0;
|
||||||
pos.y = 0;
|
pos.y = 0;
|
||||||
@@ -85,19 +87,19 @@ void draw_line(t_game *game, t_vec vec, int color)
|
|||||||
while ((game->map.content)[incr.x][incr.y])
|
while ((game->map.content)[incr.x][incr.y])
|
||||||
{
|
{
|
||||||
if ((game->map.content)[incr.x][incr.y] == '1' )
|
if ((game->map.content)[incr.x][incr.y] == '1' )
|
||||||
draw_square(game, pos, 0x00999999, 0x00000000, CELL, 0);
|
draw_square(game, pos, 0x00999999, 0x00000000, cell, 0);
|
||||||
else
|
else
|
||||||
draw_square(game, pos, 0x00555555, 0x00333333, CELL, 0);
|
draw_square(game, pos, 0x00555555, 0x00333333, cell, 0);
|
||||||
(incr.y)++;
|
(incr.y)++;
|
||||||
pos.x += CELL;
|
pos.x += cell;
|
||||||
}
|
}
|
||||||
(incr.x)++;
|
(incr.x)++;
|
||||||
pos.x = 0;
|
pos.x = 0;
|
||||||
pos.y += CELL;
|
pos.y += cell;
|
||||||
}
|
}
|
||||||
pos.x = game->plr.pos.x - CELL / 2;
|
pos.x = game->plr.pos.x - cell / 2;
|
||||||
pos.y = game->plr.pos.y - CELL / 2;
|
pos.y = game->plr.pos.y - cell / 2;
|
||||||
draw_square(game, pos, 0x00999900, 0x00330033, CELL, 1);
|
draw_square(game, pos, 0x00999900, 0x00330033, cell, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void draw_screen(t_game *game, t_rcast *rcast)
|
static void draw_screen(t_game *game, t_rcast *rcast)
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ static void init_raycast(t_rcast *rcast, t_vec *ray)
|
|||||||
if (ray->start.y < ray->end.y)
|
if (ray->start.y < ray->end.y)
|
||||||
rcast->ray_sign_y = -1;
|
rcast->ray_sign_y = -1;
|
||||||
// cell position of ray
|
// cell position of ray
|
||||||
rcast->cell_x = ray->start.x / CELL;
|
rcast->cell_x = ray->start.x / rcast->cell;
|
||||||
rcast->cell_y = ray->start.y / CELL;
|
rcast->cell_y = ray->start.y / rcast->cell;
|
||||||
// slope of ray
|
// slope of ray
|
||||||
rcast->slope_x = ray->end.x - ray->start.x;
|
rcast->slope_x = ray->end.x - ray->start.x;
|
||||||
rcast->slope_y = ray->end.y - ray->start.y;
|
rcast->slope_y = ray->end.y - ray->start.y;
|
||||||
@@ -21,23 +21,23 @@ static void init_raycast(t_rcast *rcast, t_vec *ray)
|
|||||||
rcast->next_cell_x = -rcast->ray_sign_x;
|
rcast->next_cell_x = -rcast->ray_sign_x;
|
||||||
rcast->next_cell_y = -rcast->ray_sign_y;
|
rcast->next_cell_y = -rcast->ray_sign_y;
|
||||||
// ray steps in both axis
|
// ray steps in both axis
|
||||||
rcast->ray_step_x = ft_abs(CELL * rcast->slope_y);
|
rcast->ray_step_x = ft_abs(rcast->cell * rcast->slope_y);
|
||||||
rcast->ray_step_y = ft_abs(CELL * rcast->slope_x);
|
rcast->ray_step_y = ft_abs(rcast->cell * rcast->slope_x);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_first_step(t_rcast *rcast, t_vec *ray)
|
static void init_first_step(t_rcast *rcast, t_vec *ray)
|
||||||
{
|
{
|
||||||
// first next time ray cross grid
|
// first next time ray cross grid
|
||||||
rcast->first_next_x = ray->start.x % CELL;
|
rcast->first_next_x = ray->start.x % rcast->cell;
|
||||||
if (rcast->first_next_x && rcast->ray_sign_x < 0)
|
if (rcast->first_next_x && rcast->ray_sign_x < 0)
|
||||||
rcast->first_next_x = CELL - rcast->first_next_x;
|
rcast->first_next_x = rcast->cell - rcast->first_next_x;
|
||||||
if (!rcast->first_next_x && rcast->ray_sign_x < 0)
|
if (!rcast->first_next_x && rcast->ray_sign_x < 0)
|
||||||
rcast->first_next_x = CELL;
|
rcast->first_next_x = rcast->cell;
|
||||||
rcast->first_next_y = ray->start.y % CELL;
|
rcast->first_next_y = ray->start.y % rcast->cell;
|
||||||
if (rcast->first_next_y && rcast->ray_sign_y < 0)
|
if (rcast->first_next_y && rcast->ray_sign_y < 0)
|
||||||
rcast->first_next_y = CELL - rcast->first_next_y;
|
rcast->first_next_y = rcast->cell - rcast->first_next_y;
|
||||||
if (!rcast->first_next_y && rcast->ray_sign_y < 0)
|
if (!rcast->first_next_y && rcast->ray_sign_y < 0)
|
||||||
rcast->first_next_y = CELL;
|
rcast->first_next_y = rcast->cell;
|
||||||
rcast->next_x = ft_abs(rcast->first_next_x * rcast->slope_y);
|
rcast->next_x = ft_abs(rcast->first_next_x * rcast->slope_y);
|
||||||
rcast->next_y = ft_abs(rcast->first_next_y * rcast->slope_x);
|
rcast->next_y = ft_abs(rcast->first_next_y * rcast->slope_x);
|
||||||
}
|
}
|
||||||
@@ -62,9 +62,9 @@ static void calcul_ray_end(t_rcast *rcast, t_vec *ray)
|
|||||||
{
|
{
|
||||||
if (rcast->is_x)
|
if (rcast->is_x)
|
||||||
{
|
{
|
||||||
ray->end.x = rcast->cell_x * CELL;
|
ray->end.x = rcast->cell_x * rcast->cell;
|
||||||
if (rcast->ray_sign_x == 1)
|
if (rcast->ray_sign_x == 1)
|
||||||
ray->end.x += CELL;
|
ray->end.x += rcast->cell;
|
||||||
if (rcast->slope_x)
|
if (rcast->slope_x)
|
||||||
{
|
{
|
||||||
rcast->ratio = (double)(ray->end.x - ray->start.x) / (double)rcast->slope_x;
|
rcast->ratio = (double)(ray->end.x - ray->start.x) / (double)rcast->slope_x;
|
||||||
@@ -73,9 +73,9 @@ static void calcul_ray_end(t_rcast *rcast, t_vec *ray)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ray->end.y = rcast->cell_y * CELL;
|
ray->end.y = rcast->cell_y * rcast->cell;
|
||||||
if (rcast->ray_sign_y == 1)
|
if (rcast->ray_sign_y == 1)
|
||||||
ray->end.y += CELL;
|
ray->end.y += rcast->cell;
|
||||||
if (rcast->slope_y)
|
if (rcast->slope_y)
|
||||||
{
|
{
|
||||||
rcast->ratio = (double)(ray->end.y - ray->start.y) / (double)rcast->slope_y;
|
rcast->ratio = (double)(ray->end.y - ray->start.y) / (double)rcast->slope_y;
|
||||||
@@ -89,7 +89,7 @@ void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray)
|
|||||||
ray->start.x = rcast->ray.start.x + game->plr.pos.x;
|
ray->start.x = rcast->ray.start.x + game->plr.pos.x;
|
||||||
ray->start.y = rcast->ray.start.y + game->plr.pos.y;
|
ray->start.y = rcast->ray.start.y + game->plr.pos.y;
|
||||||
ray->end.x = rcast->ray.end.x + game->plr.pos.x;
|
ray->end.x = rcast->ray.end.x + game->plr.pos.x;
|
||||||
ray->end.x += rcast->ray_nb * SCREEN_SIZE / SCREEN_DEF;
|
ray->end.x += rcast->ray_nb;
|
||||||
ray->end.y = rcast->ray.end.y + game->plr.pos.y;
|
ray->end.y = rcast->ray.end.y + game->plr.pos.y;
|
||||||
rotate(&(game->plr), &(ray->end));
|
rotate(&(game->plr), &(ray->end));
|
||||||
init_raycast(rcast, ray);
|
init_raycast(rcast, ray);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ void raycast(t_game *game, t_rcast *rcast)
|
|||||||
t_vec ray;
|
t_vec ray;
|
||||||
|
|
||||||
rcast->ray_nb = 0;
|
rcast->ray_nb = 0;
|
||||||
while (rcast->ray_nb <= SCREEN_DEF)
|
while (rcast->ray_nb <= rcast->screen_def)
|
||||||
{
|
{
|
||||||
ray_intersect(game, rcast, &ray);
|
ray_intersect(game, rcast, &ray);
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ int keypress(int keycode, t_game *game)
|
|||||||
game->k_hook[i] = 0;
|
game->k_hook[i] = 0;
|
||||||
else if (i < MAX_NB_KEY)
|
else if (i < MAX_NB_KEY)
|
||||||
game->k_hook[i] = keycode;
|
game->k_hook[i] = keycode;
|
||||||
hook_action(game);
|
// hook_action(game);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ static void init_map(t_map *map)
|
|||||||
{
|
{
|
||||||
map->size_x = 24;
|
map->size_x = 24;
|
||||||
map->size_y = 24;
|
map->size_y = 24;
|
||||||
|
map->cell = CELL;
|
||||||
// map
|
// map
|
||||||
int tmp[24][24] = {
|
int tmp[24][24] = {
|
||||||
{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},
|
||||||
@@ -64,21 +65,26 @@ static void init_map(t_map *map)
|
|||||||
|
|
||||||
static void init_raycast(t_rcast *rcast)
|
static void init_raycast(t_rcast *rcast)
|
||||||
{
|
{
|
||||||
// draw screen dist
|
double dist;
|
||||||
|
|
||||||
|
dist = (SCREEN_FOCAL / 2) * M_PI / 180;
|
||||||
|
dist = cos(dist) * ((SCREEN_DEF / 2) / sin(dist));
|
||||||
|
rcast->screen_def = SCREEN_DEF;
|
||||||
|
// screen dist
|
||||||
rcast->screen_dist.start.x = 0;
|
rcast->screen_dist.start.x = 0;
|
||||||
rcast->screen_dist.start.y = 0;
|
rcast->screen_dist.start.y = 0;
|
||||||
rcast->screen_dist.end.x = 0;
|
rcast->screen_dist.end.x = 0;
|
||||||
rcast->screen_dist.end.y = -SCREEN_DIST;
|
rcast->screen_dist.end.y = -dist;
|
||||||
// draw screen size
|
// screen size
|
||||||
rcast->screen_size.start.x = -SCREEN_SIZE / 2;
|
rcast->screen_size.start.x = -SCREEN_DEF / 2;
|
||||||
rcast->screen_size.start.y = -SCREEN_DIST;
|
rcast->screen_size.start.y = -dist;
|
||||||
rcast->screen_size.end.x = SCREEN_SIZE / 2;
|
rcast->screen_size.end.x = SCREEN_DEF / 2;
|
||||||
rcast->screen_size.end.y = -SCREEN_DIST;
|
rcast->screen_size.end.y = -dist;
|
||||||
// draw ray
|
// first ray
|
||||||
rcast->ray.start.x = 0;
|
rcast->ray.start.x = 0;
|
||||||
rcast->ray.start.y = 0;
|
rcast->ray.start.y = 0;
|
||||||
rcast->ray.end.x = -SCREEN_SIZE / 2;
|
rcast->ray.end.x = -SCREEN_DEF / 2;
|
||||||
rcast->ray.end.y = -SCREEN_DIST;
|
rcast->ray.end.y = -dist;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_plr(t_plr *plr)
|
static void init_plr(t_plr *plr)
|
||||||
@@ -118,6 +124,7 @@ t_game *init_game(void)
|
|||||||
ft_bzero(&game->k_hook, sizeof(game->k_hook));
|
ft_bzero(&game->k_hook, sizeof(game->k_hook));
|
||||||
// raycasting
|
// raycasting
|
||||||
init_raycast(&(game->rcast));
|
init_raycast(&(game->rcast));
|
||||||
|
game->rcast.cell = game->map.cell;
|
||||||
// create image and get its data address
|
// create image and get its data address
|
||||||
init_img(&(game->map_img), game);
|
init_img(&(game->map_img), game);
|
||||||
return (game);
|
return (game);
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ int plr_out_limits(t_game *game, int x, int y)
|
|||||||
|
|
||||||
xmax = game->map_img.sizel / (game->map_img.bpp / 8);
|
xmax = game->map_img.sizel / (game->map_img.bpp / 8);
|
||||||
ymax = game->map_win.size_y;
|
ymax = game->map_win.size_y;
|
||||||
cell_x = x / CELL;
|
cell_x = x / game->map.cell;
|
||||||
cell_y = y / CELL;
|
cell_y = y / game->map.cell;
|
||||||
if (is_wall(game, cell_x, cell_y))
|
if (is_wall(game, cell_x, cell_y))
|
||||||
return (1);
|
return (1);
|
||||||
if (x < 0 || y < 0 || x > xmax || y > ymax)
|
if (x < 0 || y < 0 || x > xmax || y > ymax)
|
||||||
|
|||||||
Reference in New Issue
Block a user