diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ft_draw_sprite.c | 44 | ||||
-rw-r--r-- | src/ft_get_sprite.c | 4 | ||||
-rw-r--r-- | src/ft_raycasting.c | 85 |
3 files changed, 67 insertions, 66 deletions
diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c index 28ffb10..03d3ca7 100644 --- a/src/ft_draw_sprite.c +++ b/src/ft_draw_sprite.c @@ -18,31 +18,31 @@ #include <math.h> void - ft_draw_sprite(t_cub *cl) + ft_draw_sprite(t_cub *cl, t_sprite *sprite) { - int d; - int tex_x; - int tex_y; - - cl->sprites.x = cl->sprites.s_start_x; - while (cl->sprites.x < cl->sprites.s_end_x) + int32_t d; + sprite->x = sprite->drawstartx; + while (sprite->x < sprite->drawendx) { - tex_x = (int)((cl->sprites.x - (-cl->sprites.s_w / 2 + cl->sprites.s_screen_x)) - * cl->tlist[4].img_w / cl->sprites.s_w); - cl->sprites.y = cl->sprites.s_start_y; - while (cl->sprites.y < cl->sprites.s_end_y) - { - d = cl->sprites.y * 256 - cl->wlist->y_size * 128 + cl->sprites.s_h * 128; - tex_y = ((d * cl->tlist[4].img_h) / cl->sprites.s_h) / 256; - if (*(cl->tlist[4].ptr + tex_x + tex_y * cl->tlist[4].sizeline / 4)) + sprite->tex_x = (int32_t)((sprite->x - (-sprite->spritewidth / 2 + + sprite->spritescreenx)) * cl->tlist[4].img_w / sprite->spritewidth); + sprite->y = sprite->drawstarty; + if (sprite->transformy > 0 && sprite->x >= 0 && + sprite->x < (int32_t)cl->wlist->x_size) + while (sprite->y < sprite->drawendy) { - cl->img.ptr[tex_x * 4 + (cl->img.sizeline * tex_y)] = - (char)cl->tlist[4].ptr[cl->tlist[4].tex_x * 4 + 4 * - cl->tlist[4].img_h * tex_y]; - /*puts("qwe");*/ + d = (sprite->y) * 256 - cl->wlist->y_size * 128 + + sprite->spriteheight * 128; + sprite->tex_y = ((d * cl->tlist[4].img_h) / + sprite->spriteheight) / 256; + if (*(cl->tlist[4].ptr + sprite->tex_x + sprite->tex_y * + cl->tlist[4].sizeline / 4)) + *(cl->img.ptr + sprite->x + sprite->y * + cl->img.sizeline / 4) = + *(cl->tlist[4].ptr + sprite->tex_x + sprite->tex_y * cl->tlist[4].sizeline / 4); + sprite->y++; } - cl->sprites.y++; - } - cl->sprites.x++; + sprite->x++; } + } diff --git a/src/ft_get_sprite.c b/src/ft_get_sprite.c index 76629c5..d99f12e 100644 --- a/src/ft_get_sprite.c +++ b/src/ft_get_sprite.c @@ -28,8 +28,8 @@ void { if (ft_ischarset("2", clist->mlist->map[y][x])) { - clist->sprites.s_pos_x = x + 0.5; - clist->sprites.s_pos_y = y + 0.5; + clist->sprites.spritex = x + 0.5; + clist->sprites.spritey = y + 0.5; return ; } x++; diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index be4a815..d97c231 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -18,49 +18,49 @@ #include <math.h> void - ft_calc_sprite(t_cub *cl) + ft_sprite_height(t_cub *cl, t_sprite *sprite) { - cl->sprites.s_x = cl->sprites.s_pos_x - cl->plist->pos_x; - cl->sprites.s_y = cl->sprites.s_pos_y - cl->plist->pos_y; - cl->sprites.inv_c_m = 1.0 / (cl->plist->plane_x * cl->plist->dir_y - - cl->plist->dir_x * cl->plist->plane_y); - cl->sprites.sprite_transform_x = cl->sprites.inv_c_m - * (cl->plist->dir_y * cl->sprites.s_x - - cl->plist->dir_x * cl->sprites.s_y); - cl->sprites.sprite_transform_y = cl->sprites.inv_c_m - * (-cl->plist->plane_y * cl->sprites.s_x - + cl->plist->plane_x * cl->sprites.s_y); - cl->sprites.s_screen_x = (int)((cl->wlist->x_size / 2) - * (1 + cl->sprites.sprite_transform_x - / cl->sprites.sprite_transform_y)); - cl->sprites.s_h = - abs((int)(cl->wlist->y_size * cl->sprites.sprite_transform_y)); - ft_calc_sprite_norme(cl); + sprite->spriteheight = abs((int)(cl->wlist->y_size / (sprite->transformy))); + sprite->drawstarty = -sprite->spriteheight / 2 + cl->wlist->y_size / 2; + if (sprite->drawstarty < 0) + sprite->drawstarty = 0; + sprite->drawendy = sprite->spriteheight / 2 + cl->wlist->y_size / 2; + if (sprite->drawendy >= (int)cl->wlist->y_size) + sprite->drawendy = cl->wlist->y_size; } void - ft_calc_sprite_norme(t_cub *cl) + ft_sprite_width(t_cub *cl, t_sprite *sprite) { - cl->sprites.s_start_y = -cl->sprites.s_h / 2 + cl->wlist->y_size / 2; - if (cl->sprites.s_start_y < 0) - cl->sprites.s_start_y = 0; - cl->sprites.s_end_y = cl->sprites.s_h / 2 + cl->wlist->y_size / 2; - if (cl->sprites.s_end_y > (int)cl->wlist->y_size) - cl->sprites.s_end_y = cl->wlist->y_size - 1; - cl->sprites.s_w = abs((int)(cl->wlist->y_size - / cl->sprites.sprite_transform_y)); /*sprite width*/ - cl->sprites.s_start_x = -cl->sprites.s_w / 2 + cl->sprites.s_screen_x; - if (cl->sprites.s_start_x < 0) - cl->sprites.s_start_x = 0; - cl->sprites.s_end_x = cl->sprites.s_w / 2 + cl->sprites.s_screen_x; - printf("%d\n", cl->sprites.s_w); - if (cl->sprites.s_end_x > (int)cl->wlist->x_size) - cl->sprites.s_end_x = cl->wlist->x_size - 1; - /*ft_printf("start x: %d\nend x : %d\n", cl->sprites.s_start_x, cl->sprites.s_end_x);*/ - /*ft_printf("start y: %d\nend y : %d\n", cl->sprites.s_start_y, cl->sprites.s_end_y);*/ - /*ft_draw_sprite(cl);*/ + sprite->spritewidth = abs((int)(cl->wlist->x_size / (sprite->transformy))); + sprite->drawstartx = -sprite->spritewidth / 2 + sprite->spritescreenx; + if (sprite->drawstartx < 0) + sprite->drawstartx = 0; + sprite->drawendx = sprite->spritewidth / 2 + sprite->spritescreenx; + if (sprite->drawendx >= (int)cl->wlist->x_size) + sprite->drawendx = cl->wlist->x_size; } +void + ft_calc_sprite(t_cub *cl) +{ + t_sprite sprite; + + sprite = cl->sprites; + sprite.spritex = (sprite.spritex + 0.5) - cl->plist->pos_x; + sprite.spritey = (sprite.spritey + 0.5) - cl->plist->pos_y; + sprite.invdet = 1.0 / (cl->plist->plane_x * + cl->plist->dir_y - cl->plist->dir_x * cl->plist->plane_y); + sprite.transformx = sprite.invdet * (cl->plist->dir_y * + sprite.spritex - cl->plist->dir_x * sprite.spritey); + sprite.transformy = sprite.invdet * + (-cl->plist->plane_y * sprite.spritex + cl->plist->plane_x * sprite.spritey); + sprite.spritescreenx = (int)((cl->wlist->x_size / 2) * + (1 + sprite.transformx / sprite.transformy)); + ft_sprite_height(cl, &sprite); + ft_sprite_width(cl, &sprite); + ft_draw_sprite(cl, &sprite); +} void ft_calc_tex(t_cub *clist) @@ -82,8 +82,8 @@ void - clist->tlist[clist->w_side].tex_x - 1; } -static void - ft_initray(t_cub *cl, uint16_t i) + static void +ft_initray(t_cub *cl, uint16_t i) { t_win *wl; t_player *pl; @@ -103,15 +103,15 @@ static void 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; + (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; + (1 - cl->mlist->y_step) / 2) / cl->rlist.y_ray_dir; } -void - ft_castray(t_cub *cl) + void +ft_castray(t_cub *cl) { uint16_t i; t_win *wl; @@ -130,6 +130,7 @@ 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); i++; } |