add init textures

This commit is contained in:
hugogogo
2022-05-02 16:11:54 +02:00
parent a6fd2df6d5
commit 628a0c0cad
9 changed files with 113 additions and 75 deletions

View File

@@ -26,7 +26,9 @@ SRCS += memorybook.c \
memorybook_2d.c \
memorybook_utils.c
# init/
SRCS += init_struct.c
SRCS += init_struct.c \
init_game.c \
init_textures.c
# gnl/
SRCS += get_next_line.c \
get_next_line_utils.c

View File

@@ -14,8 +14,10 @@ int shut_down();
// -------------------------------
// init_struct.c
t_game *init_struct(void);
// init_game.c
void init_game(t_game *game);
void init_plr(t_plr *plr, t_map *map);
// init_textures.c
void init_txtr(t_txt *txt, void *mlx_ptr);
// -------------------------------

View File

@@ -77,6 +77,7 @@ typedef struct s_img
int szl;
int ndn;
int height;
int width;
} t_img;
/*
@@ -91,6 +92,10 @@ typedef struct s_txt
char *txt_west;
int rgb_floor;
int rgb_ceiling;
t_img img_n;
t_img img_s;
t_img img_e;
t_img img_w;
} t_txt;
/*

View File

@@ -44,7 +44,6 @@ int main(int ac, char **av)
// receive event when clicking the red button to close the window
mlx_hook(game->win.ptr, 17, 1L << 17, shut_down, NULL);
// infinite loop that waits for events to occurs
mlx_loop_hook(game->mlx_ptr, hook_action, game);
mlx_loop(game->mlx_ptr);
return (0);

View File

@@ -22,10 +22,22 @@ void draw_floor_ceiling(t_game *game, t_rcast *rcast, t_txt *txt)
void draw_column(t_game *game, t_rcast *rcast)
{
int color;
int color_n;
int color_s;
int color_e;
int color_o;
color_n = 0x00FF00FF;
color_s = 0x0000EEEE;
color_e = 0x00DDDD00;
color_o = 0x00CCCCCC;
draw_floor_ceiling(game, rcast, &game->txt);
color = 0x00FF00FF;
if (rcast->is_x)
color = 0x00EE00EE;
draw_line(&game->img, &rcast->wall, color);
if (!rcast->is_x && rcast->slope_y > 0)
draw_line(&game->img, &rcast->wall, color_n);
if (!rcast->is_x && rcast->slope_y < 0)
draw_line(&game->img, &rcast->wall, color_s);
if (rcast->is_x && rcast->slope_x > 0)
draw_line(&game->img, &rcast->wall, color_e);
if (rcast->is_x && rcast->slope_x < 0)
draw_line(&game->img, &rcast->wall, color_o);
}

67
srcs/init/init_game.c Normal file
View File

@@ -0,0 +1,67 @@
#include "cube3d.h"
static void init_img(t_img *img, void *mlx_ptr, t_win *win)
{
img->ptr = mlx_new_image(mlx_ptr, win->size_x, win->size_y);
img->data = mlx_get_data_addr(img->ptr, &img->bpp, &img->szl, &img->ndn);
img->height = win->size_y;
}
static void init_raycast(t_rcast *rcast)
{
double dist;
dist = (SCREEN_FOCAL / 2) * M_PI / 180;
dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist));
rcast->screen_dist = dist;
rcast->screen_width = SCREEN_WIDTH;
rcast->screen_height = SCREEN_HEIGHT;
// screen size
rcast->screen_size.start.x = -SCREEN_WIDTH / 2;
rcast->screen_size.start.y = -dist;
rcast->screen_size.end.x = SCREEN_WIDTH / 2;
rcast->screen_size.end.y = -dist;
// first ray
rcast->ray.start.x = 0;
rcast->ray.start.y = 0;
rcast->ray.end.x = -SCREEN_WIDTH / 2;
rcast->ray.end.y = -dist;
}
static void init_plr(t_plr *plr, t_map *map)
{
// player first position
plr->exact.x = map->plr_x * CELL + CELL / 2;
plr->exact.y = map->plr_y * CELL + CELL / 2;
plr->pos.x = plr->exact.x;
plr->pos.y = plr->exact.y;
// rotation
plr->deg = PLR_ROT;
plr->rot = 0;
plr->cosi = 0;
plr->cosj = 0;
plr->sini = 0;
plr->sinj = 0;
}
void init_game(t_game *game)
{
// plr
init_plr(&(game->plr), &(game->map));
// init connexion to server
game->mlx_ptr = mlx_init();
mb_add(game->mlx_ptr);
// create window
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");
// create img
init_img(&(game->img), game->mlx_ptr, &game->win);
// create textures img
init_txtr(&game->txt, game->mlx_ptr);
// 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));
game->rcast.cell = game->map.cell;
}

View File

@@ -1,12 +1,5 @@
#include "cube3d.h"
static void init_img(t_img *img, void *mlx_ptr, t_win *win)
{
img->ptr = mlx_new_image(mlx_ptr, win->size_x, win->size_y);
img->data = mlx_get_data_addr(img->ptr, &img->bpp, &img->szl, &img->ndn);
img->height = win->size_y;
}
static void init_map(t_map *map)
{
map->content = NULL;
@@ -26,43 +19,6 @@ static void init_txt(t_txt *txt)
txt->txt_west = NULL;
}
static void init_raycast(t_rcast *rcast)
{
double dist;
dist = (SCREEN_FOCAL / 2) * M_PI / 180;
dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist));
rcast->screen_dist = dist;
rcast->screen_width = SCREEN_WIDTH;
rcast->screen_height = SCREEN_HEIGHT;
// screen size
rcast->screen_size.start.x = -SCREEN_WIDTH / 2;
rcast->screen_size.start.y = -dist;
rcast->screen_size.end.x = SCREEN_WIDTH / 2;
rcast->screen_size.end.y = -dist;
// first ray
rcast->ray.start.x = 0;
rcast->ray.start.y = 0;
rcast->ray.end.x = -SCREEN_WIDTH / 2;
rcast->ray.end.y = -dist;
}
void init_plr(t_plr *plr, t_map *map)
{
// player first position
plr->exact.x = map->plr_x * CELL + CELL / 2;
plr->exact.y = map->plr_y * CELL + CELL / 2;
plr->pos.x = plr->exact.x;
plr->pos.y = plr->exact.y;
// rotation
plr->deg = PLR_ROT;
plr->rot = 0;
plr->cosi = 0;
plr->cosj = 0;
plr->sini = 0;
plr->sinj = 0;
}
t_game *init_struct(void)
{
t_game *game;
@@ -74,23 +30,3 @@ t_game *init_struct(void)
init_txt(&(game->txt));
return (game);
}
void init_game(t_game *game)
{
// plr
init_plr(&(game->plr), &(game->map));
// init connexion to server
game->mlx_ptr = mlx_init();
mb_add(game->mlx_ptr);
// reate window
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");
// create img
init_img(&(game->img), game->mlx_ptr, &(game->win));
// 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));
game->rcast.cell = game->map.cell;
}

15
srcs/init/init_textures.c Normal file
View File

@@ -0,0 +1,15 @@
#include "cube3d.h"
static void init_txtr_img(t_img *img, void *mlx_ptr, char *path)
{
img->ptr = mlx_xpm_file_to_image(mlx_ptr, path, &img->width, &img->height);
img->data = mlx_get_data_addr(img->ptr, &img->bpp, &img->szl, &img->ndn);
}
void init_txtr(t_txt *txt, void *mlx_ptr)
{
init_txtr_img(&txt->img_n, mlx_ptr, txt->txt_north);
init_txtr_img(&txt->img_n, mlx_ptr, txt->txt_south);
init_txtr_img(&txt->img_n, mlx_ptr, txt->txt_east);
init_txtr_img(&txt->img_n, mlx_ptr, txt->txt_west);
}

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/16 20:54:37 by pblagoje #+# #+# */
/* Updated: 2022/04/23 18:38:15 by pblagoje ### ########.fr */
/* Updated: 2022/05/02 15:45:01 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,7 +37,7 @@ int set_path(t_txt *txt, char *path, char identifier)
return (EXIT_FAILURE);
}
int check_path(t_txt *txt, char *element, char identifier)
static int check_path(t_txt *txt, char *element, char identifier)
{
char *path;
int fd;
@@ -64,7 +64,7 @@ int check_path(t_txt *txt, char *element, char identifier)
return (EXIT_SUCCESS);
}
int check_element(t_game *game, char *element)
static int check_element(t_game *game, char *element)
{
char identifier[3];