From d2f53e38cbf711e35035a4576070a5211a1d055d Mon Sep 17 00:00:00 2001
From: salaaad2 <arthurdurant263@gmail.com>
Date: Sat, 22 Feb 2020 20:56:50 +0100
Subject: shpritz

---
 src/ft_draw_sprite.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/ft_raycasting.c  | 20 ++++++++++++++++++--
 2 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 src/ft_draw_sprite.c

(limited to 'src')

diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c
new file mode 100644
index 0000000..38c1fd9
--- /dev/null
+++ b/src/ft_draw_sprite.c
@@ -0,0 +1,44 @@
+#include <libft.h>
+#include <cub3d.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <math.h>
+
+void
+ft_draw_verline_sprite(t_cub *cl, int x, int y, int tex_y)
+{
+  cl->img.ptr[x * 4 + (cl->img.sizeline * y)] =
+		(char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 *
+    cl->tlist[cl->w_side].img_h * tex_y];
+	cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 1] =
+		(char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 *
+    cl->tlist[cl->w_side].img_h * tex_y + 1];
+	cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] =
+		(char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 *
+    cl->tlist[cl->w_side].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 ver_it;/*stripe*/
+  int hor_it;/*y*/
+  int tex_x;
+  int tex_y;
+  int d;
+
+  ver_it = cl->sp_list.s_start_x;
+  while (ver_it < cl->sp_list.s_end_x)
+  {
+    tex_x = (int)(256 * (ver_it - (-cl->sp_list.s_w / 2 + cl->sp_list.s_screen_x)) * cl->tlist[4].img_w / cl->sp_list.s_w) / 256;
+    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->sp_list.s_h * 128;
+      tex_y = ((d * cl->tlist[4].img_h) / cl->sp_list.s_h) / 256;
+      ft_draw_verline_sprite(cl, x, hor_it,  tex_y);
+    }
+    ver_it++;
+  }
+}
diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c
index 589d2da..b42e0b7 100644
--- a/src/ft_raycasting.c
+++ b/src/ft_raycasting.c
@@ -13,6 +13,7 @@
 #include <libft.h>
 #include <cub3d.h>
 #include <stdint.h>
+#include <stdlib.h>
 #include <math.h>
 
 void
@@ -23,7 +24,21 @@ void
 	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
@@ -35,7 +50,7 @@ static 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)
@@ -95,6 +110,7 @@ void
 		ft_choose_tex(cl);
 		ft_calc_tex(cl);
 		ft_calc_sprite(cl);
+    ft_draw_sprite(cl , i);
 		ft_draw_verline(cl, i, cl->rlist.wall_t, cl->rlist.wall_b);
 		i++;
 	}
-- 
cgit v1.2.3