better struct and files organization, and still only half raycast on map but no view created
This commit is contained in:
169
srcs/draw/draw.c
169
srcs/draw/draw.c
@@ -5,8 +5,8 @@ static int pxl_out_limits(t_game *game, int x, int y)
|
||||
int xmax;
|
||||
int ymax;
|
||||
|
||||
xmax = game->sizel / (game->bpp / 8);
|
||||
ymax = game->win_size_y;
|
||||
xmax = game->map_img.sizel / (game->map_img.bpp / 8);
|
||||
ymax = game->map_win.size_y;
|
||||
if (x < 0 || y < 0 || x > xmax || y > ymax)
|
||||
return (1);
|
||||
return (0);
|
||||
@@ -18,77 +18,122 @@ void draw_pixel(t_game *game, int x, int y, int color)
|
||||
|
||||
if (pxl_out_limits(game, x, y))
|
||||
return ;
|
||||
position = y * game->sizel + x * (game->bpp / 8);
|
||||
*(unsigned int*)(game->img_data + position) = color;
|
||||
position = y * game->map_img.sizel + x * (game->map_img.bpp / 8);
|
||||
*(unsigned int*)(game->map_img.data + position) = color;
|
||||
}
|
||||
|
||||
void rotate(t_game *game, int *x, int *y)
|
||||
void draw_line(t_game *game, t_vec vec, int color)
|
||||
{
|
||||
int old_x;
|
||||
int tmp_x;
|
||||
int tmp_y;
|
||||
t_coord dist;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
// do nothing if not rotating
|
||||
if (game->rot == 0)
|
||||
return ;
|
||||
// offset center
|
||||
tmp_x = *x - game->plr_x;
|
||||
tmp_y = *y - game->plr_y;
|
||||
// calculate new coordinates
|
||||
old_x = tmp_x;
|
||||
tmp_x = tmp_x * game->cosi + tmp_y * game->cosj;
|
||||
tmp_y = old_x * game->sini + tmp_y * game->sinj;
|
||||
// de-offset center
|
||||
*x = tmp_x + game->plr_x;
|
||||
*y = tmp_y + game->plr_y;
|
||||
}
|
||||
|
||||
void rotate_double(t_game *game, double *x, double *y)
|
||||
{
|
||||
double old_x;
|
||||
double tmp_x;
|
||||
double tmp_y;
|
||||
|
||||
// do nothing if not rotating
|
||||
if (game->rot == 0)
|
||||
return ;
|
||||
// offset center
|
||||
tmp_x = *x - game->plr_exact_x;
|
||||
tmp_y = *y - game->plr_exact_y;
|
||||
// calculate new coordinates
|
||||
old_x = tmp_x;
|
||||
tmp_x = tmp_x * game->cosi + tmp_y * game->cosj;
|
||||
tmp_y = old_x * game->sini + tmp_y * game->sinj;
|
||||
// de-offset center
|
||||
*x = tmp_x + game->plr_exact_x;
|
||||
*y = tmp_y + game->plr_exact_y;
|
||||
}
|
||||
|
||||
void draw_line(t_game *game, int start_x, int start_y, int end_x, int end_y, int color)
|
||||
{
|
||||
int dx;
|
||||
int dy;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
dx = end_x - start_x;
|
||||
dy = end_y - start_y;
|
||||
dist.x = vec.end.x - vec.start.x;
|
||||
dist.y = vec.end.y - vec.start.y;
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (ft_abs(i) <= ft_abs(dx) && ft_abs(j) <= ft_abs(dy))
|
||||
while (ft_abs(i) <= ft_abs(dist.x) && ft_abs(j) <= ft_abs(dist.y))
|
||||
{
|
||||
draw_pixel(game, start_x + i, start_y + j, color);
|
||||
if (!ft_abs(dx) || ft_abs(j) < ft_abs(i * dy / dx))
|
||||
j += ft_sign(dy);
|
||||
draw_pixel(game, vec.start.x + i, vec.start.y + j, color);
|
||||
if (!ft_abs(dist.x) || ft_abs(j) < ft_abs(i * dist.y / dist.x))
|
||||
j += ft_sign(dist.y);
|
||||
else
|
||||
i += ft_sign(dx);
|
||||
i += ft_sign(dist.x);
|
||||
}
|
||||
}
|
||||
|
||||
// tmp
|
||||
static void draw_square(t_game *game, t_coord pos, int border, int fill, int size, int rotation)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
t_coord new;
|
||||
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
j = 0;
|
||||
while (j < size)
|
||||
{
|
||||
new.x = pos.x + j;
|
||||
new.y = pos.y + i;
|
||||
if (rotation)
|
||||
rotate(&(game->plr), &(new));
|
||||
if (!i || i == size - 1)
|
||||
draw_pixel(game, new.x, new.y, border);
|
||||
else if (!j || j == size - 1)
|
||||
draw_pixel(game, new.x, new.y, border);
|
||||
else
|
||||
draw_pixel(game, new.x, new.y, fill);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void draw_map(t_game *game)
|
||||
{
|
||||
t_coord incr;
|
||||
t_coord pos;
|
||||
|
||||
incr.x = 0;
|
||||
pos.x = 0;
|
||||
pos.y = 0;
|
||||
while ((game->map.content)[incr.x])
|
||||
{
|
||||
incr.y = 0;
|
||||
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);
|
||||
else
|
||||
draw_square(game, pos, 0x00555555, 0x00333333, CELL, 0);
|
||||
(incr.y)++;
|
||||
pos.x += CELL;
|
||||
}
|
||||
(incr.x)++;
|
||||
pos.x = 0;
|
||||
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);
|
||||
}
|
||||
|
||||
static void draw_screen(t_game *game, t_rcast *rcast)
|
||||
{
|
||||
t_vec screen;
|
||||
|
||||
// draw screen size
|
||||
screen.start.x = rcast->screen_size.start.x + game->plr.pos.x;
|
||||
screen.start.y = rcast->screen_size.start.y + game->plr.pos.y;
|
||||
rotate(&(game->plr), &(screen.start));
|
||||
screen.end.x = rcast->screen_size.end.x + game->plr.pos.x;
|
||||
screen.end.y = rcast->screen_size.end.y + game->plr.pos.y;
|
||||
rotate(&(game->plr), &(screen.end));
|
||||
draw_line(game, screen, 0x00FFFFFF);
|
||||
// draw screen dist
|
||||
screen.start.x = rcast->screen_dist.start.x + game->plr.pos.x;
|
||||
screen.start.y = rcast->screen_dist.start.y + game->plr.pos.y;
|
||||
screen.end.x = rcast->screen_dist.end.x + game->plr.pos.x;
|
||||
screen.end.y = rcast->screen_dist.end.y + game->plr.pos.y;
|
||||
rotate(&(game->plr), &(screen.end));
|
||||
draw_line(game, screen, 0x00FFFFFF);
|
||||
}
|
||||
// tmp end
|
||||
|
||||
void draw(t_game *game)
|
||||
{
|
||||
draw_map(game);
|
||||
draw_rays(game);
|
||||
//draw_view(game);
|
||||
mlx_put_image_to_window(game->mlx_ptr, game->win_ptr, game->img_ptr, 0, 0);
|
||||
// tmp, to draw map
|
||||
draw_map(game);
|
||||
// tmp end
|
||||
|
||||
raycast(game, &(game->rcast));
|
||||
|
||||
// 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);
|
||||
// tmp end
|
||||
|
||||
// mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0);
|
||||
}
|
||||
|
||||
@@ -1,202 +0,0 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
void ray_intersect(t_game *game, int start_x, int start_y, int *end_x, int *end_y)
|
||||
{
|
||||
int cell_x;
|
||||
int cell_y;
|
||||
int slope_x;
|
||||
int slope_y;
|
||||
int ray_sign_x;
|
||||
int ray_sign_y;
|
||||
int first_next_x;
|
||||
int first_next_y;
|
||||
int next_x;
|
||||
int next_y;
|
||||
int next_cell_x;
|
||||
int next_cell_y;
|
||||
int ray_step_x;
|
||||
int ray_step_y;
|
||||
int is_x;
|
||||
|
||||
is_x = 0;
|
||||
// ray sign
|
||||
ray_sign_x = 1;
|
||||
if (start_x < *end_x)
|
||||
ray_sign_x = -1;
|
||||
ray_sign_y = 1;
|
||||
if (start_y < *end_y)
|
||||
ray_sign_y = -1;
|
||||
// cell position of ray
|
||||
cell_x = start_x / game->cell;
|
||||
cell_y = start_y / game->cell;
|
||||
// slope of ray
|
||||
slope_x = *end_x - start_x;
|
||||
slope_y = *end_y - start_y;
|
||||
// direction of next cell
|
||||
next_cell_x = -ray_sign_x;
|
||||
next_cell_y = -ray_sign_y;
|
||||
// ray steps in both axis
|
||||
ray_step_x = ft_abs(game->cell * slope_y);
|
||||
ray_step_y = ft_abs(game->cell * slope_x);
|
||||
// first next time ray cross grid
|
||||
first_next_x = start_x % game->cell;
|
||||
if (first_next_x && ray_sign_x < 0)
|
||||
first_next_x = game->cell - first_next_x;
|
||||
if (!first_next_x && ray_sign_x < 0)
|
||||
first_next_x = game->cell;
|
||||
first_next_y = start_y % game->cell;
|
||||
if (first_next_y && ray_sign_y < 0)
|
||||
first_next_y = game->cell - first_next_y;
|
||||
if (!first_next_y && ray_sign_y < 0)
|
||||
first_next_y = game->cell;
|
||||
next_x = ft_abs(first_next_x * slope_y);
|
||||
next_y = ft_abs(first_next_y * slope_x);
|
||||
// loop through grid
|
||||
if (game->ray_activ == 1) printf("\ncell_x:%i, cell_y:%i, first_next_x:%i, first_next_y:%i\n", cell_x, cell_y, first_next_x, first_next_y);
|
||||
while (!is_wall(game, cell_x, cell_y))
|
||||
{
|
||||
if (game->ray_activ == 1) printf("next_x:%-4i, next_y:%-4i, ", next_x, next_y);
|
||||
if (next_x > next_y)
|
||||
{
|
||||
cell_y += next_cell_y;
|
||||
next_y += ray_step_y;
|
||||
is_x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cell_x += next_cell_x;
|
||||
next_x += ray_step_x;
|
||||
is_x = 1;
|
||||
}
|
||||
if (game->ray_activ == 1) printf("cell_x:%-4i, cell_y:%-4i\n", cell_x, cell_y);
|
||||
}
|
||||
// end ray position
|
||||
double ratio;
|
||||
if (is_x)
|
||||
{
|
||||
*end_x = cell_x * game->cell;
|
||||
if (ray_sign_x == 1)
|
||||
*end_x += game->cell;
|
||||
if (slope_x)
|
||||
{
|
||||
ratio = (double)(*end_x - start_x) / (double)slope_x;
|
||||
*end_y = start_y + (double)slope_y * ratio;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*end_y = cell_y * game->cell;
|
||||
if (ray_sign_y == 1)
|
||||
*end_y += game->cell;
|
||||
if (slope_y)
|
||||
{
|
||||
ratio = (double)(*end_y - start_y) / (double)slope_y;
|
||||
*end_x = start_x + (double)slope_x * ratio;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_rays(t_game *game)
|
||||
{
|
||||
int start_x;
|
||||
int start_y;
|
||||
int end_x;
|
||||
int end_y;
|
||||
int i;
|
||||
|
||||
// draw screen size
|
||||
start_x = game->screen_size.start_x + game->plr_x;
|
||||
start_y = game->screen_size.start_y + game->plr_y;
|
||||
rotate(game, &start_x, &start_y);
|
||||
end_x = game->screen_size.end_x + game->plr_x;
|
||||
end_y = game->screen_size.end_y + game->plr_y;
|
||||
rotate(game, &end_x, &end_y);
|
||||
draw_line(game, start_x, start_y, end_x, end_y, 0x00FFFFFF);
|
||||
// draw rays
|
||||
i = 0;
|
||||
while (i <= SCREEN_DEF)
|
||||
{
|
||||
// tmp display one ray with infos
|
||||
if (i == game->ray_highlight)
|
||||
game->ray_activ = 1;
|
||||
// tmp end
|
||||
start_x = game->ray.start_x + game->plr_x;
|
||||
start_y = game->ray.start_y + game->plr_y;
|
||||
end_x = game->ray.end_x + game->plr_x + i * SCREEN_SIZE / SCREEN_DEF;
|
||||
end_y = game->ray.end_y + game->plr_y;
|
||||
rotate(game, &end_x, &end_y);
|
||||
ray_intersect(game, start_x, start_y, &end_x, &end_y);
|
||||
draw_line(game, start_x, start_y, end_x, end_y, 0x00FF00FF);
|
||||
// tmp display one ray with infos
|
||||
if (game->ray_activ == 1)
|
||||
draw_line(game, start_x, start_y, end_x, end_y, 0x00FF0000);
|
||||
// tmp end
|
||||
i++;
|
||||
game->ray_activ = 0;
|
||||
}
|
||||
// draw screen dist
|
||||
start_x = game->screen_dist.start_x + game->plr_x;
|
||||
start_y = game->screen_dist.start_y + game->plr_y;
|
||||
end_x = game->screen_dist.end_x + game->plr_x;
|
||||
end_y = game->screen_dist.end_y + game->plr_y;
|
||||
rotate(game, &end_x, &end_y);
|
||||
draw_line(game, start_x, start_y, end_x, end_y, 0x00FFFFFF);
|
||||
}
|
||||
|
||||
static void draw_square(t_game *game, int x, int y, int border, int fill, int size, int rotation)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int new_x;
|
||||
int new_y;
|
||||
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
j = 0;
|
||||
while (j < size)
|
||||
{
|
||||
new_x = x + j;
|
||||
new_y = y + i;
|
||||
if (rotation)
|
||||
rotate(game, &new_x, &new_y);
|
||||
if (!i || i == size - 1)
|
||||
draw_pixel(game, new_x, new_y, border);
|
||||
else if (!j || j == size - 1)
|
||||
draw_pixel(game, new_x, new_y, border);
|
||||
else
|
||||
draw_pixel(game, new_x, new_y, fill);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void draw_map(t_game *game)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int x;
|
||||
int y;
|
||||
|
||||
i = 0;
|
||||
x = 0;
|
||||
y = 0;
|
||||
while ((game->map)[i])
|
||||
{
|
||||
j = 0;
|
||||
while ((game->map)[i][j])
|
||||
{
|
||||
if ((game->map)[i][j] == '1' )
|
||||
draw_square(game, x, y, 0x00999999, 0x00000000, game->cell, 0);
|
||||
else
|
||||
draw_square(game, x, y, 0x00555555, 0x00333333, game->cell, 0);
|
||||
j++;
|
||||
x += game->cell;
|
||||
}
|
||||
i++;
|
||||
x = 0;
|
||||
y += game->cell;
|
||||
}
|
||||
draw_square(game, game->plr_x - game->cell / 2, game->plr_y - game->cell / 2, 0x00999900, 0x00330033, game->cell, 1);
|
||||
}
|
||||
102
srcs/draw/ray_intersect.c
Normal file
102
srcs/draw/ray_intersect.c
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
static void init_raycast(t_rcast *rcast, t_vec *ray)
|
||||
{
|
||||
rcast->is_x = 0;
|
||||
rcast->wall_height = 0;
|
||||
// ray sign
|
||||
rcast->ray_sign_x = 1;
|
||||
if (ray->start.x < ray->end.x)
|
||||
rcast->ray_sign_x = -1;
|
||||
rcast->ray_sign_y = 1;
|
||||
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;
|
||||
// slope of ray
|
||||
rcast->slope_x = ray->end.x - ray->start.x;
|
||||
rcast->slope_y = ray->end.y - ray->start.y;
|
||||
// direction of next cell
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
if (rcast->first_next_x && rcast->ray_sign_x < 0)
|
||||
rcast->first_next_x = 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;
|
||||
if (rcast->first_next_y && rcast->ray_sign_y < 0)
|
||||
rcast->first_next_y = CELL - rcast->first_next_y;
|
||||
if (!rcast->first_next_y && rcast->ray_sign_y < 0)
|
||||
rcast->first_next_y = 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);
|
||||
}
|
||||
|
||||
static void next_cell(t_rcast *rcast)
|
||||
{
|
||||
if (rcast->next_x > rcast->next_y)
|
||||
{
|
||||
rcast->cell_y += rcast->next_cell_y;
|
||||
rcast->next_y += rcast->ray_step_y;
|
||||
rcast->is_x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
rcast->cell_x += rcast->next_cell_x;
|
||||
rcast->next_x += rcast->ray_step_x;
|
||||
rcast->is_x = 1;
|
||||
}
|
||||
}
|
||||
|
||||
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.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);
|
||||
}
|
||||
19
srcs/draw/raycast.c
Normal file
19
srcs/draw/raycast.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
void raycast(t_game *game, t_rcast *rcast)
|
||||
{
|
||||
t_vec ray;
|
||||
|
||||
rcast->ray_nb = 0;
|
||||
while (rcast->ray_nb <= SCREEN_DEF)
|
||||
{
|
||||
ray_intersect(game, rcast, &ray);
|
||||
|
||||
// tmp, to draw the map
|
||||
draw_line(game, ray, 0x00FF00FF);
|
||||
// tmp end
|
||||
|
||||
// draw_column();
|
||||
(rcast->ray_nb)++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user