From a0f26d0407a2e514e9dcfd2baeef5ccd98f7b3dd Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 1 Feb 2020 12:33:55 +0100 Subject: texture selector, collision detection --- src/event.c | 7 ++++++- src/helper.c | 5 +++-- src/parse/parse.c | 10 ++++----- src/parse/parse_color.c | 2 +- src/render.c | 42 ++++++++++++++++++------------------- src/render_state.c | 46 +---------------------------------------- src/texture.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 90 insertions(+), 77 deletions(-) create mode 100644 src/texture.c (limited to 'src') diff --git a/src/event.c b/src/event.c index 9dcaa75..cff9e5d 100644 --- a/src/event.c +++ b/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */ -/* Updated: 2020/01/30 15:20:18 by cacharle ### ########.fr */ +/* Updated: 2020/02/01 12:30:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,9 @@ int event_keydown(int key, t_state *state) { + t_vector saved_pos; + + saved_pos = state->pos; if (key == MLXK_ESC) state->running = FALSE; else if (key == MLXK_A) @@ -35,6 +38,8 @@ int event_keydown(int key, t_state *state) helper_rotate_player(state, -ROTATE_STEP); else if (key == MLXK_RIGHT) helper_rotate_player(state, ROTATE_STEP); + if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_WALL) + state->pos = saved_pos; return (0); } diff --git a/src/helper.c b/src/helper.c index 9940848..6fa7ffe 100644 --- a/src/helper.c +++ b/src/helper.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 07:32:20 by cacharle #+# #+# */ -/* Updated: 2020/01/16 08:57:01 by cacharle ### ########.fr */ +/* Updated: 2020/02/01 11:32:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,8 @@ void helper_rotate_player(t_state *state, double rotation) /* ** Initial player direction vector -** Since the map [0 0] is in the top left corner the north/south direction are slipped. +** 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) */ diff --git a/src/parse/parse.c b/src/parse/parse.c index f4b5b66..581056a 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ -/* Updated: 2020/01/30 12:03:54 by cacharle ### ########.fr */ +/* Updated: 2020/02/01 11:35:32 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,8 +32,8 @@ t_state *parse(char *filename) "parse configuration", state, lines)); } if ((state = parse_map(state, lines + i)) == NULL) - return (error_put_return_lines_state_destroy( - "parse map", state, lines)); + return (error_put_return_lines_state_destroy("parse map", + state, lines)); helper_free_splited(lines); return (state); } @@ -73,8 +73,6 @@ static t_option_parser g_option_parsers[] = {"C", parse_ceilling_color} }; -#define OPTIONS_PARSERS_SIZE (sizeof(g_option_parsers) / sizeof(t_option_parser)) - t_bool parse_line(t_state *state, char *line) { int i; @@ -82,7 +80,7 @@ t_bool parse_line(t_state *state, char *line) if (!*line) return (TRUE); i = -1; - while (++i < (int)OPTIONS_PARSERS_SIZE) + while (++i < (int)(sizeof(g_option_parsers) / sizeof(t_option_parser))) if (ft_strncmp(g_option_parsers[i].id, line, ft_strlen(g_option_parsers[i].id)) == 0) return (g_option_parsers[i].func( diff --git a/src/parse/parse_color.c b/src/parse/parse_color.c index fd482e5..3c025de 100644 --- a/src/parse/parse_color.c +++ b/src/parse/parse_color.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 09:52:34 by cacharle #+# #+# */ -/* Updated: 2020/01/30 14:17:37 by cacharle ### ########.fr */ +/* Updated: 2020/02/01 11:32:33 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/render.c b/src/render.c index 88b0cae..820036d 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/11 13:37:17 by cacharle #+# #+# */ -/* Updated: 2020/01/30 15:17:49 by cacharle ### ########.fr */ +/* Updated: 2020/02/01 12:06:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,8 @@ int render_update(void *param) return (0); } render_update_window(state); - mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0); + mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, + state->window.id, 0, 0); return (0); } @@ -43,7 +44,7 @@ void render_column(t_state *state, int x) rstate.x = x; rstate_ray(state, &rstate); - rstate.map_pos = vector_new((double)((int)state->pos.x), (double)((int)state->pos.y)); //floor? + rstate.map_pos = vector_new((double)((int)state->pos.x), (double)((int)state->pos.y)); rstate_delta(&rstate); rstate_init_probe(state, &rstate); rstate.map_step = vector_new(rstate.ray.x < 0.0 ? -1.0 : 1.0, rstate.ray.y < 0.0 ? -1.0 : 1.0); @@ -73,35 +74,32 @@ void render_window_column(t_state *state, t_render_state *rstate) white.hexcode = 0x00ffffff; i = 0; while (i < rstate->draw_start) - ((t_color*)state->window.data)[i++ * state->window.width + rstate->x] = state->ceilling_color; - while (i < rstate->draw_end) - ((t_color*)state->window.data)[i++ * state->window.width + rstate->x] = white; - render_texture(state, rstate); - i = rstate->draw_end; + ((t_color*)state->window.data)[i++ * state->window.width + rstate->x] = + state->ceilling_color; + render_texture(state, rstate, &i); while (i < state->window.height) - ((t_color*)state->window.data)[i++ * state->window.width + rstate->x] = state->floor_color; + ((t_color*)state->window.data)[i++ * state->window.width + rstate->x] = + state->floor_color; } -void render_texture(t_state *state, t_render_state *rstate) +void render_texture(t_state *state, t_render_state *rstate, int *i) { - int i; - double step; - double tex_pos; - int tex_y; int tex_x; + int tex_y; + double tex_step; + double tex_pos; t_image *texture; - texture = get_tex(state, rstate); - tex_x = get_tex_x(state, rstate, texture); - step = 1.0 * texture->height / rstate->line_height; - tex_pos = (rstate->draw_start - state->window.height / 2 + rstate->line_height / 2) * step; - i = rstate->draw_start - 1; - while (++i < rstate->draw_end) + texture = texture_select(state, rstate); + tex_x = texture_x(state, rstate, texture); + tex_step = (double)texture->height / (double)rstate->line_height; + tex_pos = (rstate->draw_start - state->window.height / 2 + rstate->line_height / 2) * tex_step; + while (++(*i) < rstate->draw_end) { tex_y = (int)tex_pos & (texture->height - 1); - tex_pos += step; - ((t_color*)state->window.data)[i * state->window.width + rstate->x] = + tex_pos += tex_step; + ((t_color*)state->window.data)[*i * state->window.width + rstate->x] = ((t_color*)texture->data)[texture->height * tex_y + tex_x]; } } diff --git a/src/render_state.c b/src/render_state.c index 46fc7c5..ff777e2 100644 --- a/src/render_state.c +++ b/src/render_state.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 14:40:14 by cacharle #+# #+# */ -/* Updated: 2020/01/30 15:50:48 by cacharle ### ########.fr */ +/* Updated: 2020/02/01 11:39:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -159,47 +159,3 @@ void rstate_line_height(t_state *state, t_render_state *rstate) { rstate->line_height = (int)((double)state->window.height / rstate->perp_dist); } - -t_image *get_tex(t_state *state, t_render_state *rstate) -{ - if (rstate->side == SIDE_NS) - { - if (rstate->probe.y < state->pos.y) - return (state->textures + TEX_NORTH); - else - return (state->textures + TEX_SOUTH); - } - else if (rstate->side == SIDE_WE) - { - if (rstate->probe.x > state->pos.x) - return (state->textures + TEX_WEST); - else - return (state->textures + TEX_EAST); - } - return (NULL); -} - -/* -** Since we're drawing each column, all the texels we want to draw on the window -** are on a single column of the texture. -** First we find the x-coord relative to the wall we hit -*/ - -int get_tex_x(t_state *state, t_render_state *rstate, t_image *texture) -{ - int tex_x; - double wall_x; - - if (rstate->side == SIDE_WE) - wall_x = state->pos.y + rstate->perp_dist * rstate->ray.y; - else - wall_x = state->pos.x + rstate->perp_dist * rstate->ray.x; - wall_x -= floor(wall_x); - - tex_x = (int)(wall_x * (double)texture->width); - if (rstate->side == 0 && rstate->ray.x > 0) - tex_x = texture->width - tex_x - 1; - if (rstate->side == 1 && rstate->ray.y < 0) - tex_x = texture->width - tex_x - 1; - return (tex_x); -} diff --git a/src/texture.c b/src/texture.c new file mode 100644 index 0000000..725e0aa --- /dev/null +++ b/src/texture.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* texture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/01 11:38:43 by cacharle #+# #+# */ +/* Updated: 2020/02/01 12:24:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_image *texture_select(t_state *state, t_render_state *rstate) +{ + if (rstate->side == SIDE_NS) + { + if (rstate->map_pos.y < state->pos.y) + return (state->textures + TEX_NORTH); + else + return (state->textures + TEX_SOUTH); + } + else if (rstate->side == SIDE_WE) + { + if (rstate->map_pos.x > state->pos.x) + return (state->textures + TEX_WEST); + else + return (state->textures + TEX_EAST); + } + return (NULL); +} + +/* +** Since we're drawing each column, all the texels we want to draw on the window +** are on a single column of the texture. +** First we find the x-coord relative to the wall we hit +*/ + +int texture_x(t_state *state, t_render_state *rstate, t_image *texture) +{ + int tex_x; + double wall_x; + + if (rstate->side == SIDE_WE) + wall_x = state->pos.y + rstate->perp_dist * rstate->ray.y; + else + wall_x = state->pos.x + rstate->perp_dist * rstate->ray.x; + wall_x -= floor(wall_x); + tex_x = (int)(wall_x * (double)texture->width); + if ((rstate->side == SIDE_NS && rstate->ray.x > 0) || + (rstate->side == SIDE_WE && rstate->ray.y < 0)) + tex_x = texture->width - tex_x - 1; + return (tex_x); +} -- cgit