diff options
author | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-02-24 19:08:03 +0100 |
---|---|---|
committer | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-02-24 19:08:03 +0100 |
commit | 7b7d5343c6042ce90ed213e967c34a07875e2e85 (patch) | |
tree | 0943f8592c6d480a85099750dbbd6b9b045e66eb /src | |
parent | Bug fix, 1px bug is back (diff) | |
parent | oy (diff) | |
download | 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.tar.gz 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.tar.bz2 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.tar.xz 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.tar.zst 42-cub3d-7b7d5343c6042ce90ed213e967c34a07875e2e85.zip |
Merge branch 'sprites'
Diffstat (limited to '')
-rw-r--r-- | src/ft_detect.c | 3 | ||||
-rw-r--r-- | src/ft_draw_sprite.c | 41 | ||||
-rw-r--r-- | src/ft_get_sprite.c | 20 | ||||
-rw-r--r-- | src/ft_raycasting.c | 30 |
4 files changed, 90 insertions, 4 deletions
diff --git a/src/ft_detect.c b/src/ft_detect.c index fc49a2d..10d7147 100644 --- a/src/ft_detect.c +++ b/src/ft_detect.c @@ -74,8 +74,7 @@ void cl->rlist.sqy += cl->mlist->y_step; cl->rlist.side = 1; } - if (cl->mlist->map[cl->rlist.sqx][cl->rlist.sqy] == '1' - || cl->mlist->map[cl->rlist.sqx][cl->rlist.sqy] == '2') + if (cl->mlist->map[cl->rlist.sqx][cl->rlist.sqy] == '1') cl->rlist.hit = 1; } } diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c new file mode 100644 index 0000000..c90c9ac --- /dev/null +++ b/src/ft_draw_sprite.c @@ -0,0 +1,41 @@ +#include <libft.h> +#include <cub3d.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +void +ft_draw_verline_sprite(t_cub *cl, int x, int y, int tex_y) +{ + if (cl->tlist[4].tex_x) + cl->img.ptr[x * 4 + (cl->img.sizeline * y)] = + (char)cl->tlist[4].ptr[cl->tlist[4].tex_x * 4 + 4 * + cl->tlist[4].img_h * tex_y]; + cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 1] = + (char)cl->tlist[4].ptr[cl->tlist[4].tex_x * 4 + 4 * + cl->tlist[4].img_h * tex_y + 1]; + cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] = + (char)cl->tlist[4].ptr[cl->tlist[4].tex_x * 4 + 4 * + cl->tlist[4].img_h * tex_y + 2]; + cl->img.ptr[x * 4 + cl->wlist->x_size * y + 3] = (char)0; +} + +void + ft_draw_sprite(t_cub *cl, int x) +{ + int hor_it;/*y*/ + int d; + int tex_y; + + hor_it = cl->sp_list.s_start_y; + while (hor_it < cl->sp_list.s_end_y) + { + d = hor_it * 256 - cl->wlist->y_size * 128 + cl->rlist.line_h * 128; + d = (d <= 0) ? (-d) : (d); + tex_y = ((d * cl->tlist[4].img_h) / cl->rlist.line_h) / 256; + (tex_y < 0) ? (tex_y = 0) : 0; + ft_draw_verline_sprite(cl, x, hor_it, tex_y); + hor_it++; + } +} diff --git a/src/ft_get_sprite.c b/src/ft_get_sprite.c index 43de290..806489f 100644 --- a/src/ft_get_sprite.c +++ b/src/ft_get_sprite.c @@ -17,8 +17,26 @@ void ft_get_sprite_spawn(t_cub *clist) { + size_t x; + size_t y; - (void)clist; + x = 1; + y = 1; + while (clist->mlist->map[y]) + { + while (clist->mlist->map[y][x]) + { + if (ft_ischarset("2", clist->mlist->map[y][x])) + { + clist->sp_list.s_pos_x = x + 0.5; + clist->sp_list.s_pos_y = y + 0.5; + return ; + } + x++; + } + x = 1; + y++; + } } int8_t diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index dd2be64..dd68cd3 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -13,9 +13,35 @@ #include <libft.h> #include <cub3d.h> #include <stdint.h> +#include <stdlib.h> #include <math.h> void + ft_calc_sprite(t_cub *cl) +{ + cl->sp_list.s_x = cl->sp_list.s_pos_x - cl->plist->pos_x; + cl->sp_list.s_y = cl->sp_list.s_pos_y - cl->plist->pos_y; + cl->sp_list.inv_c_m = 1.0 / (cl->plist->plane_x * cl->plist->dir_x - cl->plist->dir_y * cl->plist->plane_y); + cl->sp_list.sprite_transform_x = cl->sp_list.inv_c_m * (cl->plist->dir_y * cl->sp_list.s_x - cl->plist->dir_x * cl->sp_list.s_y); + cl->sp_list.sprite_transform_y = cl->sp_list.inv_c_m * (cl->plist->plane_y * cl->sp_list.s_x - cl->plist->plane_x * cl->sp_list.s_y); + cl->sp_list.s_screen_x = (int)((cl->wlist->y_size / 2) * (1 + cl->sp_list.sprite_transform_x / cl->sp_list.sprite_transform_y)); + cl->sp_list.s_h = abs((int)(cl->wlist->y_size * cl->sp_list.sprite_transform_y)); /*sprite height*/ + cl->sp_list.s_start_y = -cl->sp_list.s_h / 2 + cl->wlist->y_size / 2; + if (cl->sp_list.s_start_y < 0) + cl->sp_list.s_start_y = 0; + cl->sp_list.s_end_y = cl->sp_list.s_h / 2 + cl->wlist->y_size / 2; + if (cl->sp_list.s_end_y > (int)cl->wlist->y_size) + cl->sp_list.s_end_y = cl->wlist->y_size - 1;; + cl->sp_list.s_w = abs((int)(cl->wlist->x_size * cl->sp_list.sprite_transform_y)); /*sprite width*/ + cl->sp_list.s_start_x = -cl->sp_list.s_w / 2 + cl->sp_list.s_screen_x; + if (cl->sp_list.s_start_x < 0) + cl->sp_list.s_start_y = 0; + cl->sp_list.s_end_x = cl->sp_list.s_w / 2 + cl->wlist->x_size; + if (cl->sp_list.s_end_x < (int)cl->wlist->x_size) + cl->sp_list.s_end_x = cl->wlist->x_size - 1; +} + +static void ft_calc_tex(t_cub *clist) { if (clist->rlist.side == 0) @@ -24,7 +50,7 @@ void else clist->rlist.wall_hit_x = (clist->plist->pos_y) + clist->rlist.wall_dist * clist->rlist.x_ray_dir; - clist->rlist.wall_hit_x -= floor(clist->rlist.wall_hit_x); + clist->rlist.wall_hit_x -= floor(clist->rlist.wall_hit_x); clist->tlist[clist->w_side].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) @@ -83,7 +109,9 @@ void cl->rlist.wall_b = wl->y_size - 1; ft_choose_tex(cl); ft_calc_tex(cl); + ft_calc_sprite(cl); ft_draw_verline(cl, i, cl->rlist.wall_t, cl->rlist.wall_b); + ft_draw_sprite(cl , i); i++; } } |