add place for game window
This commit is contained in:
@@ -9,8 +9,10 @@
|
|||||||
# define PLR_MV 1
|
# define PLR_MV 1
|
||||||
/* nbr key you can press at the same time */
|
/* nbr key you can press at the same time */
|
||||||
# define MAX_NB_KEY 3
|
# define MAX_NB_KEY 3
|
||||||
/* screen definition */
|
/* screen width */
|
||||||
# define SCREEN_DEF 100
|
# define SCREEN_WIDTH 500
|
||||||
|
/* screen height */
|
||||||
|
# define SCREEN_HEIGHT 200
|
||||||
/* screen focal (in degree) */
|
/* screen focal (in degree) */
|
||||||
# define SCREEN_FOCAL 90
|
# define SCREEN_FOCAL 90
|
||||||
/* size of a cell on the map */
|
/* size of a cell on the map */
|
||||||
|
|||||||
@@ -113,18 +113,18 @@ typedef struct s_plr
|
|||||||
typedef struct s_game
|
typedef struct s_game
|
||||||
{
|
{
|
||||||
void *mlx_ptr;
|
void *mlx_ptr;
|
||||||
// map window
|
|
||||||
t_win map_win;
|
|
||||||
t_img map_img;
|
|
||||||
// game window
|
// game window
|
||||||
// t_win win;
|
t_win win;
|
||||||
// t_img img;
|
t_img img;
|
||||||
// player
|
// player
|
||||||
t_plr plr;
|
t_plr plr;
|
||||||
// rays
|
// rays
|
||||||
t_rcast rcast;
|
t_rcast rcast;
|
||||||
// map
|
// map
|
||||||
t_map map;
|
t_map map;
|
||||||
|
// map window
|
||||||
|
t_win map_win;
|
||||||
|
t_img map_img;
|
||||||
// key hook
|
// key hook
|
||||||
int k_hook[MAX_NB_KEY];
|
int k_hook[MAX_NB_KEY];
|
||||||
} t_game;
|
} t_game;
|
||||||
|
|||||||
@@ -11,8 +11,9 @@ void destroy_mlx(void *param)
|
|||||||
mlx_destroy_window(game->mlx_ptr, game->map_win.ptr);
|
mlx_destroy_window(game->mlx_ptr, game->map_win.ptr);
|
||||||
// tmp end
|
// tmp end
|
||||||
|
|
||||||
// mlx_destroy_image(game->mlx_ptr, game->img.ptr);
|
mlx_destroy_image(game->mlx_ptr, game->img.ptr);
|
||||||
// mlx_destroy_window(game->mlx_ptr, game->win.ptr);
|
mlx_destroy_window(game->mlx_ptr, game->win.ptr);
|
||||||
|
|
||||||
mlx_destroy_display(game->mlx_ptr);
|
mlx_destroy_display(game->mlx_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,17 +34,6 @@ int main(int ac, char **av)
|
|||||||
// draw game a first time before it start
|
// draw game a first time before it start
|
||||||
draw(game);
|
draw(game);
|
||||||
|
|
||||||
// tmp, to draw map
|
|
||||||
// receive a keypress event
|
|
||||||
mlx_hook(game->map_win.ptr, 2, 1L << 0, keypress, game);
|
|
||||||
// receive a keyprelease event
|
|
||||||
mlx_hook(game->map_win.ptr, 3, 1L << 1, keyrelease, game);
|
|
||||||
// receive event when clicking the red button to close the window
|
|
||||||
mlx_hook(game->map_win.ptr, 17, 1L << 17, shut_down, NULL);
|
|
||||||
// infinite loop that waits for events to occurs
|
|
||||||
// tmp end
|
|
||||||
|
|
||||||
/*
|
|
||||||
// receive a keypress event
|
// receive a keypress event
|
||||||
mlx_hook(game->win.ptr, 2, 1L << 0, keypress, game);
|
mlx_hook(game->win.ptr, 2, 1L << 0, keypress, game);
|
||||||
// receive a keyprelease event
|
// receive a keyprelease event
|
||||||
@@ -51,7 +41,6 @@ int main(int ac, char **av)
|
|||||||
// receive event when clicking the red button to close the window
|
// receive event when clicking the red button to close the window
|
||||||
mlx_hook(game->win.ptr, 17, 1L << 17, shut_down, NULL);
|
mlx_hook(game->win.ptr, 17, 1L << 17, shut_down, NULL);
|
||||||
// infinite loop that waits for events to occurs
|
// infinite loop that waits for events to occurs
|
||||||
*/
|
|
||||||
|
|
||||||
mlx_loop_hook(game->mlx_ptr, hook_action, game);
|
mlx_loop_hook(game->mlx_ptr, hook_action, game);
|
||||||
mlx_loop(game->mlx_ptr);
|
mlx_loop(game->mlx_ptr);
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ void draw(t_game *game)
|
|||||||
|
|
||||||
// tmp, to draw map
|
// tmp, to draw map
|
||||||
draw_screen(game, &(game->rcast));
|
draw_screen(game, &(game->rcast));
|
||||||
mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, 0);
|
mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, SCREEN_HEIGHT);
|
||||||
// tmp end
|
// tmp end
|
||||||
|
|
||||||
// mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0);
|
// mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0);
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ static void init_raycast(t_rcast *rcast, t_vec *ray)
|
|||||||
|
|
||||||
static void init_first_step(t_rcast *rcast, t_vec *ray)
|
static void init_first_step(t_rcast *rcast, t_vec *ray)
|
||||||
{
|
{
|
||||||
// first next time ray cross grid
|
|
||||||
rcast->first_next_x = ray->start.x % rcast->cell;
|
rcast->first_next_x = ray->start.x % rcast->cell;
|
||||||
if (rcast->first_next_x && rcast->ray_sign_x < 0)
|
if (rcast->first_next_x && rcast->ray_sign_x < 0)
|
||||||
rcast->first_next_x = rcast->cell - rcast->first_next_x;
|
rcast->first_next_x = rcast->cell - rcast->first_next_x;
|
||||||
@@ -58,70 +57,6 @@ static void next_cell(t_rcast *rcast)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void calcul_ray_end(t_rcast *rcast, t_vec *ray)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
if (rcast->is_x)
|
|
||||||
{
|
|
||||||
ray->end.x = rcast->cell_x * rcast->cell;
|
|
||||||
if (rcast->ray_sign_x == 1)
|
|
||||||
ray->end.x += rcast->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 * rcast->cell;
|
|
||||||
if (rcast->ray_sign_y == 1)
|
|
||||||
ray->end.y += rcast->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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
int *nd1;
|
|
||||||
int *cell1;
|
|
||||||
int *sign1;
|
|
||||||
int *st1;
|
|
||||||
int *slp1;
|
|
||||||
int *nd2;
|
|
||||||
int *st2;
|
|
||||||
int *slp2;
|
|
||||||
|
|
||||||
if (rcast->is_x)
|
|
||||||
{
|
|
||||||
nd1 = &(ray->end.x);
|
|
||||||
cell1 = &(rcast->cell_x);
|
|
||||||
sign1 = &(rcast->ray_sign_x);
|
|
||||||
st1 = &(ray->start.x);
|
|
||||||
slp1 = &(rcast->slope_x);
|
|
||||||
nd2 = &(ray->end.y);
|
|
||||||
st2 = &(ray->start.y);
|
|
||||||
slp2 = &(rcast->slope_y);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
nd1 = &(ray->end.y);
|
|
||||||
cell1 = &(rcast->cell_y);
|
|
||||||
sign1 = &(rcast->ray_sign_y);
|
|
||||||
st1 = &(ray->start.y);
|
|
||||||
slp1 = &(rcast->slope_y);
|
|
||||||
nd2 = &(ray->end.x);
|
|
||||||
st2 = &(ray->start.x);
|
|
||||||
slp2 = &(rcast->slope_x);
|
|
||||||
}
|
|
||||||
*nd1 = *cell1 * rcast->cell;
|
|
||||||
if (*sign1 == 1)
|
|
||||||
*nd1 += rcast->cell;
|
|
||||||
if (*slp1)
|
|
||||||
*nd2 = *st2 + (double)(*slp2) * (double)(*nd1 - *st1) / (double)(*slp1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray)
|
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.x = rcast->ray.start.x + game->plr.pos.x;
|
||||||
@@ -132,9 +67,6 @@ void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray)
|
|||||||
rotate(&(game->plr), &(ray->end));
|
rotate(&(game->plr), &(ray->end));
|
||||||
init_raycast(rcast, ray);
|
init_raycast(rcast, ray);
|
||||||
init_first_step(rcast, ray);
|
init_first_step(rcast, ray);
|
||||||
// loop through grid
|
|
||||||
while (!is_wall(game, rcast->cell_x, rcast->cell_y))
|
while (!is_wall(game, rcast->cell_x, rcast->cell_y))
|
||||||
next_cell(rcast);
|
next_cell(rcast);
|
||||||
// end ray position
|
|
||||||
calcul_ray_end(rcast, ray);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,42 @@
|
|||||||
#include "cube3d.h"
|
#include "cube3d.h"
|
||||||
|
|
||||||
|
// tmp, to draw rays
|
||||||
|
static void calcul_ray_end(t_rcast *rcast, t_vec *ray)
|
||||||
|
{
|
||||||
|
if (rcast->is_x)
|
||||||
|
{
|
||||||
|
ray->end.x = rcast->cell_x * rcast->cell;
|
||||||
|
if (rcast->ray_sign_x == 1)
|
||||||
|
ray->end.x += rcast->cell;
|
||||||
|
if (rcast->slope_x)
|
||||||
|
{
|
||||||
|
rcast->ratio = (double)(ray->end.x - ray->start.x);
|
||||||
|
rcast->ratio /= (double)rcast->slope_x;
|
||||||
|
ray->end.y = ray->start.y + (double)rcast->slope_y * rcast->ratio;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ray->end.y = rcast->cell_y * rcast->cell;
|
||||||
|
if (rcast->ray_sign_y == 1)
|
||||||
|
ray->end.y += rcast->cell;
|
||||||
|
if (rcast->slope_y)
|
||||||
|
{
|
||||||
|
rcast->ratio = (double)(ray->end.y - ray->start.y);
|
||||||
|
rcast->ratio /= (double)rcast->slope_y;
|
||||||
|
ray->end.x = ray->start.x + (double)rcast->slope_x * rcast->ratio;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// tmp end
|
||||||
|
|
||||||
|
void draw_column(t_game *game, t_rcast *rcast, t_vec *ray)
|
||||||
|
{
|
||||||
|
(void)game;
|
||||||
|
(void)rcast;
|
||||||
|
(void)ray;
|
||||||
|
}
|
||||||
|
|
||||||
void raycast(t_game *game, t_rcast *rcast)
|
void raycast(t_game *game, t_rcast *rcast)
|
||||||
{
|
{
|
||||||
t_vec ray;
|
t_vec ray;
|
||||||
@@ -10,10 +47,11 @@ void raycast(t_game *game, t_rcast *rcast)
|
|||||||
ray_intersect(game, rcast, &ray);
|
ray_intersect(game, rcast, &ray);
|
||||||
|
|
||||||
// tmp, to draw the map
|
// tmp, to draw the map
|
||||||
|
calcul_ray_end(rcast, &ray);
|
||||||
draw_line(game, ray, 0x00FF00FF);
|
draw_line(game, ray, 0x00FF00FF);
|
||||||
// tmp end
|
// tmp end
|
||||||
|
|
||||||
// draw_column();
|
draw_column(game, rcast, &ray);
|
||||||
(rcast->ray_nb)++;
|
(rcast->ray_nb)++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,22 +68,22 @@ static void init_raycast(t_rcast *rcast)
|
|||||||
double dist;
|
double dist;
|
||||||
|
|
||||||
dist = (SCREEN_FOCAL / 2) * M_PI / 180;
|
dist = (SCREEN_FOCAL / 2) * M_PI / 180;
|
||||||
dist = cos(dist) * ((SCREEN_DEF / 2) / sin(dist));
|
dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist));
|
||||||
rcast->screen_def = SCREEN_DEF;
|
rcast->screen_def = SCREEN_WIDTH;
|
||||||
// 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;
|
||||||
rcast->screen_dist.end.x = 0;
|
rcast->screen_dist.end.x = 0;
|
||||||
rcast->screen_dist.end.y = -dist;
|
rcast->screen_dist.end.y = -dist;
|
||||||
// screen size
|
// screen size
|
||||||
rcast->screen_size.start.x = -SCREEN_DEF / 2;
|
rcast->screen_size.start.x = -SCREEN_WIDTH / 2;
|
||||||
rcast->screen_size.start.y = -dist;
|
rcast->screen_size.start.y = -dist;
|
||||||
rcast->screen_size.end.x = SCREEN_DEF / 2;
|
rcast->screen_size.end.x = SCREEN_WIDTH / 2;
|
||||||
rcast->screen_size.end.y = -dist;
|
rcast->screen_size.end.y = -dist;
|
||||||
// first ray
|
// first ray
|
||||||
rcast->ray.start.x = 0;
|
rcast->ray.start.x = 0;
|
||||||
rcast->ray.start.y = 0;
|
rcast->ray.start.y = 0;
|
||||||
rcast->ray.end.x = -SCREEN_DEF / 2;
|
rcast->ray.end.x = -SCREEN_WIDTH / 2;
|
||||||
rcast->ray.end.y = -dist;
|
rcast->ray.end.y = -dist;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,21 +111,30 @@ t_game *init_game(void)
|
|||||||
init_map(&(game->map));
|
init_map(&(game->map));
|
||||||
// plr
|
// plr
|
||||||
init_plr(&(game->plr));
|
init_plr(&(game->plr));
|
||||||
// size window map
|
|
||||||
game->map_win.size_x = game->map.size_x * CELL;
|
|
||||||
game->map_win.size_y = game->map.size_y * CELL;
|
|
||||||
// init connexion to server
|
// init connexion to server
|
||||||
game->mlx_ptr = mlx_init();
|
game->mlx_ptr = mlx_init();
|
||||||
mb_add(game->mlx_ptr);
|
mb_add(game->mlx_ptr);
|
||||||
|
|
||||||
|
// tmp draw map
|
||||||
|
// size window map
|
||||||
|
game->map_win.size_x = game->map.size_x * CELL;
|
||||||
|
game->map_win.size_y = game->map.size_y * CELL + SCREEN_HEIGHT;
|
||||||
// create the window
|
// create the window
|
||||||
game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x,
|
game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x,
|
||||||
game->map_win.size_y, "test");
|
game->map_win.size_y, "map");
|
||||||
|
// create image and get its data address
|
||||||
|
init_img(&(game->map_img), game);
|
||||||
|
// tmp end
|
||||||
|
|
||||||
|
game->win.size_x = SCREEN_WIDTH;
|
||||||
|
game->win.size_y = SCREEN_HEIGHT;
|
||||||
|
game->win.ptr = mlx_new_window(game->mlx_ptr, game->win.size_x,
|
||||||
|
game->win.size_y, "cub3d");
|
||||||
|
init_img(&(game->img), game);
|
||||||
// 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
|
||||||
init_raycast(&(game->rcast));
|
init_raycast(&(game->rcast));
|
||||||
game->rcast.cell = game->map.cell;
|
game->rcast.cell = game->map.cell;
|
||||||
// create image and get its data address
|
|
||||||
init_img(&(game->map_img), game);
|
|
||||||
return (game);
|
return (game);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user