From 988e058680280e25d345b17d840c3c6d40e30a76 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 10 Jan 2020 11:44:44 +0100 Subject: refactoring --- Makefile | 6 +- cub3d.h | 71 +++++++++++---------- error.c | 11 ++++ event.c | 6 +- generator | 1 - graphics.c | 186 ------------------------------------------------------- libft | 2 +- linear_algebra.c | 40 ------------ main.c | 83 +++++++++++++++---------- render.c | 152 +++++++++++++++++++++++++++++++++++++++++++++ state.c | 95 ++++++++++++++++++++++++++++ vector.c | 45 ++++++++++++++ 12 files changed, 402 insertions(+), 296 deletions(-) create mode 100644 error.c delete mode 160000 generator delete mode 100644 graphics.c delete mode 100644 linear_algebra.c create mode 100644 render.c create mode 100644 state.c create mode 100644 vector.c diff --git a/Makefile b/Makefile index e59c2a3..84a4fae 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ LIBFT_PATH = ./libft MINILIBX_PATH = ./miniLibX CC = gcc -CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -I. -Wall -Wextra -g #-Werror +CCFLAGS = -I$(LIBFT_PATH) -I$(MINILIBX_PATH) -I. -Wall -Wextra #-Werror LDFLAGS = -L$(LIBFT_PATH) -lft \ -L$(MINILIBX_PATH) -lmlx \ -framework OpenGL -framework AppKit -lm @@ -12,7 +12,7 @@ SRC = main.c event.c parse/parse.c parse/parse_east_texture.c \ parse/parse_north_texture.c parse/parse_south_texture.c \ parse/parse_west_texture.c parse/parse_ceilling_color.c \ parse/parse_floor_color.c parse/parse_resolution.c \ - parse/parse_sprite_texture.c graphics.c linear_algebra.c + parse/parse_sprite_texture.c state.c vector.c render.c OBJ = $(SRC:.c=.o) INCLUDE = cub3d.h @@ -32,6 +32,8 @@ fclean: clean libft_fclean re: fclean all +bonus: all + libft_all: make -C $(LIBFT_PATH) all diff --git a/cub3d.h b/cub3d.h index 2ea4fe8..8c5f06c 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2019/11/19 17:38:22 by cacharle ### ########.fr */ +/* Updated: 2020/01/10 11:29:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include @@ -14,6 +14,13 @@ #ifndef CUB3D_H # define CUB3D_H +# include +# include +# include +# include +# include "mlx.h" +# include "libft.h" + # define WINDOW_TITLE "cub3D" # define MLXK_ESC 53 # define MLXK_W 13 @@ -26,13 +33,6 @@ # define MLX_LITTLE_ENDIAN 0 # define MLX_BIG_ENDIAN 1 -# include -# include -# include -# include -# include "mlx.h" -# include "libft.h" - # define TRUE 1 # define FALSE 0 @@ -55,18 +55,18 @@ typedef union t_byte r; t_byte g; t_byte b; - } rgb; + } rgb; } t_color; typedef enum { - CELL_EMPTY, - CELL_WALL, - CELL_ITEM, - CELL_LOOK_NORTH, - CELL_LOOK_SOUTH, - CELL_LOOK_WEST, - CELL_LOOK_EAST + CELL_EMPTY = 1 << 1, + CELL_WALL = 1 << 2, + CELL_ITEM = 1 << 3, + CELL_LOOK_NORTH = 1 << 4, + CELL_LOOK_SOUTH = 1 << 5, + CELL_LOOK_WEST = 1 << 6, + CELL_LOOK_EAST = 1 << 7 } t_cell; typedef t_cell** t_map; @@ -114,20 +114,20 @@ typedef struct s_state t_color ceilling_color; t_color floor_color; t_image window_img; - t_image north_texture; - t_image south_texture; - t_image west_texture; - t_image east_texture; + t_image *north_texture; + t_image *south_texture; + t_image *west_texture; + t_image *east_texture; } t_state; -typedef t_bool (*t_option_parser_func)(t_parsing *parsing, char *line); - typedef struct s_option_parser { char *id; t_option_parser_func func; } t_option_parser; +typedef t_bool (*t_option_parser_func)(t_parsing *parsing, char *line); + typedef enum { SIDE_NORTH_SOUTH, @@ -161,28 +161,37 @@ t_bool parse_ceilling_color(t_parsing *parsing, char *line); ** event.c */ -int handle_key(int key, void *param); +int handle_keydown(int key, void *param); /* ** graphics.c */ -t_state *create_state(void *mlx_ptr, void *window_ptr, t_parsing *parsing); -int graphics_update(void *param); -void draw_column(t_state *state, int x); -void write_color(char *data, int y, int x, t_color color); +t_state *state_new(void *mlx_ptr, void *window_ptr, t_parsing *parsing); +void state_new_player(t_state *state, t_parsing *parsing); +void state_destroy(t_state *state); +t_image *load_texture(char *path); /* ** render.c */ +int render_update(void *param); +void render_column(t_state *state, int x); + +/* +** vector.c +*/ + +t_vector vector_add(t_vector a, t_vector b); +t_vector vector_scale(t_vector v, double scalar); +t_vector vector_rotate(t_vector v, double angle); +double vector_norm(t_vector v); /* -** linear_algebra.c +** error.c */ -t_vector vector_add(t_vector a, t_vector b); -t_vector vector_scale(t_vector v, double scalar); -t_vector vector_rotate(t_vector v, double angle); +void error_put_usage(void); #endif diff --git a/error.c b/error.c new file mode 100644 index 0000000..577597a --- /dev/null +++ b/error.c @@ -0,0 +1,11 @@ +#include "cub3d.h" + +void error_put_usage_exit(char *name) +{ + ft_putstr_fd(name, STDERR_FILENO); + ft_putendl_fd(": missing file operand", STDERR_FILENO); + ft_putstr_fd("Usage: ", STDERR_FILENO); + ft_putstr_fd(name, STDERR_FILENO); + ft_putendl_fd(" [.cub file] [--save]", STDERR_FILENO); + exit(EXIT_FAILURE); +} diff --git a/event.c b/event.c index 2ff0150..58c2eac 100644 --- a/event.c +++ b/event.c @@ -6,16 +6,16 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2019/11/18 17:49:10 by cacharle ### ########.fr */ +/* Updated: 2020/01/10 11:27:56 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" #define ROTATE_SIZE (M_PI / 20.0) -#define MOVE_SPEED 0.5 +#define MOVE_SPEED 0.25 -int handle_key(int key, void *param) +int handle_keydown(int key, void *param) { t_state *state; diff --git a/generator b/generator deleted file mode 160000 index fa840dd..0000000 --- a/generator +++ /dev/null @@ -1 +0,0 @@ -Subproject commit fa840dd345f1f23cc419afdbfe67ace4eb018cb8 diff --git a/graphics.c b/graphics.c deleted file mode 100644 index 02b326d..0000000 --- a/graphics.c +++ /dev/null @@ -1,186 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* graphics.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */ -/* Updated: 2019/11/19 17:50:51 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_state *create_state(void *mlx_ptr, void *window_ptr, t_parsing *parsing) -{ - t_state *state; - - if ((state = (t_state*)malloc(sizeof(t_state))) == NULL) - return (NULL); - state->window_img.id = mlx_new_image(mlx_ptr, parsing->resolution_width, parsing->resolution_height); - state->window_img.width = parsing->resolution_width; - state->window_img.height = parsing->resolution_height; - state->window_img.data = mlx_get_data_addr(state->window_img.id, - &state->window_img.depth, &state->window_img.size_line, &state->window_img.endian); - printf("%d\n", state->window_img.width); - printf("%d\n", state->window_img.height); - printf("%d\n", state->window_img.depth); - printf("%d\n", state->window_img.size_line); - printf("%d\n", state->window_img.endian); - - state->north_texture.id = mlx_xpm_file_to_image(mlx_ptr, - parsing->north_texture_path, &state->north_texture.width, &state->north_texture.height); - if (state->north_texture.id == NULL) - return (NULL); - state->north_texture.data = mlx_get_data_addr(state->north_texture.id, &state->north_texture.depth, - &state->north_texture.size_line, &state->north_texture.endian); - /* printf("%d\n", state->north_texture.endian); */ - /* state->south_texture = ; */ - /* state->west_texture = ; */ - /* state->east_texture = ; */ - - state->mlx_ptr = mlx_ptr; - state->window_ptr = window_ptr; - state->window_width = parsing->resolution_width; - state->window_height = parsing->resolution_height; - state->running = TRUE; - state->pos.x = 1.1; - state->pos.y = 1.1; - /* need to be normalized */ - state->dir.x = 1.0; - state->dir.y = 0.0; - state->plane.x = 0.0; - state->plane.y = 0.66; - state->map = parsing->map; - state->map_width = parsing->map_width; - state->map_height = parsing->map_height; - state->ceilling_color = parsing->ceilling_color; - state->floor_color = parsing->floor_color; - return (state); -} - -int graphics_update(void *param) -{ - int x; - t_state *state; - - state = param; - if (!state->running) - { - mlx_destroy_window(state->mlx_ptr, state->window_ptr); - exit(0); - } - mlx_clear_window(state->mlx_ptr, state->window_ptr); - x = -1; - while (++x < state->window_width) - draw_column(state, x); - - /* for (int i = 0; i < 200000; i++) */ - /* state->window_img.data[i] = 127; */ - mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window_img.id, 0, 0); - /* mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->north_texture.id, 0, 0); */ - /* for (int i = 0; i < 200; i++) */ - /* printf("%d ", state->window_img.data[i]); */ - return (0); -} - -void draw_column(t_state *state, int x) -{ - /* - * -1 0 1 - * v v v - * #################### - * # | # - * # | # - * # | # - * # | # - * # | # - * #################### - */ - double camera_x = 2 * x / (double)state->window_width - 1; - - /* camera plane length related to current column */ - t_vector ray; - ray = vector_add(state->dir, vector_scale(state->plane, camera_x)); - /* ray.x = state->dir.x + state->plane.x * camera_x; */ - /* ray.y = state->dir.y + state->plane.y * camera_x; */ - /* printf("[%f %f]\n", ray.x, ray.y); */ - - /* map pos*/ - int map_x = (int)state->pos.x; - int map_y = (int)state->pos.y; - - /* dist to first encountered wall */ - double side_dist_x; - double side_dist_y; - - /* delta between grid lines from ray percepective */ - double delta_dist_x = sqrt(1 + (SQUARE(ray.y) / SQUARE(ray.x))); //fabs(1.0 / ray.x); - double delta_dist_y = sqrt(1 + (SQUARE(ray.x) / SQUARE(ray.y))); //hfabs(1.0 / ray.y); - - /* dist to wall (perpendicular to avoid fisheye effect) */ - double perp_wall_dist; - - /* step on size for the `map_. +=` */ - int map_step_x = ray.x < 0 ? -1 : 1; - int map_step_y = ray.y < 0 ? -1 : 1; - - side_dist_x = ray.x < 0 ? state->pos.x - map_x : map_x + 1.0 - state->pos.x; - side_dist_x *= delta_dist_x; - side_dist_y = ray.y < 0 ? state->pos.y - map_y : map_y + 1.0 - state->pos.y; - side_dist_y *= delta_dist_y; - - t_side side; - while (TRUE) - { - if (side_dist_x < side_dist_y) - { - side_dist_x += delta_dist_x; /* increment real dist */ - map_x += map_step_x; /* increment map dist */ - side = SIDE_WEST_EAST; - } - else - { - side_dist_y += delta_dist_y; - map_y += map_step_y; - side = SIDE_NORTH_SOUTH; - } - if (state->map[map_y][map_x] == CELL_WALL) - break; - } - if (side == SIDE_WEST_EAST) - perp_wall_dist = ((double)map_x - state->pos.x + (map_step_x == -1 ? 1 : 0)) / ray.x; - else //if (side == SIDE_NORTH_SOUTH) - perp_wall_dist = ((double)map_y - state->pos.y + (map_step_y == -1 ? 1 : 0)) / ray.y; - /* perp_wall_dist = sqrt(SQUARE(state->pos.x - side_dist_x) + SQUARE(state->pos.y - side_dist_y)); */ - - int line_height; - /* if (perp_wall_dist <= 0) */ - /* line_height = state->window_height; */ - /* else */ - line_height = (int)(state->window_height / perp_wall_dist); - /* printf("%f\n", perp_wall_dist); */ - /* printf("%d\n", line_height); */ - int draw_start = state->window_height / 2 - line_height / 2; - if (draw_start < 0) - draw_start = 0; - int draw_end = state->window_height / 2 + line_height / 2; - if (draw_end >= state->window_height) - draw_end = state->window_height - 1; - - int tex_x; - - /* int wall_x = side == SIDE_WEST_EAST ? pos */ - - int i; - i = 0; - t_color white; - white.hexcode = 0x00ffffff; - while (i < draw_start) - ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = state->ceilling_color; - while (i < draw_end) - ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = white; - while (i < state->window_height) - ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = state->floor_color; -} diff --git a/libft b/libft index d0c146c..7fbd92d 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit d0c146c2274198814e106b17ea1f9461a1b0b81a +Subproject commit 7fbd92d957e5d30d3478b94ec0abd07aacc054bb diff --git a/linear_algebra.c b/linear_algebra.c deleted file mode 100644 index 881f8ff..0000000 --- a/linear_algebra.c +++ /dev/null @@ -1,40 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* linear_algebra.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/11/18 01:28:01 by cacharle #+# #+# */ -/* Updated: 2019/11/18 01:32:41 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cub3d.h" - -t_vector vector_add(t_vector a, t_vector b) -{ - a.x += b.x; - a.y += b.y; - return a; -} - -t_vector vector_scale(t_vector v, double scalar) -{ - v.x *= scalar; - v.y *= scalar; - return v; -} - -/* -** rotate counter clockwise -*/ - -t_vector vector_rotate(t_vector v, double angle) -{ - t_vector rotated; - - rotated.x = cos(angle) * v.x - sin(angle) * v.y; - rotated.y = sin(angle) * v.x + cos(angle) * v.y; - return (rotated); -} diff --git a/main.c b/main.c index feff126..ef2ec56 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ -/* Updated: 2019/11/19 17:09:53 by cacharle ### ########.fr */ +/* Updated: 2020/01/10 10:59:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,24 +14,61 @@ int main(int argc, char **argv) { - if (argc != 2) + t_parsing *p; + void *mlx_ptr; + void *window_ptr; + t_state *state; + + if (argc == 3 && ft_strcmp(argv[2], "--save") == 0) + return (save_image()); + else if (argc != 2) + error_put_usage_exit(argv[0]); + if ((p = parse(argv[1])) == NULL) + { + ft_putendl_fd("Error: wrong file format", STDERR_FILENO); + return (1); + } + + if ((mlx_ptr = mlx_init()) == NULL) + { + ft_putendl_fd("Error: minilibx init", STDERR_FILENO); + return (1); + } + if ((window_ptr = mlx_new_window(mlx_ptr, p->resolution_width, + p->resolution_height, WINDOW_TITLE)) == NULL) { - ft_putendl("Error"); + ft_putendl_fd("Error: minilibx window creation", STDERR_FILENO); return (1); } + if ((state = state_new(mlx_ptr, window_ptr, p)) == NULL) + { + ft_putendl_fd("Error: state creation", STDERR_FILENO); + return (1); + } + + mlx_hook(window_ptr, 2, (1L<<1), handle_keydown, (void*)state); + mlx_loop_hook(mlx_ptr, graphics_update, (void*)state); + mlx_loop(mlx_ptr); + + return (0); +} + +/* +int main(int argc, char argv) +{ t_parsing *p = parse(argv[1]); if (p == NULL) return (1); - /* printf("R %d %d\n", p->resolution_width, p->resolution_height); */ - /* printf("NO %s\n", p->north_texture_path); */ - /* printf("SO %s\n", p->south_texture_path); */ - /* printf("WE %s\n", p->west_texture_path); */ - /* printf("EA %s\n\n", p->east_texture_path); */ - /* printf("S %s\n", p->sprite_texture_path); */ - /* printf("F %d,%d,%d\n", p->floor_color.r, p->floor_color.g, p->floor_color.b); */ - /* printf("C %d,%d,%d\n\n", p->ceilling_color.r, p->ceilling_color.g, p->ceilling_color.b); */ + printf("R %d %d\n", p->resolution_width, p->resolution_height); + printf("NO %s\n", p->north_texture_path); + printf("SO %s\n", p->south_texture_path); + printf("WE %s\n", p->west_texture_path); + printf("EA %s\n\n", p->east_texture_path); + printf("S %s\n", p->sprite_texture_path); + printf("F %d,%d,%d\n", p->floor_color.r, p->floor_color.g, p->floor_color.b); + printf("C %d,%d,%d\n\n", p->ceilling_color.r, p->ceilling_color.g, p->ceilling_color.b); - /* printf("%dx%d\n", p->map_height, p->map_width); */ + printf("%dx%d\n", p->map_height, p->map_width); for (int i = 0; i < p->map_height; i++) { for (int j = 0; j < p->map_width; j++) @@ -47,24 +84,6 @@ int main(int argc, char **argv) } printf("\n"); } - - void *mlx_ptr = mlx_init(); - if (mlx_ptr == NULL) - return (1); - void *window_ptr = mlx_new_window(mlx_ptr, p->resolution_width, p->resolution_height, WINDOW_TITLE); - if (window_ptr == NULL) - return (1); - - t_state *state = create_state(mlx_ptr, window_ptr, p); - if (state == NULL) - return (1); - - /* for (int i = 0; i < 20; i++) */ - /* draw_column(state, i); */ - - mlx_key_hook(window_ptr, handle_key, (void*)state); - mlx_loop_hook(mlx_ptr, graphics_update, (void*)state); - mlx_loop(mlx_ptr); - - return (0); + return 0; } +*/ diff --git a/render.c b/render.c new file mode 100644 index 0000000..687e2e0 --- /dev/null +++ b/render.c @@ -0,0 +1,152 @@ +#include "cub3d.h" + +int render_update(void *param) +{ + int x; + t_state *state; + + state = param; + if (!state->running) + { + state_destroy(state); + exit(0); + } + mlx_clear_window(state->mlx_ptr, state->window_ptr); + x = -1; + while (++x < state->window_width) + render_column(state, x); + + /* for (int i = 0; i < 200000; i++) */ + /* state->window_img.data[i] = 127; */ + mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window_img.id, 0, 0); + /* mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->north_texture.id, 0, 0); */ + /* for (int i = 0; i < 200; i++) */ + /* printf("%d ", state->window_img.data[i]); */ + return (0); +} + +void render_column(t_state *state, int x) +{ + /* + * -1 0 1 + * v v v + * #################### + * # | # + * # | # + * # | # + * # | # + * # | # + * #################### + */ + double camera_x = 2 * x / (double)state->window_width - 1; + + /* camera plane length related to current column */ + t_vector ray; + ray = vector_add(state->dir, vector_scale(state->plane, camera_x)); + /* ray.x = state->dir.x + state->plane.x * camera_x; */ + /* ray.y = state->dir.y + state->plane.y * camera_x; */ + /* printf("[%f %f]\n", ray.x, ray.y); */ + + /* map pos*/ + int map_x = (int)state->pos.x; + int map_y = (int)state->pos.y; + + /* dist to first encountered wall */ + double side_dist_x; + double side_dist_y; + + /* delta between grid lines from ray percepective */ + double delta_dist_x = sqrt(1 + (SQUARE(ray.y) / SQUARE(ray.x))); //fabs(1.0 / ray.x); + double delta_dist_y = sqrt(1 + (SQUARE(ray.x) / SQUARE(ray.y))); //hfabs(1.0 / ray.y); + + /* dist to wall (perpendicular to avoid fisheye effect) */ + double perp_wall_dist; + + /* step on size for the `map_. +=` */ + int map_step_x = ray.x < 0 ? -1 : 1; + int map_step_y = ray.y < 0 ? -1 : 1; + + side_dist_x = ray.x < 0 ? state->pos.x - map_x : map_x + 1.0 - state->pos.x; + side_dist_x *= delta_dist_x; + side_dist_y = ray.y < 0 ? state->pos.y - map_y : map_y + 1.0 - state->pos.y; + side_dist_y *= delta_dist_y; + + t_side side; + while (TRUE) + { + if (side_dist_x < side_dist_y) + { + side_dist_x += delta_dist_x; /* increment real dist */ + map_x += map_step_x; /* increment map dist */ + side = SIDE_WEST_EAST; + } + else + { + side_dist_y += delta_dist_y; + map_y += map_step_y; + side = SIDE_NORTH_SOUTH; + } + if (state->map[map_y][map_x] == CELL_WALL) + break; + } + if (side == SIDE_WEST_EAST) + perp_wall_dist = ((double)map_x - state->pos.x + (map_step_x == -1 ? 1 : 0)) / ray.x; + else //if (side == SIDE_NORTH_SOUTH) + perp_wall_dist = ((double)map_y - state->pos.y + (map_step_y == -1 ? 1 : 0)) / ray.y; + /* perp_wall_dist = sqrt(SQUARE(state->pos.x - side_dist_x) + SQUARE(state->pos.y - side_dist_y)); */ + + int line_height; + /* if (perp_wall_dist <= 0) */ + /* line_height = state->window_height; */ + /* else */ + line_height = (int)(state->window_height / perp_wall_dist); + /* printf("%f\n", perp_wall_dist); */ + /* printf("%d\n", line_height); */ + int draw_start = state->window_height / 2 - line_height / 2; + if (draw_start < 0) + draw_start = 0; + int draw_end = state->window_height / 2 + line_height / 2; + if (draw_end >= state->window_height) + draw_end = state->window_height - 1; + + /* int tex_x; */ + /* int wall_x = side == SIDE_WEST_EAST ? pos */ + /* t_vector unit_ray = vector_scale(ray, 1.0 / vector_norm(ray)); */ + /* t_vector dist_v = vector_scale(unit_ray, side == SIDE_WEST_EAST ? side_dist_y : side_dist_x); */ + /* printf("[%f %f]\n", dist_v.x, dist_v.y); */ + /* #<{(| printf("[%f %f]\n", dist_v.x, dist_v.y); |)}># */ + /* */ + /* double mod = side == SIDE_WEST_EAST ? dist_v.y : dist_v.x; */ + /* mod = fabs(mod); */ + /* mod -= floor(mod); */ + + + /* tex_x = (int)map_range(side == SIDE_WEST_EAST ? dist_v.y : dist_v.x, 0, 1, 0, state->north_texture.width); */ + /* tex_x = side = SIDE_WEST_EAST ? side_dist_y : side_dist_x) */ + /* printf("%d\n", tex_x); */ + + /* double wall_x; */ + /* if (side == SIDE_WEST_EAST) */ + /* wall_x = pos_y + prep_wall_dist * ray.y; */ + /* else */ + /* wall_x = pos_x + prep_wall_dist * ray.x; */ + /* wall_x -= floor(wall_x); */ + + int i; + i = 0; + t_color white; + white.hexcode = 0x00ffffff; + while (i < draw_start) + ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = state->ceilling_color; + while (i < draw_end) + ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = white; + while (i < state->window_height) + ((t_color*)state->window_img.data)[i++ * state->window_img.width + x] = state->floor_color; +} + +/* double map_range(double x, double src_lo, double src_hi, double dest_lo, double dest_hi) */ +/* { */ +/* double src_len = src_hi - src_lo; */ +/* double dest_len = dest_hi - dest_lo; */ +/* return ((x - src_lo) / src_len) * dest_len + dest_lo; */ +/* } */ diff --git a/state.c b/state.c new file mode 100644 index 0000000..eb9b3eb --- /dev/null +++ b/state.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* graphics.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */ +/* Updated: 2020/01/10 11:44:24 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_state *state_new(void *mlx_ptr, void *window_ptr, t_parsing *parsing) +{ + t_state *state; + + if ((state = (t_state*)malloc(sizeof(t_state))) == NULL) + return (NULL); + if ((state->north_texture = load_texture(parsing->north_texture_path)) == NULL) + return (NULL); + if ((state->south_texture = load_texture(parsing->south_texture_path)) == NULL) + return (NULL); + if ((state->west_texture = load_texture(parsing->west_texture_path)) == NULL) + return (NULL); + if ((state->east_texture = load_texture(parsing->east_texture_path)) == NULL) + return (NULL); + + state->window_img.id = mlx_new_image(mlx_ptr, parsing->resolution_width, parsing->resolution_height); + state->window_img.width = parsing->resolution_width; + state->window_img.height = parsing->resolution_height; + state->window_img.data = mlx_get_data_addr(state->window_img.id, + &state->window_img.depth, &state->window_img.size_line, &state->window_img.endian); + + state->running = TRUE; + state->mlx_ptr = mlx_ptr; + state->window_ptr = window_ptr; + state->window_width = parsing->resolution_width; + state->window_height = parsing->resolution_height; + state->map = parsing->map; + state->map_width = parsing->map_width; + state->map_height = parsing->map_height; + state->ceilling_color = parsing->ceilling_color; + state->floor_color = parsing->floor_color; + + state_new_player(state, parsing); + + return (state); +} + +void state_new_player(t_state *state, t_parsing *parsing) +{ + int i; + int j; + + i = -1; + while (++i < parsing->map_height) + { + j = -1; + while (++j < parsing->map_width) + if (parsing->map[i][j] & (CELL_LOOK_NORTH | CELL_LOOK_SOUTH + | CELL_LOOK_WEST | CELL_LOOK_EAST) + { + state->pos.x = (double)j + 0.5; + state->pos.y = (double)i + 0.5; + // break 2nd loop? + break; + } + } + /* need to be normalized */ + state->dir.x = 0.0; + state->dir.y = 0.0; + if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_NORTH) + state->dir.y = 1.0; + else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_SOUTH) + state->dir.y = -1.0; + else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_WEST) + state->dir.x = -1.0; + else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_EAST) + state->dir.x = 1.0; + + state->plane.x = 0.0; + state->plane.y = 0.66; +} + +void state_destroy(t_state *state) +{ + mlx_destroy_window(state->mlx_ptr, state->window_ptr); + free(state->north_texture); + free(state->south_texture); + free(state->west_texture); + free(state->east_texture); + free(state); +} diff --git a/vector.c b/vector.c new file mode 100644 index 0000000..babaf12 --- /dev/null +++ b/vector.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* linear_algebra.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/18 01:28:01 by cacharle #+# #+# */ +/* Updated: 2019/11/19 18:21:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_vector vector_add(t_vector a, t_vector b) +{ + a.x += b.x; + a.y += b.y; + return a; +} + +t_vector vector_scale(t_vector v, double scalar) +{ + v.x *= scalar; + v.y *= scalar; + return v; +} + +/* +** rotate counter clockwise +*/ + +t_vector vector_rotate(t_vector v, double angle) +{ + t_vector rotated; + + rotated.x = cos(angle) * v.x - sin(angle) * v.y; + rotated.y = sin(angle) * v.x + cos(angle) * v.y; + return (rotated); +} + +double vector_norm(t_vector v) +{ + return sqrt(SQUARE(v.x) + SQUARE(v.y)); +} -- cgit