Files
42_INT_10_cube3d/srcs/draw/draw_window.c

203 lines
4.7 KiB
C

#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);
}