better struct and files organization, and still only half raycast on map but no view created

This commit is contained in:
hugogogo
2022-04-22 17:38:29 +02:00
parent 00c9dfd6b8
commit fac1f78230
17 changed files with 602 additions and 505 deletions

View File

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