142 lines
3.3 KiB
C
142 lines
3.3 KiB
C
#include "cube3d.h"
|
|
|
|
static int pxl_out_limits(t_img *img, int x, int y)
|
|
{
|
|
int xmax;
|
|
int ymax;
|
|
|
|
xmax = img->szl / (img->bpp / 8);
|
|
ymax = img->height;
|
|
if (x < 0 || y < 0 || x > xmax || y > ymax)
|
|
return (1);
|
|
return (0);
|
|
}
|
|
|
|
void draw_pixel(t_img *img, int x, int y, int color)
|
|
{
|
|
unsigned int position;
|
|
|
|
if (pxl_out_limits(img, x, y))
|
|
return ;
|
|
position = y * img->szl + x * (img->bpp / 8);
|
|
*(unsigned int*)(img->data + position) = color;
|
|
}
|
|
|
|
void draw_line(t_img *img, t_vec *vec, int color)
|
|
{
|
|
t_coord dist;
|
|
int i;
|
|
int j;
|
|
|
|
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(dist.x) && ft_abs(j) <= ft_abs(dist.y))
|
|
{
|
|
draw_pixel(img, (*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(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->map_img, new.x, new.y, border);
|
|
else if (!j || j == size - 1)
|
|
draw_pixel(&game->map_img, new.x, new.y, border);
|
|
else
|
|
draw_pixel(&game->map_img, new.x, new.y, fill);
|
|
j++;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
static void draw_map(t_game *game)
|
|
{
|
|
t_coord incr;
|
|
t_coord pos;
|
|
int cell;
|
|
|
|
cell = game->map.cell;
|
|
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->map_img, &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->map_img, &screen, 0x00FFFFFF);
|
|
}
|
|
// tmp end
|
|
|
|
void draw(t_game *game)
|
|
{
|
|
// 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, SCREEN_HEIGHT);
|
|
// tmp end
|
|
|
|
mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0);
|
|
}
|