From 628a0c0cad1ea0a0594a852eae04898c2ab67a29 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 2 May 2022 16:11:54 +0200 Subject: [PATCH] add init textures --- Makefile | 4 ++- headers/cube3d_proto.h | 4 ++- headers/cube3d_struct.h | 5 +++ srcs/cube3d.c | 1 - srcs/draw/draw_column.c | 22 ++++++++++--- srcs/init/init_game.c | 67 +++++++++++++++++++++++++++++++++++++++ srcs/init/init_struct.c | 64 ------------------------------------- srcs/init/init_textures.c | 15 +++++++++ srcs/parsing/check_path.c | 6 ++-- 9 files changed, 113 insertions(+), 75 deletions(-) create mode 100644 srcs/init/init_game.c create mode 100644 srcs/init/init_textures.c diff --git a/Makefile b/Makefile index 3d49b1f..d87d4a5 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 1187710..b5bdf6e 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -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); // ------------------------------- diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index e529288..b2074ec 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -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; /* diff --git a/srcs/cube3d.c b/srcs/cube3d.c index f30c68d..ca058fd 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -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); diff --git a/srcs/draw/draw_column.c b/srcs/draw/draw_column.c index 6fe585f..0594d9c 100644 --- a/srcs/draw/draw_column.c +++ b/srcs/draw/draw_column.c @@ -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); } diff --git a/srcs/init/init_game.c b/srcs/init/init_game.c new file mode 100644 index 0000000..e5463ee --- /dev/null +++ b/srcs/init/init_game.c @@ -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; +} diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index e24d529..9e172d7 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -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; -} diff --git a/srcs/init/init_textures.c b/srcs/init/init_textures.c new file mode 100644 index 0000000..e14f906 --- /dev/null +++ b/srcs/init/init_textures.c @@ -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); +} diff --git a/srcs/parsing/check_path.c b/srcs/parsing/check_path.c index 5aae789..3fd3d1a 100644 --- a/srcs/parsing/check_path.c +++ b/srcs/parsing/check_path.c @@ -6,7 +6,7 @@ /* By: pblagoje +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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];