aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_draw_sprite.c
diff options
context:
space:
mode:
authorRudy Bousset <rbousset@z2r4p3.le-101.fr>2020-03-09 18:53:35 +0100
committerRudy Bousset <rbousset@z2r4p3.le-101.fr>2020-03-09 18:53:35 +0100
commit00f719e87a853f1c0b5dfc7085f605b452e75314 (patch)
tree1af5b195d41e26a284bec552aa9f07e4cc217db8 /src/ft_draw_sprite.c
parentNorme (diff)
parentgang (diff)
download42-cub3d-00f719e87a853f1c0b5dfc7085f605b452e75314.tar.gz
42-cub3d-00f719e87a853f1c0b5dfc7085f605b452e75314.tar.bz2
42-cub3d-00f719e87a853f1c0b5dfc7085f605b452e75314.tar.xz
42-cub3d-00f719e87a853f1c0b5dfc7085f605b452e75314.tar.zst
42-cub3d-00f719e87a853f1c0b5dfc7085f605b452e75314.zip
Merge branch 'shpritz'
Diffstat (limited to 'src/ft_draw_sprite.c')
-rw-r--r--src/ft_draw_sprite.c128
1 files changed, 103 insertions, 25 deletions
diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c
index 9dc1b63..1393d38 100644
--- a/src/ft_draw_sprite.c
+++ b/src/ft_draw_sprite.c
@@ -18,36 +18,114 @@
#include <math.h>
void
- ft_draw_verline_sprite(t_cub *cl, int x, int y, int tex_y)
+ ft_sort_sprites(t_cub *cl)
{
- 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;
+ float dist_tab[4096];
+ uint32_t tmp;
+ int32_t it;
+
+ it = 0;
+ while (it < cl->mlist.sprite_nbr)
+ {
+ dist_tab[it] = ((cl->plist.pos_x - cl->sprites[it].s_pos_x) *
+ (cl->plist.pos_x - cl->sprites[it].s_pos_x) +
+ (cl->plist.pos_y - cl->sprites[it].s_pos_y) *
+ (cl->plist.pos_y - cl->sprites[it].s_pos_y));
+ cl->mlist.sprite_order[it] = it;
+ it++;
+ }
+ it = 0;
+ while (it < cl->mlist.sprite_nbr)
+ {
+ if (dist_tab[it] > dist_tab[it + 1])
+ {
+ tmp = dist_tab[it];
+ dist_tab[it] = dist_tab[it + 1];
+ dist_tab[it + 1] = tmp;
+ tmp = cl->mlist.sprite_order[it];
+ cl->mlist.sprite_order[it] = cl->mlist.sprite_order[it + 1];
+ cl->mlist.sprite_order[it + 1] = tmp;
+ it = 0;
+ }
+ }
+}
+
+void
+ ft_sprite_height(t_cub *cl, t_sprite *sprite)
+{
+ 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 - 1;
+}
+
+void
+ ft_sprite_width(t_cub *cl, t_sprite *sprite)
+{
+ 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 - 1;
+}
+
+void
+ ft_calc_sprite(t_cub *cl)
+{
+ t_sprite sprite;
+ int i = 0;
+
+ while (i < cl->mlist.sprite_nbr)
+ {
+ sprite = cl->sprites[i];
+ sprite.spritey = sprite.s_pos_x - (cl->plist.pos_x - 0.5);
+ sprite.spritex = sprite.s_pos_y - (cl->plist.pos_y - 0.5);
+ 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);
+ i++;
+ }
}
void
- ft_draw_sprite(t_cub *cl, int x)
+ ft_draw_sprite(t_cub *cl, t_sprite *sprite)
{
- int hor_it;
- int d;
- int tex_y;
+ int32_t d;
- hor_it = cl->sp_list.s_start_y;
- while (hor_it < cl->sp_list.s_end_y)
+ sprite->x = sprite->drawstartx;
+ while (sprite->x < sprite->drawendx)
{
- 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++;
- }
+ sprite->tex_x = (int32_t)((sprite->x - (-sprite->spritewidth / 2 +
+ sprite->spritescreenx)) * cl->tlist[4].img_w / sprite->spritewidth);
+ sprite->y = sprite->drawstarty;
+ while (sprite->y < sprite->drawendy)
+ {
+ d = sprite->y * 256 - cl->wlist.y_size * 128 +
+ sprite->spriteheight * 128;
+ sprite->tex_y = ((d * cl->tlist[4].img_h / 2) /
+ sprite->spriteheight) / 128;
+ if (cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y]
+ && cl->rlist.wall_dist_tab[sprite->x] > sprite->transformy)
+ {
+ cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline)] =
+ cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y];
+ cl->img.ptr[sprite->x * 4 + (sprite->y *
+ cl->img.sizeline) + 1] =
+ cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y + 1];
+ cl->img.ptr[sprite->x * 4 + (sprite->y *
+ cl->img.sizeline) + 2] =
+ cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y + 2];
+ }
+ sprite->y++;
+ }
+ sprite->x++;
+ }
}