From ab6fe38f996eacef60d6c283e906d70546764f2f Mon Sep 17 00:00:00 2001 From: Sadoming Date: Fri, 31 Jan 2025 16:59:03 +0100 Subject: [PATCH 1/3] Update README.md --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 594ce2b..e0a9eb4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # Cub3D error tile -~ Started on: 13/10/2024 ~ *** ~ Ended on: 16/01/2025 Ranked with 105 --- I will re-do it! (In a minute now.. and for myself :>) +~ Started on: 13/10/2024 ~ *** ~ Ended on: RE-DOING [![Stars](https://img.shields.io/github/stars/Sulig/Cub3D?color=ffff00&label=Stars&logo=Stars&style=?style=flat)](https://github.com/Sulig/Cub3D) [![Size](https://img.shields.io/github/repo-size/Sulig/Cub3D?color=blue&label=Size&logo=Size&style=?style=flat)](https://github.com/Sulig/Cub3D) @@ -42,7 +41,7 @@ You can change the window size by just dragging the screen or clicking on the ic Close the game with `esc` or close directly with the cross icon. -### BONUS ADDED +## BONUS ADDED If you execute this command: `make run-bonus` you can play at this game with these bonus: - Move the camera with the mouse! @@ -52,7 +51,10 @@ If you execute this command: `make run-bonus` you can play at this game with the *** +> [!WARNING]\ +> This branch is in progress and reciving more changes! +> This can cause a break or and overload on a run! + *** ~ Made by sadoming ~ -~ With the collaboration of Andmart2 ~ *** From 4ee2f972a9eb3406a6536b47a1bd13bcc5f1f94b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sasha=20Dom=C3=ADnguez=20Ferrer?= Date: Thu, 6 Mar 2025 16:34:13 +0100 Subject: [PATCH 2/3] Re Doing Raycast --- Makefile | 17 ++- README.md | 21 +-- bonus/action_bonus.c | 85 ------------ bonus/bonus_actions_bonus.c | 81 ------------ bonus/check_file_bonus.c | 67 ---------- bonus/check_map_bonus.c | 71 ---------- bonus/collisions_bonus.c | 52 -------- bonus/cub_main_bonus.c | 35 ----- bonus/inc_bonus/consts_bonus.h | 68 ---------- bonus/inc_bonus/cub_structs_bonus.h | 194 ---------------------------- bonus/inc_bonus/game_bonus.h | 73 ----------- bonus/man_memory_bonus.c | 70 ---------- bonus/paint_minimap_bonus.c | 111 ---------------- bonus/parse_info_bonus.c | 152 ---------------------- bonus/print_errors_bonus.c | 78 ----------- bonus/raycasting_bonus.c | 140 -------------------- bonus/raycollision_bonus.c | 148 --------------------- bonus/start_bonus.c | 97 -------------- bonus/utilities_bonus.c | 88 ------------- libft/src/arrays/ft_arrpop.c | 17 ++- libft/src/ft_str/ft_strstr.c | 4 +- src/action.c | 16 +-- src/check_file.c | 71 +++++++++- src/check_map.c | 136 ++++++++++++++----- src/collisions.c | 46 ------- src/cub_main.c | 4 +- src/ft_print_map_t.c | 2 +- src/inc/consts.h | 35 +++-- src/inc/cub_structs.h | 17 ++- src/inc/error_messages.h | 39 ++++++ src/inc/game.h | 12 +- src/man_memory.c | 7 +- src/parse_info.c | 4 +- src/print_errors.c | 38 +++--- src/raycasting.c | 4 +- src/raycollision.c | 4 +- src/start.c | 4 +- src/textures.c | 60 +++++++++ src/utilities.c | 17 +-- 39 files changed, 382 insertions(+), 1803 deletions(-) delete mode 100644 bonus/action_bonus.c delete mode 100644 bonus/bonus_actions_bonus.c delete mode 100644 bonus/check_file_bonus.c delete mode 100644 bonus/check_map_bonus.c delete mode 100644 bonus/collisions_bonus.c delete mode 100644 bonus/cub_main_bonus.c delete mode 100644 bonus/inc_bonus/consts_bonus.h delete mode 100644 bonus/inc_bonus/cub_structs_bonus.h delete mode 100644 bonus/inc_bonus/game_bonus.h delete mode 100644 bonus/man_memory_bonus.c delete mode 100644 bonus/paint_minimap_bonus.c delete mode 100644 bonus/parse_info_bonus.c delete mode 100644 bonus/print_errors_bonus.c delete mode 100644 bonus/raycasting_bonus.c delete mode 100644 bonus/raycollision_bonus.c delete mode 100644 bonus/start_bonus.c delete mode 100644 bonus/utilities_bonus.c delete mode 100644 src/collisions.c create mode 100644 src/inc/error_messages.h create mode 100644 src/textures.c diff --git a/Makefile b/Makefile index b667b8e..74d25d3 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: sadoming +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 17:25:36 by sadoming #+# #+# # -# Updated: 2025/01/16 16:25:14 by sadoming ### ########.fr # +# Updated: 2025/03/05 20:30:08 by sadoming ### ########.fr # # # # **************************************************************************** # @@ -15,7 +15,7 @@ BONUS := cub3D_bonus # Default map to run MAP_DIR := ./assets/maps/ -MAP_NAME := $(MAP_DIR)other-map.cub +MAP_NAME := $(MAP_DIR)example-map2.cub RUN_MAP_NAME:= $(MAPS)$(MAP_NAME) # ------------------ # @@ -75,7 +75,7 @@ MLX_BUILD_DIR := ./MLX42/build/ SRC_SRC := cub_main.c man_memory.c check_file.c check_map.c\ print_errors.c ft_print_map_t.c parse_info.c\ start.c action.c raycasting.c raycollision.c\ - utilities.c collisions.c + textures.c utilities.c SRC := $(addprefix $(SRC_DIR), $(SRC_SRC)) @@ -125,8 +125,7 @@ mlx42: #-------------------------------------------------------------# author: @echo "$(P)~ **************************************** ~\n" - @echo " ~\t Made by Sadoming \t ~" - @echo " ~ With the collaboration of Andmart2 ~" + @echo " ~\t Made by Sadoming \t ~\n" @echo "~ **************************************** ~\n$(DEF)\n" #-------------------------------------------------------------# bonus: mlx42 $(LIB_LIB) $(BONUS) @@ -211,17 +210,17 @@ $(BONUS): $(OBJSB) $(LIB_LIB) $(MLX_LIB) # ********************************************************************************* # # Debug region -val: $(NAME) +val: all $(NAME) @valgrind --leak-check=full --track-origins=yes ./$(NAME) $(RUN_MAP_NAME) -val_s: $(NAME) +val_s: all $(NAME) @valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) $(RUN_MAP_NAME) #-----------# -val-b: $(BONUS) +val-b: bonus $(BONUS) @valgrind --leak-check=full --track-origins=yes ./$(BONUS) $(RUN_MAP_NAME) -val_s-b: $(BONUS) +val_s-b: bonus $(BONUS) @valgrind --leak-check=full --show-leak-kinds=all ./$(BONUS) $(RUN_MAP_NAME) #-----------# diff --git a/README.md b/README.md index e0a9eb4..1771fe3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Cub3D error tile -~ Started on: 13/10/2024 ~ *** ~ Ended on: RE-DOING +~ Started on: 13/10/2024 ~ *** ~ Ended on: Re-Doing [![Stars](https://img.shields.io/github/stars/Sulig/Cub3D?color=ffff00&label=Stars&logo=Stars&style=?style=flat)](https://github.com/Sulig/Cub3D) [![Size](https://img.shields.io/github/repo-size/Sulig/Cub3D?color=blue&label=Size&logo=Size&style=?style=flat)](https://github.com/Sulig/Cub3D) @@ -19,9 +19,10 @@ Your goal will be to make a dynamic view inside a maze, in which you’ll have t - [The New_Libft](https://github.com/Sulig/New_Libft) - [MinilibX Codam](https://github.com/codam-coding-college/MLX42) - // Cloned into repo when you "make" -- The Cub3D `src` and some bonus! +- The Cub3D `src` (Re-Doing) - [Subject English](https://github.com/Sulig/Cub3D/blob/master/Cub3D_subject-en.pdf) -** + +*** ## CONTROLS Move on the map with: `W A S D` @@ -41,20 +42,12 @@ You can change the window size by just dragging the screen or clicking on the ic Close the game with `esc` or close directly with the cross icon. -## BONUS ADDED -If you execute this command: `make run-bonus` you can play at this game with these bonus: - - - Move the camera with the mouse! - - Take a look at where you are in a fabulous minimap! - - (Yet added in mandatory) Don't walk across the walls! - - ¡GHOST MODE! -> `SPACE` +### NO BONUS IN THIS BRANCH! +There's no bonus in this branch, because i'm re-doing the Raycast in mandatory. +When finished, i will do bonus. *** -> [!WARNING]\ -> This branch is in progress and reciving more changes! -> This can cause a break or and overload on a run! - *** ~ Made by sadoming ~ *** diff --git a/bonus/action_bonus.c b/bonus/action_bonus.c deleted file mode 100644 index 7b3eb1a..0000000 --- a/bonus/action_bonus.c +++ /dev/null @@ -1,85 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* action_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/16 19:05:22 by andmart2 #+# #+# */ -/* Updated: 2025/01/14 19:27:56 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -/* Rotate the perspective view */ -void rotate(t_game *gm, char direction) -{ - if (direction == ROT_LEFT) - gm->ply.pa -= ANGLE_D; - else if (direction == ROT_RIGHT) - gm->ply.pa += ANGLE_D; - if (gm->ply.pa < 0) - gm->ply.pa += 2 * PI; - if (gm->ply.pa > 2 * PI) - gm->ply.pa -= 2 * PI; - gm->ply.pdx = cos(gm->ply.pa) * VEL; - gm->ply.pdy = sin(gm->ply.pa) * VEL; -} - -static void move_player(t_game *gm, double angle, int sign) -{ - gm->ply.new_px = gm->ply.px; - gm->ply.new_py = gm->ply.py; - gm->ply.new_px += sign * VEL * cos(angle); - gm->ply.new_py += sign * VEL * sin(angle); -} - -/* Translate / Move the player */ -void translate(t_game *gm, char dir) -{ - if (dir == MOV_LEFT) - move_player(gm, gm->ply.pa + P2, -1); - else if (dir == MOV_RIGHT) - move_player(gm, gm->ply.pa + P2, 1); - else if (dir == MOV_UP) - move_player(gm, gm->ply.pa, 1); - else if (dir == MOV_DOWN) - move_player(gm, gm->ply.pa, -1); - if (can_move_to(gm)) - { - gm->ply.px = gm->ply.new_px; - gm->ply.py = gm->ply.new_py; - } - gm->ply.ipx = gm->ply.px / CUB_SCALE; - gm->ply.ipy = gm->ply.py / CUB_SCALE; - if (gm->ply.ipx < 0 || gm->ply.ipy < 0) - print_other_err(PLY_TRSNMAP); -} - -void hook_keyboard(void *param) -{ - t_game *gm; - - gm = (t_game *)param; - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_ESCAPE)) - mlx_close_window(gm->mlxd->mlx); - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_W)) - translate(gm, MOV_UP); - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_UP)) - translate(gm, MOV_UP); - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_S)) - translate(gm, MOV_DOWN); - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_DOWN)) - translate(gm, MOV_DOWN); - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_A)) - translate(gm, MOV_LEFT); - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_D)) - translate(gm, MOV_RIGHT); - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_LEFT)) - rotate(gm, ROT_LEFT); - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_RIGHT)) - rotate(gm, ROT_RIGHT); - gm = bonus_actions(gm); - raycasting(gm); -} diff --git a/bonus/bonus_actions_bonus.c b/bonus/bonus_actions_bonus.c deleted file mode 100644 index 06c697c..0000000 --- a/bonus/bonus_actions_bonus.c +++ /dev/null @@ -1,81 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* bonus_actions_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/01/14 18:14:03 by sadoming #+# #+# */ -/* Updated: 2025/01/14 19:46:44 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -/* Look for mouse position -* - If action = 0 -> Start mouse position -* - If action = 1 -> Compare movement and apply rotation -*/ -t_game *hook_rotation(t_game *gm, int action) -{ - int32_t mouse_x; - int32_t mouse_y; - - mlx_get_mouse_pos(gm->mlxd->mlx, &mouse_x, &mouse_y); - if (action) - { - if (mouse_x < gm->msp_x) - rotate(gm, ROT_LEFT); - else if (mouse_x > gm->msp_x) - rotate(gm, ROT_RIGHT); - } - gm->msp_x = mouse_x; - gm->msp_y = mouse_y; - return (gm); -} - -/* Resize the window */ -t_game *resize_window(t_game *gm) -{ - size_t w; - size_t h; - - w = gm->mlxd->mlx->width; - h = gm->mlxd->mlx->height; - if (gm->scr_h == h && gm->scr_w == w) - return (gm); - if (h < w) - w = h; - while (w % RAYS) - { - if (w < 60) - w++; - else - w--; - } - mlx_resize_image(gm->mlxd->wimg, w, w); - gm->ray.scale = w / RAYS; - gm->scr_w = w; - gm->scr_h = w; - return (gm); -} - -/* BONUS ACTIONS */ -/* -* - Resize the window -* - Move vision with moving the mouse -* - Gost Mode! -*/ -t_game *bonus_actions(t_game *gm) -{ - if (mlx_is_key_down(gm->mlxd->mlx, MLX_KEY_SPACE)) - { - if (gm->gost) - gm->gost = 0; - else - gm->gost = 1; - } - gm = hook_rotation(gm, 1); - gm = resize_window(gm); - return (gm); -} diff --git a/bonus/check_file_bonus.c b/bonus/check_file_bonus.c deleted file mode 100644 index 67a2d0d..0000000 --- a/bonus/check_file_bonus.c +++ /dev/null @@ -1,67 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* check_file_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/04 20:12:11 by sadoming #+# #+# */ -/* Updated: 2025/01/14 17:10:28 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -/* Verifies the format of file */ -int check_format(char *file) -{ - char *format; - - if (open(file, O_RDONLY) == -1) - print_err_arfor(1); - format = ft_strstr(file, ".cub"); - if (!format) - print_err_arfor(2); - format = NULL; - return (1); -} - -/* Checks if map->file have dupplicated info or missing info */ -int check_dupmiss(t_map *map) -{ - size_t checker[6]; - size_t cnt; - - cnt = -1; - checker[0] = ft_strinarr_cnd(map->file, "NO"); - checker[1] = ft_strinarr_cnd(map->file, "SO"); - checker[2] = ft_strinarr_cnd(map->file, "WE"); - checker[3] = ft_strinarr_cnd(map->file, "EA"); - checker[4] = ft_strinarr_cnd(map->file, "F"); - checker[5] = ft_strinarr_cnd(map->file, "C"); - while (++cnt < 6) - { - if (!checker[cnt]) - print_err_dupmiss(0); - else if (checker[cnt] > 1) - print_err_dupmiss(1); - } - return (1); -} - -/* Check if the color is in correct range of [0 - 255] */ -int check_colors(t_map *map) -{ - int i; - - i = 0; - while (i < 3) - { - if (map->c_rgb[i] < 0 || map->c_rgb[i] > 255) - return (0); - if (map->f_rgb[i] < 0 || map->f_rgb[i] > 255) - return (0); - i++; - } - return (1); -} diff --git a/bonus/check_map_bonus.c b/bonus/check_map_bonus.c deleted file mode 100644 index 826ae67..0000000 --- a/bonus/check_map_bonus.c +++ /dev/null @@ -1,71 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* check_map_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/10 20:51:50 by andmart2 #+# #+# */ -/* Updated: 2025/01/14 19:49:24 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -int ft_is_valid_neighbor(char c) -{ - return (c == ' ' || c == '1'); -} - -static int ft_check_map_spaces(t_map *m, size_t i, size_t j) -{ - if (i > 0 && !ft_is_valid_neighbor(m->map[i - 1][j])) - return (0); - if (i < m->height - 1 && !ft_is_valid_neighbor(m->map[i + 1][j])) - return (0); - if (j > 0 && !ft_is_valid_neighbor(m->map[i][j - 1])) - return (0); - if (j < m->width - 1 && !ft_is_valid_neighbor(m->map[i][j + 1])) - return (0); - return (1); -} - -static int ft_check_map_chars(t_map *m, size_t i, size_t j) -{ - char c; - - c = m->map[i][j]; - if (c == ' ' || c == '0' || c == '\t') - return (1); - if (ft_strchr(WALLS, c)) - return (1); - if (c == 'N' || c == 'S' || c == 'W' || c == 'E') - return (1); - return (0); -} - -void ft_check_valid_map(t_map *m) -{ - size_t i; - size_t j; - - i = 0; - while (i < m->height) - { - j = 0; - while (m->map[i][j]) - { - if (!ft_check_map_chars(m, i, j)) - print_other_err("Invalid map, wrong characters!\n"); - if (m->map[i][j] == ' ') - { - if (!ft_check_map_spaces(m, i, j)) - print_other_err("Invalid map, not closed properly\n"); - } - j++; - } - i++; - } - if (!check_colors(m)) - print_other_err("Invalid ceiling or floor colors\n"); -} diff --git a/bonus/collisions_bonus.c b/bonus/collisions_bonus.c deleted file mode 100644 index 6e90da9..0000000 --- a/bonus/collisions_bonus.c +++ /dev/null @@ -1,52 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* collisions_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/18 19:10:25 by sadoming #+# #+# */ -/* Updated: 2025/01/15 20:29:32 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -char get_cell(t_map *map, long x, long y, int mapp) -{ - if (mapp) - { - x /= CUB_SCALE; - y /= CUB_SCALE; - } - if (y <= 0 || y >= (long)map->height) - return ('1'); - if (x <= 0 || x >= (long)map->width) - return ('1'); - if (!map->map[y][x]) - return ('1'); - return (map->map[y][x]); -} - -/* Manage collisions with a 'rackmap' tool */ -int can_move_to(t_game *gm) -{ - char next_horz; - char next_vert; - char next_diag; - int move; - - move = 1; - next_horz = get_cell(gm->map, gm->ply.new_px, gm->ply.py, 1); - next_vert = get_cell(gm->map, gm->ply.px, gm->ply.new_py, 1); - next_diag = get_cell(gm->map, gm->ply.new_px, gm->ply.new_py, 1); - if (ft_strchr(WALLS, next_diag)) - move = 0; - else if (ft_strchr(WALLS, next_horz)) - move = 0; - else if (ft_strchr(WALLS, next_vert)) - move = 0; - if (gm->gost) - move = gm->gost; - return (move); -} diff --git a/bonus/cub_main_bonus.c b/bonus/cub_main_bonus.c deleted file mode 100644 index ade7b31..0000000 --- a/bonus/cub_main_bonus.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cub_main_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/04 19:32:54 by sadoming #+# #+# */ -/* Updated: 2025/01/14 17:10:28 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -int main(int argc, char **args) -{ - t_map *map; - - map = NULL; - if (argc != 2 || !args) - print_err_arfor(0); - if (check_format(args[1])) - { - map = new_map(map, args[1]); - if (map) - { - check_dupmiss(map); - parse_fileinfo_intovars(map); - ft_check_valid_map(map); - start(map); - free_map(map); - } - } - exit(0); -} diff --git a/bonus/inc_bonus/consts_bonus.h b/bonus/inc_bonus/consts_bonus.h deleted file mode 100644 index f5177e5..0000000 --- a/bonus/inc_bonus/consts_bonus.h +++ /dev/null @@ -1,68 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* consts_bonus.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/10/21 16:49:31 by sadoming #+# #+# */ -/* Updated: 2025/01/15 20:25:33 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef CONSTS_BONUS_H -# define CONSTS_BONUS_H - -/* ## SCREEN ## */ -# define TITLE "CUB3D BONUS" -# define WIN_WIDTH 1080 -# define WIN_HEIGHT 1080 - -/* ## MINI MAP ## */ -# define MAP_WINDOW_X 50 -# define MAP_WINDOW_Y 50 - -# define MINMAP_CELL_H 26 -# define MINMAP_CELL_W 26 -# define VIEW_RADIUS 5 -/* ## MINI MAP END ## */ - -# define ICON "./assets/textures/so-long/exit.png" -/* ## SCREEN END ## */ - -/* ## MATH CONSTS ## */ -# define PI 3.14159265358 -# define P2 1.570796327 -# define P3 4.71238898 -# define DR 0.0174533 -# define RAYS 60 -# define RAYS_SUB 30 -# define VEL 5 -# define ANGLE_D 0.1 - -# define CUB_SCALE 64 -/* ## MATH CONSTS END ## */ - -/* ## ACTIONS CONSTS ## */ -# define ROT_LEFT '>' -# define ROT_RIGHT '<' -# define MOV_UP '0' -# define MOV_DOWN '1' -# define MOV_LEFT '2' -# define MOV_RIGHT '3' -# define HORZ 'H' -# define VERT 'V' -/* ## ACTIONS CONSTS END ## */ - -/* ## ERRORS CONSTS ## */ -# define BAD_PLY_NUM "The file don't have/or have multiple player/s!\n" -# define PLY_TRSNMAP "Player have traspased to an unexistent ubication\n" -/* ## ERRORS CONSTS END ## */ - -/* OTHERS */ -# define DOOR 'D' -# define WALLS "1D" -# define EXIT_SUCCESS 0 -# define EXIT_FAILURE 1 - -#endif diff --git a/bonus/inc_bonus/cub_structs_bonus.h b/bonus/inc_bonus/cub_structs_bonus.h deleted file mode 100644 index 1768996..0000000 --- a/bonus/inc_bonus/cub_structs_bonus.h +++ /dev/null @@ -1,194 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cub_structs_bonus.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/04 12:48:59 by sadoming #+# #+# */ -/* Updated: 2025/01/15 20:25:06 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef CUB_STRUCTS_BONUS_H -# define CUB_STRUCTS_BONUS_H - -# include "game_bonus.h" - -/* MLX DATA */ -/* - - mlx -> The MLX - - wing -> The image where will ocour the render of game - - icon -> The Icon game - - tx_no, _so, _we & _ea -> Set and convert textures loaded from file - - tx_ac -> The Actual texture to apply to the wall -*/ -typedef struct s_mlxd -{ - mlx_t *mlx; - mlx_image_t *wimg; - mlx_texture_t *icon; - mlx_texture_t *tx_no; - mlx_texture_t *tx_so; - mlx_texture_t *tx_we; - mlx_texture_t *tx_ea; - mlx_texture_t *tx_ac; -} t_mlxd; - -/* RECTANGLE STRUCT */ -/* - - width -> Rect width - - height -> Rect height - - fill -> Rect fill color (int32) -*/ -typedef struct s_rect -{ - size_t width; - size_t height; - int32_t fill; -} t_rect; - -/* MINIMAP */ -typedef struct s_point -{ - size_t x; - size_t y; -} t_point; - -typedef struct s_view_area -{ - t_point start; - t_point end; -} t_view_area; - -typedef struct s_draw_params -{ - t_rect rect; - int map_x; - int map_y; -} t_draw_params; - -/* PLAYER DATA */ -/* - - px -> Player X Position - - py -> Player Y Position - - pdx -> Player Delta X - - pdy -> Player Delta Y - - pa -> Player Angle - ----- - - ipx -> Player X Position in Map - - ipy -> Player Y Position in Map - ----- -*/ -typedef struct s_ply -{ - double px; - double py; - double new_px; - double new_py; - double pdx; - double pdy; - double pa; - long ipx; - long ipy; - long ipx_add_xo; - long ipx_sub_xo; - long ipy_add_yo; - long ipy_sub_yo; -} t_ply; - -/* MAP & FILE DATA */ -/* - - file -> Data file attached to program - - map -> 2D Map - - tx_no, _so, _we & _ea -> The textures loaded from file - - pla -> Player Focuss ("NSWE") - ----- - - height -> Max height of map - - width -> Max width of map - - size -> height * width of map - ----- - - f_rgb & c_rgb -> Color background (Sky & Floor) - ----- - - has_player -> Counts num of players - - ply -> Attach to player_struct -*/ -typedef struct s_map -{ - char **file; - char **map; - char *tx_no; - char *tx_so; - char *tx_we; - char *tx_ea; - char pla; - size_t height; - size_t width; - size_t size; - int f_rgb[3]; - int c_rgb[3]; - int has_player; - t_ply ply; -} t_map; - -/* RAYCAST DATA */ -typedef struct s_ray -{ - size_t dof_x; - size_t dof_y; - size_t mx; - size_t my; - size_t mp; - double hx; - double hy; - double vx; - double vy; - double rx; - double ry; - double ra; - double xo; - double yo; - double dis_h; - double dis_v; - double dist; - double line_h; - double line_o; - double line_t; - double scale; - double delta_x; - double delta_y; - double htan; - double vtan; - double theta; -} t_ray; - -/* DATA OF A CUB-RECT TEXTURED */ -typedef struct s_tex -{ - double wall_x; - double x; - double y; - int tx; - int ty; - t_rect rc; -} t_tex; - -/* THE PRINCIPAL STRUCTURE */ -typedef struct s_game -{ - t_mlxd *mlxd; - t_map *map; - t_ply ply; - t_ray ray; - t_tex tex; - int32_t c_flr; - int32_t c_sky; - size_t scr_w; - size_t scr_h; - int32_t msp_x; - int32_t msp_y; - int door; - int gost; -} t_game; - -#endif diff --git a/bonus/inc_bonus/game_bonus.h b/bonus/inc_bonus/game_bonus.h deleted file mode 100644 index e3e0afe..0000000 --- a/bonus/inc_bonus/game_bonus.h +++ /dev/null @@ -1,73 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* game_bonus.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/10/22 13:09:36 by sadoming #+# #+# */ -/* Updated: 2025/01/15 19:54:40 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef GAME_BONUS_H -# define GAME_BONUS_H - -# include "../../libft/inc/libft.h" -# include "../../MLX42/include/MLX42/MLX42.h" - -# include -# include - -# include "consts_bonus.h" -# include "cub_structs_bonus.h" - -/* PRINTING ERRORS */ -void print_err_arfor(int error); -void print_errmalloc(void); -void print_mlxerror(void); -void print_err_dupmiss(int error); -void print_other_err(char *err); - -/* CHECKERS */ -int check_format(char *file); -int check_dupmiss(t_map *map); -int check_colors(t_map *map); -void ft_check_valid_map(t_map *m); - -/* PARSER */ -t_map *parse_fileinfo_intovars(t_map *map); - -/* MEMORY */ -void free_mlxd(t_mlxd *mlxd); -void *free_map(t_map *map); -t_map *new_map(t_map *map, char *file); - -/* COLLISIONS */ -int can_move_to(t_game *gm); - -/* ACTIONS */ -t_game *hook_rotation(t_game *gm, int action); -t_game *resize_window(t_game *gm); -t_game *bonus_actions(t_game *gm); -void rotate(t_game *gm, char direction); -void translate(t_game *gm, char dir); -void hook_keyboard(void *param); - -/* GAME */ -void start(t_map *map); -t_game *check_hrzlines(t_game *gm); -t_game *check_vrtlines(t_game *gm); -void raycasting(t_game *game); -void printmap(t_game *gm); - -/* UTILITIES */ -int ft_is_valid_neighbor(char c); -char get_cell(t_map *map, long x, long y, int mapp); -double dist(double ax, double ay, double bx, double by); -int32_t ft_pixel(int32_t r, int32_t g, int32_t b, int32_t a); -uint32_t get_rgba(mlx_texture_t *texture, size_t x, size_t y); -void printrect(mlx_image_t *paint, size_t x, size_t y, t_rect rect); -void paint_bg(t_game *game); - -#endif diff --git a/bonus/man_memory_bonus.c b/bonus/man_memory_bonus.c deleted file mode 100644 index d0e272b..0000000 --- a/bonus/man_memory_bonus.c +++ /dev/null @@ -1,70 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* man_memory_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/05 13:40:39 by sadoming #+# #+# */ -/* Updated: 2025/01/15 17:04:49 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -void free_mlxd(t_mlxd *mlxd) -{ - if (!mlxd) - return ; - if (mlxd->tx_no) - mlx_delete_texture(mlxd->tx_no); - if (mlxd->tx_so) - mlx_delete_texture(mlxd->tx_so); - if (mlxd->tx_we) - mlx_delete_texture(mlxd->tx_we); - if (mlxd->tx_ea) - mlx_delete_texture(mlxd->tx_ea); - if (mlxd->icon) - mlx_delete_texture(mlxd->icon); - ft_bzero(mlxd, sizeof(t_mlxd)); -} - -void *free_map(t_map *map) -{ - map->file = ft_free_arrstr(map->file); - map->map = ft_free_arrstr(map->map); - map->tx_no = ft_free_str(map->tx_no); - map->tx_so = ft_free_str(map->tx_so); - map->tx_we = ft_free_str(map->tx_we); - map->tx_ea = ft_free_str(map->tx_ea); - ft_bzero(map, sizeof(t_map)); - free(map); - return (NULL); -} - -t_map *new_map(t_map *map, char *file) -{ - char *rfile; - char *tmp; - int fd; - - rfile = NULL; - map = ft_calloc(sizeof(t_map), 1); - if (!map) - print_errmalloc(); - fd = open(file, O_RDONLY); - tmp = get_next_line(fd); - if (!tmp) - ft_printf_fd(2, "%sError\nSomething went wrong!\n", R); - while (tmp) - { - rfile = ft_strjoin_free_fst(rfile, tmp); - tmp = ft_free_str(tmp); - tmp = get_next_line(fd); - } - map->file = ft_split(rfile, '\n'); - rfile = ft_free_str(rfile); - if (!map->file) - print_errmalloc(); - return (map); -} diff --git a/bonus/paint_minimap_bonus.c b/bonus/paint_minimap_bonus.c deleted file mode 100644 index ce33282..0000000 --- a/bonus/paint_minimap_bonus.c +++ /dev/null @@ -1,111 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* paint_minimap_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/16 17:37:56 by sadoming #+# #+# */ -/* Updated: 2025/01/15 20:37:35 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -t_view_area calculate_view_area(t_game *gm, int ply_ipx, int ply_ipy) -{ - t_view_area area; - - if (ply_ipx - VIEW_RADIUS >= 0) - area.start.x = ply_ipx - VIEW_RADIUS; - else - area.start.x = 0; - if (ply_ipy - VIEW_RADIUS >= 0) - area.start.y = ply_ipy - VIEW_RADIUS; - else - area.start.y = 0; - if (ply_ipx + VIEW_RADIUS < (int)gm->map->width) - area.end.x = ply_ipx + VIEW_RADIUS; - else - area.end.x = (int)gm->map->width - 1; - if (ply_ipy + VIEW_RADIUS < (int)gm->map->height) - area.end.y = ply_ipy + VIEW_RADIUS; - else - area.end.y = (int)gm->map->height - 1; - return (area); -} - -void paint_player(t_game *gm, t_point start) -{ - t_rect m; - int x; - int y; - - m.height = MINMAP_CELL_H; - m.width = MINMAP_CELL_W; - x = (gm->ply.ipx - start.x) * m.width + MAP_WINDOW_X; - y = (gm->ply.ipy - start.y) * m.height + MAP_WINDOW_Y; - m.fill = ft_pixel(255, 20, 20, 156); - printrect(gm->mlxd->wimg, x, y, m); - x = (gm->ply.ipx + cos(gm->ply.pa) - start.x) * m.width + MAP_WINDOW_X; - y = (gm->ply.ipy + sin(gm->ply.pa) - start.y) * m.height + MAP_WINDOW_Y; - m.fill = ft_pixel(255, 200, 20, 156); - printrect(gm->mlxd->wimg, x, y, m); -} - -t_draw_params prepare_drawing_params(size_t x, size_t y, t_view_area area, - t_game *gm) -{ - t_draw_params params; - - params.rect.height = MINMAP_CELL_H; - params.rect.width = MINMAP_CELL_W; - params.map_x = (x - area.start.x) * params.rect.width + MAP_WINDOW_X; - params.map_y = (y - area.start.y) * params.rect.height + MAP_WINDOW_Y; - if (gm->map->map[y][x] == '0') - { - params.rect.fill = ft_pixel(20, 20, 20, 50); - } - else - { - params.rect.fill = ft_pixel(200, 200, 200, 50); - } - return (params); -} - -void draw_map_area(t_game *gm, t_view_area area) -{ - size_t x; - size_t y; - t_draw_params params; - - y = area.start.y; - while (y <= area.end.y) - { - x = area.start.x; - while (x <= area.end.x) - { - if (y < gm->map->height && x < ft_strlen(gm->map->map[y])) - { - params = prepare_drawing_params(x, y, area, gm); - printrect(gm->mlxd->wimg, params.map_x, params.map_y, - params.rect); - } - x++; - } - y++; - } -} - -void printmap(t_game *gm) -{ - t_view_area area; - int ply_ipx; - int ply_ipy; - - ply_ipx = (int)(gm->ply.ipx); - ply_ipy = (int)(gm->ply.ipy); - area = calculate_view_area(gm, ply_ipx, ply_ipy); - draw_map_area(gm, area); - paint_player(gm, area.start); -} diff --git a/bonus/parse_info_bonus.c b/bonus/parse_info_bonus.c deleted file mode 100644 index e624c24..0000000 --- a/bonus/parse_info_bonus.c +++ /dev/null @@ -1,152 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parse_info_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/05 17:20:16 by sadoming #+# #+# */ -/* Updated: 2025/01/15 20:27:20 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -static t_map *fill_pathtotextures(t_map *map) -{ - size_t pos_pop; - - map->tx_no = map->file[ft_search_str(map->file, "NO")]; - map->tx_so = map->file[ft_search_str(map->file, "SO")]; - map->tx_we = map->file[ft_search_str(map->file, "WE")]; - map->tx_ea = map->file[ft_search_str(map->file, "EA")]; - map->tx_no = ft_strdup(map->tx_no); - map->tx_so = ft_strdup(map->tx_so); - map->tx_we = ft_strdup(map->tx_we); - map->tx_ea = ft_strdup(map->tx_ea); - pos_pop = ft_search_str(map->file, "NO"); - map->file = (char **)arrpop((void **)map->file, pos_pop); - pos_pop = ft_search_str(map->file, "SO"); - map->file = (char **)arrpop((void **)map->file, pos_pop); - pos_pop = ft_search_str(map->file, "WE"); - map->file = (char **)arrpop((void **)map->file, pos_pop); - pos_pop = ft_search_str(map->file, "EA"); - map->file = (char **)arrpop((void **)map->file, pos_pop); - return (map); -} - -static t_map *fill_flcl_color(t_map *map) -{ - char *cl_tmp; - char *fl_tmp; - char **tmp; - - cl_tmp = map->file[ft_search_str(map->file, "C")]; - fl_tmp = map->file[ft_search_str(map->file, "F")]; - cl_tmp = ft_strtrim(cl_tmp, "C \t"); - fl_tmp = ft_strtrim(fl_tmp, "F \t"); - tmp = ft_split(cl_tmp, ','); - if (!tmp || arrsize_str(tmp) != 3) - print_err_dupmiss(2); - map->c_rgb[0] = ft_atoi(tmp[0]); - map->c_rgb[1] = ft_atoi(tmp[1]); - map->c_rgb[2] = ft_atoi(tmp[2]); - tmp = ft_free_arrstr(tmp); - tmp = ft_split(fl_tmp, ','); - if (!tmp || arrsize_str(tmp) != 3) - print_err_dupmiss(2); - map->f_rgb[0] = ft_atoi(tmp[0]); - map->f_rgb[1] = ft_atoi(tmp[1]); - map->f_rgb[2] = ft_atoi(tmp[2]); - tmp = ft_free_arrstr(tmp); - cl_tmp = ft_free_str(cl_tmp); - fl_tmp = ft_free_str(fl_tmp); - return (map); -} - -static t_map *clean_file(t_map *map) -{ - size_t pos_pop; - char *tmp; - - pos_pop = ft_search_str(map->file, "F"); - map->file = (char **)arrpop((void **)map->file, pos_pop); - pos_pop = ft_search_str(map->file, "C"); - map->file = (char **)arrpop((void **)map->file, pos_pop); - map->map = (char **)arrdup((void **)map->file); - if (!map->map) - print_errmalloc(); - tmp = ft_strtrim(map->tx_no, "NO \t"); - map->tx_no = ft_strremplace(map->tx_no, tmp); - tmp = ft_free_str(tmp); - tmp = ft_strtrim(map->tx_so, "SO \t"); - map->tx_so = ft_strremplace(map->tx_so, tmp); - tmp = ft_free_str(tmp); - tmp = ft_strtrim(map->tx_we, "WE \t"); - map->tx_we = ft_strremplace(map->tx_we, tmp); - tmp = ft_free_str(tmp); - tmp = ft_strtrim(map->tx_ea, "EA \t"); - map->tx_ea = ft_strremplace(map->tx_ea, tmp); - tmp = ft_free_str(tmp); - return (map); -} - -static t_map *set_mapsize(t_map *map) -{ - size_t len; - - map->height = -1; - while (map->map[++map->height]) - { - len = -1; - while (map->map[map->height][++len]) - { - if (ft_strchr("NSWE", map->map[map->height][len])) - { - map->pla = map->map[map->height][len]; - map->map[map->height][len] = '0'; - map->has_player++; - map->ply.ipx = len; - map->ply.ipy = map->height; - } - } - if (len > map->width) - map->width = len; - } - map->size = map->height * map->width; - if (!map->has_player || map->has_player > 1) - print_other_err(BAD_PLY_NUM); - return (map); -} - -/* -* Parse the recollected info `map->file` -* into the corresponent vars -*/ -t_map *parse_fileinfo_intovars(t_map *map) -{ - size_t size; - size_t len; - char *tmp; - - size = -1; - map = fill_pathtotextures(map); - map = fill_flcl_color(map); - map = clean_file(map); - while (map->map[++size]) - { - len = 0; - while (map->map[size][len]) - { - if (map->map[size][len] == '\t') - { - map->map[size][len] = ' '; - tmp = ft_strinter(map->map[size], " ", len); - map->map[size] = ft_strremplace(map->map[size], tmp); - tmp = ft_free_str(tmp); - } - len++; - } - } - return (set_mapsize(map)); -} diff --git a/bonus/print_errors_bonus.c b/bonus/print_errors_bonus.c deleted file mode 100644 index 5e05b53..0000000 --- a/bonus/print_errors_bonus.c +++ /dev/null @@ -1,78 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* print_errors_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/04 19:51:53 by sadoming #+# #+# */ -/* Updated: 2025/01/14 17:10:28 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -/* Print custom error on invalid args or file extension -* - For invalid number args -> 0 -* - For invalid/error file -> 1 -* - For invalid extension -> 2 -*/ -void print_err_arfor(int error) -{ - ft_printf_fd(2, R); - if (!error) - ft_printf_fd(2, "Error\nIncorrect number of arguments\n"); - else if (error == 1) - { - ft_printf_fd(2, "Error\nThis file don't exist!\n"); - exit(EXIT_FAILURE); - } - if (error == 2) - ft_printf_fd(2, "Error\nInvalid Extension\n"); - ft_printf_fd(2, "%s Please introduce ONE file .cub", Y); - ft_printf_fd(2, " like this example:\n./cub3D map.cub %s\n", D); - exit(EXIT_FAILURE); -} - -/* Print Malloc error */ -void print_errmalloc(void) -{ - ft_printf_fd(STDERR_FILENO, RR); - ft_printf_fd(2, "Insuficient Memory or Malloc Error"); - ft_printf_fd(2, D); - exit(EXIT_FAILURE); -} - -/* Print MLX error */ -void print_mlxerror(void) -{ - ft_printf_fd(2, mlx_strerror(mlx_errno)); - exit(EXIT_FAILURE); -} - -/* Print custom error on invalid info -* - For missing info -> 0 -* - For duplicate info -> 1 -* - For invalid info -> 2 -*/ -void print_err_dupmiss(int error) -{ - ft_printf_fd(2, R); - if (!error) - ft_printf_fd(2, "Error\nSome info is missing!\n"); - else if (error == 1) - ft_printf_fd(2, "Error\nSome info is duplicated!\n"); - else if (error == 2) - ft_printf_fd(2, "Error\nInvalid info encountered!\n"); - ft_printf_fd(2, D); - exit(EXIT_FAILURE); -} - -/* Print Custom Error */ -void print_other_err(char *err) -{ - ft_printf_fd(2, R); - ft_printf_fd(2, err); - ft_printf_fd(2, D); - exit(EXIT_FAILURE); -} diff --git a/bonus/raycasting_bonus.c b/bonus/raycasting_bonus.c deleted file mode 100644 index 56b393c..0000000 --- a/bonus/raycasting_bonus.c +++ /dev/null @@ -1,140 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* raycasting_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/11 19:01:29 by sadoming #+# #+# */ -/* Updated: 2025/01/16 16:23:43 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -/* -* Calculate how many times has to repeat a pixel -* of the texture to apply and print -* ** See `get_rgba` in utilities.c -*/ -static t_game *texture_and_print(t_game *gm, int r) -{ - size_t y; - size_t x; - - y = -1; - gm->tex.rc.height = 1; - gm->tex.rc.width = 1; - if (gm->ray.dis_v < gm->ray.dis_h) - gm->tex.wall_x = fmod(gm->ray.rx, CUB_SCALE) / CUB_SCALE; - else - gm->tex.wall_x = fmod(gm->ray.ry, CUB_SCALE) / CUB_SCALE; - while (++y < gm->ray.line_t) - { - x = -1; - gm->tex.ty = (y * gm->mlxd->tx_ac->height) / gm->ray.line_t; - while (++x < gm->ray.scale) - { - gm->tex.tx = gm->tex.wall_x * gm->mlxd->tx_ac->width; - gm->tex.rc.fill = get_rgba(gm->mlxd->tx_ac, gm->tex.tx, gm->tex.ty); - gm->tex.x = (gm->scr_h + r * gm->ray.scale) + x; - gm->tex.y = ((gm->scr_h / 2) - gm->ray.line_t / 2) + y; - printrect(gm->mlxd->wimg, gm->tex.x, gm->tex.y, gm->tex.rc); - } - } - return (gm); -} - -static t_game *calculate_line_h(t_game *gm) -{ - double ca; - - ca = gm->ply.pa - gm->ray.ra; - if (ca < 0) - ca += 2 * PI; - if (ca > 2 * PI) - ca -= 2 * PI; - gm->ray.dist = gm->ray.dist * cos(ca); - if (gm->ray.dist < CUB_SCALE + 1) - gm->ray.dist = CUB_SCALE + 1; - gm->ray.line_h = (CUB_SCALE * gm->scr_w) / gm->ray.dist; - if (gm->ray.line_h > gm->scr_h) - gm->ray.line_h = gm->scr_h; - gm->ray.line_o = gm->scr_h / 2 - gm->ray.line_h / 2; - gm->ray.line_t = gm->ray.line_h + gm->ray.line_o; - if (gm->ray.line_t > gm->scr_h) - gm->ray.line_t = gm->scr_h; - return (gm); -} - -/* Set the wall orientation as `horizontal` */ -static t_game *horizontal_wall(t_game *gm) -{ - gm->ray.rx = gm->ray.hx; - gm->ray.ry = gm->ray.hy; - gm->ray.dist = gm->ray.dis_h; - gm->ray.delta_x = gm->ray.rx - gm->ply.px; - gm->ray.delta_y = gm->ray.ry - gm->ply.py; - gm->ray.theta = atan2(gm->ray.delta_y, gm->ray.delta_x); - if (gm->ray.theta > -PI / 2 && gm->ray.theta < P2) - gm->mlxd->tx_ac = gm->mlxd->tx_ea; - else - gm->mlxd->tx_ac = gm->mlxd->tx_we; - if (gm->door) - gm->mlxd->tx_ac = gm->mlxd->icon; - return (gm); -} - -/* Knowing the distance to the wall - with dis_v & dis_h -> Detect wall orientation - * Minnor distance will be the correct dist -*/ -static t_game *check_wall_orientation(t_game *gm) -{ - if (gm->ray.dis_h < gm->ray.dis_v) - return (horizontal_wall(gm)); - else - { - gm->ray.rx = gm->ray.vx; - gm->ray.ry = gm->ray.vy; - gm->ray.dist = gm->ray.dis_v; - gm->ray.delta_x = gm->ray.rx - gm->ply.px; - gm->ray.delta_y = gm->ray.ry - gm->ply.py; - gm->ray.theta = atan2(gm->ray.delta_y, gm->ray.delta_x); - if (gm->ray.theta > 0 && gm->ray.theta < PI) - gm->mlxd->tx_ac = gm->mlxd->tx_so; - else - gm->mlxd->tx_ac = gm->mlxd->tx_no; - } - if (gm->door) - gm->mlxd->tx_ac = gm->mlxd->icon; - return (gm); -} - -void raycasting(t_game *game) -{ - int r; - - r = 0; - game->ray.ra = game->ply.pa - DR * RAYS_SUB; - if (game->ray.ra < 0) - game->ray.ra += 2 * PI; - if (game->ray.ra > 2 * PI) - game->ray.ra -= 2 * PI; - paint_bg(game); - while (r < RAYS) - { - game = check_hrzlines(game); - game = check_vrtlines(game); - game = check_wall_orientation(game); - game = calculate_line_h(game); - game = texture_and_print(game, r); - game->ray.ra += DR; - if (game->ray.ra < 0) - game->ray.ra += 2 * PI; - if (game->ray.ra > 2 * PI) - game->ray.ra -= 2 * PI; - r++; - } - printmap(game); -} diff --git a/bonus/raycollision_bonus.c b/bonus/raycollision_bonus.c deleted file mode 100644 index c6eb662..0000000 --- a/bonus/raycollision_bonus.c +++ /dev/null @@ -1,148 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* raycollision_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/11 19:19:24 by sadoming #+# #+# */ -/* Updated: 2025/01/14 20:03:51 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -static int check_wall(t_game *gm) -{ - char cell; - - if (gm->ray.mp >= gm->map->size) - return (1); - if (gm->ray.my >= gm->map->height) - gm->ray.my = gm->map->height - 1; - if (gm->ray.mx >= gm->map->width) - gm->ray.mx = gm->map->width - 1; - if (gm->ray.mp < gm->map->size) - { - cell = gm->map->map[gm->ray.my][gm->ray.mx]; - if (ft_strchr(WALLS, cell)) - { - if (cell == DOOR) - gm->door = 1; - else - gm->door = 0; - return (1); - } - } - return (0); -} - -static t_game *hrz_lines_bucle(t_game *gm, double hx, double hy) -{ - while (gm->ray.dof_x < gm->map->width) - { - gm->ray.mx = (size_t)(gm->ray.rx) >> 6; - gm->ray.my = (size_t)(gm->ray.ry) >> 6; - gm->ray.mp = gm->ray.my * gm->ray.mx; - if (check_wall(gm)) - { - hx = gm->ray.rx; - hy = gm->ray.ry; - gm->ray.hx = gm->ray.rx; - gm->ray.hy = gm->ray.ry; - gm->ray.dis_h = dist(gm->ply.px, gm->ply.py, hx, hy); - gm->ray.dof_x = gm->map->width; - } - else - { - gm->ray.rx += gm->ray.xo; - gm->ray.ry += gm->ray.yo; - gm->ray.dof_x += 1; - } - } - return (gm); -} - -/* Check Horizontal Lines */ -t_game *check_hrzlines(t_game *gm) -{ - gm->ray.dof_x = 0; - gm->ray.dis_h = 1000000000; - gm->ray.htan = -tan(gm->ray.ra); - if (gm->ray.ra > P2 && gm->ray.ra < P3) - { - gm->ray.rx = (((int)gm->ply.px >> 6) << 6) - 0.0001; - gm->ray.ry = (gm->ply.px - gm->ray.rx) * gm->ray.htan + gm->ply.py; - gm->ray.xo = -CUB_SCALE; - gm->ray.yo = -gm->ray.xo * gm->ray.htan; - } - if (gm->ray.ra < P2 || gm->ray.ra > P3) - { - gm->ray.rx = (((int)gm->ply.px >> 6) << 6) + CUB_SCALE; - gm->ray.ry = (gm->ply.px - gm->ray.rx) * gm->ray.htan + gm->ply.py; - gm->ray.xo = CUB_SCALE; - gm->ray.yo = -gm->ray.xo * gm->ray.htan; - } - if (gm->ray.ra == 0 || gm->ray.ra == PI) - { - gm->ray.rx = gm->ply.px; - gm->ray.ry = gm->ply.py; - gm->ray.dof_x = gm->map->width; - } - return (hrz_lines_bucle(gm, gm->ray.hx, gm->ray.hy)); -} - -static t_game *vrt_lines_bucle(t_game *gm, double vx, double vy) -{ - while (gm->ray.dof_y < gm->map->height) - { - gm->ray.mx = (size_t)(gm->ray.rx) >> 6; - gm->ray.my = (size_t)(gm->ray.ry) >> 6; - gm->ray.mp = gm->ray.my * gm->ray.mx; - if (check_wall(gm)) - { - vx = gm->ray.rx; - vy = gm->ray.ry; - gm->ray.vx = gm->ray.rx; - gm->ray.vy = gm->ray.ry; - gm->ray.dis_v = dist(gm->ply.px, gm->ply.py, vx, vy); - gm->ray.dof_y = gm->map->height; - } - else - { - gm->ray.rx += gm->ray.xo; - gm->ray.ry += gm->ray.yo; - gm->ray.dof_y += 1; - } - } - return (gm); -} - -/* Check Vertical Lines */ -t_game *check_vrtlines(t_game *gm) -{ - gm->ray.dof_y = 0; - gm->ray.dis_v = 1000000000; - gm->ray.vtan = -1 / tan(gm->ray.ra); - if (gm->ray.ra > PI) - { - gm->ray.ry = (((int)gm->ply.py >> 6) << 6) - 0.0001; - gm->ray.rx = (gm->ply.py - gm->ray.ry) * gm->ray.vtan + gm->ply.px; - gm->ray.yo = -CUB_SCALE; - gm->ray.xo = -gm->ray.yo * gm->ray.vtan; - } - if (gm->ray.ra < PI) - { - gm->ray.ry = (((int)gm->ply.py >> 6) << 6) + CUB_SCALE; - gm->ray.rx = (gm->ply.py - gm->ray.ry) * gm->ray.vtan + gm->ply.px; - gm->ray.yo = CUB_SCALE; - gm->ray.xo = -gm->ray.yo * gm->ray.vtan; - } - if (gm->ray.ra == 0 || gm->ray.ra == PI) - { - gm->ray.rx = gm->ply.px; - gm->ray.ry = gm->ply.py; - gm->ray.dof_y = gm->map->height; - } - return (vrt_lines_bucle(gm, gm->ray.vx, gm->ray.vy)); -} diff --git a/bonus/start_bonus.c b/bonus/start_bonus.c deleted file mode 100644 index cf5d2e0..0000000 --- a/bonus/start_bonus.c +++ /dev/null @@ -1,97 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* start_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/04 17:47:42 by sadoming #+# #+# */ -/* Updated: 2025/01/15 18:08:58 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -/* -* Start MLX and all the textures -*/ -static t_mlxd init_mlxdata(t_map *map, t_mlxd mlxd) -{ - mlxd.mlx = mlx_init(WIN_WIDTH, WIN_HEIGHT, TITLE, true); - if (!mlxd.mlx) - print_mlxerror(); - mlxd.wimg = mlx_new_image(mlxd.mlx, WIN_WIDTH, WIN_HEIGHT); - if (!mlxd.wimg) - print_mlxerror(); - mlxd.tx_no = mlx_load_png(map->tx_no); - if (!mlxd.tx_no) - print_mlxerror(); - mlxd.tx_so = mlx_load_png(map->tx_so); - if (!mlxd.tx_so) - print_mlxerror(); - mlxd.tx_we = mlx_load_png(map->tx_we); - if (!mlxd.tx_we) - print_mlxerror(); - mlxd.tx_ea = mlx_load_png(map->tx_ea); - if (!mlxd.tx_ea) - print_mlxerror(); - mlxd.icon = mlx_load_png(ICON); - if (!mlxd.icon) - print_mlxerror(); - mlx_set_icon(mlxd.mlx, mlxd.icon); - return (mlxd); -} - -/* -* Start player vars and set bgcolor -*/ -static t_game start_player(t_game game) -{ - int32_t color[3]; - - if (game.map->pla == 'N') - game.ply.pa = P3; - else if (game.map->pla == 'S') - game.ply.pa = P2; - else if (game.map->pla == 'W') - game.ply.pa = PI; - else if (game.map->pla == 'E') - game.ply.pa = 0; - game.ply.px = game.ply.ipx * CUB_SCALE; - game.ply.py = game.ply.ipy * CUB_SCALE; - game.ply.pdx = cos(game.ply.pa) * 5; - game.ply.pdy = sin(game.ply.pa) * 5; - color[0] = (int32_t)game.map->c_rgb[0]; - color[1] = (int32_t)game.map->c_rgb[1]; - color[2] = (int32_t)game.map->c_rgb[2]; - game.c_sky = ft_pixel(color[0], color[1], color[2], 255); - color[0] = (int32_t)game.map->f_rgb[0]; - color[1] = (int32_t)game.map->f_rgb[1]; - color[2] = (int32_t)game.map->f_rgb[2]; - game.c_flr = ft_pixel(color[0], color[1], color[2], 255); - game.ray.scale = WIN_WIDTH / RAYS; - return (game); -} - -void start(t_map *map) -{ - t_mlxd mlxd; - t_game game; - - ft_bzero(&game, sizeof(t_game)); - mlxd = init_mlxdata(map, mlxd); - if (mlx_image_to_window(mlxd.mlx, mlxd.wimg, 0, 0) < 0) - print_mlxerror(); - game.mlxd = &mlxd; - game.map = map; - game.ply = map->ply; - game.scr_h = WIN_HEIGHT; - game.scr_w = WIN_WIDTH; - game = start_player(game); - game = *hook_rotation(&game, 0); - mlx_loop_hook(mlxd.mlx, hook_keyboard, &game); - mlx_loop(mlxd.mlx); - mlx_terminate(mlxd.mlx); - free_mlxd(&mlxd); - ft_bzero(&game, sizeof(t_game)); -} diff --git a/bonus/utilities_bonus.c b/bonus/utilities_bonus.c deleted file mode 100644 index 3265c8a..0000000 --- a/bonus/utilities_bonus.c +++ /dev/null @@ -1,88 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* utilities_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/10 18:38:17 by sadoming #+# #+# */ -/* Updated: 2025/01/14 17:10:28 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc_bonus/game_bonus.h" - -/* Calculate distance - * between player and rays endpoint - * ** Use Pythagorean theorem -- -*/ -double dist(double ax, double ay, double bx, double by) -{ - double dx; - double dy; - - dx = bx - ax; - dy = by - ay; - return (sqrt(dx * dx + dy * dy)); -} - -/* Returns a uint32_t representing the color inserted in this function - * R [0-255] - G [0-255] - B [0-255] - && ALPHA [0-255] -*/ -int32_t ft_pixel(int32_t r, int32_t g, int32_t b, int32_t a) -{ - return (r << 24 | g << 16 | b << 8 | a); -} - -/* Get the pixel color in texture */ -uint32_t get_rgba(mlx_texture_t *texture, size_t x, size_t y) -{ - int r; - int g; - int b; - int a; - - r = texture->pixels[y * texture->width * 4 + (x * 4)]; - g = texture->pixels[y * texture->width * 4 + (x * 4) + 1]; - b = texture->pixels[y * texture->width * 4 + (x * 4) + 2]; - a = texture->pixels[y * texture->width * 4 + (x * 4) + 3]; - return (r << 24 | g << 16 | b << 8 | a); -} - -/* Prints a Rectangle - * - `paint` => Canvas MLX - * - `x` && `y` => Start Position - * - `rect` => *- width, height & color in a struct -*/ -void printrect(mlx_image_t *paint, size_t x, size_t y, t_rect rect) -{ - size_t sy; - size_t sx; - - sy = 0; - while (sy < rect.height) - { - sx = 0; - while (sx < rect.width) - { - mlx_put_pixel(paint, x + sx, y + sy, rect.fill); - sx++; - } - sy++; - } -} - -void paint_bg(t_game *game) -{ - t_rect sky; - t_rect floor; - - sky.height = game->scr_h; - sky.width = game->scr_w; - sky.fill = game->c_sky; - floor.height = sky.height / 2; - floor.width = sky.width; - floor.fill = game->c_flr; - printrect(game->mlxd->wimg, 0, 0, sky); - printrect(game->mlxd->wimg, 0, floor.height, floor); -} diff --git a/libft/src/arrays/ft_arrpop.c b/libft/src/arrays/ft_arrpop.c index 72059c3..594146d 100644 --- a/libft/src/arrays/ft_arrpop.c +++ b/libft/src/arrays/ft_arrpop.c @@ -6,19 +6,22 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/14 12:42:13 by sadoming #+# #+# */ -/* Updated: 2024/12/18 20:01:32 by sadoming ### ########.fr */ +/* Updated: 2025/02/28 13:20:11 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../inc/arrays.h" #include "../../inc/memory.h" -/* - * Pop `add` into a new array (on last position) - * And free the original array. - * *Free the original if !size || size >= arrsize(org) - * *Return Original if pop >= arrsize(org) - */ +/* Pop an element from an array + * - Return a new array without the element + * ** Free the original if !size || size >= arrsize(org) + * *** `size` is the size of the new array + * *** (If size is 0, the array will be freed) + * ** Return Original if pop >= arrsize(org) + * *** `pop` is the index of the element to pop + * ** Return NULL if !new_arr (Memory Allocation Error) +*/ void **arrpop(void **org, size_t pop) { char **new_arr; diff --git a/libft/src/ft_str/ft_strstr.c b/libft/src/ft_str/ft_strstr.c index 186e219..8965c03 100644 --- a/libft/src/ft_str/ft_strstr.c +++ b/libft/src/ft_str/ft_strstr.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/30 17:57:36 by sadoming #+# #+# */ -/* Updated: 2024/10/01 17:05:12 by sadoming ### ########.fr */ +/* Updated: 2025/02/28 13:23:20 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,6 +78,8 @@ char *ft_strnstr(const char *haystack, const char *needle, size_t len) * if !to_search || !arr -> return 0 * if coincidence, return the arr pos on encountered * if no coincidence, return arr len + * ** arr must be NULL terminated + * ** It will return the first coincidence */ size_t ft_search_str(char **arr, char *to_search) { diff --git a/src/action.c b/src/action.c index 9af083e..c03e8c4 100644 --- a/src/action.c +++ b/src/action.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/16 19:05:22 by andmart2 #+# #+# */ -/* Updated: 2025/01/14 16:18:54 by sadoming ### ########.fr */ +/* Created: 2024/12/16 19:05:22 by sadoming #+# #+# */ +/* Updated: 2025/03/05 19:12:57 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,15 +46,13 @@ void translate(t_game *gm, char dir) move_player(gm, gm->ply.pa, 1); else if (dir == MOV_DOWN) move_player(gm, gm->ply.pa, -1); - if (can_move_to(gm)) - { - gm->ply.px = gm->ply.new_px; - gm->ply.py = gm->ply.new_py; - } + gm->ply.px = gm->ply.new_px; + gm->ply.py = gm->ply.new_py; gm->ply.ipx = gm->ply.px / CUB_SCALE; gm->ply.ipy = gm->ply.py / CUB_SCALE; - if (gm->ply.ipx < 0 || gm->ply.ipy < 0) - print_other_err(PLY_TRSNMAP); + gm->map->ply = gm->ply; + if (gm->ply.ipx > MAX || gm->ply.ipy > MAX) + print_custom_err(PLY_TRSNMAP); } void hook_keyboard(void *param) diff --git a/src/check_file.c b/src/check_file.c index c12d8f2..7c0bd5f 100644 --- a/src/check_file.c +++ b/src/check_file.c @@ -6,23 +6,23 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/04 20:12:11 by sadoming #+# #+# */ -/* Updated: 2025/01/14 16:18:54 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 18:47:28 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ #include "./inc/game.h" -/* Verifies the format of file */ +/* Verifies the format of file and check if exist */ int check_format(char *file) { char *format; - if (open(file, O_RDONLY) == -1) - print_err_arfor(1); format = ft_strstr(file, ".cub"); if (!format) - print_err_arfor(2); + print_err_arfor(1); format = NULL; + if (open(file, O_RDONLY) == -1) + print_err_arfor(2); return (1); } @@ -42,9 +42,15 @@ int check_dupmiss(t_map *map) while (++cnt < 6) { if (!checker[cnt]) + { + map = free_map(map); print_err_dupmiss(0); + } else if (checker[cnt] > 1) + { + map = free_map(map); print_err_dupmiss(1); + } } return (1); } @@ -58,10 +64,61 @@ int check_colors(t_map *map) while (i < 3) { if (map->c_rgb[i] < 0 || map->c_rgb[i] > 255) - return (0); + { + map = free_map(map); + print_custom_err(BAD_CCOLOR); + } if (map->f_rgb[i] < 0 || map->f_rgb[i] > 255) - return (0); + { + map = free_map(map); + print_custom_err(BAD_FCOLOR); + } i++; } return (1); } + +/* Check if the map contains only a valid symbols */ +int check_valid_symbol(t_map *map) +{ + size_t i; + size_t j; + + i = 0; + while (i < map->height) + { + j = 0; + while (map->map[i][j]) + { + if (!ft_strchr(MAP_CHARS, map->map[i][j])) + { + map = free_map(map); + print_custom_err(INVALID_MAP); + } + j++; + } + i++; + } + return (1); +} + +/* Check if player is not in the corners of map */ +int check_player_inmap(t_map *m) +{ + if (m->ply.ipy == 0 || m->ply.ipx == 0) + { + m = free_map(m); + print_custom_err(INVALID_MAP); + } + if (m->ply.ipy >= m->height - 1 || m->ply.ipx >= m->width - 1) + { + m = free_map(m); + print_custom_err(INVALID_MAP); + } + if (m->ply.ipx >= ft_strlen(m->map[m->ply.ipy]) - 1) + { + m = free_map(m); + print_custom_err(INVALID_MAP); + } + return (1); +} diff --git a/src/check_map.c b/src/check_map.c index c2efbf3..616ab4a 100644 --- a/src/check_map.c +++ b/src/check_map.c @@ -5,65 +5,131 @@ /* +:+ +:+ +:+ */ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/10 20:51:50 by andmart2 #+# #+# */ -/* Updated: 2025/01/14 19:20:56 by sadoming ### ########.fr */ +/* Created: 2025/02/28 13:38:36 by sadoming #+# #+# */ +/* Updated: 2025/03/05 19:07:21 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ #include "./inc/game.h" -int ft_is_valid_neighbor(char c) +/* Flood fill Method to see if map is closed */ +/** Fill with '+' wherever is possible to go +* - fill -> The map +* - size -> The size of the map +* - y -> The y position to start filling +* - x -> The x position to start filling +*/ +static void ft_path(char **fill, t_lcn size, size_t y, size_t x) { - return (c == ' ' || c == '1'); -} - -static int ft_check_map_spaces(t_map *m, size_t i, size_t j) -{ - if (i > 0 && !ft_is_valid_neighbor(m->map[i - 1][j])) - return (0); - if (i < m->height - 1 && !ft_is_valid_neighbor(m->map[i + 1][j])) - return (0); - if (j > 0 && !ft_is_valid_neighbor(m->map[i][j - 1])) - return (0); - if (j < m->width - 1 && !ft_is_valid_neighbor(m->map[i][j + 1])) - return (0); - return (1); + if (y >= size.y) + return ; + if (y >= size.y && x >= size.x) + { + if (fill[y][x]) + fill[y][x] = '+'; + return ; + } + if (x >= size.x || x >= ft_strlen(fill[y])) + return ; + if (fill[y][x] != FLOOR && fill[y][x] != ' ') + return ; + if (fill[y]) + if (fill[y][x]) + fill[y][x] = '+'; + ft_path(fill, size, y, x + 1); + ft_path(fill, size, y, x - 1); + ft_path(fill, size, y - 1, x); + ft_path(fill, size, y + 1, x); } -static int ft_check_map_chars(t_map *m, size_t i, size_t j) +/* Search the first floor cell in map +* * if not found return floor_cell (MAX, MAX) +*/ +static t_lcn search_first_floor_cell(t_map *m, t_lcn mapsize) { - char c; + t_lcn flor_cell; - c = m->map[i][j]; - if (c == ' ' || c == '0' || c == '1' || c == '\0') - return (1); - if (c == 'N' || c == 'S' || c == 'W' || c == 'E') - return (1); - return (0); + flor_cell.y = 0; + while (flor_cell.y < mapsize.y) + { + flor_cell.x = 0; + while (flor_cell.x < ft_strlen(m->map[flor_cell.y])) + { + if (m->map[flor_cell.y][flor_cell.x] == FLOOR) + { + if (flor_cell.y == 0 || flor_cell.x == 0) + { + m = free_map(m); + print_custom_err(MAP_NOTCLOSED); + } + return (flor_cell); + } + flor_cell.x++; + } + flor_cell.y++; + } + flor_cell.y = mapsize.y; + flor_cell.x = mapsize.x; + return (flor_cell); } -void ft_check_valid_map(t_map *m) +/* Check if the map is closed +* - if not closed print error +* - if closed replace '+' with '0' +*/ +static t_map *check_closed_map(t_map *map) { size_t i; size_t j; i = 0; - while (i < m->height) + while (map && map->map[i]) { j = 0; - while (m->map[i][j]) + while (map && map->map[i][j]) { - if (m->map[i][j] == ' ') + if (map && map->map[i][j] == '+') { - if (!ft_check_map_spaces(m, i, j)) - print_other_err("Invalid map, not closed properly\n"); + if (i == 0 || j == 0) + map = free_map(map); + else if (i >= arrsize_str(map->map) - 1) + map = free_map(map); + else if (j >= ft_strlen(map->map[i])) + map = free_map(map); + else + map->map[i][j] = FLOOR; } - if (!ft_check_map_chars(m, i, j)) - print_other_err("Invalid map, wrong characters!\n"); j++; } i++; } - if (!check_colors(m)) - print_other_err("Invalid ceiling or floor colors\n"); + return (map); +} + +/* Check this things: +* - If the map have valid colors (0-255) +* - If map have valid symbols (01NSWE) +* - If the player is in the map +* - If the map is closed +*/ +void check_valid_map(t_map *m) +{ + t_lcn map; + t_lcn floor; + + map.x = m->width - 1; + map.y = m->height - 1; + check_colors(m); + check_valid_symbol(m); + check_player_inmap(m); + while (42) + { + floor = search_first_floor_cell(m, map); + if (floor.y == map.y || floor.x == map.x) + break ; + ft_path(m->map, map, floor.y, floor.x); + } + m = check_closed_map(m); + if (!m) + print_custom_err(MAP_NOTCLOSED); } diff --git a/src/collisions.c b/src/collisions.c deleted file mode 100644 index f98a443..0000000 --- a/src/collisions.c +++ /dev/null @@ -1,46 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* collisions.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadoming +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/18 19:10:25 by sadoming #+# #+# */ -/* Updated: 2025/01/15 19:53:21 by sadoming ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "./inc/game.h" - -char get_cell(t_map *map, long x, long y, int mapp) -{ - if (mapp) - { - x /= CUB_SCALE; - y /= CUB_SCALE; - } - if (y <= 0 || y >= (long)map->height) - return ('1'); - if (x <= 0 || x >= (long)map->width) - return ('1'); - if (!map->map[y][x]) - return ('1'); - return (map->map[y][x]); -} - -/* Manage collisions with a 'rackmap' tool */ -int can_move_to(t_game *gm) -{ - char next_horz; - char next_vert; - char next_diag; - - next_horz = get_cell(gm->map, gm->ply.new_px, gm->ply.py, 1); - next_vert = get_cell(gm->map, gm->ply.px, gm->ply.new_py, 1); - next_diag = get_cell(gm->map, gm->ply.new_px, gm->ply.new_py, 1); - if (next_diag == '1') - return (0); - if (next_horz == '1' && next_vert == '1') - return (0); - return (1); -} diff --git a/src/cub_main.c b/src/cub_main.c index 2c43643..d43e370 100644 --- a/src/cub_main.c +++ b/src/cub_main.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/04 19:32:54 by sadoming #+# #+# */ -/* Updated: 2025/01/14 16:18:54 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 17:08:23 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int main(int argc, char **args) { check_dupmiss(map); parse_fileinfo_intovars(map); - ft_check_valid_map(map); + check_valid_map(map); start(map); free_map(map); } diff --git a/src/ft_print_map_t.c b/src/ft_print_map_t.c index 4c82f7b..ea4b4c1 100644 --- a/src/ft_print_map_t.c +++ b/src/ft_print_map_t.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/29 18:59:29 by sadoming #+# #+# */ -/* Updated: 2025/01/14 17:10:44 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 17:58:59 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/inc/consts.h b/src/inc/consts.h index a392cc1..65b1d78 100644 --- a/src/inc/consts.h +++ b/src/inc/consts.h @@ -6,13 +6,15 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/21 16:49:31 by sadoming #+# #+# */ -/* Updated: 2025/01/15 16:10:38 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 16:23:53 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CONSTS_H # define CONSTS_H +# include "error_messages.h" + /* ## SCREEN ## */ # define TITLE "CUB3D" # define WIN_WIDTH 1080 @@ -25,9 +27,15 @@ # define PI 3.14159265358 # define P2 1.570796327 # define P3 4.71238898 + # define DR 0.0174533 +# define FOV 1.04719755 + # define RAYS 60 -# define RAYS_SUB 30 + +# define FVIEW 60 +# define HEIGHT 1.6 + # define VEL 5 # define ANGLE_D 0.1 @@ -37,20 +45,27 @@ /* ## ACTIONS CONSTS ## */ # define ROT_LEFT '>' # define ROT_RIGHT '<' -# define MOV_UP '0' -# define MOV_DOWN '1' -# define MOV_LEFT '2' -# define MOV_RIGHT '3' +# define MOV_UP 'W' +# define MOV_DOWN 'S' +# define MOV_LEFT 'A' +# define MOV_RIGHT 'D' # define HORZ 'H' # define VERT 'V' /* ## ACTIONS CONSTS END ## */ -/* ## ERRORS CONSTS ## */ -# define BAD_PLY_NUM "The file don't have/or have multiple player/s!\n" -# define PLY_TRSNMAP "Player have traspased to an unexistent ubication\n" -/* ## ERRORS CONSTS END ## */ +/* OTHERS */ +# define MAP_CHARS "01NSWE \t" +# define PLY_CHARS "NSWE" +# define FLOOR '0' +# define WALL '1' + +# define MAX 4294967294 + +# define FALSE 0 +# define TRUE 1 # define EXIT_SUCCESS 0 # define EXIT_FAILURE 1 +/* ## END ## */ #endif diff --git a/src/inc/cub_structs.h b/src/inc/cub_structs.h index e5cbcd5..9304431 100644 --- a/src/inc/cub_structs.h +++ b/src/inc/cub_structs.h @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/04 12:48:59 by sadoming #+# #+# */ -/* Updated: 2025/01/13 20:00:14 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 19:05:25 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,17 @@ typedef struct s_mlxd mlx_texture_t *tx_ac; } t_mlxd; +/* LOCATION STRUCT */ +/* + - x -> X Position + - y -> Y Position +*/ +typedef struct s_lcn +{ + size_t x; + size_t y; +} t_lcn; + /* RECTANGLE STRUCT */ /* - width -> Rect width @@ -69,8 +80,8 @@ typedef struct s_ply double pdx; double pdy; double pa; - long ipx; - long ipy; + size_t ipx; + size_t ipy; long ipx_add_xo; long ipx_sub_xo; long ipy_add_yo; diff --git a/src/inc/error_messages.h b/src/inc/error_messages.h new file mode 100644 index 0000000..6737009 --- /dev/null +++ b/src/inc/error_messages.h @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error_messages.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadoming +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/31 17:13:32 by sadoming #+# #+# */ +/* Updated: 2025/03/05 16:37:51 by sadoming ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ERROR_MESSAGES_H +# define ERROR_MESSAGES_H + +/* ## ERRORS CONSTS ## */ + +# define MALLOC_ERROR "Insuficient Memory or Malloc Error\n" + +# define BAD_ARGS "Error\nIncorrect number of arguments\n" +# define BAD_FILE "Error\nThis file don't exist!\n" +# define BAD_EXT "Error\nInvalid Extension\n" + +# define BAD_MISS "Error\nSome info is missing!\n" +# define BAD_DUP "Error\nSome info is duplicated!\n" +# define BAD_INFO "Error\nInvalid info encountered!\n" +# define BAD_CCOLOR "Error\nInvalid Ceiling Color\n" +# define BAD_FCOLOR "Error\nInvalid Floor Color\n" +# define BAD_MAP "Error\nInvalid Map\n" + +# define MAP_NOTCLOSED "Invalid map, not closed properly\n" +# define INVALID_CHARS "Invalid map, encountered invalid characters\n" +# define INVALID_MAP "Invalid map, player is out of bounds\n" + +# define BAD_PLY_NUM "The file don't have/or have multiple player/s!\n" +# define PLY_TRSNMAP "Player have traspased to an unexistent ubication\n" +/* ## ERRORS CONSTS END ## */ + +#endif diff --git a/src/inc/game.h b/src/inc/game.h index 1b5c475..ea05687 100644 --- a/src/inc/game.h +++ b/src/inc/game.h @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 13:09:36 by sadoming #+# #+# */ -/* Updated: 2025/01/15 20:29:58 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 16:20:03 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,15 +25,17 @@ /* PRINTING ERRORS */ void print_err_arfor(int error); void print_errmalloc(void); -void print_mlxerror(void); void print_err_dupmiss(int error); -void print_other_err(char *err); +void print_mlxerror(void); +void print_custom_err(char *err); /* CHECKERS */ int check_format(char *file); int check_dupmiss(t_map *map); int check_colors(t_map *map); -void ft_check_valid_map(t_map *m); +int check_valid_symbol(t_map *map); +int check_player_inmap(t_map *m); +void check_valid_map(t_map *m); /* PARSER */ t_map *parse_fileinfo_intovars(t_map *map); @@ -61,9 +63,9 @@ void start(t_map *map); t_game *check_hrzlines(t_game *gm); t_game *check_vrtlines(t_game *gm); void raycasting(t_game *game); +//t_game *texture_and_print(t_game *gm, int r); /* UTILITIES */ -int ft_is_valid_neighbor(char c); char get_cell(t_map *map, long x, long y, int mapp); double dist(double ax, double ay, double bx, double by); int32_t ft_pixel(int32_t r, int32_t g, int32_t b, int32_t a); diff --git a/src/man_memory.c b/src/man_memory.c index e6a009c..55a2dc0 100644 --- a/src/man_memory.c +++ b/src/man_memory.c @@ -6,12 +6,13 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/05 13:40:39 by sadoming #+# #+# */ -/* Updated: 2025/01/14 17:10:53 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 19:03:04 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ #include "./inc/game.h" +/* Free MLX Memory Reservated */ void free_mlxd(t_mlxd *mlxd) { if (!mlxd) @@ -29,6 +30,7 @@ void free_mlxd(t_mlxd *mlxd) ft_bzero(mlxd, sizeof(t_mlxd)); } +/* Free struct Map Memory */ void *free_map(t_map *map) { map->file = ft_free_arrstr(map->file); @@ -42,6 +44,9 @@ void *free_map(t_map *map) return (NULL); } +/* Create a new file-map with the file attached to program + * * If something went wrong, print an error message + */ t_map *new_map(t_map *map, char *file) { char *rfile; diff --git a/src/parse_info.c b/src/parse_info.c index 2472f75..93f2484 100644 --- a/src/parse_info.c +++ b/src/parse_info.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/05 17:20:16 by sadoming #+# #+# */ -/* Updated: 2025/01/15 20:27:16 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 16:27:09 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -115,7 +115,7 @@ static t_map *set_mapsize(t_map *map) } map->size = map->height * map->width; if (!map->has_player || map->has_player > 1) - print_other_err(BAD_PLY_NUM); + print_custom_err(BAD_PLY_NUM); return (map); } diff --git a/src/print_errors.c b/src/print_errors.c index 71a985e..5fda51e 100644 --- a/src/print_errors.c +++ b/src/print_errors.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/04 19:51:53 by sadoming #+# #+# */ -/* Updated: 2025/01/14 16:18:54 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 16:27:01 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,21 +14,21 @@ /* Print custom error on invalid args or file extension * - For invalid number args -> 0 -* - For invalid/error file -> 1 -* - For invalid extension -> 2 +* - For invalid extension -> 1 +* - For invalid/error file -> 2 */ void print_err_arfor(int error) { ft_printf_fd(2, R); if (!error) - ft_printf_fd(2, "Error\nIncorrect number of arguments\n"); + ft_printf_fd(2, BAD_ARGS); else if (error == 1) + ft_printf_fd(2, BAD_EXT); + else if (error == 2) { - ft_printf_fd(2, "Error\nThis file don't exist!\n"); + ft_printf_fd(2, BAD_FILE); exit(EXIT_FAILURE); } - if (error == 2) - ft_printf_fd(2, "Error\nInvalid Extension\n"); ft_printf_fd(2, "%s Please introduce ONE file .cub", Y); ft_printf_fd(2, " like this example:\n./cub3D map.cub %s\n", D); exit(EXIT_FAILURE); @@ -38,18 +38,11 @@ void print_err_arfor(int error) void print_errmalloc(void) { ft_printf_fd(STDERR_FILENO, RR); - ft_printf_fd(2, "Insuficient Memory or Malloc Error"); + ft_printf_fd(2, MALLOC_ERROR); ft_printf_fd(2, D); exit(EXIT_FAILURE); } -/* Print MLX error */ -void print_mlxerror(void) -{ - ft_printf_fd(2, mlx_strerror(mlx_errno)); - exit(EXIT_FAILURE); -} - /* Print custom error on invalid info * - For missing info -> 0 * - For duplicate info -> 1 @@ -59,17 +52,24 @@ void print_err_dupmiss(int error) { ft_printf_fd(2, R); if (!error) - ft_printf_fd(2, "Error\nSome info is missing!\n"); + ft_printf_fd(2, BAD_MISS); else if (error == 1) - ft_printf_fd(2, "Error\nSome info is duplicated!\n"); + ft_printf_fd(2, BAD_DUP); else if (error == 2) - ft_printf_fd(2, "Error\nInvalid info encountered!\n"); + ft_printf_fd(2, BAD_INFO); ft_printf_fd(2, D); exit(EXIT_FAILURE); } +/* Print MLX error */ +void print_mlxerror(void) +{ + ft_printf_fd(2, mlx_strerror(mlx_errno)); + exit(EXIT_FAILURE); +} + /* Print Custom Error */ -void print_other_err(char *err) +void print_custom_err(char *err) { ft_printf_fd(2, R); ft_printf_fd(2, err); diff --git a/src/raycasting.c b/src/raycasting.c index 08a4671..ef56cef 100644 --- a/src/raycasting.c +++ b/src/raycasting.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/11 19:01:29 by sadoming #+# #+# */ -/* Updated: 2025/01/14 16:18:54 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 20:15:50 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -112,7 +112,7 @@ void raycasting(t_game *game) int r; r = 0; - game->ray.ra = game->ply.pa - DR * RAYS_SUB; + game->ray.ra = game->ply.pa - DR * (RAYS / 2); if (game->ray.ra < 0) game->ray.ra += 2 * PI; if (game->ray.ra > 2 * PI) diff --git a/src/raycollision.c b/src/raycollision.c index 74ec764..9aa15e4 100644 --- a/src/raycollision.c +++ b/src/raycollision.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/11 19:19:24 by sadoming #+# #+# */ -/* Updated: 2025/01/14 16:18:54 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 19:46:44 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ static int check_wall(t_game *gm) if (gm->ray.mx >= gm->map->width) gm->ray.mx = gm->map->width - 1; if (gm->ray.mp < gm->map->size) - if (gm->map->map[gm->ray.my][gm->ray.mx] == '1') + if (gm->map->map[gm->ray.my][gm->ray.mx] == WALL) return (1); return (0); } diff --git a/src/start.c b/src/start.c index 3e6f23a..909810c 100644 --- a/src/start.c +++ b/src/start.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/04 17:47:42 by sadoming #+# #+# */ -/* Updated: 2025/01/15 16:59:12 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 20:28:20 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,7 +86,7 @@ t_game *resize_window(t_game *gm) w = h; while (w % RAYS) { - if (w < 60) + if (w < FVIEW) w++; else w--; diff --git a/src/textures.c b/src/textures.c new file mode 100644 index 0000000..aa1b491 --- /dev/null +++ b/src/textures.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* textures.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadoming +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/05 20:29:32 by sadoming #+# #+# */ +/* Updated: 2025/03/05 20:36:12 by sadoming ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "./inc/game.h" + +/* Get the pixel color in texture */ +uint32_t get_rgba(mlx_texture_t *texture, size_t x, size_t y) +{ + int r; + int g; + int b; + int a; + + r = texture->pixels[y * texture->width * 4 + (x * 4)]; + g = texture->pixels[y * texture->width * 4 + (x * 4) + 1]; + b = texture->pixels[y * texture->width * 4 + (x * 4) + 2]; + a = texture->pixels[y * texture->width * 4 + (x * 4) + 3]; + return (r << 24 | g << 16 | b << 8 | a); +} + +static void draw_pixel_column(t_game *gm, int r, int y) +{ + size_t x; + double step_tex; + double tex_pos; + + x = 0; + step_tex = (double)gm->mlxd->tx_ac->height / gm->ray.line_t; + tex_pos = y * step_tex; + gm->tex.ty = (int)tex_pos % gm->mlxd->tx_ac->height; + while (x < gm->ray.scale) + { + gm->tex.tx = (int)(gm->tex.wall_x * gm->mlxd->tx_ac->width) % gm->mlxd->tx_ac->width; + uint32_t color = get_rgba(gm->mlxd->tx_ac, gm->tex.tx, gm->tex.ty); + mlx_put_pixel(gm->mlxd->wimg, r * gm->ray.scale + x, y + gm->ray.line_o, color); + x++; + } +} + +t_game *texture_and_print(t_game *gm, int r) +{ + int y; + + y = 0; + while (y < gm->ray.line_t) + { + draw_pixel_column(gm, r, y); + y++; + } + return (gm); +} diff --git a/src/utilities.c b/src/utilities.c index 703c213..67160a4 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/10 18:38:17 by sadoming #+# #+# */ -/* Updated: 2025/01/14 16:18:54 by sadoming ### ########.fr */ +/* Updated: 2025/03/05 20:30:22 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,21 +34,6 @@ int32_t ft_pixel(int32_t r, int32_t g, int32_t b, int32_t a) return (r << 24 | g << 16 | b << 8 | a); } -/* Get the pixel color in texture */ -uint32_t get_rgba(mlx_texture_t *texture, size_t x, size_t y) -{ - int r; - int g; - int b; - int a; - - r = texture->pixels[y * texture->width * 4 + (x * 4)]; - g = texture->pixels[y * texture->width * 4 + (x * 4) + 1]; - b = texture->pixels[y * texture->width * 4 + (x * 4) + 2]; - a = texture->pixels[y * texture->width * 4 + (x * 4) + 3]; - return (r << 24 | g << 16 | b << 8 | a); -} - /* Prints a Rectangle * - `paint` => Canvas MLX * - `x` && `y` => Start Position From dbc0ae437324618bff64299b690e151bca24550e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sasha=20Dom=C3=ADnguez=20Ferrer?= Date: Thu, 6 Mar 2025 19:53:58 +0100 Subject: [PATCH 3/3] Ni siquiera con AI.. --- MLX42 | 1 + assets/maps/error-maps/notclosed.cub | 22 +++++ assets/textures/so-long/floor-diamond.png | Bin 1205 -> 0 bytes src/inc/consts.h | 12 ++- src/inc/cub_structs.h | 96 +++++++++++++--------- src/inc/game.h | 4 +- src/raycasting.c | 2 +- src/start.c | 12 ++- src/textures.c | 73 ++++++++-------- src/utilities.c | 17 +++- 10 files changed, 151 insertions(+), 88 deletions(-) create mode 160000 MLX42 create mode 100644 assets/maps/error-maps/notclosed.cub delete mode 100644 assets/textures/so-long/floor-diamond.png diff --git a/MLX42 b/MLX42 new file mode 160000 index 0000000..ce254c3 --- /dev/null +++ b/MLX42 @@ -0,0 +1 @@ +Subproject commit ce254c3a19af8176787601a2ac3490100a5c4c61 diff --git a/assets/maps/error-maps/notclosed.cub b/assets/maps/error-maps/notclosed.cub new file mode 100644 index 0000000..3aaf957 --- /dev/null +++ b/assets/maps/error-maps/notclosed.cub @@ -0,0 +1,22 @@ +NO ./assets/textures/so-long/gold-brick.png +SO ./assets/textures/so-long/diamond-rug.png +WE ./assets/textures/error-tile-colors/error-tile.png +EA ./assets/textures/error-tile-colors/error-tile_GREEN.png + +F 220,100,0 +C 225,30,0 + + 1111111111111111111111111 + 1000000000110000000000001 + 0011000001110000000000001 + 1001000000000000000000001 +111111111011000001110000000000001 +100000000011000001110111111111111 +11110111111111011100000010001 +11110111111111011101010010001 +11000000110101011100000010001 +10000000000000001100000010001 +10000000000000001101010010001 +11000001110101011111011110N0111 +11110111 1110101 101111010001 +11111111 1111111 111111111111 diff --git a/assets/textures/so-long/floor-diamond.png b/assets/textures/so-long/floor-diamond.png deleted file mode 100644 index 21c674e69d52f01af86c40b10ccce3746daaf821..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1205 zcmeAS@N?(olHy`uVBq!ia0vp^DImp=*uGi$Y>;L7n>f+1ik>$LNIbJ_~?g7i1>oKE)-oXUF0qAlHTUQ;^HA&WZ3&mykV z&hA)xw}>r8x@GQZgD(oZ6`BlO<}EvMS;SGoY}!r^XYNa4NtG2lcI8%9ys+Vs^$_Q< zo}QTD(AKb_STL<@f}z5jn5LP$DO?=Z;t6W(Gno(dNv0VIuyI(s&A8|&q4;J-f*W%o zEDGD-mmVLIMDX8{>6Ub6CncO>+c>} zf4{D^d%E7^=6<_(HSr7|E}O?cc=&hsj;gn6bw7R;M@g*z7k{g4?xC&EId=b&xwzGe zh2hSQ((12Cw{D(hd3XA}U1aq2xNra3uU8+uvVA+Ba%k$!s=k%;!}D1Wv2D!#X*R|8 zv}8h5^A*Jhl^<;0r!0J~$kB9AEsL@8`Cjd4#fG~Py8~Xmw%JjhE+DXy+edrC+Qo-H zC-fZr`1yIW4d==}rUzGI&zr3=dwp+5sWb!I@d@X;ik8J*6WOh8-B|hO{QchB`g=Zp z<651||L*S13VR-bOEaGfB*aO~3~#u*TRZb|UiONgNtesM&AegVxaOK-gXL?PLtKhq znIhL(8~e;Ik9;E#BzvU(>(}jeQ}s`6`fJ~3wy(cH-z_xk#AVeU!`|}5)W}2pOm9?A zCbWxo=^Q?MzrHcOp?H1lfxq(m4+xjzRU3aEAi(_q};6{mzMXB{$ToYeez zajdn?D@o_+3D4Rls97#Q$8(rT(YNXy>!We z9R}+hJVX>OO)yyJ=n5m^4`-NEXbvtRVE-dYPVS*@18I%Pt-} rUw8JpTgRd^(ofkBPq$=Md|lt3R#Uogi{UX~am3*1>gTe~DWM4fwW1^T diff --git a/src/inc/consts.h b/src/inc/consts.h index 65b1d78..e508ad4 100644 --- a/src/inc/consts.h +++ b/src/inc/consts.h @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/21 16:49:31 by sadoming #+# #+# */ -/* Updated: 2025/03/06 16:23:53 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 19:48:54 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,17 +29,15 @@ # define P3 4.71238898 # define DR 0.0174533 -# define FOV 1.04719755 +# define OTHER 1.04719755 # define RAYS 60 +# define CUB_SCALE 64 -# define FVIEW 60 -# define HEIGHT 1.6 - +# define FOV 60 +# define HEIGHT 32 # define VEL 5 # define ANGLE_D 0.1 - -# define CUB_SCALE 64 /* ## MATH CONSTS END ## */ /* ## ACTIONS CONSTS ## */ diff --git a/src/inc/cub_structs.h b/src/inc/cub_structs.h index 9304431..510c1b7 100644 --- a/src/inc/cub_structs.h +++ b/src/inc/cub_structs.h @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/04 12:48:59 by sadoming #+# #+# */ -/* Updated: 2025/03/05 19:05:25 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 19:52:17 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,8 +61,10 @@ typedef struct s_rect /* PLAYER DATA */ /* - - px -> Player X Position - - py -> Player Y Position + - px -> Player X Position in World + - py -> Player Y Position in World + - new_px -> New Player X Position in World + - new_py -> New Player Y Position in World - pdx -> Player Delta X - pdy -> Player Delta Y - pa -> Player Angle @@ -82,47 +84,12 @@ typedef struct s_ply double pa; size_t ipx; size_t ipy; - long ipx_add_xo; - long ipx_sub_xo; - long ipy_add_yo; - long ipy_sub_yo; } t_ply; -/* MAP & FILE DATA */ +/* RAYCAST DATA */ /* - - file -> Data file attached to program - - map -> 2D Map - - tx_no, _so, _we & _ea -> The textures loaded from file - - pla -> Player Focuss ("NSWE") - ----- - - height -> Max height of map - - width -> Max width of map - - size -> height * width of map - ----- - - f_rgb & c_rgb -> Color background (Sky & Floor) - ----- - - has_player -> Counts num of players - - ply -> Attach to player_struct + ** This can change, by the way i don't know how to implement it */ -typedef struct s_map -{ - char **file; - char **map; - char *tx_no; - char *tx_so; - char *tx_we; - char *tx_ea; - char pla; - size_t height; - size_t width; - size_t size; - int f_rgb[3]; - int c_rgb[3]; - int has_player; - t_ply ply; -} t_map; - -/* RAYCAST DATA */ typedef struct s_ray { size_t dof_x; @@ -154,9 +121,13 @@ typedef struct s_ray } t_ray; /* DATA OF A CUB-RECT TEXTURED */ +/* + ** Same as t_ray struct +*/ typedef struct s_tex { double wall_x; + double step; double x; double y; int tx; @@ -164,7 +135,52 @@ typedef struct s_tex t_rect rc; } t_tex; +/* MAP & FILE DATA */ +/* + - file -> Data file attached to program + - map -> 2D Map + - tx_no, _so, _we & _ea -> The textures loaded from file + - pla -> Player Focuss ("NSWE") + ----- + - height -> Max height of map + - width -> Max width of map + - size -> height * width of map + ----- + - f_rgb & c_rgb -> Color background (Sky & Floor) + ----- + - has_player -> Counts num of players + - ply -> Attach to player_struct +*/ +typedef struct s_map +{ + char **file; + char **map; + char *tx_no; + char *tx_so; + char *tx_we; + char *tx_ea; + char pla; + size_t height; + size_t width; + size_t size; + int f_rgb[3]; + int c_rgb[3]; + int has_player; + t_ply ply; +} t_map; + /* THE PRINCIPAL STRUCTURE */ +/* + - mlxd -> The MLX Data + - map -> The Map Data + - ply -> The Player Data + - ray -> The Ray Data + - tex -> The Texture Data + - c_flr -> The Floor Color + - c_sky -> The Sky Color + - scr_w -> The Screen Width + - scr_h -> The Screen Height +*/ typedef struct s_game { t_mlxd *mlxd; diff --git a/src/inc/game.h b/src/inc/game.h index ea05687..da7ab0d 100644 --- a/src/inc/game.h +++ b/src/inc/game.h @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 13:09:36 by sadoming #+# #+# */ -/* Updated: 2025/03/06 16:20:03 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 19:23:17 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,7 +63,7 @@ void start(t_map *map); t_game *check_hrzlines(t_game *gm); t_game *check_vrtlines(t_game *gm); void raycasting(t_game *game); -//t_game *texture_and_print(t_game *gm, int r); +t_game *draw_textured_wall(t_game *gm, size_t r); /* UTILITIES */ char get_cell(t_map *map, long x, long y, int mapp); diff --git a/src/raycasting.c b/src/raycasting.c index ef56cef..6225083 100644 --- a/src/raycasting.c +++ b/src/raycasting.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/11 19:01:29 by sadoming #+# #+# */ -/* Updated: 2025/03/05 20:15:50 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 19:48:44 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/start.c b/src/start.c index 909810c..f57de30 100644 --- a/src/start.c +++ b/src/start.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/04 17:47:42 by sadoming #+# #+# */ -/* Updated: 2025/03/05 20:28:20 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 19:26:04 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,8 +42,12 @@ static t_mlxd init_mlxdata(t_map *map, t_mlxd mlxd) return (mlxd); } -/* -* Start player vars and set bgcolor +/* Start player vars and set bgcolor +* * Set player angle +* * Set player position +* * Set player direction +* * Set sky color +* * Set floor color */ static t_game start_player(t_game game) { @@ -86,7 +90,7 @@ t_game *resize_window(t_game *gm) w = h; while (w % RAYS) { - if (w < FVIEW) + if (w < FOV) w++; else w--; diff --git a/src/textures.c b/src/textures.c index aa1b491..dfb24af 100644 --- a/src/textures.c +++ b/src/textures.c @@ -6,54 +6,61 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/05 20:29:32 by sadoming #+# #+# */ -/* Updated: 2025/03/05 20:36:12 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 19:28:49 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ #include "./inc/game.h" -/* Get the pixel color in texture */ -uint32_t get_rgba(mlx_texture_t *texture, size_t x, size_t y) +static t_game *set_texture_direction(t_game *gm) { - int r; - int g; - int b; - int a; - - r = texture->pixels[y * texture->width * 4 + (x * 4)]; - g = texture->pixels[y * texture->width * 4 + (x * 4) + 1]; - b = texture->pixels[y * texture->width * 4 + (x * 4) + 2]; - a = texture->pixels[y * texture->width * 4 + (x * 4) + 3]; - return (r << 24 | g << 16 | b << 8 | a); + if (gm->ray.side == 1) + { + if (gm->ray.dir_y > 0) + gm->mlxd->tx_ac = gm->mlxd->tx_so; + else + gm->mlxd->tx_ac = gm->mlxd->tx_no; + } + else + { + if (gm->ray.dir_x > 0) + gm->mlxd->tx_ac = gm->mlxd->tx_ea; + else + gm->mlxd->tx_ac = gm->mlxd->tx_we; + } + return (gm); } -static void draw_pixel_column(t_game *gm, int r, int y) +static void paint_pixel(t_game *gm, int x, int y, t_tex t) { - size_t x; - double step_tex; - double tex_pos; - - x = 0; - step_tex = (double)gm->mlxd->tx_ac->height / gm->ray.line_t; - tex_pos = y * step_tex; - gm->tex.ty = (int)tex_pos % gm->mlxd->tx_ac->height; - while (x < gm->ray.scale) - { - gm->tex.tx = (int)(gm->tex.wall_x * gm->mlxd->tx_ac->width) % gm->mlxd->tx_ac->width; - uint32_t color = get_rgba(gm->mlxd->tx_ac, gm->tex.tx, gm->tex.ty); - mlx_put_pixel(gm->mlxd->wimg, r * gm->ray.scale + x, y + gm->ray.line_o, color); - x++; - } + if (x < 0 || x >= (int)gm->scr_w || y < 0 || y >= (int)gm->scr_h) + return ; + t.color = get_rgba(gm->mlxd->tx_ac, t.x, t.y); + mlx_put_pixel(gm->mlxd->wimg, x, y, t.color); } -t_game *texture_and_print(t_game *gm, int r) +t_game *draw_textured_wall(t_game *gm, size_t r) { - int y; + t_tex t; + int y; - y = 0; + set_texture_direction(gm); + t.width = gm->mlxd->tx_ac->width; + t.height = gm->mlxd->tx_ac->height; + if (gm->ray.side == 0) + t.wall_x = gm->ply.py + gm->ray.dist * gm->ray.dir_y; + else + t.wall_x = gm->ply.px + gm->ray.dist * gm->ray.dir_x; + t.wall_x -= floor(t.wall_x); + t.x = (int)(t.wall_x * (double)t.width); + t.step = 1.0 * t.height / gm->ray.line_h; + t.tex_pos = (gm->ray.line_o - gm->scr_h / 2 + gm->ray.line_h / 2) * t.step; + y = gm->ray.line_o; while (y < gm->ray.line_t) { - draw_pixel_column(gm, r, y); + t.y = (int)t.tex_pos & (t.height - 1); + t.tex_pos += t.step; + paint_pixel(gm, r, y, t); y++; } return (gm); diff --git a/src/utilities.c b/src/utilities.c index 67160a4..92d2533 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -6,7 +6,7 @@ /* By: sadoming +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/10 18:38:17 by sadoming #+# #+# */ -/* Updated: 2025/03/05 20:30:22 by sadoming ### ########.fr */ +/* Updated: 2025/03/06 16:55:52 by sadoming ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,21 @@ int32_t ft_pixel(int32_t r, int32_t g, int32_t b, int32_t a) return (r << 24 | g << 16 | b << 8 | a); } +/* Get the pixel color in texture */ +uint32_t get_rgba(mlx_texture_t *texture, size_t x, size_t y) +{ + int r; + int g; + int b; + int a; + + r = texture->pixels[y * texture->width * 4 + (x * 4)]; + g = texture->pixels[y * texture->width * 4 + (x * 4) + 1]; + b = texture->pixels[y * texture->width * 4 + (x * 4) + 2]; + a = texture->pixels[y * texture->width * 4 + (x * 4) + 3]; + return (r << 24 | g << 16 | b << 8 | a); +} + /* Prints a Rectangle * - `paint` => Canvas MLX * - `x` && `y` => Start Position