changed functions draw line and pixel to receive an img ptr instead of the whole structure
This commit is contained in:
@@ -62,8 +62,8 @@ int is_wall(t_game *game, int cell_x, int cell_y);
|
|||||||
// SRC/DRAW
|
// SRC/DRAW
|
||||||
// -------------------------------
|
// -------------------------------
|
||||||
// draw.c
|
// draw.c
|
||||||
void draw_pixel(t_game *game, int x, int y, int color);
|
void draw_pixel(t_img *img, int x, int y, int color);
|
||||||
void draw_line(t_game *game, t_vec vec, int color);
|
void draw_line(t_img *img, t_vec vec, int color);
|
||||||
void draw(t_game *game);
|
void draw(t_game *game);
|
||||||
// raycast.c
|
// raycast.c
|
||||||
void raycast(t_game *game, t_rcast *rcast);
|
void raycast(t_game *game, t_rcast *rcast);
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ typedef struct s_rcast
|
|||||||
int is_x;
|
int is_x;
|
||||||
int ray_nb;
|
int ray_nb;
|
||||||
int wall_height;
|
int wall_height;
|
||||||
|
int screen_center;
|
||||||
double ratio;
|
double ratio;
|
||||||
} t_rcast;
|
} t_rcast;
|
||||||
|
|
||||||
@@ -73,8 +74,9 @@ typedef struct s_img
|
|||||||
void *ptr;
|
void *ptr;
|
||||||
char *data;
|
char *data;
|
||||||
int bpp;
|
int bpp;
|
||||||
int sizel;
|
int szl;
|
||||||
int endian;
|
int ndn;
|
||||||
|
int height;
|
||||||
} t_img;
|
} t_img;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -1,28 +1,28 @@
|
|||||||
#include "cube3d.h"
|
#include "cube3d.h"
|
||||||
|
|
||||||
static int pxl_out_limits(t_game *game, int x, int y)
|
static int pxl_out_limits(t_img *img, int x, int y)
|
||||||
{
|
{
|
||||||
int xmax;
|
int xmax;
|
||||||
int ymax;
|
int ymax;
|
||||||
|
|
||||||
xmax = game->map_img.sizel / (game->map_img.bpp / 8);
|
xmax = img->szl / (img->bpp / 8);
|
||||||
ymax = game->map_win.size_y;
|
ymax = img->height;
|
||||||
if (x < 0 || y < 0 || x > xmax || y > ymax)
|
if (x < 0 || y < 0 || x > xmax || y > ymax)
|
||||||
return (1);
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_pixel(t_game *game, int x, int y, int color)
|
void draw_pixel(t_img *img, int x, int y, int color)
|
||||||
{
|
{
|
||||||
unsigned int position;
|
unsigned int position;
|
||||||
|
|
||||||
if (pxl_out_limits(game, x, y))
|
if (pxl_out_limits(img, x, y))
|
||||||
return ;
|
return ;
|
||||||
position = y * game->map_img.sizel + x * (game->map_img.bpp / 8);
|
position = y * img->szl + x * (img->bpp / 8);
|
||||||
*(unsigned int*)(game->map_img.data + position) = color;
|
*(unsigned int*)(img->data + position) = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_line(t_game *game, t_vec vec, int color)
|
void draw_line(t_img *img, t_vec vec, int color)
|
||||||
{
|
{
|
||||||
t_coord dist;
|
t_coord dist;
|
||||||
int i;
|
int i;
|
||||||
@@ -34,7 +34,7 @@ void draw_line(t_game *game, t_vec vec, int color)
|
|||||||
j = 0;
|
j = 0;
|
||||||
while (ft_abs(i) <= ft_abs(dist.x) && ft_abs(j) <= ft_abs(dist.y))
|
while (ft_abs(i) <= ft_abs(dist.x) && ft_abs(j) <= ft_abs(dist.y))
|
||||||
{
|
{
|
||||||
draw_pixel(game, vec.start.x + i, vec.start.y + j, color);
|
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))
|
if (!ft_abs(dist.x) || ft_abs(j) < ft_abs(i * dist.y / dist.x))
|
||||||
j += ft_sign(dist.y);
|
j += ft_sign(dist.y);
|
||||||
else
|
else
|
||||||
@@ -60,11 +60,11 @@ void draw_line(t_game *game, t_vec vec, int color)
|
|||||||
if (rotation)
|
if (rotation)
|
||||||
rotate(&(game->plr), &(new));
|
rotate(&(game->plr), &(new));
|
||||||
if (!i || i == size - 1)
|
if (!i || i == size - 1)
|
||||||
draw_pixel(game, new.x, new.y, border);
|
draw_pixel(&game->map_img, new.x, new.y, border);
|
||||||
else if (!j || j == size - 1)
|
else if (!j || j == size - 1)
|
||||||
draw_pixel(game, new.x, new.y, border);
|
draw_pixel(&game->map_img, new.x, new.y, border);
|
||||||
else
|
else
|
||||||
draw_pixel(game, new.x, new.y, fill);
|
draw_pixel(&game->map_img, new.x, new.y, fill);
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
@@ -113,14 +113,14 @@ void draw_line(t_game *game, t_vec vec, int color)
|
|||||||
screen.end.x = rcast->screen_size.end.x + game->plr.pos.x;
|
screen.end.x = rcast->screen_size.end.x + game->plr.pos.x;
|
||||||
screen.end.y = rcast->screen_size.end.y + game->plr.pos.y;
|
screen.end.y = rcast->screen_size.end.y + game->plr.pos.y;
|
||||||
rotate(&(game->plr), &(screen.end));
|
rotate(&(game->plr), &(screen.end));
|
||||||
draw_line(game, screen, 0x00FFFFFF);
|
draw_line(&game->map_img, screen, 0x00FFFFFF);
|
||||||
// draw screen dist
|
// draw screen dist
|
||||||
screen.start.x = rcast->screen_dist.start.x + game->plr.pos.x;
|
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.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.x = rcast->screen_dist.end.x + game->plr.pos.x;
|
||||||
screen.end.y = rcast->screen_dist.end.y + game->plr.pos.y;
|
screen.end.y = rcast->screen_dist.end.y + game->plr.pos.y;
|
||||||
rotate(&(game->plr), &(screen.end));
|
rotate(&(game->plr), &(screen.end));
|
||||||
draw_line(game, screen, 0x00FFFFFF);
|
draw_line(&game->map_img, screen, 0x00FFFFFF);
|
||||||
}
|
}
|
||||||
// tmp end
|
// tmp end
|
||||||
|
|
||||||
|
|||||||
@@ -32,9 +32,25 @@
|
|||||||
|
|
||||||
void draw_column(t_game *game, t_rcast *rcast, t_vec *ray)
|
void draw_column(t_game *game, t_rcast *rcast, t_vec *ray)
|
||||||
{
|
{
|
||||||
|
t_vec wall;
|
||||||
|
|
||||||
(void)game;
|
(void)game;
|
||||||
(void)rcast;
|
(void)rcast;
|
||||||
(void)ray;
|
(void)ray;
|
||||||
|
(void)wall;
|
||||||
|
|
||||||
|
wall.start.x = rcast->ray_nb;
|
||||||
|
wall.end.x = rcast->ray_nb;
|
||||||
|
if (rcast->is_x == 1 && rcast->slope_y != 0)
|
||||||
|
{
|
||||||
|
rcast->wall_height = rcast->next_x - rcast->ray_step_x;
|
||||||
|
rcast->wall_height /= ft_abs(rcast->slope_y);
|
||||||
|
rcast->wall_height /= 2;
|
||||||
|
//printf("rcast->wall: %i\n", rcast->wall_height);
|
||||||
|
wall.start.y = rcast->screen_center / 2 + rcast->wall_height;
|
||||||
|
wall.end.y = rcast->screen_center / 2 - rcast->wall_height;
|
||||||
|
}
|
||||||
|
// draw_line(&game->img, wall, 0x00FF0000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void raycast(t_game *game, t_rcast *rcast)
|
void raycast(t_game *game, t_rcast *rcast)
|
||||||
@@ -48,7 +64,7 @@ void raycast(t_game *game, t_rcast *rcast)
|
|||||||
|
|
||||||
// tmp, to draw the map
|
// tmp, to draw the map
|
||||||
calcul_ray_end(rcast, &ray);
|
calcul_ray_end(rcast, &ray);
|
||||||
draw_line(game, ray, 0x00FF00FF);
|
draw_line(&game->map_img, ray, 0x00FF00FF);
|
||||||
// tmp end
|
// tmp end
|
||||||
|
|
||||||
draw_column(game, rcast, &ray);
|
draw_column(game, rcast, &ray);
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
#include "cube3d.h"
|
#include "cube3d.h"
|
||||||
|
|
||||||
static void init_img(t_img *img, t_game *game)
|
static void init_img(t_img *img, void *mlx_ptr, t_win *win)
|
||||||
{
|
{
|
||||||
img->ptr = mlx_new_image(game->mlx_ptr, game->map_win.size_x,
|
img->ptr = mlx_new_image(mlx_ptr, win->size_x, win->size_y);
|
||||||
game->map_win.size_y);
|
img->data = mlx_get_data_addr(img->ptr, &img->bpp, &img->szl, &img->ndn);
|
||||||
img->data = mlx_get_data_addr(img->ptr, &(img->bpp),
|
img->height = win->size_y;
|
||||||
&(img->sizel), &(img->endian));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_map(t_map *map)
|
static void init_map(t_map *map)
|
||||||
@@ -70,6 +69,7 @@ static void init_raycast(t_rcast *rcast)
|
|||||||
dist = (SCREEN_FOCAL / 2) * M_PI / 180;
|
dist = (SCREEN_FOCAL / 2) * M_PI / 180;
|
||||||
dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist));
|
dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist));
|
||||||
rcast->screen_def = SCREEN_WIDTH;
|
rcast->screen_def = SCREEN_WIDTH;
|
||||||
|
rcast->screen_center = SCREEN_HEIGHT / 2;
|
||||||
// screen dist
|
// screen dist
|
||||||
rcast->screen_dist.start.x = 0;
|
rcast->screen_dist.start.x = 0;
|
||||||
rcast->screen_dist.start.y = 0;
|
rcast->screen_dist.start.y = 0;
|
||||||
@@ -116,21 +116,17 @@ t_game *init_game(void)
|
|||||||
mb_add(game->mlx_ptr);
|
mb_add(game->mlx_ptr);
|
||||||
|
|
||||||
// tmp draw map
|
// tmp draw map
|
||||||
// size window map
|
|
||||||
game->map_win.size_x = game->map.size_x * CELL;
|
game->map_win.size_x = game->map.size_x * CELL;
|
||||||
game->map_win.size_y = game->map.size_y * CELL + SCREEN_HEIGHT;
|
game->map_win.size_y = game->map.size_y * CELL + SCREEN_HEIGHT;
|
||||||
// create the window
|
game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x, game->map_win.size_y, "map");
|
||||||
game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x,
|
init_img(&(game->map_img), game->mlx_ptr, &(game->map_win));
|
||||||
game->map_win.size_y, "map");
|
|
||||||
// create image and get its data address
|
|
||||||
init_img(&(game->map_img), game);
|
|
||||||
// tmp end
|
// tmp end
|
||||||
|
|
||||||
game->win.size_x = SCREEN_WIDTH;
|
game->win.size_x = SCREEN_WIDTH;
|
||||||
game->win.size_y = SCREEN_HEIGHT;
|
game->win.size_y = SCREEN_HEIGHT;
|
||||||
game->win.ptr = mlx_new_window(game->mlx_ptr, game->win.size_x,
|
game->win.ptr = mlx_new_window(game->mlx_ptr, game->win.size_x,
|
||||||
game->win.size_y, "cub3d");
|
game->win.size_y, "cub3d");
|
||||||
init_img(&(game->img), game);
|
init_img(&(game->img), game->mlx_ptr, &(game->win));
|
||||||
// k(ey)_hook is the array containing the values of key press events
|
// k(ey)_hook is the array containing the values of key press events
|
||||||
ft_bzero(&game->k_hook, sizeof(game->k_hook));
|
ft_bzero(&game->k_hook, sizeof(game->k_hook));
|
||||||
// raycasting
|
// raycasting
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ int plr_out_limits(t_game *game, int x, int y)
|
|||||||
int cell_x;
|
int cell_x;
|
||||||
int cell_y;
|
int cell_y;
|
||||||
|
|
||||||
xmax = game->map_img.sizel / (game->map_img.bpp / 8);
|
xmax = game->map_img.szl / (game->map_img.bpp / 8);
|
||||||
ymax = game->map_win.size_y;
|
ymax = game->map_win.size_y;
|
||||||
cell_x = x / game->map.cell;
|
cell_x = x / game->map.cell;
|
||||||
cell_y = y / game->map.cell;
|
cell_y = y / game->map.cell;
|
||||||
|
|||||||
Reference in New Issue
Block a user