params adjust angle of view and size of game window
This commit is contained in:
@@ -75,7 +75,9 @@ void draw_line(t_game *game, t_vec vec, int color)
|
||||
{
|
||||
t_coord incr;
|
||||
t_coord pos;
|
||||
int cell;
|
||||
|
||||
cell = game->map.cell;
|
||||
incr.x = 0;
|
||||
pos.x = 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])
|
||||
{
|
||||
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
|
||||
draw_square(game, pos, 0x00555555, 0x00333333, CELL, 0);
|
||||
draw_square(game, pos, 0x00555555, 0x00333333, cell, 0);
|
||||
(incr.y)++;
|
||||
pos.x += CELL;
|
||||
pos.x += cell;
|
||||
}
|
||||
(incr.x)++;
|
||||
pos.x = 0;
|
||||
pos.y += CELL;
|
||||
pos.y += cell;
|
||||
}
|
||||
pos.x = game->plr.pos.x - CELL / 2;
|
||||
pos.y = game->plr.pos.y - CELL / 2;
|
||||
draw_square(game, pos, 0x00999900, 0x00330033, CELL, 1);
|
||||
pos.x = game->plr.pos.x - cell / 2;
|
||||
pos.y = game->plr.pos.y - cell / 2;
|
||||
draw_square(game, pos, 0x00999900, 0x00330033, cell, 1);
|
||||
}
|
||||
|
||||
static void draw_screen(t_game *game, t_rcast *rcast)
|
||||
@@ -132,7 +134,7 @@ void draw(t_game *game)
|
||||
|
||||
// tmp, to draw map
|
||||
draw_screen(game, &(game->rcast));
|
||||
mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, 0);
|
||||
mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, SCREEN_HEIGHT);
|
||||
// tmp end
|
||||
|
||||
// mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0);
|
||||
|
||||
@@ -12,8 +12,8 @@ static void init_raycast(t_rcast *rcast, t_vec *ray)
|
||||
if (ray->start.y < ray->end.y)
|
||||
rcast->ray_sign_y = -1;
|
||||
// cell position of ray
|
||||
rcast->cell_x = ray->start.x / CELL;
|
||||
rcast->cell_y = ray->start.y / CELL;
|
||||
rcast->cell_x = ray->start.x / rcast->cell;
|
||||
rcast->cell_y = ray->start.y / rcast->cell;
|
||||
// slope of ray
|
||||
rcast->slope_x = ray->end.x - ray->start.x;
|
||||
rcast->slope_y = ray->end.y - ray->start.y;
|
||||
@@ -21,23 +21,22 @@ static void init_raycast(t_rcast *rcast, t_vec *ray)
|
||||
rcast->next_cell_x = -rcast->ray_sign_x;
|
||||
rcast->next_cell_y = -rcast->ray_sign_y;
|
||||
// ray steps in both axis
|
||||
rcast->ray_step_x = ft_abs(CELL * rcast->slope_y);
|
||||
rcast->ray_step_y = ft_abs(CELL * rcast->slope_x);
|
||||
rcast->ray_step_x = ft_abs(rcast->cell * rcast->slope_y);
|
||||
rcast->ray_step_y = ft_abs(rcast->cell * rcast->slope_x);
|
||||
}
|
||||
|
||||
static void init_first_step(t_rcast *rcast, t_vec *ray)
|
||||
{
|
||||
// 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)
|
||||
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)
|
||||
rcast->first_next_x = CELL;
|
||||
rcast->first_next_y = ray->start.y % CELL;
|
||||
rcast->first_next_x = rcast->cell;
|
||||
rcast->first_next_y = ray->start.y % rcast->cell;
|
||||
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)
|
||||
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_y = ft_abs(rcast->first_next_y * rcast->slope_x);
|
||||
}
|
||||
@@ -58,45 +57,16 @@ static void next_cell(t_rcast *rcast)
|
||||
}
|
||||
}
|
||||
|
||||
static void calcul_ray_end(t_rcast *rcast, t_vec *ray)
|
||||
{
|
||||
if (rcast->is_x)
|
||||
{
|
||||
ray->end.x = rcast->cell_x * CELL;
|
||||
if (rcast->ray_sign_x == 1)
|
||||
ray->end.x += CELL;
|
||||
if (rcast->slope_x)
|
||||
{
|
||||
rcast->ratio = (double)(ray->end.x - ray->start.x) / (double)rcast->slope_x;
|
||||
ray->end.y = ray->start.y + (double)rcast->slope_y * rcast->ratio;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ray->end.y = rcast->cell_y * CELL;
|
||||
if (rcast->ray_sign_y == 1)
|
||||
ray->end.y += CELL;
|
||||
if (rcast->slope_y)
|
||||
{
|
||||
rcast->ratio = (double)(ray->end.y - ray->start.y) / (double)rcast->slope_y;
|
||||
ray->end.x = ray->start.x + (double)rcast->slope_x * rcast->ratio;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.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_nb * SCREEN_SIZE / SCREEN_DEF;
|
||||
ray->end.x += rcast->ray_nb;
|
||||
ray->end.y = rcast->ray.end.y + game->plr.pos.y;
|
||||
rotate(&(game->plr), &(ray->end));
|
||||
init_raycast(rcast, ray);
|
||||
init_first_step(rcast, ray);
|
||||
// loop through grid
|
||||
while (!is_wall(game, rcast->cell_x, rcast->cell_y))
|
||||
next_cell(rcast);
|
||||
// end ray position
|
||||
calcul_ray_end(rcast, ray);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,57 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
// tmp, to draw rays
|
||||
static void calcul_ray_end(t_rcast *rcast, t_vec *ray)
|
||||
{
|
||||
if (rcast->is_x)
|
||||
{
|
||||
ray->end.x = rcast->cell_x * rcast->cell;
|
||||
if (rcast->ray_sign_x == 1)
|
||||
ray->end.x += rcast->cell;
|
||||
if (rcast->slope_x)
|
||||
{
|
||||
rcast->ratio = (double)(ray->end.x - ray->start.x);
|
||||
rcast->ratio /= (double)rcast->slope_x;
|
||||
ray->end.y = ray->start.y + (double)rcast->slope_y * rcast->ratio;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ray->end.y = rcast->cell_y * rcast->cell;
|
||||
if (rcast->ray_sign_y == 1)
|
||||
ray->end.y += rcast->cell;
|
||||
if (rcast->slope_y)
|
||||
{
|
||||
rcast->ratio = (double)(ray->end.y - ray->start.y);
|
||||
rcast->ratio /= (double)rcast->slope_y;
|
||||
ray->end.x = ray->start.x + (double)rcast->slope_x * rcast->ratio;
|
||||
}
|
||||
}
|
||||
}
|
||||
// tmp end
|
||||
|
||||
void draw_column(t_game *game, t_rcast *rcast, t_vec *ray)
|
||||
{
|
||||
(void)game;
|
||||
(void)rcast;
|
||||
(void)ray;
|
||||
}
|
||||
|
||||
void raycast(t_game *game, t_rcast *rcast)
|
||||
{
|
||||
t_vec ray;
|
||||
|
||||
rcast->ray_nb = 0;
|
||||
while (rcast->ray_nb <= SCREEN_DEF)
|
||||
while (rcast->ray_nb <= rcast->screen_def)
|
||||
{
|
||||
ray_intersect(game, rcast, &ray);
|
||||
|
||||
// tmp, to draw the map
|
||||
calcul_ray_end(rcast, &ray);
|
||||
draw_line(game, ray, 0x00FF00FF);
|
||||
// tmp end
|
||||
|
||||
// draw_column();
|
||||
draw_column(game, rcast, &ray);
|
||||
(rcast->ray_nb)++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user