From 542ae0113ac850e0d5216c7e2dc4543e66a1237f Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 18 Nov 2019 02:56:38 +0100 Subject: Added rotation, mlx_clear_window feelsgoodman --- Makefile | 2 +- cub3d.h | 28 ++++++++++++++---- event.c | 17 +++++++---- graphics.c | 68 ++++++++++++++++---------------------------- linear_algebra.c | 40 ++++++++++++++++++++++++++ parse/parse_ceilling_color.c | 8 +++--- parse/parse_floor_color.c | 8 +++--- 7 files changed, 108 insertions(+), 63 deletions(-) create mode 100644 linear_algebra.c diff --git a/Makefile b/Makefile index 18e2181..f529a12 100644 --- a/Makefile +++ b/Makefile @@ -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 + parse/parse_sprite_texture.c graphics.c linear_algebra.c OBJ = $(SRC:.c=.o) INCLUDE = cub3d.h diff --git a/cub3d.h b/cub3d.h index eded199..5d089bf 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/16 12:51:46 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 02:43:17 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include @@ -15,11 +15,13 @@ # define CUB3D_H # define WINDOW_TITLE "cub3D" +# define MLXK_ESC 53 # define MLXK_W 13 # define MLXK_A 0 # define MLXK_S 1 # define MLXK_D 2 -# define MLXK_ESC 53 +# define MLXK_LEFT 123 +# define MLXK_RIGHT 124 # include # include @@ -31,6 +33,8 @@ # define TRUE 1 # define FALSE 0 +# define SQUARE(x) ((x) * (x)) + typedef int t_bool; typedef struct @@ -41,9 +45,14 @@ typedef struct typedef struct { - int r; - int g; - int b; + int hexcode; + struct + { + t_byte b; + t_byte g; + t_byte r; + t_byte empty; + } rgb; } t_color; typedef enum @@ -88,6 +97,8 @@ typedef struct s_state t_map map; int map_width; int map_height; + t_color ceilling_color; + t_color floor_color; } t_state; typedef t_bool (*t_option_parser_func)(t_parsing *parsing, char *line); @@ -142,5 +153,12 @@ t_vector vector_scale(t_vector v, double scalar); */ +/* +** linear_algebra.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); #endif diff --git a/event.c b/event.c index 0d6c113..ab3d12b 100644 --- a/event.c +++ b/event.c @@ -6,12 +6,15 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2019/11/16 12:44:15 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 02:31:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" +#define ROTATE_SIZE (M_PI / 20.0) +#define MOVE_SPEED 0.5 + int handle_key(int key, void *param) { t_state *state; @@ -21,12 +24,16 @@ int handle_key(int key, void *param) if (key == MLXK_ESC) state->running = FALSE; if (key == MLXK_A) - state->pos.x -= 0.5; + state->pos = vector_add(state->pos, vector_rotate(vector_scale(state->dir, MOVE_SPEED), M_PI_2)); if (key == MLXK_D) - state->pos.x += 0.5; + state->pos = vector_add(state->pos, vector_rotate(vector_scale(state->dir, MOVE_SPEED), -M_PI_2)); if (key == MLXK_W) - state->pos.y -= 0.5; + state->pos = vector_add(state->pos, vector_scale(state->dir, MOVE_SPEED)); if (key == MLXK_S) - state->pos.y += 0.5; + state->pos = vector_add(state->pos, vector_scale(state->dir, -MOVE_SPEED)); + if (key == MLXK_LEFT) + state->dir = vector_rotate(state->dir, -ROTATE_SIZE); + if (key == MLXK_RIGHT) + state->dir = vector_rotate(state->dir, ROTATE_SIZE); return (0); } diff --git a/graphics.c b/graphics.c index 2f4ea40..821e891 100644 --- a/graphics.c +++ b/graphics.c @@ -14,12 +14,14 @@ t_state *create_state(void *mlx_ptr, void *window_ptr, t_parsing *parsing) state->pos.x = 1.0; state->pos.y = 1.0; state->dir.x = 1.0; - state->dir.y = 0.0; + state->dir.y = 1.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); } @@ -44,6 +46,7 @@ int graphics_update(void *param) /* for (int j = 0; j < 4; j++) */ /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x + x2 + i - 2,y + y2 + j - 2, 0x00ffff00); */ + mlx_clear_window(state->mlx_ptr, state->window_ptr); int x; x = -1; @@ -85,13 +88,13 @@ void draw_column(t_state *state, int x) int map_x = (int)state->pos.x; int map_y = (int)state->pos.y; - /* dist to first encounter wall */ + /* dist to first encountered wall */ double side_dist_x; double side_dist_y; /* delta between grid lines from ray percepective */ - double delta_dist_x = fabs(1.0 / ray.x); - double delta_dist_y = fabs(1.0 / ray.y); + 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; @@ -100,24 +103,18 @@ void draw_column(t_state *state, int x) int map_step_x = ray.x < 0 ? -1 : 1; int map_step_y = ray.y < 0 ? -1 : 1; - if (ray.x < 0) - side_dist_x = (state->pos.x - map_x) * delta_dist_x; - else - side_dist_x = (map_x + 1.0 - state->pos.x) * delta_dist_x; - - if (ray.y < 0) - side_dist_y = (state->pos.y - map_y) * delta_dist_y; - else - side_dist_y = (map_y + 1.0 - state->pos.y) * delta_dist_y; + 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; - int hit = FALSE; - while (!hit) + 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 */ + map_x += map_step_x; /* increment map dist */ side = SIDE_WEST_EAST; } else @@ -134,35 +131,18 @@ void draw_column(t_state *state, int x) else //if (side == SIDE_NORTH_SOUTH) perp_wall_dist = (map_y - state->pos.y + (1 - map_step_y) / 2) / ray.y; - int line_height = (int)(state->window_height / perp_wall_dist); - int draw_start = -line_height / 2 + state->window_height / 2; - if (draw_start < 0) - draw_start = 0; - int draw_end = line_height / 2 + state->window_height / 2; - if (draw_end >= state->window_height) - draw_end = state->window_height - 1; - + 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; */ + + /* for (int i = 0; i < draw_start; i++) */ + /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i, state->ceilling_color.hexcode); */ for (int i = draw_start; i < draw_end; i++) mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i, 0x00ffffff); + /* for (int i = draw_end; i < state->window_height; i++) */ + /* mlx_pixel_put(state->mlx_ptr, state->window_ptr, x, i, state->floor_color.hexcode); */ } - -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; -} - - -/* sqrt( x^2 + y^2 ) / x */ - - - diff --git a/linear_algebra.c b/linear_algebra.c new file mode 100644 index 0000000..881f8ff --- /dev/null +++ b/linear_algebra.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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/parse/parse_ceilling_color.c b/parse/parse_ceilling_color.c index 67291d0..a451f94 100644 --- a/parse/parse_ceilling_color.c +++ b/parse/parse_ceilling_color.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:31:32 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:31:37 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 02:43:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,10 +15,10 @@ t_bool parse_ceilling_color(t_parsing *parsing, char *line) { line++; - parsing->ceilling_color.r = ft_atoi(line); + parsing->ceilling_color.rgb.r = ft_atoi(line); line = ft_strchr(line, ',') + 1; - parsing->ceilling_color.g = ft_atoi(line); + parsing->ceilling_color.rgb.g = ft_atoi(line); line = ft_strchr(line, ',') + 1; - parsing->ceilling_color.b = ft_atoi(line); + parsing->ceilling_color.rgb.b = ft_atoi(line); return (TRUE); } diff --git a/parse/parse_floor_color.c b/parse/parse_floor_color.c index ef2204a..3885586 100644 --- a/parse/parse_floor_color.c +++ b/parse/parse_floor_color.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:32 by cacharle #+# #+# */ -/* Updated: 2019/11/15 09:31:16 by cacharle ### ########.fr */ +/* Updated: 2019/11/18 02:43:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,10 +15,10 @@ t_bool parse_floor_color(t_parsing *parsing, char *line) { line++; - parsing->floor_color.r = ft_atoi(line); + parsing->floor_color.rgb.r = ft_atoi(line); line = ft_strchr(line, ',') + 1; - parsing->floor_color.g = ft_atoi(line); + parsing->floor_color.rgb.g = ft_atoi(line); line = ft_strchr(line, ',') + 1; - parsing->floor_color.b = ft_atoi(line); + parsing->floor_color.rgb.b = ft_atoi(line); return (TRUE); } -- cgit