diff --git a/MLX42/include/MLX42/MLX42_Int.h b/MLX42/include/MLX42/MLX42_Int.h index 45f7229..5a7d051 100644 --- a/MLX42/include/MLX42/MLX42_Int.h +++ b/MLX42/include/MLX42/MLX42_Int.h @@ -6,7 +6,7 @@ /* By: W2Wizard +#+ */ /* +#+ */ /* Created: 2021/12/27 23:55:34 by W2Wizard #+# #+# */ -/* Updated: 2023/11/10 03:22:56 by joppe ######## odam.nl */ +/* Updated: 2023/12/21 01:39:28 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/Makefile b/Makefile index 5c1d3a0..c079909 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,3 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: # -# +:+ +:+ +:+ # -# By: jboeve +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2023/08/22 13:32:22 by jboeve #+# #+# # -# Updated: 2023/12/13 16:25:01 by jboeve ######## odam.nl # -# # -# **************************************************************************** # - ###################### # OS Dependent flags # ###################### @@ -23,7 +11,7 @@ else ifeq ($(shell uname -m),x86_64) endif NAME := app -RUN_CMD := ./$(NAME) test_maps/simple.cub +RUN_CMD := ./$(NAME) test_maps/valid.cub # CFLAGS += -Wall -Wextra -Werror CFLAGS += -Wall -Wextra @@ -49,17 +37,17 @@ SRCS = parser/check_elements.c \ parser/utils_one.c \ parser/utils_two.c \ utils/error.c \ - utils/free.c \ + utils/free.c \ + utils/vec2d_utils.c \ + utils/colors.c \ + game/game.c \ + game/draw.c \ + game/keys.c \ + game/player.c \ + game/raycaster.c \ + game/render.c \ cub3d.c \ - game.c \ - input.c \ - render.c \ - draw.c \ - player.c \ - utils.c \ test_utils.c \ - map.c \ - vector.c \ timer.c HEADER_DIR := include diff --git a/include/meta.h b/include/meta.h index b20dd55..8f3cfaf 100644 --- a/include/meta.h +++ b/include/meta.h @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve #+# #+# */ -/* Updated: 2023/12/13 16:24:24 by jboeve ######## odam.nl */ +/* Updated: 2024/01/02 19:51:20 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,11 +22,11 @@ #include #include #include +#include #include "timer.h" -#include "vector.h" #include "libft.h" -#include "parser.h" +#include "vector.h" #include "get_next_line.h" #include "MLX42/MLX42.h" @@ -45,34 +45,37 @@ #define PI 3.1415926535 // Window settings -#define WINDOW_WIDTH 1920 -#define WINDOW_HEIGHT 1080 -#define WINDOW_TITLE "Gammoe" +// #define WINDOW_WIDTH 1920 +// #define WINDOW_HEIGHT 1080 -#define PLAYER_VIEWPORT_X 720 -#define PLAYER_VIEWPORT_Y 512 -#define PLAYER_VIEWPORT_WALL_WIDTH 8 +#define WINDOW_WIDTH 1280 +#define WINDOW_HEIGHT 720 + +#define WINDOW_TITLE "Gammoe" #define TICK_RATE (1.0 / 60.0) // TODO Move all this stuff to some kind of game.h #define CELL_WIDTH 64 #define CELL_HEIGHT 64 +#define CELL_SIZE 64 -#define PLAYER_WIDTH 16 -#define PLAYER_HEIGHT 16 -#define PLAYER_RAY_COUNT 90 -#define PLAYER_WALK_SPEED 15 -#define PLAYER_ROTATE_SPEED 5 +#define PLAYER_MOVE_SPEED 5.0 #define PLAYER_RUN_MODIFIER 2.5 +#define PLAYER_ROTATE_SPEED 5.0 +#define PLAYER_ROTATE_MODIFIER 2.5 + + +#define PLAYER_MOV_SPEED 0.08 #define COLOR_BACKGROUND 0x111111FF #define COLOR_PLAYER 0xFFFFFFFF -#define VEC_X 0 -#define VEC_Y 1 - +// #define FOV 0.66 +#define FOV 0.80 +typedef bool (t_ray_hitfunc) (void *p, uint32_t x, uint32_t y); +typedef struct s_meta t_meta; typedef enum e_cell_type { MAP_EMPTY, @@ -80,8 +83,6 @@ typedef enum e_cell_type { MAP_SPACE, } t_cell_type; - - typedef union s_rgba { uint32_t value; @@ -94,30 +95,35 @@ typedef union s_rgba }; } t_rgba; + +typedef enum e_side { + HIT_NONE, + HIT_NS, + HIT_EW, +} t_side; + typedef struct s_ray { - t_vec2f start; - t_vec2f end; + t_side hit_side; + double length; + t_vec2d direction; } t_ray; -typedef struct s_meta t_meta; -// NOTE: Maybe switch to double instead of float? typedef struct s_player { - t_meta *meta; - // TODO Have a map_position which will be the position relative to the leftmost square. - // Based on that position we can just `position / CELL_WIDTH` to find the cell position. - t_vec2i map_cell; - t_vec2f position; - t_vec2f direction; - t_vec2f beam; - t_ray rays[PLAYER_RAY_COUNT]; - float angle_rad; + t_meta *meta; + t_ray rays[WINDOW_WIDTH]; + t_vec2d cam_plane; + t_vec2d position; + t_vec2d direction; } t_player; + typedef struct s_map { t_cell_type *level; uint32_t width; uint32_t height; + t_vec2u player_start; + char player_start_dir; } t_map; typedef struct s_tex { @@ -134,13 +140,14 @@ typedef struct s_meta { mlx_image_t *image; t_timer update_timer; t_timer fps_timer; - t_player player; + t_player player; uint32_t fps; t_map map; t_tex tex; char *map_file; } t_meta; + // cub3d.c int cub3d(int argc, char *argv[]); @@ -149,109 +156,45 @@ void game_init(t_meta *meta); void game_loop(void* param); // player.c -void player_move(t_player *p, t_vec2f transform); -void player_look(t_player *p, double angle); +void player_move(t_player *p, t_vec2d transform); +void player_turn(t_player *p, float radiant); void player_raycast(t_player *p); -// input.c -void key_hook(mlx_key_data_t keydata, void* param); -void cursor_hook(double xpos, double ypos, void* param); +// keys.c +void mouse_hook(double xpos, double ypos, void* param); +void keys_handle(t_meta *meta, double time_delta); // render.c -t_vec2i render_get_draw_offset(); -void render_player_view(mlx_image_t *image, t_player *p); -void render_player(mlx_image_t *image, t_player *p); +void render_player_viewport(mlx_image_t *image, t_player *p); void render_clear_bg(mlx_image_t *image); -void render_map_grid(mlx_image_t *image, t_map *m); - -// map.c -t_cell_type map_get_cell_type(t_map *m, t_vec2f pos); +void render_minimap(mlx_image_t *image, t_map *m); // draw.c void draw_rect(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color); void draw_line(mlx_image_t *image, t_vec2i start, t_vec2i end, t_rgba c); void draw_put_pixel(mlx_image_t* image, uint32_t x, uint32_t y, uint32_t color); -// utils.c -float deg_to_rad(float deg); - // test_utils.c -void print_vec2f(const char *s, t_vec2f vec); -void print_vec2i(const char *s, t_vec2i vec); void print_cell(t_cell_type cell); -void game_init(t_meta *meta); -void game_loop(void* param); // keys.c -void keyhook(mlx_key_data_t keydata, void* param); +void keys_update(mlx_key_data_t keydata, void *param); // draw.c void draw_square(mlx_image_t* image, uint32_t x_pos, uint32_t y_pos, uint32_t width, uint32_t height, uint32_t color); void cube_put_pixel(mlx_image_t* image, uint32_t x, uint32_t y, uint32_t color); -// check_map.c -int check_map(t_meta *meta, char *rect); -int find_index(t_meta *meta, uint32_t x, uint32_t y); - -// free.c -void meta_free(t_meta *meta); - -// PARSER - -// parser.c -char *read_file(int fd); -int map_ext(char *file); -int parser(t_meta *meta, char *map_file); - -// parse_map.c -bool is_map_line(char *file); -int input_map(t_meta *meta, char *file); - -// parse_elements.c -int input_texture(t_tex *tex, char *file); -int input_colour(t_tex *tex, char *file); -int save_elements(t_tex *tex, char *file); -int parse_elements(t_meta *meta, char *file); - -// check_colors.c -bool valid_rgb_value(char *file); -bool is_valid_color(char *file); -bool colors_valid(char *file); - -// check_elements.c -bool is_valid_element(char *file); -bool only_spaces(char *file); -bool is_map_element(char *file); -bool elements_order(char *file); -bool check_missing(int *found); -bool is_missing(char *file); -bool is_duplicate(char *file); - -// check_map.c -bool is_map_chars_valid(char *map); -int flood_fill(t_meta *meta, char *map, int x, int y); -bool save_start_pos(t_meta *meta, char *map); -bool is_floor_exposed(t_meta *meta, char *map); - -// parse_textures.c -void get_colour_value(char *file, t_rgba *col); -char *get_tex_val(char *file); -bool is_texture(char *file); -bool is_colour(char *file); - -// utils_one.c -void skip_line(char **file); -void skip_spaces(char **file); -void skip_digits(char **file); -int valid_map_char(char c); -int player_pos_char(char c); - -// utils_two.c -uint32_t find_width(char *map); -uint32_t find_height(char *map); -char *make_rect(char *map, uint32_t w, uint32_t h); +// raycaster.c +t_ray raycaster_cast(t_meta *meta, t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit); // test_utils.c -void print_map(char *map, uint32_t w, uint32_t h); +void print_map(char *map, uint32_t w, uint32_t h); + +// colors.c +int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a); +int32_t find_wall_color(t_side side); + +// free.c +void meta_free(t_meta *meta); #endif diff --git a/include/parser.h b/include/parser.h index 182f993..cf9ffe8 100644 --- a/include/parser.h +++ b/include/parser.h @@ -1,17 +1,17 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* meta.h :+: :+: :+: */ +/* parser.h :+: :+: */ /* +:+ */ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve #+# #+# */ -/* Updated: 2023/11/09 18:54:26 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 19:05:30 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#ifndef PASER_H -#define PASER_H +#ifndef PARSER_H +#define PARSER_H # define TOO_MANY_PLAYERS "Map should not have more than one player position\n" # define INVALID_CHAR "Map has invalid characters\n" @@ -54,7 +54,64 @@ int pr_err(t_err type); // check_map.c int valid_map_char(char c); -int player_pos_char(char c); -bool is_map_chars_valid(char *map); +int player_pos_char(char c); +bool is_map_chars_valid(char *map); +int check_map(t_meta *meta, char *rect); +int find_index(t_meta *meta, uint32_t x, uint32_t y); -#endif \ No newline at end of file + +// parser.c +char *read_file(int fd); +int map_ext(char *file); +int parser(t_meta *meta, char *map_file); + +// parse_map.c +bool is_map_line(char *file); +int input_map(t_meta *meta, char *file); + +// parse_elements.c +int input_texture(t_tex *tex, char *file); +int input_colour(t_tex *tex, char *file); +int save_elements(t_tex *tex, char *file); +int parse_elements(t_meta *meta, char *file); + +// check_colors.c +bool valid_rgb_value(char *file); +bool is_valid_color(char *file); +bool colors_valid(char *file); + +// check_elements.c +bool is_valid_element(char *file); +bool only_spaces(char *file); +bool is_map_element(char *file); +bool elements_order(char *file); +bool check_missing(int *found); +bool is_missing(char *file); +bool is_duplicate(char *file); + +// check_map.c +bool is_map_chars_valid(char *map); +int flood_fill(t_meta *meta, char *map, int x, int y); +bool save_start_pos(t_meta *meta, char *map); +bool is_floor_exposed(t_meta *meta, char *map); + +// parse_textures.c +void get_colour_value(char *file, t_rgba *col); +char *get_tex_val(char *file); +bool is_texture(char *file); +bool is_colour(char *file); + +// utils_one.c +void skip_line(char **file); +void skip_spaces(char **file); +void skip_digits(char **file); +int valid_map_char(char c); +int player_pos_char(char c); + +// utils_two.c +uint32_t find_width(char *map); +uint32_t find_height(char *map); +char *make_rect(char *map, uint32_t w, uint32_t h); + + +#endif diff --git a/include/vector.h b/include/vector.h index 85205b8..0aa61c7 100644 --- a/include/vector.h +++ b/include/vector.h @@ -5,33 +5,40 @@ /* +:+ */ /* By: joppe +#+ */ /* +#+ */ -/* Created: 2023/11/11 03:51:14 by joppe #+# #+# */ -/* Updated: 2023/11/18 13:21:55 by jboeve ######## odam.nl */ +/* Created: 2024/01/02 19:07:15 by joppe #+# #+# */ +/* Updated: 2024/01/02 19:07:43 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef VECTOR_H #define VECTOR_H -#include -#include +#include -#define VEC_X 0 -#define VEC_Y 1 +typedef struct s_vec2d { + double x; + double y; +} t_vec2d; -// Use the SIMD Vector instructions (very cool :D) -// https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Vector-Extensions.html#Vector-Extensions -typedef float t_vec2f __attribute__ ((vector_size (sizeof(float) * 2))); -typedef int32_t t_vec2i __attribute__ ((vector_size (sizeof(int32_t) * 2))); -typedef uint32_t t_vec2u __attribute__ ((vector_size (sizeof(uint32_t) * 2))); +typedef struct s_vec2i { + int32_t x; + int32_t y; +} t_vec2i; +typedef struct s_vec2u { + uint32_t x; + uint32_t y; +} t_vec2u; -// vector.c -t_vec2i vec2f_to_vec2i(t_vec2f vec); -t_vec2f vec2i_to_vec2f(t_vec2i vec); -t_vec2f vec2f_rotate2d(float angle); -t_vec2f vec2f_normalize(t_vec2f vec); -float deg_to_rad(float deg); -#endif +// vec2d_utils.c +t_vec2d vec2d_add(t_vec2d v1, t_vec2d v2); +t_vec2d vec2d_scalar_product(t_vec2d vec, double scalar); +void print_vec2d(char *str, t_vec2d vector); +t_vec2d vec2d_rotate(t_vec2d old, double radiant); +t_vec2d vec2u_to_vec2d(t_vec2u v); +double deg_to_rad(float deg); + + +#endif // !VECTOR_H diff --git a/src/cub3d.c b/src/cub3d.c index 9c29684..557efb7 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -1,25 +1,20 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* cub3d.c :+: :+: :+: */ +/* cub3d.c :+: :+: */ /* +:+ */ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/07 15:36:26 by jboeve #+# #+# */ -/* Updated: 2023/11/12 20:30:33 by joppe ######## odam.nl */ +/* Updated: 2024/01/02 18:58:50 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ +#include "MLX42/MLX42.h" #include "meta.h" +#include "parser.h" -// Exit the program as failure. -static void ft_error(void) -{ - fprintf(stderr, "%s", mlx_strerror(mlx_errno)); - exit(EXIT_FAILURE); -} - static void fps_hook(void *param) { t_meta *meta = param; @@ -28,7 +23,8 @@ static void fps_hook(void *param) timer_init(&meta->fps_timer, mlx_get_time); if (timer_delta(&meta->fps_timer) >= 1) { - printf("FPS: [%u]\n", meta->fps); + if (meta->fps) + printf("FPS: [%u]\n", meta->fps); timer_start(&meta->fps_timer); meta->fps = 0; } @@ -36,42 +32,56 @@ static void fps_hook(void *param) meta->fps++; } + +// Exit the program as failure. +static void ft_error(void) +{ + fprintf(stderr, "%s", mlx_strerror(mlx_errno)); + exit(EXIT_FAILURE); +} + + void leaks(void) { system("leaks -q app"); } -int cub3d(int argc, char **argv) +// change to create a different image for the minimap vs. main viewport +int init_mlx_images(t_meta *meta) { - t_meta meta; - - // UNUSED(argv); - if (argc != 2) - return (pr_err(ARG_ERR)); - // Zero our struct to prevent garbage data. - ft_bzero(&meta, sizeof(t_meta)); - if (parser(&meta, argv[1])) - return(meta_free(&meta), 1); - // MLX allows you to define its core behaviour before startup. - meta.mlx = mlx_init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, true); - if (!meta.mlx) + meta->mlx = mlx_init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, true); + if (!meta->mlx) { ft_error(); return EXIT_FAILURE; } // Create and display the image. - meta.image = mlx_new_image(meta.mlx, WINDOW_WIDTH, WINDOW_HEIGHT); - if (!meta.image || (mlx_image_to_window(meta.mlx, meta.image, 0, 0) < 0)) + meta->image = mlx_new_image(meta->mlx, WINDOW_WIDTH, WINDOW_HEIGHT); + if (!meta->image || (mlx_image_to_window(meta->mlx, meta->image, 0, 0) < 0)) { ft_error(); - return EXIT_FAILURE; + return (EXIT_FAILURE); } + return (EXIT_SUCCESS); +} + +int cub3d(int argc, char **argv) +{ + t_meta meta; + + if (argc != 2) + return (pr_err(ARG_ERR)); + ft_bzero(&meta, sizeof(t_meta)); + if (parser(&meta, argv[1])) + return(meta_free(&meta), EXIT_FAILURE); + // TODO Error check. + init_mlx_images(&meta); game_init(&meta); - mlx_cursor_hook(meta.mlx, cursor_hook, &meta); - mlx_key_hook(meta.mlx, key_hook, &meta); - mlx_loop_hook(meta.mlx, fps_hook, &meta); + mlx_set_cursor_mode(meta.mlx, MLX_MOUSE_HIDDEN); mlx_loop_hook(meta.mlx, game_loop, &meta); + mlx_loop_hook(meta.mlx, fps_hook, &meta); + mlx_cursor_hook(meta.mlx, mouse_hook, &meta); mlx_loop(meta.mlx); mlx_terminate(meta.mlx); meta_free(&meta); diff --git a/src/game.c b/src/game.c deleted file mode 100644 index abe1822..0000000 --- a/src/game.c +++ /dev/null @@ -1,110 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* game.c :+: :+: :+: */ -/* +:+ */ -/* By: joppe +#+ */ -/* +#+ */ -/* Created: 2023/11/08 22:35:05 by joppe #+# #+# */ -/* Updated: 2023/11/18 20:39:45 by jboeve ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "MLX42/MLX42.h" -#include "meta.h" -#include "timer.h" -#include "vector.h" -#include -#include -#include -#include -#include -#include -#include - -// Temp -#define MAP_WIDTH 8 -#define MAP_HEIGHT 8 - -t_cell_type MAP[] = { - MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, - MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_WALL, MAP_SPACE, MAP_WALL, - MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_WALL, - MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_WALL, MAP_WALL, MAP_WALL, - MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_WALL, - MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_WALL, - MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_WALL, MAP_SPACE, MAP_SPACE, MAP_SPACE, MAP_WALL, - MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, MAP_WALL, -}; - -void game_init(t_meta *meta) -{ - t_player* const p = &meta->player; - - timer_init(&meta->update_timer, mlx_get_time); - timer_start(&meta->update_timer); - // Give player a reference to meta struct. - p->meta = meta; - - printf("Player direction X: %f Y: %f\n", meta->player.direction[VEC_X], meta->player.direction[VEC_Y]); - printf("Player position X: %f Y: %f\n", meta->player.position[VEC_X], meta->player.position[VEC_Y]); - // // Setup player initial position, later this correspond with the PLAYER_START in the map. - // p->position[VEC_X] = (float) meta->map.width * CELL_WIDTH / 2; - // p->position[VEC_Y] = (float) meta->map.height * CELL_WIDTH/ 2; - - - // player_look(p, deg_to_rad(180.0f)); -} - -// This function handles all the "simulation" type stuff such as moving players opening doors, etc. -static void game_update(t_meta *meta, double time_delta) -{ - t_player *const p = &meta->player; - const float rotate = (PLAYER_ROTATE_SPEED * time_delta); - float move = (PLAYER_WALK_SPEED * 10 * time_delta); - - if (mlx_is_key_down(meta->mlx, MLX_KEY_LEFT_SHIFT)) - move *= PLAYER_RUN_MODIFIER; - if (mlx_is_key_down(meta->mlx, MLX_KEY_W)) - player_move(p, move * p->direction); - if (mlx_is_key_down(meta->mlx, MLX_KEY_S)) - player_move(p, -move * p->direction); - - if (mlx_is_key_down(meta->mlx, MLX_KEY_A)) - player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle_rad + (3 * PI / 2))) * move); - if (mlx_is_key_down(meta->mlx, MLX_KEY_D)) - player_move(p, vec2f_normalize(vec2f_rotate2d(p->angle_rad + (PI / 2))) * move); - - if (mlx_is_key_down(meta->mlx, MLX_KEY_Q)) - player_look(p, -rotate); - if (mlx_is_key_down(meta->mlx, MLX_KEY_E)) - player_look(p, rotate); -} - - -void game_loop(void* param) -{ - t_meta *const meta = param; - // Time it took to draw a frame. - double frame_time; - // Time it took for each iteration of the game_update loop. - double delta_time; - - frame_time = timer_delta(&meta->update_timer); - - // This approximation is slightly faster than `(frame_time > 0.000000)`. - while (fabs(frame_time) >= 0.000005) - { - // `delta_time` is capped at `TICK_RATE`. - delta_time = fmin(frame_time, TICK_RATE); - game_update(meta, delta_time); - - frame_time -= delta_time; - } - timer_start(&meta->update_timer); - - render_clear_bg(meta->image); - render_map_grid(meta->image, &meta->map); - render_player_view(meta->image, &meta->player); - render_player(meta->image, &meta->player); -} diff --git a/src/draw.c b/src/game/draw.c similarity index 58% rename from src/draw.c rename to src/game/draw.c index 760c5cf..146a4b9 100644 --- a/src/draw.c +++ b/src/game/draw.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* draw.c :+: :+: :+: */ +/* draw.c :+: :+: */ /* +:+ */ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/09 01:32:57 by joppe #+# #+# */ -/* Updated: 2023/11/09 17:55:11 by yzaim ### ########.fr */ +/* Updated: 2023/12/20 18:38:27 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,40 +58,40 @@ static int32_t direction(int32_t val) static void bresenham_move(t_bresenham *line) { - if (line->delta[VEC_X] > line->delta[VEC_Y]) + if (line->delta.x > line->delta.y) { - line->current[VEC_X] += line->direction[VEC_X]; - line->slow_move += line->delta[VEC_Y]; - if (line->slow_move * 2 >= line->delta[VEC_X]) + line->current.x += line->direction.x; + line->slow_move += line->delta.y; + if (line->slow_move * 2 >= line->delta.x) { - line->current[VEC_Y] += line->direction[VEC_Y]; - line->slow_move -= line->delta[VEC_X]; + line->current.y += line->direction.y; + line->slow_move -= line->delta.x; } } else { - line->current[VEC_Y] += line->direction[VEC_Y]; - line->slow_move += line->delta[VEC_X]; - if (line->slow_move * 2 >= line->delta[VEC_Y]) + line->current.y += line->direction.y; + line->slow_move += line->delta.x; + if (line->slow_move * 2 >= line->delta.y) { - line->current[VEC_X] += line->direction[VEC_X]; - line->slow_move -= line->delta[VEC_Y]; + line->current.x += line->direction.x; + line->slow_move -= line->delta.y; } } } static void bresenham(mlx_image_t *meta, t_bresenham *line, t_rgba c) { - if (line->delta[VEC_X] > line->delta[VEC_Y]) - line->slow_move = (line->delta[VEC_Y] - line->delta[VEC_X]) / 2; + if (line->delta.x > line->delta.y) + line->slow_move = (line->delta.y - line->delta.x) / 2; else - line->slow_move = (line->delta[VEC_X] - line->delta[VEC_Y]) / 2; - while (line->current[VEC_X] != line->end[VEC_X] || line->current[VEC_Y] != line->end[VEC_Y]) + line->slow_move = (line->delta.x - line->delta.y) / 2; + while (line->current.x != line->end.x || line->current.y != line->end.y) { - draw_put_pixel(meta, line->current[VEC_X], line->current[VEC_Y], c.value); + draw_put_pixel(meta, line->current.x, line->current.y, c.value); bresenham_move(line); } - draw_put_pixel(meta, line->current[VEC_X], line->current[VEC_Y], c.value); + draw_put_pixel(meta, line->current.x, line->current.y, c.value); } void draw_line(mlx_image_t *image, t_vec2i start, t_vec2i end, t_rgba c) @@ -101,11 +101,11 @@ void draw_line(mlx_image_t *image, t_vec2i start, t_vec2i end, t_rgba c) line.current = start; line.end = end; - line.delta[VEC_Y] = abs(line.current[VEC_Y] - line.end[VEC_Y]); - line.delta[VEC_X] = abs(line.current[VEC_X] - line.end[VEC_X]); + line.delta.y = abs(line.current.y - line.end.y); + line.delta.x = abs(line.current.x - line.end.x); - line.direction[VEC_Y] = direction(line.end[VEC_Y] - line.current[VEC_Y] > 0); - line.direction[VEC_X] = direction(line.end[VEC_X] - line.current[VEC_X] > 0); + line.direction.y = direction(line.end.y - line.current.y > 0); + line.direction.x = direction(line.end.x - line.current.x > 0); bresenham(image, &line, c); } diff --git a/src/game/game.c b/src/game/game.c new file mode 100644 index 0000000..4963f6d --- /dev/null +++ b/src/game/game.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* game.c :+: :+: */ +/* +:+ */ +/* By: joppe +#+ */ +/* +#+ */ +/* Created: 2023/11/08 22:35:05 by joppe #+# #+# */ +/* Updated: 2024/01/02 19:49:20 by joppe ######## odam.nl */ + +/* */ +/* ************************************************************************** */ + +#include "meta.h" +#include "timer.h" +#include "vector.h" +#include +#include + +void set_player_start_position(t_player *p, char dir) +{ + if (dir == 'N') + { + p->direction.x = 0; + p->direction.y = -1; + p->cam_plane.x = FOV; + p->cam_plane.y = 0; + } + else if (dir == 'S') + { + p->direction.x = 0; + p->direction.y = 1; + p->cam_plane.x = FOV; + p->cam_plane.y = 0; + } + else if (dir == 'E') + { + p->direction.x = 1; + p->direction.y = 0; + p->cam_plane.x = 0; + p->cam_plane.y = FOV; + } + else // W + { + p->direction.x = -1; + p->direction.y = 0; + p->cam_plane.x = 0; + p->cam_plane.y = FOV; + } + p->position = vec2u_to_vec2d(p->meta->map.player_start); + player_move(p, (t_vec2d) {0.0, 0.0}); +} + +void game_init(t_meta *meta) +{ + t_player* const p = &meta->player; + timer_init(&meta->update_timer, mlx_get_time); + timer_start(&meta->update_timer); + + p->meta = meta; + set_player_start_position(&meta->player, meta->map.player_start_dir); +} + +static void game_update(t_meta *meta, double time_delta) +{ + keys_handle(meta, time_delta); +} + +void game_loop(void* param) +{ + t_meta *const meta = param; + double frame_time; + double delta_time; + + frame_time = timer_delta(&meta->update_timer); + + // This approximation is slightly faster than `(frame_time > 0.000000)`. + while (fabs(frame_time) >= 0.000005) + { + // `delta_time` is capped at `TICK_RATE`. + delta_time = fmin(frame_time, TICK_RATE); + game_update(meta, delta_time); + + frame_time -= delta_time; + } + timer_start(&meta->update_timer); + render_player_viewport(meta->image, &meta->player); +} diff --git a/src/game/keys.c b/src/game/keys.c new file mode 100644 index 0000000..a6523ad --- /dev/null +++ b/src/game/keys.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* keys.c :+: :+: */ +/* +:+ */ +/* By: jboeve +#+ */ +/* +#+ */ +/* Created: 2023/12/15 14:05:30 by jboeve #+# #+# */ +/* Updated: 2024/01/02 19:37:53 by joppe ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "MLX42/MLX42.h" +#include "meta.h" +#include +#include + +void keys_handle(t_meta *meta, double delta_time) +{ + t_player* const p = &meta->player; + float speed = PLAYER_MOVE_SPEED * delta_time; + float rotate_speed = PLAYER_ROTATE_SPEED * delta_time; + + if (mlx_is_key_down(meta->mlx, MLX_KEY_LEFT_SHIFT)) + speed *= PLAYER_RUN_MODIFIER; + if (mlx_is_key_down(meta->mlx, MLX_KEY_LEFT_CONTROL)) + rotate_speed /= PLAYER_ROTATE_MODIFIER; + if (mlx_is_key_down(meta->mlx, MLX_KEY_ESCAPE)) + mlx_close_window(meta->mlx); + if (mlx_is_key_down(meta->mlx, MLX_KEY_W)) + player_move(p, (t_vec2d) {p->direction.x * speed, p->direction.y * speed}); + if (mlx_is_key_down(meta->mlx, MLX_KEY_S)) + player_move(p, (t_vec2d) {p->direction.x * -speed, p->direction.y * -speed}); + if (mlx_is_key_down(meta->mlx, MLX_KEY_A)) + player_move(p, (t_vec2d) {(vec2d_rotate(p->direction, PI / 2).x) * -speed, (vec2d_rotate(p->direction, PI / 2).y) * -speed}); + if (mlx_is_key_down(meta->mlx, MLX_KEY_D)) + player_move(p, (t_vec2d) {(vec2d_rotate(p->direction, PI / 2).x) * speed, (vec2d_rotate(p->direction, PI / 2).y) * speed}); + if (mlx_is_key_down(meta->mlx, MLX_KEY_Q)) + player_turn(p, -rotate_speed); + if (mlx_is_key_down(meta->mlx, MLX_KEY_E)) + player_turn(p, rotate_speed); +} + +void mouse_hook(double xpos, double ypos, void *param) +{ + t_meta *meta = param; + static double x_old = 0; + const float rotate_speed = 0.200f; + static bool going_right = false; + + static double time_old = 0; + if (time_old == 0) + time_old = mlx_get_time(); + if (x_old == 0) + x_old = xpos; + + if (mlx_get_time() - time_old > 0.20) + time_old = mlx_get_time(); + + float delta_time = mlx_get_time() - time_old; + float speed = delta_time * fabs(x_old - xpos) * rotate_speed; + if (xpos > WINDOW_WIDTH /2) + { + if (!going_right) + { + // printf("going left\n"); + delta_time = mlx_get_time() - time_old; + speed = delta_time * fabs(x_old - xpos) * rotate_speed; + } + player_turn(meta, speed); + going_right = true; + } + else + { + if (going_right) + { + // printf("going right\n"); + delta_time = mlx_get_time() - time_old; + speed = delta_time * fabs(x_old - xpos) * rotate_speed; + } + player_turn(meta, -speed); + going_right = false; + } + // printf("delta_time [%lf]\n", delta_time); + // printf("speed [%lf]\n", speed); + // printf("mousepos [%lf]\n", xpos); + time_old = mlx_get_time(); + mlx_set_mouse_pos(meta->mlx, WINDOW_WIDTH / 2, WINDOW_HEIGHT /2); + x_old = xpos; +} diff --git a/src/utils.c b/src/game/minimap.c similarity index 62% rename from src/utils.c rename to src/game/minimap.c index 38e4c5d..8e78c9b 100644 --- a/src/utils.c +++ b/src/game/minimap.c @@ -1,19 +1,24 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* keys.c :+: :+: :+: */ +/* minimap.c :+: :+: */ /* +:+ */ -/* By: joppe +#+ */ +/* By: jboeve +#+ */ /* +#+ */ -/* Created: 2023/11/12 20:04:23 by joppe #+# #+# */ -/* Updated: 2023/11/12 20:04:28 by joppe ######## odam.nl */ +/* Created: 2023/12/15 14:01:46 by jboeve #+# #+# */ +/* Updated: 2023/12/15 14:07:39 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ +#include "MLX42/MLX42.h" #include "meta.h" -float deg_to_rad(float deg) +void minimap_update(t_minimap *m, t_player *p) { - return deg * (PI / 180); + } +void minimap_render(t_minimap *m) +{ + +} diff --git a/src/game/player.c b/src/game/player.c new file mode 100644 index 0000000..020e5ac --- /dev/null +++ b/src/game/player.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* player.c :+: :+: */ +/* +:+ */ +/* By: joppe +#+ */ +/* +#+ */ +/* Created: 2023/11/10 02:25:34 by joppe #+# #+# */ +/* Updated: 2024/01/02 19:37:31 by joppe ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "MLX42/MLX42.h" +#include "libft.h" +#include "meta.h" +#include +#include +#include +#include +#include + +bool bound_check(void *param, uint32_t x, uint32_t y) +{ + t_meta *const meta = (t_meta *) param; + if (x < meta->map.width && y < meta->map.height) + return (meta->map.level[(y * meta->map.width) + x] == MAP_WALL); + else + { + UNIMPLEMENTED("Map out of bounds."); + } +} + +void player_move(t_player *p, t_vec2d transform) +{ + t_vec2d new_position; + + new_position.x = (p->position.x + (transform.x)); + new_position.y = (p->position.y + (transform.y)); + // TODO Use raycast to check if we can move. + if (!bound_check(p->meta, new_position.x, new_position.y)) + { + p->position.x += transform.x; + p->position.y += transform.y; + } + player_raycast(p); +} + +// negative rotation parameter turns left vs positive rotation parameter turns right +void player_turn(t_player *p, float radiant) +{ + p->direction = vec2d_rotate(p->direction, radiant); + p->cam_plane = vec2d_rotate(p->cam_plane, radiant); + player_raycast(p); +} + +void player_raycast(t_player *p) +{ + uint32_t w = p->meta->image->width; + uint32_t col; + double camera_x; + t_vec2d ray_start; + + // TODO Just create the player.plane here instead of saving it. + col = 0; + while(col < w) + { + camera_x = (2 * col / ((double) w) - 1); + ray_start = vec2d_add(p->direction, vec2d_scalar_product(p->cam_plane, camera_x)); + p->rays[col] = raycaster_cast(p->meta, p->position, ray_start, bound_check); + col++; + } +} diff --git a/src/game/raycaster.c b/src/game/raycaster.c new file mode 100644 index 0000000..89f1bc7 --- /dev/null +++ b/src/game/raycaster.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* raycaster.c :+: :+: */ +/* +:+ */ +/* By: jboeve +#+ */ +/* +#+ */ +/* Created: 2023/12/15 15:20:09 by jboeve #+# #+# */ +/* Updated: 2024/01/02 18:55:06 by joppe ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "meta.h" +#include + +inline static t_vec2d calculate_delta_dist(t_vec2d ray_direction) +{ + t_vec2d delta_dist; + const double tolerance = 0.005; + + if (fabs(-ray_direction.x) < tolerance) + delta_dist.x = INT32_MAX; + else + delta_dist.x = fabs(1 / ray_direction.x); + if (fabs(-ray_direction.y) < tolerance) + delta_dist.y = INT32_MAX; + else + delta_dist.y = fabs(1 / ray_direction.y); + return (delta_dist); +} + +inline static t_vec2d calculate_side_dist(t_vec2d ray_direction, \ + t_vec2d player_pos, t_vec2d map_pos, t_vec2d delta_dist) +{ + t_vec2d side_dist; + + if (ray_direction.x < 0) + side_dist.x = (player_pos.x - map_pos.x) * delta_dist.x; + else + side_dist.x = (map_pos.x + 1.0 - player_pos.x) * delta_dist.x; + if (ray_direction.y < 0) + side_dist.y = (player_pos.y - map_pos.y) * delta_dist.y; + else + side_dist.y = (map_pos.y + 1.0 - player_pos.y) * delta_dist.y; + return (side_dist); +} + +inline static t_vec2d calculate_step_size(t_vec2d ray_direction) +{ + const bool comp_x = (ray_direction.x < 0); + const bool comp_y = (ray_direction.y < 0); + const int8_t dir_x = (comp_x * -1) + (!comp_x * 1); + const int8_t dir_y = (comp_y * -1) + (!comp_y * 1); + + return ((t_vec2d){dir_x, dir_y}); +} + +inline static double calculate_ray_length(t_side hit_side, \ + t_vec2d side_dist, t_vec2d delta_dist) +{ + if (hit_side == HIT_NS) + return (side_dist.x - delta_dist.x); + else + return (side_dist.y - delta_dist.y); +} + +inline static t_side ray_move(t_vec2d *side_dist, t_vec2d *delta_dist, \ + t_vec2d *step_size, t_vec2d *map_pos) +{ + if (side_dist->x < side_dist->y) + { + side_dist->x += delta_dist->x; + map_pos->x += step_size->x; + return (HIT_NS); + } + else + { + side_dist->y += delta_dist->y; + map_pos->y += step_size->y; + return (HIT_EW); + } +} + +t_ray raycaster_cast(t_meta *meta, t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit) +{ + t_ray ray; + t_vec2d map_pos; + t_vec2d side_dist; + t_vec2d step_size; + t_vec2d delta_dist; + + map_pos.x = (int)pp.x; + map_pos.y = (int)pp.y; + delta_dist = calculate_delta_dist(dir); + side_dist = calculate_side_dist(dir, pp, map_pos, delta_dist); + step_size = calculate_step_size(dir); + while (1) + { + ray.hit_side = ray_move(&side_dist, &delta_dist, &step_size, &map_pos); + if (hit(meta, map_pos.x, map_pos.y)) + break; + } + ray.length = calculate_ray_length(ray.hit_side, side_dist, delta_dist); + return (ray); +} diff --git a/src/game/render.c b/src/game/render.c new file mode 100644 index 0000000..8aa0343 --- /dev/null +++ b/src/game/render.c @@ -0,0 +1,124 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* render.c :+: :+: */ +/* +:+ */ +/* By: joppe +#+ */ +/* +#+ */ +/* Created: 2023/11/08 23:14:20 by joppe #+# #+# */ +/* Updated: 2024/01/02 19:32:09 by joppe ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "meta.h" +#include +#include +#include +#include + + +const t_rgba CELL_COLORS[] = { + [MAP_EMPTY] = {0xff00aaff}, + [MAP_WALL] = {0x004ebaff}, + [MAP_SPACE] = {0x696969ff}, +}; + +void draw_cell(mlx_image_t *image, t_map *m, uint32_t cell_x, uint32_t cell_y) +{ + const size_t x_offset = (cell_x * CELL_WIDTH); + const size_t y_offset = (cell_y * CELL_HEIGHT); + + const t_cell_type cell = (m->level[(cell_y * m->width) + cell_x]); + + draw_rect(image, x_offset, y_offset, CELL_WIDTH, CELL_HEIGHT, CELL_COLORS[cell].value); +} + +void render_clear_bg(mlx_image_t *image) +{ + const uint32_t size = image->width * image->height; + size_t i; + + i = 0; + while (i < size) + { + mlx_put_pixel(image, i, 0, COLOR_BACKGROUND); + i++; + } +} + + +////////////////////////// +t_vec2d calculate_draw_start_and_end1(double ray_length, uint32_t h) +{ + //calculate lowest and highest pixel to fill in current stripe + uint32_t start; + uint32_t end; + double line_height; + + line_height = (int)(h / ray_length); + if (line_height > h) + line_height = h; + start = -line_height / 2 + ((double)h) / 2; + end = line_height / 2 + ((double)h) / 2; + if (end >= h) + end = h - 1; + return ((t_vec2d) {start, end}); +} + +void draw_column1(t_meta *meta, t_vec2d line, t_side side, uint32_t col, uint32_t h) +{ + uint32_t color; + uint32_t row; + + row = 0; + // draw_col(meta->image, start, end, color); + while (row < h) + { + // ceiling + if (row < line.x) + { + color = set_color(0, 0, 0, 255); + } + // floor + else if (row > line.y) + { + color = set_color(255, 255, 255, 255); + } + else + { + color = find_wall_color(side); + } + mlx_put_pixel(meta->image, col, row, color); + row++; + } +} + +void render_player_viewport(mlx_image_t *image, t_player *p) +{ + uint32_t i = 0; + while(i < image->width) + { + t_vec2d line = calculate_draw_start_and_end1(p->rays[i].length, image->height); + draw_column1(p->meta, line, p->rays[i].hit_side, i, image->height); + i++; + } +} + +void render_minimap(mlx_image_t *image, t_map *m) +{ + size_t cell_y; + size_t cell_x; + + cell_y = 0; + while (cell_y < m->height) + { + cell_x = 0; + while (cell_x < m->width) + { + draw_cell(image, m, cell_x, cell_y); + cell_x++; + } + cell_y++; + } +} + diff --git a/src/input.c b/src/input.c deleted file mode 100644 index bc2c233..0000000 --- a/src/input.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* input.c :+: :+: */ -/* +:+ */ -/* By: joppe +#+ */ -/* +#+ */ -/* Created: 2023/11/08 23:03:00 by joppe #+# #+# */ -/* Updated: 2023/11/13 21:10:34 by joppe ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "MLX42/MLX42.h" -#include "meta.h" -#include "vector.h" -#include -#include - -void key_hook(mlx_key_data_t keydata, void* param) -{ - t_meta *meta = param; - - if (keydata.key == MLX_KEY_ESCAPE) - mlx_close_window(meta->mlx); -} - - -void cursor_hook(double xpos, double ypos, void* param) -{ - t_meta *meta = param; - - if (mlx_is_mouse_down(meta->mlx, MLX_MOUSE_BUTTON_LEFT)) - { - t_vec2f drag = (t_vec2f){ xpos, ypos} - vec2i_to_vec2f(render_get_draw_offset()); - player_move(&meta->player, drag - meta->player.position); - } -} - diff --git a/src/main.c b/src/main.c index fa5c418..565d43d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,13 +6,12 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/07 15:28:54 by jboeve #+# #+# */ -/* Updated: 2023/12/13 16:38:28 by jboeve ######## odam.nl */ +/* Updated: 2024/01/02 17:49:04 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" -#include "vector.h" -#include +#include int main(int argc, char *argv[]) { diff --git a/src/map.c b/src/map.c deleted file mode 100644 index 5b5afa8..0000000 --- a/src/map.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* map.c :+: :+: */ -/* +:+ */ -/* By: joppe +#+ */ -/* +#+ */ -/* Created: 2023/11/12 16:49:44 by joppe #+# #+# */ -/* Updated: 2023/11/12 20:14:09 by joppe ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "vector.h" -#include "meta.h" -#include -#include - - - -static t_vec2i map_pos_to_index(t_vec2f pos) -{ - const t_vec2i player_pos = vec2f_to_vec2i(pos); - return ((t_vec2i){player_pos[VEC_X] / CELL_WIDTH, player_pos[VEC_Y] / CELL_HEIGHT}); -} - -t_cell_type map_get_cell_type(t_map *m, t_vec2f pos) -{ - t_vec2i map_index = map_pos_to_index(pos); - size_t index = (map_index[VEC_Y] * m->width) + map_index[VEC_X]; - if (index > m->width * m->height) - { - printf("index [%ld]\n", index); - UNIMPLEMENTED("Array out of bounds!"); - } - return (m->level[index]); -} diff --git a/src/parser/check_colors.c b/src/parser/check_colors.c index 0c63506..0acb1ef 100644 --- a/src/parser/check_colors.c +++ b/src/parser/check_colors.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* check_colors.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 18:57:09 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "meta.h" +#include "parser.h" // check if color code values are between 0-255 bool valid_rgb_value(char *file) @@ -71,4 +71,4 @@ bool colors_valid(char *file) skip_line(&file); } return (true); -} \ No newline at end of file +} diff --git a/src/parser/check_elements.c b/src/parser/check_elements.c index 7fd9752..2c285b4 100644 --- a/src/parser/check_elements.c +++ b/src/parser/check_elements.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* check_elements.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 18:57:17 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "meta.h" +#include "parser.h" // checks if the element abbreviations are valid bool is_valid_element(char *file) @@ -130,4 +130,4 @@ bool is_duplicate(char *file) skip_line(&file); } return (false); -} \ No newline at end of file +} diff --git a/src/parser/check_map.c b/src/parser/check_map.c index ce2344a..f5bab2c 100644 --- a/src/parser/check_map.c +++ b/src/parser/check_map.c @@ -6,11 +6,11 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/12/13 16:44:53 by jboeve ######## odam.nl */ +/* Updated: 2024/01/02 18:57:23 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "meta.h" +#include "parser.h" #include // valid chars : 1, 0, N, S, E, W @@ -54,19 +54,6 @@ int flood_fill(t_meta *meta, char *map, int x, int y) return (ret); } -// change if statements -void save_start_direction(t_meta *meta, char p) -{ - if (p == 'N') - meta->player.direction[VEC_Y] = 1; - if (p == 'S') - meta->player.direction[VEC_Y] = -1; - if (p == 'E') - meta->player.direction[VEC_X] = 1; - if (p == 'W') - meta->player.direction[VEC_X] = -1; -} - bool save_start_pos(t_meta *meta, char *map) { uint32_t x; @@ -82,9 +69,11 @@ bool save_start_pos(t_meta *meta, char *map) { if (player_pos_char(map[find_index(meta, x, y)])) { - meta->player.position[VEC_Y] = y; - meta->player.position[VEC_X] = x; - save_start_direction(meta, map[find_index(meta, x, y)]); + meta->map.player_start.x = x; + meta->map.player_start.y = y; + // meta->map.player_start_x = x; + // meta->map.player_start_y = y; + meta->map.player_start_dir = map[find_index(meta, x, y)]; found = true; } x++; @@ -152,8 +141,8 @@ int check_map(t_meta *meta, char *rect) return (EXIT_FAILURE); if (!save_start_pos(meta, rect)) return (pr_err(NO_PLAYER)); - rect[find_index(meta, meta->player.position[VEC_X], meta->player.position[VEC_Y])] = '0'; - if (flood_fill(meta, rect, meta->player.position[VEC_X], meta->player.position[VEC_Y])) + rect[find_index(meta, meta->map.player_start.x, meta->map.player_start.y)] = '0'; + if (flood_fill(meta, rect, meta->map.player_start.x, meta->map.player_start.y)) return (pr_err(INV_WALLS)); if (is_floor_exposed(meta, rect)) // maybe change to a warning? return (pr_err(OUT_OF_BOUNDS)); diff --git a/src/parser/check_walls.c b/src/parser/check_walls.c index ebc0f77..e04d236 100644 --- a/src/parser/check_walls.c +++ b/src/parser/check_walls.c @@ -1,4 +1,16 @@ -#include "meta.h" +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* check_walls.c :+: :+: */ +/* +:+ */ +/* By: joppe +#+ */ +/* +#+ */ +/* Created: 2024/01/02 18:57:27 by joppe #+# #+# */ +/* Updated: 2024/01/02 18:57:30 by joppe ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" // returns 1 if walls are not closed int check_horiz_left(t_meta *meta, char *map) diff --git a/src/parser/parse_elements.c b/src/parser/parse_elements.c index 539fe45..ecdeac3 100644 --- a/src/parser/parse_elements.c +++ b/src/parser/parse_elements.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* parse_elements.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 18:56:57 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "meta.h" +#include "parser.h" // saves path value and checks of value us empty int input_texture(t_tex *tex, char *file) @@ -83,4 +83,4 @@ int parse_elements(t_meta *meta, char *file) return (EXIT_FAILURE); free(file); return (EXIT_SUCCESS); -} \ No newline at end of file +} diff --git a/src/parser/parse_map.c b/src/parser/parse_map.c index 8201063..dad77b2 100644 --- a/src/parser/parse_map.c +++ b/src/parser/parse_map.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* parse_map.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 18:57:45 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "meta.h" +#include "parser.h" bool is_map_line(char *file) { @@ -45,6 +45,5 @@ int input_map(t_meta *meta, char *file) return (pr_err(MALL_ERR)); return (EXIT_SUCCESS); } - else - return (pr_err(MISSING_MAP), EXIT_FAILURE); -} \ No newline at end of file + return (pr_err(MISSING_MAP), EXIT_FAILURE); +} diff --git a/src/parser/parse_textures.c b/src/parser/parse_textures.c index e8d5db1..383cabf 100644 --- a/src/parser/parse_textures.c +++ b/src/parser/parse_textures.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* parse_textures.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 18:57:49 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "meta.h" +#include "parser.h" // saves the RGB values of the F and C elements // check if RGB is correct? diff --git a/src/parser/parser.c b/src/parser/parser.c index 1b0f5bb..b388f40 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -1,17 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* parser.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 18:57:56 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "meta.h" -# include "test_utils.h" +#include "parser.h" +#include "test_utils.h" char *read_file(int fd) { @@ -31,16 +31,11 @@ char *read_file(int fd) } // check if there are characters before .cub and no characters after -int map_ext(char *file) +int map_extension(char *file) { // char str[4] = {'.', 'c', 'u', 'b'}; const char *str = ".cub"; - int i; - int j; - i = 0; - j = 0; - char *dot = ft_strrchr(file, '.'); if (dot == file) @@ -50,6 +45,12 @@ int map_ext(char *file) return (EXIT_SUCCESS); } +void save_map_dimensions(char *map_file, uint32_t *width, uint32_t *height) +{ + *width = find_width(map_file); // find largest width + *height = find_height(map_file); // find height of map +} + // parse map into 1D array // index = (y * w) + x (input y and x coordinates to find index pos in array) int parser(t_meta *meta, char *map_file) @@ -58,7 +59,7 @@ int parser(t_meta *meta, char *map_file) char *file = NULL; char *rect = NULL; - if (map_ext(map_file)) // check map ext + if (map_extension(map_file)) // check map ext return(pr_err(INV_EXT), EXIT_FAILURE); fd = open(map_file, O_RDONLY); // open file if (fd == -1) @@ -68,17 +69,16 @@ int parser(t_meta *meta, char *map_file) return(pr_err(MALL_ERR)); if (parse_elements(meta, file)) return (free(file), EXIT_FAILURE); - - meta->map.width = find_width(meta->map_file); // find largest width - meta->map.height = find_height(meta->map_file); // find height of map - // w * h sized rectangle + save_map_dimensions(meta->map_file, &meta->map.width, &meta->map.height); rect = make_rect(meta->map_file, meta->map.width, meta->map.height); + // printing map for debugging + print_map(rect, meta->map.width, meta->map.height); free(meta->map_file); if (!rect) return(pr_err(MALL_ERR), EXIT_FAILURE); if (check_map(meta, rect)) return (free(rect), EXIT_FAILURE); - print_map_cell(meta->map.level, meta->map.width, meta->map.height); + printf("PLAYER DIR: %c\n", meta->map.player_start_dir); free(rect); return (EXIT_SUCCESS); } diff --git a/src/parser/utils_one.c b/src/parser/utils_one.c index b0a2192..7f30339 100644 --- a/src/parser/utils_one.c +++ b/src/parser/utils_one.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* utils_one.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 18:58:12 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "meta.h" +#include "libft.h" // skips a line on the file until '\n' char void skip_line(char **file) @@ -45,5 +45,5 @@ int valid_map_char(char c) int player_pos_char(char c) { - return (c == 'N' || c == 'S' || c == 'E' || c == 'S'); -} \ No newline at end of file + return (c == 'N' || c == 'S' || c == 'E' || c == 'W'); +} diff --git a/src/parser/utils_two.c b/src/parser/utils_two.c index 95d273a..df4240c 100644 --- a/src/parser/utils_two.c +++ b/src/parser/utils_two.c @@ -6,12 +6,11 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/12/13 16:41:33 by jboeve ######## odam.nl */ +/* Updated: 2024/01/02 18:58:36 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" -#include // func to find longest width in map uint32_t find_width(char *map) diff --git a/src/player.c b/src/player.c deleted file mode 100644 index 1455aa1..0000000 --- a/src/player.c +++ /dev/null @@ -1,79 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* player.c :+: :+: */ -/* +:+ */ -/* By: joppe +#+ */ -/* +#+ */ -/* Created: 2023/11/10 02:25:34 by joppe #+# #+# */ -/* Updated: 2023/11/18 20:45:45 by jboeve ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "MLX42/MLX42.h" -#include "libft.h" -#include "meta.h" -#include "vector.h" -#include -#include -#include -#include - - -void player_move(t_player *p, t_vec2f transform) -{ - p->position += transform; - player_look(p, 0.0); -} - -void player_look(t_player *p, double angle) -{ - const uint32_t len = 50; - - p->angle_rad = fmod(p->angle_rad + angle, 2 * PI); - if (p->angle_rad < 0) - p->angle_rad += 2 * PI; - - p->direction = vec2f_normalize(vec2f_rotate2d(p->angle_rad)); - p->beam = p->position + p->direction * (t_vec2f) {len, len}; - - player_raycast(p); -} - - -static t_ray raycast(t_map *map, t_vec2f start, t_vec2f angle, size_t depth) -{ - t_cell_type cell; - size_t len; - t_ray r; - - r.start = start; - r.end = start; - len = 0; - while (len < depth) - { - cell = map_get_cell_type(map, r.end); - r.end = r.start + angle * (t_vec2f) {len, len}; - if (cell == MAP_WALL) - break; - len++; - } - return r; -} - -// TODO Abstract out. -// Draws a line until we encounter a wall -void player_raycast(t_player *p) -{ - const size_t depth = 500; - t_vec2f dir; - size_t i; - - i = 0; - while (i < PLAYER_RAY_COUNT) - { - dir = vec2f_normalize(vec2f_rotate2d(p->angle_rad + deg_to_rad(i) - (deg_to_rad(PLAYER_RAY_COUNT) / 2))); - p->rays[i] = raycast(&p->meta->map, p->position, dir, depth); - i++; - } -} diff --git a/src/prog.c b/src/prog.c deleted file mode 100644 index eec57fa..0000000 --- a/src/prog.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "enemy.h" -#include -#include -#include - -typedef void enemy; - -enemy_t *enemy_init(const char *name); -void enemy_die(void *ptr); - - -//int main() -//{ -// enemy *e = enemy_init("sjon"); -// enemy_die(e); -//} diff --git a/src/render.c b/src/render.c deleted file mode 100644 index 19fe219..0000000 --- a/src/render.c +++ /dev/null @@ -1,129 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* render.c :+: :+: */ -/* +:+ */ -/* By: joppe +#+ */ -/* +#+ */ -/* Created: 2023/11/08 23:14:20 by joppe #+# #+# */ -/* Updated: 2023/12/13 16:07:42 by jboeve ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "meta.h" -#include "vector.h" -#include -#include -#include -#include - - -#define MAP_OFFSET_X 0 -#define MAP_OFFSET_Y 0 - -const t_vec2i DRAW_OFFSET = { - MAP_OFFSET_X, MAP_OFFSET_Y -}; - -const t_rgba CELL_COLORS[] = { - [MAP_EMPTY] = {0xff00aaff}, - [MAP_WALL] = {0x004ebaff}, - [MAP_SPACE] = {0x696969ff}, -}; - - -t_vec2i render_get_draw_offset() -{ - return (DRAW_OFFSET); -} - -void draw_cell(mlx_image_t *image, t_map *m, uint32_t cell_x, uint32_t cell_y) -{ - // const size_t x_offset = (cell_x * CELL_WIDTH) + cell_x + DRAW_OFFSET[VEC_X]; - // const size_t y_offset = (cell_y * CELL_HEIGHT) + cell_y + DRAW_OFFSET[VEC_Y]; - - const size_t x_offset = (cell_x * CELL_WIDTH) + DRAW_OFFSET[VEC_X]; - const size_t y_offset = (cell_y * CELL_HEIGHT) + DRAW_OFFSET[VEC_Y]; - - const t_cell_type cell = (m->level[(cell_y * m->width) + cell_x]); - - draw_rect(image, x_offset, y_offset, CELL_WIDTH, CELL_HEIGHT, CELL_COLORS[cell].value); -} - -// The player is essentially just a single point/pixel, around which we draw a square with the "player point" in its center. -void render_player(mlx_image_t *image, t_player *p) -{ - t_vec2i draw_pos = vec2f_to_vec2i(p->position) + DRAW_OFFSET; - draw_pos[VEC_X] -= ((float) PLAYER_WIDTH / 2); - draw_pos[VEC_Y] -= ((float) PLAYER_HEIGHT / 2); - - // Draw the player square. - draw_rect(image, draw_pos[VEC_X], draw_pos[VEC_Y], - PLAYER_WIDTH, PLAYER_HEIGHT, COLOR_PLAYER); - - size_t i = 0; - while (i < PLAYER_RAY_COUNT) - { - draw_line(image, vec2f_to_vec2i(p->rays[i].start) + DRAW_OFFSET, - vec2f_to_vec2i(p->rays[i].end) + DRAW_OFFSET, - (t_rgba) {0xFF1500FF}); - i++; - } - - // Draw the player look direction. - draw_line(image, vec2f_to_vec2i(p->position) + DRAW_OFFSET, - vec2f_to_vec2i(p->beam) + DRAW_OFFSET, - (t_rgba) {0x00FF00FF}); -} - -float ray_distance(t_ray r) -{ - return sqrtf((r.end[VEC_X] - r.start[VEC_X]) * (r.end[VEC_X] - r.start[VEC_X]) + (r.end[VEC_Y] - r.start[VEC_Y]) * (r.end[VEC_Y] - r.start[VEC_Y])); -} - -void render_player_view(mlx_image_t *image, t_player *p) -{ - size_t i = 0; - t_ray tmp; - int height = 1; - const int x_offset = PLAYER_VIEWPORT_X + 1; - - while (i < PLAYER_RAY_COUNT) - { - tmp = p->rays[i]; - height = 500 - ray_distance(tmp); - draw_rect(image, x_offset + (PLAYER_VIEWPORT_WALL_WIDTH * i), PLAYER_VIEWPORT_Y, PLAYER_VIEWPORT_WALL_WIDTH, height, 0xfc7b25ff); - i++; - } -} - -void render_map_grid(mlx_image_t *image, t_map *m) -{ - size_t cell_y; - size_t cell_x; - - cell_y = 0; - while (cell_y < m->height) - { - cell_x = 0; - while (cell_x < m->width) - { - draw_cell(image, m, cell_x, cell_y); - cell_x++; - } - cell_y++; - } -} - -void render_clear_bg(mlx_image_t *image) -{ - const uint32_t size = image->width * image->height; - size_t i; - - i = 0; - while (i < size) - { - mlx_put_pixel(image, i, 0, COLOR_BACKGROUND); - i++; - } -} diff --git a/src/test_utils.c b/src/test_utils.c index ca73fbb..1ab8ced 100644 --- a/src/test_utils.c +++ b/src/test_utils.c @@ -6,7 +6,7 @@ /* By: joppe +#+ */ /* +#+ */ /* Created: 2023/11/10 16:32:18 by joppe #+# #+# */ -/* Updated: 2023/12/13 16:24:39 by jboeve ######## odam.nl */ +/* Updated: 2023/12/20 18:34:36 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,16 +23,6 @@ const char *CELL_NAMES[] = { }; -void print_vec2f(const char *s, t_vec2f vec) -{ - printf("[%s] [%f][%f]\n",s, vec[VEC_X], vec[VEC_Y]); -} - -void print_vec2i(const char *s, t_vec2i vec) -{ - printf("[%s] [%d][%d]\n",s, vec[VEC_X], vec[VEC_Y]); -} - void print_cell(t_cell_type cell) { printf("cell [%s]\n", CELL_NAMES[cell]); @@ -66,4 +56,4 @@ void print_map_cell(t_cell_type *level, uint32_t w, uint32_t h) printf("\n"); i++; } -} \ No newline at end of file +} diff --git a/src/utils/colors.c b/src/utils/colors.c new file mode 100644 index 0000000..d86d7f6 --- /dev/null +++ b/src/utils/colors.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* colors.c :+: :+: */ +/* +:+ */ +/* By: joppe +#+ */ +/* +#+ */ +/* Created: 2023/11/08 22:35:05 by joppe #+# #+# */ +/* Updated: 2023/12/14 17:45:59 by jboeve ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "meta.h" + +int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a) +{ + return ((r << 24) | (g << 16) | (b << 8) | a); +} + +int32_t find_wall_color(t_side side) +{ + int32_t color; + + if (side == HIT_NS) + color = set_color(75, 0, 130, 255); + else + color = set_color(138, 48, 226, 255); + return (color); +} \ No newline at end of file diff --git a/src/utils/error.c b/src/utils/error.c index f4b16e0..1a0a9e1 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -1,16 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* error.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 18:56:38 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" +#include "parser.h" int pr(char *err, int exit_code) { @@ -41,4 +42,4 @@ int pr_err(t_err type) if (type >= 0 && type < 15) return (write(2, msg[type], ft_strlen(msg[type])), 1); return (0); -} \ No newline at end of file +} diff --git a/src/utils/free.c b/src/utils/free.c index b3643ee..c4c966c 100644 --- a/src/utils/free.c +++ b/src/utils/free.c @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser.c :+: :+: :+: */ +/* free.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+# #+# */ -/* Updated: 2023/11/09 18:52:24 by yzaim ### ########.fr */ +/* Updated: 2024/01/02 19:06:47 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" -void free_t_tex(t_tex *tex) +static void free_t_tex(t_tex *tex) { free(tex->no); free(tex->so); @@ -25,4 +25,4 @@ void meta_free(t_meta *meta) { free_t_tex(&(meta->tex)); free(meta->map.level); -} \ No newline at end of file +} diff --git a/src/utils/vec2d_utils.c b/src/utils/vec2d_utils.c new file mode 100644 index 0000000..06cb788 --- /dev/null +++ b/src/utils/vec2d_utils.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* vec2d_utils.c :+: :+: */ +/* +:+ */ +/* By: jboeve +#+ */ +/* +#+ */ +/* Created: 2023/12/14 18:30:23 by jboeve #+# #+# */ +/* Updated: 2024/01/02 17:43:51 by joppe ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "meta.h" +#include + +t_vec2d vec2d_add(t_vec2d v1, t_vec2d v2) +{ + t_vec2d sum; + + sum.x = v1.x + v2.x; + sum.y = v1.y + v2.y; + return (sum); +} + +t_vec2d vec2d_scalar_product(t_vec2d vec, double scalar) +{ + return ((t_vec2d){vec.x * scalar, vec.y * scalar}); +} + +void print_vec2d(char *str, t_vec2d vector) +{ + printf("%s X: %lf | Y: %lf\n", str, vector.x, vector.y); +} + +t_vec2d vec2d_rotate(t_vec2d old, double radiant) +{ + t_vec2d new; + + new.x = old.x * cos(radiant) - old.y * sin(radiant); + new.y = old.x * sin(radiant) + old.y * cos(radiant); + return (new); +} + +t_vec2d vec2u_to_vec2d(t_vec2u v) +{ + return (t_vec2d) {v.x, v.y}; +} + +double deg_to_rad(float deg) +{ + return deg * (PI / 180); +} + + +// t_vec2f vec2f_normalize(t_vec2f vec) +// { +// float m = sqrt(vec[VEC_X] * vec[VEC_X] + vec[VEC_Y] * vec[VEC_Y]); +// return (vec / m); +// } diff --git a/src/vector.c b/src/vector.c deleted file mode 100644 index 4314d9d..0000000 --- a/src/vector.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* vector.c :+: :+: */ -/* +:+ */ -/* By: joppe +#+ */ -/* +#+ */ -/* Created: 2023/11/10 02:28:35 by joppe #+# #+# */ -/* Updated: 2023/11/12 20:04:20 by joppe ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "meta.h" -#include -#include - -t_vec2i vec2f_to_vec2i(t_vec2f vec) -{ - return ((t_vec2i) {vec[VEC_X], vec[VEC_Y]}); -} - -t_vec2f vec2i_to_vec2f(t_vec2i vec) -{ - return ((t_vec2f) {vec[VEC_X], vec[VEC_Y]}); -} - -// https://en.wikipedia.org/wiki/Rotation_matrix -t_vec2f vec2f_rotate2d(float angle) -{ - return ((t_vec2f) {(cos(angle)) - (sin(angle)), (sin(angle)) + (cos(angle))}); -} - -t_vec2f vec2f_normalize(t_vec2f vec) -{ - float m = sqrt(vec[VEC_X] * vec[VEC_X] + vec[VEC_Y] * vec[VEC_Y]); - return vec / m; -} - diff --git a/test_maps/simple.cub b/test_maps/simple.cub index 68928c1..e427f64 100644 --- a/test_maps/simple.cub +++ b/test_maps/simple.cub @@ -4,8 +4,8 @@ EA path_to_east SO path_to_south F 123,234,213 C 12,34,3 -111111 -100001 -10N001 -100001 -111111 +11111 +10001 +10E01 +10001 +11111 diff --git a/test_maps/simple1.cub b/test_maps/simple1.cub new file mode 100644 index 0000000..8b2a5bb --- /dev/null +++ b/test_maps/simple1.cub @@ -0,0 +1,11 @@ +NO path_to_north +WE path_to_west +EA path_to_east +SO path_to_south +F 123,234,213 +C 12,34,3 +111111 +110001 +10N001 +100001 +111111 diff --git a/test_maps/valid.cub b/test_maps/valid.cub index c857f4b..385b9c3 100644 --- a/test_maps/valid.cub +++ b/test_maps/valid.cub @@ -10,7 +10,7 @@ C 12, 34,3 1000010001 1000110001 1000100001 -1000000001 +1000100001 100000000111 100000000001 1000000E011