Files
42_INT_10_cube3d/srcs/init/init_struct.c
2022-04-23 20:12:13 +02:00

162 lines
4.5 KiB
C

#include "cube3d.h"
static void init_img(t_img *img, t_game *game)
{
img->ptr = mlx_new_image(game->mlx_ptr, game->map_win.size_x,
game->map_win.size_y);
img->data = mlx_get_data_addr(img->ptr, &(img->bpp),
&(img->sizel), &(img->endian));
}
static void init_map(t_map *map)
{
/* map->size_x = 24;
map->size_y = 24;
// map
int tmp[24][24] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int i = 0;
map->content = mb_alloc(sizeof(char*) * (map->size_y + 1));
(map->content)[map->size_y] = NULL;
while (i < 24)
{
(map->content)[i] = mb_alloc(sizeof(char) * (map->size_x + 1));
(map->content)[i][map->size_x] = '\0';
i++;
}
int j;
i = 0;
while (i < map->size_y)
{
j = 0;
while (j < map->size_x)
{
(map->content)[i][j] = tmp[i][j] + '0';
j++;
}
i++;
} */
map->content = NULL;
map->tmp_str = NULL;
map->size_x = 0;
map->size_y = 0;
map->plr_x = 0;
map->plr_y = 0;
}
static void init_txt(t_txt *txt)
{
txt->txt_north = NULL;
txt->txt_south = NULL;
txt->txt_east = NULL;
txt->txt_west = NULL;
}
static void init_raycast(t_rcast *rcast)
{
// draw screen dist
rcast->screen_dist.start.x = 0;
rcast->screen_dist.start.y = 0;
rcast->screen_dist.end.x = 0;
rcast->screen_dist.end.y = -SCREEN_DIST;
// draw screen size
rcast->screen_size.start.x = -SCREEN_SIZE / 2;
rcast->screen_size.start.y = -SCREEN_DIST;
rcast->screen_size.end.x = SCREEN_SIZE / 2;
rcast->screen_size.end.y = -SCREEN_DIST;
// draw ray
rcast->ray.start.x = 0;
rcast->ray.start.y = 0;
rcast->ray.end.x = -SCREEN_SIZE / 2;
rcast->ray.end.y = -SCREEN_DIST;
}
void init_plr(t_plr *plr, t_map *map)
{
// player first position
plr->exact.x = map->plr_x * CELL;
plr->exact.y = map->plr_y * CELL;
plr->pos.x = plr->exact.x;
plr->pos.y = plr->exact.y;
// rotation
plr->rot = 0;
plr->cosi = 0;
plr->cosj = 0;
plr->sini = 0;
plr->sinj = 0;
}
t_game *init_struct(void)
{
t_game *game;
game = mb_alloc(sizeof(t_game));
// map
init_map(&(game->map));
// init textures and floor/ceiling colors
init_txt(&(game->txt));
/* // 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
game->mlx_ptr = mlx_init();
mb_add(game->mlx_ptr);
// create the window
game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x,
game->map_win.size_y, "test");
// k(ey)_hook is the array containing the values of key press events
ft_bzero(&game->k_hook, sizeof(game->k_hook));
// raycasting
init_raycast(&(game->rcast));
// create image and get its data address
init_img(&(game->map_img), game); */
return (game);
}
void init_game(t_game *game)
{
// plr
init_plr(&(game->plr), &(game->map));
// 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
game->mlx_ptr = mlx_init();
mb_add(game->mlx_ptr);
// create the window
game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x,
game->map_win.size_y, "test");
// k(ey)_hook is the array containing the values of key press events
ft_bzero(&game->k_hook, sizeof(game->k_hook));
// raycasting
init_raycast(&(game->rcast));
// create image and get its data address
init_img(&(game->map_img), game);
}