diff options
| -rw-r--r-- | capture.c | 7 | ||||
| -rw-r--r-- | cub3d.h | 3 | ||||
| -rw-r--r-- | event.c | 3 | ||||
| -rw-r--r-- | helper.c | 13 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | main.c | 5 | ||||
| -rw-r--r-- | minimalist.cub | 2 | ||||
| -rw-r--r-- | render.c | 41 | ||||
| -rw-r--r-- | render_state.c | 65 | ||||
| -rw-r--r-- | vector.c | 5 |
10 files changed, 96 insertions, 48 deletions
@@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 13:15:11 by cacharle #+# #+# */ -/* Updated: 2020/01/15 15:08:34 by cacharle ### ########.fr */ +/* Updated: 2020/01/16 07:49:17 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,9 @@ int capture(t_state *state) { - render_update_window(state); - write_bmp(&state->window); + printf("capture\n"); + /* render_update_window(state); */ + /* write_bmp(&state->window); */ return (0); } @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */ -/* Updated: 2020/01/15 15:10:45 by cacharle ### ########.fr */ +/* Updated: 2020/01/16 08:08:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdio.h> @@ -255,7 +255,6 @@ void rstate_line_height(t_state *state, t_render_state *rstate); void rstate_next_probe(t_render_state *rstate); t_image *get_tex(t_state *state, t_render_state *rstate); - /* ** capture.c */ @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2020/01/12 09:27:14 by cacharle ### ########.fr */ +/* Updated: 2020/01/16 07:52:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ int event_keydown(int key, void *param) { t_state *state; + printf("event: %d\n", key); state = (t_state*)param; if (key == MLXK_ESC) state->running = FALSE; @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 07:32:20 by cacharle #+# #+# */ -/* Updated: 2020/01/15 14:40:49 by cacharle ### ########.fr */ +/* Updated: 2020/01/16 08:57:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,12 +36,19 @@ void helper_rotate_player(t_state *state, double rotation) state->plane = vector_rotate(state->plane, rotation); } +/* +** Initial player direction vector +** Since the map [0 0] is in the top left corner the north/south direction are slipped. +** The camera plane has to stay perpendicular to the direction and +** create a camera with a 66 degree angle (which is a recommended angle for fps) +*/ + 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_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) diff --git a/libft b/libft -Subproject fbd1d450b0c2da394cbb02fd61ab75b2719bfb7 +Subproject 6fc0bdd2c983126b1b332677898f31def237e3e @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */ -/* Updated: 2020/01/15 15:08:45 by cacharle ### ########.fr */ +/* Updated: 2020/01/16 09:19:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,13 @@ int main(int argc, char **argv) { t_state *state; - if (argc != 2 && argc != 3 && ft_strcmp(argv[2], "--save")) + if (argc != 2 && !(argc == 3 && ft_strcmp(argv[2], "--save") == 0)) error_put_usage_exit(argv[0]); if ((state = state_new(parse_check(parse(argv[1])))) == NULL) return (1); if (argc == 3) return (capture(state)); + printf("yo\n"); 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); diff --git a/minimalist.cub b/minimalist.cub index 863c37d..c0c9a7e 100644 --- a/minimalist.cub +++ b/minimalist.cub @@ -1,4 +1,4 @@ -R 640 480 +R 400 400 NO ./textures/brick.xpm SO ./textures/brick.xpm WE ./textures/brick.xpm @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 13:37:17 by cacharle #+# #+# */ -/* Updated: 2020/01/15 15:09:32 by cacharle ### ########.fr */ +/* Updated: 2020/01/16 10:06:04 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,11 @@ void render_update_window(t_state *state) x = -1; while (++x < state->window.width) + { + /* x = state->window.width / 2 + 1; */ + /* printf("\nx %d\n", x); */ render_column(state, x); + } } void render_column(t_state *state, int x) @@ -43,34 +47,59 @@ void render_column(t_state *state, int x) t_render_state rstate; rstate.x = x; + /* printf("1\n"); */ rstate_ray(state, &rstate); - vector_new((double)((int)state->pos.x), (double)((int)state->pos.y)); + /* printf("ray [%f %f]\n", rstate.ray.x, rstate.ray.y); */ + rstate.map_pos = vector_new((double)((int)state->pos.x), (double)((int)state->pos.y)); //floor? + /* printf("map_pos [%f %f]\n", rstate.map_pos.x, rstate.map_pos.y); */ rstate_delta(&rstate); + /* printf("delta [%f %f]\n", rstate.delta.x, rstate.delta.y); */ rstate_init_probe(state, &rstate); - vector_new(rstate.ray.x < 0.0 ? -1.0 : 1.0, rstate.ray.y < 0.0 ? -1.0 : 1.0); + /* printf("probe [%f %f]\n", rstate.probe.x, rstate.probe.y); */ + rstate.map_step = vector_new(rstate.ray.x < 0.0 ? -1.0 : 1.0, rstate.ray.y < 0.0 ? -1.0 : 1.0); + /* printf("map_step [%f %f]\n", rstate.map_step.x, rstate.map_step.y); */ while (TRUE) { rstate.side = rstate.probe.x < rstate.probe.y ? SIDE_WE : SIDE_WE; + /* printf("side %s\n", rstate.side == SIDE_WE ? "side we" : "side ns"); */ rstate_next_probe(&rstate); + /* printf("2\n"); */ + /* printf("> [%d %d]\n", (int)rstate.map_pos.x, (int)rstate.map_pos.y); */ if (state->map[(int)rstate.map_pos.y][(int)rstate.map_pos.x] == CELL_WALL) break ; + /* printf("3\n"); */ } + /* printf("[%d %d]\n", (int)rstate.map_pos.x, (int)rstate.map_pos.y); */ rstate_line_height(state, &rstate); + /* printf("perp dist %f\n", rstate_perp_dist(state, &rstate)); */ + printf("line height %d\n", rstate.line_height); + rstate.draw_start = WINDOW_MID_HEIGHT(state) - rstate.line_height / 2; + rstate.draw_end = WINDOW_MID_HEIGHT(state) + rstate.line_height / 2; + printf("%d -> %d\n", rstate.draw_start, rstate.draw_end); render_window_column(state, &rstate); - // WINDOW_MID_HEIGHT(state) - line_height / 2 - // WINDOW_MID_HEIGHT(state) + line_height / 2 } void render_window_column(t_state *state, t_render_state *rstate) { int i; t_color white; + t_color black; white.hexcode = 0x00ffffff; + black.hexcode = 0x00000000; i = -1; - /* while (++i < draw_start) */ + while (++i < rstate->draw_start) + ((t_color*)state->window.data)[i * state->window.width + rstate->x] = black; + while (++i < rstate->draw_end) + ((t_color*)state->window.data)[i * state->window.width + rstate->x] = white; + while (++i < state->window_height) + ((t_color*)state->window.data)[i * state->window.width + rstate->x] = black; + + /* printf("%d -> %d\n", rstate->draw_start, rstate->draw_end); */ + /* while (++i < rstate->draw_start) */ /* ((t_color*)state->window.data)[i * state->window.width + rstate->x] = */ /* state->ceilling_color; */ + /* i = rstate->draw_end; */ /* i = render_texture(state, rstate); */ /* while (++i < state->window_height) */ /* ((t_color*)state->window.data)[i * state->window.width + rstate->x] = */ diff --git a/render_state.c b/render_state.c index a539016..82ee588 100644 --- a/render_state.c +++ b/render_state.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 14:40:14 by cacharle #+# #+# */ -/* Updated: 2020/01/15 15:06:39 by cacharle ### ########.fr */ +/* Updated: 2020/01/16 10:03:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,21 +65,21 @@ void rstate_delta(t_render_state *rstate) ** we multiply them by their corresponding delta. ** 0 <= perpendicular distance <= 1 is a ratio, how much of the full delta we need to take. ** -** if (ray.x < 0) -** current.x = state->pos.x - map_pos.x; +** if (rstate->ray.x < 0) +** rstate->probe.x = state->pos.x - rstate->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; +** rstate->probe.x = fabs(state->pos.x - rstate->map_pos.x + 1.0); +** if (rstate->ray.y < 0) +** rstate->probe.y = state->pos.y - rstate->map_pos.y; ** else -** current.y = fabs(state->pos.y - map_pos.y + 1.0); -** current.x *= delta.x; -** current.y *= delta.y; +** rstate->probe.y = fabs(state->pos.y - rstate->map_pos.y + 1.0); +** rstate->probe.x *= rstate->delta.x; +** rstate->probe.y *= rstate->delta.y; */ void rstate_init_probe(t_state *state, t_render_state *rstate) { - rstate->probe = vector_apply(VECTOR_SUB(state->pos, rstate->map_pos), &fabs); + rstate->probe = VECTOR_SUB(state->pos, rstate->map_pos); if (rstate->ray.x > 0) rstate->probe.x += 1.0; if (rstate->ray.y > 0) @@ -89,6 +89,27 @@ void rstate_init_probe(t_state *state, t_render_state *rstate) } /* +** Move the probe to it's next iteration by advancing to the nearest square unit +** in the x or y direction. +** This advance is represented both with the +** player/ray percpective and the map pecpective. +*/ + +void rstate_next_probe(t_render_state *rstate) +{ + if (rstate->probe.x < rstate->probe.y) + { + rstate->probe.x += rstate->delta.x; + rstate->map_pos.x += rstate->map_step.x; + } + else + { + rstate->probe.y += rstate->delta.y; + rstate->map_pos.y += rstate->map_step.y; + } +} + +/* ** perpendicular distance between the wall hit and the camera plane. ** We don't use euclidean distance because it would cause a fisheye effect. ** @@ -116,10 +137,12 @@ void rstate_init_probe(t_state *state, t_render_state *rstate) double rstate_perp_dist(t_state *state, t_render_state *rstate) { if (rstate->side == SIDE_NS) - return (rstate->probe.y - state->pos.y + state->dir.y); - else if (rstate->side == SIDE_NS) - return (rstate->probe.x - state->pos.x + state->dir.x); - return (0.0); + return ((rstate->map_pos.x - state->pos.x + (1.0 - rstate->map_step.x) / 2.0) / rstate->ray.x); // fait des trucs bizzare + /* return (rstate->map_pos.y - state->pos.y + state->dir.y); */ + else if (rstate->side == SIDE_WE) + return ((rstate->map_pos.y - state->pos.y + (1.0 - rstate->map_step.y) / 2.0) / rstate->ray.y); + /* return (rstate->map_pos.x - state->pos.x + state->dir.x); */ + return (1.0); } /* @@ -133,20 +156,6 @@ void rstate_line_height(t_state *state, t_render_state *rstate) (int)((double)state->window.height / rstate_perp_dist(state, rstate)); } -void rstate_next_probe(t_render_state *rstate) -{ - if (rstate->probe.x < rstate->probe.y) - { - rstate->probe.x += rstate->delta.x; - rstate->map_pos.x += rstate->map_step.x; - } - else - { - rstate->probe.y += rstate->delta.y; - rstate->map_pos.y += rstate->map_step.y; - } -} - t_image *get_tex(t_state *state, t_render_state *rstate) { if (rstate->side == SIDE_NS) @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/18 01:28:01 by cacharle #+# #+# */ -/* Updated: 2020/01/15 15:09:08 by cacharle ### ########.fr */ +/* Updated: 2020/01/16 08:43:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,8 @@ t_vector vector_rotate(t_vector v, double angle) double vector_norm(t_vector v) { - return (sqrt(SQUARE(v.x) + SQUARE(v.y))); + /* return (sqrt(SQUARE(v.x) + SQUARE(v.y))); */ + return (hypot(v.x, v.y)); } t_vector vector_new(double x, double y) |
