diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-11 13:43:04 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-12 15:45:52 +0100 |
| commit | a0c774e4963945b9a0d94e5b2cc7420960741a8b (patch) | |
| tree | c26c92eda37c03a1016a36724fad3f955373a7a0 | |
| parent | 2fdb9ad1d66e28e056f31bc6321d2c198db33290 (diff) | |
| download | cub3d-a0c774e4963945b9a0d94e5b2cc7420960741a8b.tar.gz cub3d-a0c774e4963945b9a0d94e5b2cc7420960741a8b.tar.bz2 cub3d-a0c774e4963945b9a0d94e5b2cc7420960741a8b.zip | |
Refactoring render.c
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | capture.c | 25 | ||||
| -rw-r--r-- | cub3d.h | 24 | ||||
| -rw-r--r-- | error.c | 17 | ||||
| -rw-r--r-- | event.c | 32 | ||||
| -rw-r--r-- | helper.c | 142 | ||||
| -rw-r--r-- | main.c | 20 | ||||
| -rw-r--r-- | minilibx/Makefile (renamed from miniLibX/Makefile) | 0 | ||||
| -rw-r--r-- | minilibx/font.c (renamed from miniLibX/font.c) | 0 | ||||
| -rw-r--r-- | minilibx/font.xcf (renamed from miniLibX/font.xcf) | bin | 52570 -> 52570 bytes | |||
| -rw-r--r-- | minilibx/mlx.h (renamed from miniLibX/mlx.h) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_init_loop.m (renamed from miniLibX/mlx_init_loop.m) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_int.h (renamed from miniLibX/mlx_int.h) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_int_str_to_wordtab.c (renamed from miniLibX/mlx_int_str_to_wordtab.c) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_mouse.m (renamed from miniLibX/mlx_mouse.m) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_new_image.m (renamed from miniLibX/mlx_new_image.m) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_new_window.h (renamed from miniLibX/mlx_new_window.h) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_new_window.m (renamed from miniLibX/mlx_new_window.m) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_opengl.h (renamed from miniLibX/mlx_opengl.h) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_opengl.m (renamed from miniLibX/mlx_opengl.m) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_png.c (renamed from miniLibX/mlx_png.c) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_png.h (renamed from miniLibX/mlx_png.h) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_rgb.c (renamed from miniLibX/mlx_rgb.c) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_shaders.c (renamed from miniLibX/mlx_shaders.c) | 0 | ||||
| -rw-r--r-- | minilibx/mlx_xpm.c (renamed from miniLibX/mlx_xpm.c) | 0 | ||||
| -rw-r--r-- | minimalist.cub | 4 | ||||
| -rw-r--r-- | render.c | 173 | ||||
| -rw-r--r-- | state.c | 29 | ||||
| -rw-r--r-- | vector.c | 40 |
29 files changed, 324 insertions, 185 deletions
@@ -19,7 +19,8 @@ SRC = main.c \ vector.c \ render.c \ helper.c \ - error.c + error.c \ + capture.c OBJ = $(SRC:.c=.o) INCLUDE = cub3d.h diff --git a/capture.c b/capture.c new file mode 100644 index 0000000..e1b14b8 --- /dev/null +++ b/capture.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* capture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 13:15:11 by cacharle #+# #+# */ +/* Updated: 2020/01/11 13:32:54 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int capture(t_state *state) +{ + render_update_window(state); + write_bmp(&state->window); + return (0); +} + +void write_bmp(t_image *image) +{ + +} @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2020/01/11 12:54:44 by cacharle ### ########.fr */ +/* Updated: 2020/01/12 15:35:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdio.h> @@ -51,12 +51,12 @@ typedef union unsigned int hexcode; struct { + t_byte empty; t_byte r; t_byte g; t_byte b; - t_byte alpha; } rgb; -} t_color; +} t_color; typedef enum { @@ -85,7 +85,7 @@ enum typedef struct { int width; - int height; + int height; void *id; char *data; int depth; @@ -123,8 +123,8 @@ typedef struct s_option_parser typedef enum { - SIDE_NORTH_SOUTH, - SIDE_WEST_EAST + SIDE_NS, + SIDE_WE } t_side; /* @@ -187,6 +187,7 @@ void load_texture(void *mlx_ptr, t_image *image, char *path); */ int render_update(void *param); +void render_update_window(t_state *state); void render_column(t_state *state, int x); /* @@ -206,7 +207,8 @@ void error_put_usage_exit(char *name); void error_put(char *message); void *error_put_return(char *message); void *error_put_return_state_destroy(char *message, t_state *state); -void *error_put_return_lines_state_destroy(char *message, t_state *state, char **lines); +void *error_put_return_lines_state_destroy( + char *message, t_state *state, char **lines); /* ** helper.c @@ -214,5 +216,13 @@ void *error_put_return_lines_state_destroy(char *message, t_state *state, char t_bool helper_is_player_cell(t_cell cell); void helper_free_splited(char **splited); +void helper_rotate_player(t_state *state, double rotation); + +/* +** capture.c +*/ + +int capture(t_state *state); +void write_bmp(t_image *image); #endif @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 13:33:14 by cacharle #+# #+# */ +/* Updated: 2020/01/11 13:34:18 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" void error_put_usage_exit(char *name) @@ -25,10 +37,11 @@ void *error_put_return(char *message) void *error_put_return_state_destroy(char *message, t_state *state) { state_destroy(state); - return error_put_return(message); + return (error_put_return(message)); } -void *error_put_return_lines_state_destroy(char *message, t_state *state, char **lines) +void *error_put_return_lines_state_destroy( + char *message, t_state *state, char **lines) { helper_free_splited(lines); return (error_put_return_state_destroy(message, state)); @@ -6,13 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2020/01/11 10:10:05 by cacharle ### ########.fr */ +/* Updated: 2020/01/12 09:27:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -#define ROTATE_SIZE (M_PI / 20.0) +#define ROTATE_STEP (M_PI / 20.0) #define MOVE_SPEED 0.25 int event_keydown(int key, void *param) @@ -22,17 +22,21 @@ int event_keydown(int key, void *param) state = (t_state*)param; if (key == MLXK_ESC) state->running = FALSE; - if (key == MLXK_A) - state->pos = vector_add(state->pos, vector_scale(vector_rotate(state->dir, M_PI_2), MOVE_SPEED)); - if (key == MLXK_D) - state->pos = vector_add(state->pos, vector_scale(vector_rotate(state->dir, -M_PI_2), MOVE_SPEED)); - if (key == MLXK_W) - state->pos = vector_add(state->pos, vector_scale(state->dir, MOVE_SPEED)); - if (key == MLXK_S) - 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); + else if (key == MLXK_A) + state->pos = vector_add(state->pos, + vector_scale(vector_rotate(state->dir, M_PI_2), MOVE_SPEED)); + else if (key == MLXK_D) + state->pos = vector_add(state->pos, + vector_scale(vector_rotate(state->dir, -M_PI_2), MOVE_SPEED)); + else if (key == MLXK_W) + state->pos = vector_add(state->pos, + vector_scale(state->dir, MOVE_SPEED)); + else if (key == MLXK_S) + state->pos = vector_add(state->pos, + vector_scale(state->dir, -MOVE_SPEED)); + else if (key == MLXK_LEFT) + helper_rotate_player(state, -ROTATE_STEP); + else if (key == MLXK_RIGHT) + helper_rotate_player(state, ROTATE_STEP); return (0); } @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 07:32:20 by cacharle #+# #+# */ -/* Updated: 2020/01/11 11:22:15 by cacharle ### ########.fr */ +/* Updated: 2020/01/12 15:43:29 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,3 +29,143 @@ void helper_free_splited(char **splited) free(splited[i]); free(splited); } + +void helper_rotate_player(t_state *state, double rotation) +{ + state->dir = vector_rotate(state->dir, rotation); + state->plane = vector_rotate(state->plane, rotation); +} + +void helper_init_dir_plane(t_state *state, int y, int x) +{ + if (state->map[y][x] == CELL_LOOK_NORTH) + state->dir.y = 1.0; + else if (state->map[y][x] == CELL_LOOK_SOUTH) + state->dir.y = -1.0; + else if (state->map[y][x] == CELL_LOOK_WEST) + state->dir.x = -1.0; + else if (state->map[y][x] == CELL_LOOK_EAST) + state->dir.x = 1.0; + state->plane = vector_rotate(state->dir, M_PI_2); + state->plane = vector_scale(state->plane, 1.0 / vector_norm(state->plane)); + state->plane = vector_scale(state->plane, 0.66); + state->plane = vector_apply(state->plane, &fabs); +} + +/* +** -1 0 1 <-- camera_x +** v v v +** ################ +** # | # <-- screen +** # | # +** ################ +** +** camera_x is the x column from the camera percpective +** scaling the plane vector and adding it to the direction vector +** to create a vector in the *direction* of the column x. +*/ + +t_vector get_ray(t_state *state, int x) +{ + double camera_x; + + camera_x = 2 * x / (double)state->window.width - 1; + return (vector_add(state->dir, vector_scale(state->plane, camera_x))); +} + +/* +** delta between each grid unit form the vector percpective +** +** This can be simplified like (for some obscure reason): +** delta.x = fabs(1.0 / ray.x); +** delta.y = fabs(1.0 / ray.y); +*/ + +t_vector get_delta(t_vector ray) +{ + t_vector delta; + + delta.x = sqrt(1.0 + (SQUARE(ray.y) / SQUARE(ray.x))); + delta.y = sqrt(1.0 + (SQUARE(ray.x) / SQUARE(ray.y))); + return (delta); +} + +/* +** first delta between player position and first grid unit +** +** if (ray.x < 0) +** current.x = state->pos.x - map_pos.x; +** else +** current.x = fabs(state->pos.x - map_pos.x + 1.0); +** if (ray.y < 0) +** current.y = state->pos.y - map_pos.y; +** else +** current.y = fabs(state->pos.y - map_pos.y + 1.0); +** +** current x and y are the perpendicular distance to the nearest wall, +** we multiply them by their corresponding delta. +** 0 <= perpendicular distance <= 1 is a ratio, how much of the full delta we need to take. +** +** current.x *= delta.x; +** current.y *= delta.y; +*/ + +t_vector get_init_delta(t_state *state, t_vector *ray, t_vector *map_pos, t_vector *delta) +{ + t_vector current; + + current = vector_apply(&fabs, VECTOR_SUB(state->pos, *map_pos)); + if (ray.x > 0) + current.x += 1.0; + if (ray.y > 0) + current.y += 1.0; + current.x *= delta.x; + current.y *= delta.y; + return (current); +} + +t_vector get_map_step(t_vector *ray) +{ + return (vector_new(ray.x < 0 ? -1 : 1, + ray.y < 0 ? -1 : 1)); +} + +t_vector get_perp_dist(t_state *state, t_vector *ray, t_vector *map_pos) +{ + +} + +int get_line_height(t_state *state, int prep_dist, t_side side) +{ + int line_height; + + if (perp_wall_dist <= 0) + line_height = state->window_height; + else + line_height = (int)(state->window.height / perp_wall_dist); + return (line_height); +} + +int get_perp_wall_dist(t_state *state, t_vector *map_pos, + t_vector *ray, int map_step_x, int map_step_y) +{ + 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; +} + +void helper_ray_next(t_vector *current, t_vector *map_pos, + t_vector delta, t_vector map_step) +{ + if (current->x < current->y) + { + current->x += delta.x; + map_pos->x += map_step.x; + } + else + { + current->y += delta.y; + map_pos->y += map_step.y; + } +} @@ -6,33 +6,30 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ -/* Updated: 2020/01/11 13:04:38 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 13:27:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -/* + int main(int argc, char **argv) { t_state *state; - if (argc == 3 && ft_strcmp(argv[2], "--save") == 0) - return (save_image()); - else - if (argc != 2) + if (argc != 2 && argc != 3 && ft_strcmp(argv[2], "--save")) error_put_usage_exit(argv[0]); - if ((state = state_new(parse(argv[1]))) == NULL) - { - ft_putendl_fd("Error: state creation", STDERR_FILENO); + if ((state = state_new(parse_check(parse(argv[1])))) == NULL) return (1); - } + if (argc == 3) + return (capture(state)); mlx_hook(state->window_ptr, 2, (1L << 1), event_keydown, (void*)state); mlx_loop_hook(state->mlx_ptr, render_update, (void*)state); mlx_loop(state->mlx_ptr); return (0); -}*/ +} +/* int main(int argc, char **argv) { (void)argc; @@ -74,3 +71,4 @@ int main(int argc, char **argv) state_destroy(s); return 0; } +*/ diff --git a/miniLibX/Makefile b/minilibx/Makefile index 2e44836..2e44836 100644 --- a/miniLibX/Makefile +++ b/minilibx/Makefile diff --git a/miniLibX/font.c b/minilibx/font.c index 656d248..656d248 100644 --- a/miniLibX/font.c +++ b/minilibx/font.c diff --git a/miniLibX/font.xcf b/minilibx/font.xcf Binary files differindex b9c8c82..b9c8c82 100644 --- a/miniLibX/font.xcf +++ b/minilibx/font.xcf diff --git a/miniLibX/mlx.h b/minilibx/mlx.h index e75b4af..e75b4af 100644 --- a/miniLibX/mlx.h +++ b/minilibx/mlx.h diff --git a/miniLibX/mlx_init_loop.m b/minilibx/mlx_init_loop.m index 0377771..0377771 100644 --- a/miniLibX/mlx_init_loop.m +++ b/minilibx/mlx_init_loop.m diff --git a/miniLibX/mlx_int.h b/minilibx/mlx_int.h index a93c1c9..a93c1c9 100644 --- a/miniLibX/mlx_int.h +++ b/minilibx/mlx_int.h diff --git a/miniLibX/mlx_int_str_to_wordtab.c b/minilibx/mlx_int_str_to_wordtab.c index 3b2ef2e..3b2ef2e 100644 --- a/miniLibX/mlx_int_str_to_wordtab.c +++ b/minilibx/mlx_int_str_to_wordtab.c diff --git a/miniLibX/mlx_mouse.m b/minilibx/mlx_mouse.m index 94ce0a9..94ce0a9 100644 --- a/miniLibX/mlx_mouse.m +++ b/minilibx/mlx_mouse.m diff --git a/miniLibX/mlx_new_image.m b/minilibx/mlx_new_image.m index 46e1df1..46e1df1 100644 --- a/miniLibX/mlx_new_image.m +++ b/minilibx/mlx_new_image.m diff --git a/miniLibX/mlx_new_window.h b/minilibx/mlx_new_window.h index d173e61..d173e61 100644 --- a/miniLibX/mlx_new_window.h +++ b/minilibx/mlx_new_window.h diff --git a/miniLibX/mlx_new_window.m b/minilibx/mlx_new_window.m index 6d4fea4..6d4fea4 100644 --- a/miniLibX/mlx_new_window.m +++ b/minilibx/mlx_new_window.m diff --git a/miniLibX/mlx_opengl.h b/minilibx/mlx_opengl.h index bb39267..bb39267 100644 --- a/miniLibX/mlx_opengl.h +++ b/minilibx/mlx_opengl.h diff --git a/miniLibX/mlx_opengl.m b/minilibx/mlx_opengl.m index 554d79b..554d79b 100644 --- a/miniLibX/mlx_opengl.m +++ b/minilibx/mlx_opengl.m diff --git a/miniLibX/mlx_png.c b/minilibx/mlx_png.c index ca8024d..ca8024d 100644 --- a/miniLibX/mlx_png.c +++ b/minilibx/mlx_png.c diff --git a/miniLibX/mlx_png.h b/minilibx/mlx_png.h index 83ef145..83ef145 100644 --- a/miniLibX/mlx_png.h +++ b/minilibx/mlx_png.h diff --git a/miniLibX/mlx_rgb.c b/minilibx/mlx_rgb.c index 3f98717..3f98717 100644 --- a/miniLibX/mlx_rgb.c +++ b/minilibx/mlx_rgb.c diff --git a/miniLibX/mlx_shaders.c b/minilibx/mlx_shaders.c index 4a58955..4a58955 100644 --- a/miniLibX/mlx_shaders.c +++ b/minilibx/mlx_shaders.c diff --git a/miniLibX/mlx_xpm.c b/minilibx/mlx_xpm.c index 78d8c9b..78d8c9b 100644 --- a/miniLibX/mlx_xpm.c +++ b/minilibx/mlx_xpm.c diff --git a/minimalist.cub b/minimalist.cub index 2272c1b..863c37d 100644 --- a/minimalist.cub +++ b/minimalist.cub @@ -19,6 +19,6 @@ C 0,225,0 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1 -1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 N 0 1 -1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 N 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 @@ -1,8 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 13:37:17 by cacharle #+# #+# */ +/* Updated: 2020/01/12 15:45:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "cub3d.h" -int render_update(void *param) +#define WINDOW_MID_HEIGHT(state) (state->window.height / 2) + +int render_update(void *param) { - int x; t_state *state; state = param; @@ -11,136 +24,56 @@ int render_update(void *param) state_destroy(state); exit(EXIT_SUCCESS); } - mlx_clear_window(state->mlx_ptr, state->window_ptr); - x = -1; - while (++x < state->window.width) - render_column(state, x); + render_update_window(state); mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0); return (0); } -void render_column(t_state *state, int x) +void render_update_window(t_state *state) { - /* - * -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; + int x; - 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; + x = -1; + while (++x < state->window.width) + render_column(state, x); +} - t_side side; +void render_column(t_state *state, int x) +{ + t_vector ray; + t_vector map_pos; + t_vector delta; + t_side side; + int line_height; + + ray = get_ray(state, x); + map_pos = vector_new((double)((int)state->pos.x), (double)((int)state->pos.y)); + delta = get_delta(ray); + current = get_init_delta(state, ray, map_pos, delta); + map_step = get_map_step(&ray); 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) + side = current.x < current.y ? SIDE_WE : SIDE_WE; + helper_ray_next(¤t, map_pos, delta, map_step); + if (state->map[(int)map_pos.y][(int)map_pos.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); */ + line_height = get_line_height(state, get_perp_dist(), side); + render_window_column(state, x, WINDOW_MID_HEIGHT(state) - line_height / 2, + WINDOW_MID_HEIGHT(state) + line_height / 2); +} - /* 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); */ +void render_window_column(t_state *state, int x, int start, int end) +{ + int i; + t_color white; - int i; - i = 0; - t_color white; white.hexcode = 0x00ffffff; - while (i < draw_start) - ((t_color*)state->window.data)[i++ * state->window.width + x] = state->ceilling_color; - while (i < draw_end) - ((t_color*)state->window.data)[i++ * state->window.width + x] = white; - while (i < state->window_height) - ((t_color*)state->window.data)[i++ * state->window.width + x] = state->floor_color; + i = -1; + while (++i < draw_start) + ((t_color*)state->window.data)[i * state->window.width + x] = state->ceilling_color; + while (++i < draw_end) + ((t_color*)state->window.data)[i * state->window.width + x] = white; + while (++i < state->window_height) + ((t_color*)state->window.data)[i * state->window.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; */ -/* } */ @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */ -/* Updated: 2020/01/11 13:10:45 by cacharle ### ########.fr */ +/* Updated: 2020/01/12 10:37:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,8 @@ t_state *state_new(t_state *state) i = -1; while (++i < TEXTURES_NUM) { - load_texture(state->mlx_ptr, &state->textures[i], state->textures_path[i]); + load_texture(state->mlx_ptr, &state->textures[i], + state->textures_path[i]); if (state->textures[i].id == NULL) return (error_put_return_state_destroy("load texture", state)); } @@ -37,8 +38,14 @@ t_state *state_new(t_state *state) state->window.width, state->window.height)) == NULL) return (state_destroy(state)); state->window.data = mlx_get_data_addr(state->window.id, - &state->window.depth, &state->window.size_line, &state->window.endian); + &state->window.depth, &state->window.size_line, + &state->window.endian); state_init_player(state); + /* printf("%x\n", state->floor_color.rgb.r); */ + /* printf("%x\n", state->floor_color.rgb.g); */ + /* printf("%x\n", state->floor_color.rgb.b); */ + /* printf("endian %d", state->window.endian); */ + /* return NULL; */ return (state); } @@ -56,21 +63,9 @@ void state_init_player(t_state *state) { state->pos.x = (double)j + 0.5; state->pos.y = (double)i + 0.5; - // break 2nd loop? - break; } } - /* need to be normalized */ - if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_NORTH) - state->dir.y = 1.0; - else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_SOUTH) - state->dir.y = -1.0; - else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_WEST) - state->dir.x = -1.0; - else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_EAST) - state->dir.x = 1.0; - state->plane.x = 0.0; - state->plane.y = 0.66; + helper_init_dir_plane(state, (int)state->pos.y, (int)state->pos.x); } t_state *state_new_empty(void) @@ -106,7 +101,7 @@ void *state_destroy(t_state *state) while (++i < TEXTURES_NUM) { free(state->textures_path[i]); - if (state->mlx_ptr != NULL&& state->textures[i].id != NULL) + if (state->mlx_ptr != NULL && state->textures[i].id != NULL) mlx_destroy_image(state->mlx_ptr, state->textures[i].id); } if (state->mlx_ptr && state->window_ptr) @@ -6,40 +6,60 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/18 01:28:01 by cacharle #+# #+# */ -/* Updated: 2019/11/19 18:21:22 by cacharle ### ########.fr */ +/* Updated: 2020/01/12 14:39:34 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -t_vector vector_add(t_vector a, t_vector b) +#define VECTOR_MINUS(v) vector_scale(v, -1.0) +#define VECTOR_SUB(v, w) vector_add(v, VECTOR_MINUS(w)) +#define VECTOR_ADD_CONST(v, c) vector_add(v, vector_new(c, c)) + +t_vector vector_add(t_vector a, t_vector b) { a.x += b.x; a.y += b.y; - return a; + return (a); } -t_vector vector_scale(t_vector v, double scalar) +t_vector vector_scale(t_vector v, double scalar) { v.x *= scalar; v.y *= scalar; - return v; + return (v); } /* ** rotate counter clockwise */ -t_vector vector_rotate(t_vector v, double angle) +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; + 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) +double vector_norm(t_vector v) +{ + return (sqrt(SQUARE(v.x) + SQUARE(v.y))); +} + +t_vector vector_new(double x, double y) +{ + t_vector v; + + v.x = x; + v.y = y; + return (v); +} + +t_vector vector_apply(t_vector v, double (*f)(double)) { - return sqrt(SQUARE(v.x) + SQUARE(v.y)); + v.x = f(v.x); + v.y = f(v.y); + return (v); } |
