From 39b3e74b166e69814d2bec2853d6b53cf274af78 Mon Sep 17 00:00:00 2001 From: salaaad2 Date: Mon, 17 Feb 2020 17:46:11 +0100 Subject: add shadows and beginning of wall coordinate hit detection --- map/map_one.cub | 28 ++++++++++++++-------------- src/ft_draw_verline.c | 2 +- src/ft_raycasting.c | 9 +++++++-- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/map/map_one.cub b/map/map_one.cub index b2635f8..00bd7db 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -9,17 +9,17 @@ S ./map/img/segfot.xpm C 50,100,200 F 150,150,124 -11111111111111111 1 -1 00000000011000001 -1N0100000000000001 -101100000111000001 -111111111111111111 -100000000011000001 -111101111111110111 -111101111111110111 -11000 0001101010111 -100020000000000011 -100000000000000011 -1100000011010101 11 -111101111111010111 -111111111111111111 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 +1 0 0 1 0 0 0 0 0 0 0 0 0 0 W 0 0 1 +1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 +1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 +1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 +1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 diff --git a/src/ft_draw_verline.c b/src/ft_draw_verline.c index bcddbc7..0fad33a 100644 --- a/src/ft_draw_verline.c +++ b/src/ft_draw_verline.c @@ -60,7 +60,7 @@ int8_t ft_choose_tex(cl); while (y <= y2) { - *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = 0x0000ffaa; + *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = (cl->rlist.side) ? 0x0000eeaa : 0x0000ffaa; y++; } ft_draw_floor(cl, y, x); diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 98f1c81..7ce45d5 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -10,19 +10,23 @@ /* */ /* ************************************************************************** */ +#include #include #include #include +#include void ft_calc_tex(t_cub *clist) { - if (clist->rlist.side == 1) + if (clist->rlist.side == 0) clist->rlist.wall_hit_x = clist->plist->pos_y + clist->rlist.wall_dist * clist->rlist.y_ray_dir; else clist->rlist.wall_hit_x = clist->plist->pos_x + clist->rlist.wall_dist * clist->rlist.x_ray_dir; + clist->rlist.wall_hit_x -= floor(clist->rlist.wall_hit_x); + /*clist->rlist.tex_x =(int)(clist->rlist.wall_hit_x * clist->tlist[clist->w_side].img_w);*/ } static void @@ -44,8 +48,10 @@ static void cl->rlist.sqy = (int16_t)cl->rlist.y_ray_pos; ft_detect(cl); if (cl->rlist.side == 0) + { cl->rlist.wall_dist = (cl->rlist.sqx - cl->rlist.x_ray_pos + (1 - cl->mlist->x_step) / 2) / cl->rlist.x_ray_dir; + } else cl->rlist.wall_dist = (cl->rlist.sqy - cl->rlist.y_ray_pos + (1 - cl->mlist->y_step) / 2) / cl->rlist.y_ray_dir; @@ -71,7 +77,6 @@ void cl->rlist.wall_b = wl->y_size - 1; ft_choose_tex(cl); ft_calc_tex(cl); - printf("%f\n", cl->rlist.wall_hit_x); ft_draw_verline(cl, i, cl->rlist.wall_t - 1, cl->rlist.wall_b); i++; } -- cgit v1.2.3 From 84b6ca845a07421b10b527f3c18a49eabd8fb2f9 Mon Sep 17 00:00:00 2001 From: salaaad2 Date: Mon, 17 Feb 2020 18:52:58 +0100 Subject: have correct width and hit zone, supposedly not much left --- inc/cub3d_structs.h | 4 ++-- src/ft_raycasting.c | 4 +++- src/ft_tex_init.c | 10 ---------- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 38472a6..00f85d7 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -36,8 +36,6 @@ typedef struct s_img int endian; int img_w; int img_h; - int tex_x; - int tex_y; } t_img; typedef struct s_rgb @@ -85,6 +83,8 @@ typedef struct s_ray size_t sqy; uint8_t hit; double wall_hit_x; + int tex_x; + int tex_y; } t_ray; typedef struct s_map diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 7ce45d5..26a3ec9 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -26,7 +26,8 @@ void clist->rlist.wall_hit_x = clist->plist->pos_x + clist->rlist.wall_dist * clist->rlist.x_ray_dir; clist->rlist.wall_hit_x -= floor(clist->rlist.wall_hit_x); - /*clist->rlist.tex_x =(int)(clist->rlist.wall_hit_x * clist->tlist[clist->w_side].img_w);*/ + clist->rlist.tex_x = (clist->rlist.wall_hit_x * (double)clist->tlist[clist->w_side].img_w); + printf("wall_hit_x : %f\ntex_x : %d\nimg_w : %d\n", clist->rlist.wall_hit_x, clist->rlist.tex_x, clist->tlist[clist->w_side].img_w); } static void @@ -65,6 +66,7 @@ void i = 0; wl = cl->wlist; + ft_wall_tex_init(cl); while (i < wl->x_size) { ft_initray(cl, i); diff --git a/src/ft_tex_init.c b/src/ft_tex_init.c index 166f431..724877f 100644 --- a/src/ft_tex_init.c +++ b/src/ft_tex_init.c @@ -23,15 +23,6 @@ ** 4 : sprite */ -void ft_puttex(t_cub *cl) -{ - int i; - - i = 0; - while (i < cl->tlist[0].img_w) - i += 4; -} - void ft_wall_tex_init(t_cub *cl) { cl->tlist[0].img = mlx_xpm_file_to_image(cl->wlist->wlx, @@ -54,5 +45,4 @@ void ft_wall_tex_init(t_cub *cl) cl->mlist->sprite_path, &cl->tlist[4].img_w, &cl->tlist[4].img_h); cl->tlist[4].ptr = mlx_get_data_addr(cl->tlist[4].img, &cl->tlist[4].bpp, &cl->tlist[4].sizeline, &cl->tlist[4].endian); - ft_puttex(cl); } -- cgit v1.2.3 From 79993a44dcd7a3428a2b09065e33f0fbc6bb69fc Mon Sep 17 00:00:00 2001 From: salaaad2 Date: Mon, 17 Feb 2020 20:25:16 +0100 Subject: some things --- map/map_one.cub | 2 +- src/ft_draw_textures.c | 8 ++++---- src/ft_raycasting.c | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/map/map_one.cub b/map/map_one.cub index 00bd7db..9be9d48 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -11,7 +11,7 @@ F 150,150,124 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 W 0 0 1 +1 0 0 1 0 0 0 0 0 0 0 0 0 0 N 0 0 1 1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 diff --git a/src/ft_draw_textures.c b/src/ft_draw_textures.c index 5d02036..46e51d7 100644 --- a/src/ft_draw_textures.c +++ b/src/ft_draw_textures.c @@ -24,11 +24,11 @@ void ft_choose_tex(t_cub *clist) { if (clist->rlist.side == 0 && clist->rlist.x_ray_dir < 0) - clist->w_side = 0; - else if (clist->rlist.side == 0 && clist->rlist.x_ray_dir > 0) clist->w_side = 1; + else if (clist->rlist.side == 0 && clist->rlist.x_ray_dir > 0) + clist->w_side = 0; else if (clist->rlist.side == 1 && clist->rlist.y_ray_dir > 0) - clist->w_side = 2; - else clist->w_side = 3; + else + clist->w_side = 2; } diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 26a3ec9..6423b9d 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -26,8 +26,18 @@ void clist->rlist.wall_hit_x = clist->plist->pos_x + clist->rlist.wall_dist * clist->rlist.x_ray_dir; clist->rlist.wall_hit_x -= floor(clist->rlist.wall_hit_x); - clist->rlist.tex_x = (clist->rlist.wall_hit_x * (double)clist->tlist[clist->w_side].img_w); - printf("wall_hit_x : %f\ntex_x : %d\nimg_w : %d\n", clist->rlist.wall_hit_x, clist->rlist.tex_x, clist->tlist[clist->w_side].img_w); + clist->rlist.tex_x = (int)(clist->rlist.wall_hit_x * + (double)clist->tlist[clist->w_side].img_w); + if (clist->rlist.side == 0 && clist->rlist.x_ray_dir > 0) + { + clist->rlist.tex_x = clist->tlist[clist->w_side].img_w - clist->rlist.tex_x - 1; + } + if (clist->rlist.side == 1 && clist->rlist.y_ray_dir < 0) + { + clist->rlist.tex_x = clist->tlist[clist->w_side].img_w - clist->rlist.tex_x - 1; + } + printf("tex_x : %d\nwall_hit_x : %f\ntex_x : %d\nimg_w : %d\n", + clist->rlist.wall_hit_x, clist->rlist.tex_x, clist->tlist[clist->w_side].img_w); } static void -- cgit v1.2.3 From 87351b03a3abf46d9b7338cc8f290ae9b6fc5f08 Mon Sep 17 00:00:00 2001 From: salaaad2 Date: Mon, 17 Feb 2020 20:41:51 +0100 Subject: on est la --- src/ft_raycasting.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 6423b9d..e2530a1 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -36,7 +36,7 @@ void { clist->rlist.tex_x = clist->tlist[clist->w_side].img_w - clist->rlist.tex_x - 1; } - printf("tex_x : %d\nwall_hit_x : %f\ntex_x : %d\nimg_w : %d\n", + printf("wall_hit_x : %f\ntex_x : %d\nimg_w : %d\n", clist->rlist.wall_hit_x, clist->rlist.tex_x, clist->tlist[clist->w_side].img_w); } -- cgit v1.2.3 From 75f4768974461a9f3ee51b920c8c8f2e22c5693b Mon Sep 17 00:00:00 2001 From: Rudy Bousset Date: Mon, 17 Feb 2020 20:45:15 +0100 Subject: Read to merge --- src/ft_convert_keycode.c | 35 ++++++++++++++++++++++++++++++ src/ft_key_loop.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ src/ft_key_release.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 src/ft_convert_keycode.c create mode 100644 src/ft_key_loop.c create mode 100644 src/ft_key_release.c diff --git a/src/ft_convert_keycode.c b/src/ft_convert_keycode.c new file mode 100644 index 0000000..5a037c5 --- /dev/null +++ b/src/ft_convert_keycode.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_convert_keycode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/17 19:00:20 by rbousset #+# #+# */ +/* Updated: 2020/02/17 19:00:21 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +int32_t + ft_convert_keycode(const int32_t tmp_code) +{ + uint16_t keycode; + + keycode = UINT16_MAX; + (tmp_code == FT_W_KEY) ? (keycode = 0) : 0; + (tmp_code == FT_A_KEY) ? (keycode = 1) : 0; + (tmp_code == FT_S_KEY) ? (keycode = 2) : 0; + (tmp_code == FT_D_KEY) ? (keycode = 3) : 0; + (tmp_code == FT_L_ARR_KEY) ? (keycode = 4) : 0; + (tmp_code == FT_R_ARR_KEY) ? (keycode = 5) : 0; + (tmp_code == 3) ? (keycode = UINT16_MAX) : 0; + (tmp_code == 4) ? (keycode = UINT16_MAX) : 0; + (tmp_code == 5) ? (keycode = UINT16_MAX) : 0; + (tmp_code == FT_ESC_KEY) ? (keycode = FT_ESC_KEY) : 0; + (tmp_code == FT_F1_KEY) ? (keycode = FT_F1_KEY) : 0; + (tmp_code == FT_TAB_KEY) ? (keycode = FT_TAB_KEY) : 0; + return (keycode); +} diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c new file mode 100644 index 0000000..b902e33 --- /dev/null +++ b/src/ft_key_loop.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_key_loop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/17 20:06:26 by rbousset #+# #+# */ +/* Updated: 2020/02/17 20:06:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +static void + ft_collision(float old_y, float old_x, t_player *pl, t_map *ml) +{ + const size_t x = pl->pos_x; + const size_t y = pl->pos_y; + + if (ml->map[y][x] == '1') + { + pl->pos_y = old_y; + pl->pos_x = old_x; + } +} + +int + ft_key_loop(t_cub *cl) +{ + int (*fun_ptr[6])(t_cub*); + uint8_t i; + const float old_y = cl->plist->pos_y; + const float old_x = cl->plist->pos_x; + + fun_ptr[0] = ft_w_key; + fun_ptr[1] = ft_a_key; + fun_ptr[2] = ft_s_key; + fun_ptr[3] = ft_d_key; + fun_ptr[4] = ft_left_key; + fun_ptr[5] = ft_right_key; + i = 0; + while (i < 5 && cl->key_input[i] != -1 && cl->key_input[i] <= 5) + { + (*fun_ptr[cl->key_input[i]])(cl); + ft_collision(old_y, old_x, cl->plist, cl->mlist); + ft_draw_scene(cl); + i++; + } + return (0); +} diff --git a/src/ft_key_release.c b/src/ft_key_release.c new file mode 100644 index 0000000..58eb2bc --- /dev/null +++ b/src/ft_key_release.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_key_release.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/17 18:43:56 by rbousset #+# #+# */ +/* Updated: 2020/02/17 18:43:59 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +static void + ft_decale(t_cub *cl) +{ + uint8_t i; + + i = 0; + while (i < 3) + { + if (cl->key_input[i] == -1 && cl->key_input[i + 1] != -1) + { + cl->key_input[i] = cl->key_input[i + 1]; + cl->key_input[i + 1] = -1; + } + i++; + } +} + +static void + ft_pop_key(uint16_t keycode, t_cub *clist) +{ + uint8_t i; + + i = 0; + while (i < 5) + { + if (clist->key_input[i] == keycode) + clist->key_input[i] = -1; + i++; + } + ft_decale(clist); +} + +int + ft_key_release(int keycode, t_cub *clist) +{ + keycode = ft_convert_keycode(keycode); + ft_pop_key(keycode, clist); + return (0); +} -- cgit v1.2.3