diff options
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | inc/cub3d.h | 1 | ||||
| -rw-r--r-- | inc/cub3d_structs.h | 3 | ||||
| -rw-r--r-- | src/ft_draw_sprite.c | 44 | ||||
| -rw-r--r-- | src/ft_raycasting.c | 20 | 
5 files changed, 69 insertions, 4 deletions
| @@ -58,6 +58,7 @@ SRCS_NAME	+= ft_draw_textures.c  SRCS_NAME	+= ft_key_release.c  SRCS_NAME	+= ft_convert_keycode.c  SRCS_NAME	+= ft_key_loop.c +SRCS_NAME	+= ft_draw_sprite.c  #--------------------------------------------------------------------------------------------------#  SRCS		= $(addprefix ${SRCS_DIR},${SRCS_NAME})  #--------------------------------------------------------------------------------------------------# @@ -151,14 +152,14 @@ endif  	@$(MAKE) --no-print-directory -C ${LFT_DIR} clean  	${RM} ${OBJS_DIR}  #--------------------------------------------------------------------------------------------------# -fclean:		clean +fclean: clean  	@$(MAKE) --no-print-directory -C ${LFT_DIR} fclean  	${RM} ${NAME}  	${RM} ${NAME}.dSYM  #--------------------------------------------------------------------------------------------------#  re:		fclean all  #--------------------------------------------------------------------------------------------------# -run:	all +run: all  	@./${NAME} "map/map_one.cub"  #--------------------------------------------------------------------------------------------------#  .PHONY:	all clean clean fclean re run default diff --git a/inc/cub3d.h b/inc/cub3d.h index 87f6ef3..2d9853c 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -83,5 +83,6 @@ void				ft_draw_circle(int32_t a, int32_t b,  								int32_t color, t_cub *cl);  void				ft_draw_texture(t_cub *cl, int x, int y, int tex_y);  int8_t				ft_check_not_found(const char *path); +void			ft_draw_sprite(t_cub *cl, int i);  #	endif diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 72cc377..4c397b2 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -49,6 +49,7 @@ typedef struct		s_rgb  typedef struct		s_sprite  { +	int32_t			s_screen_x;  	int32_t			s_pos_x;  	int32_t			s_pos_y;  	double			s_x; @@ -57,6 +58,8 @@ typedef struct		s_sprite  	int32_t			s_w;  	int32_t			s_start_x;  	int32_t			s_start_y; +	int32_t			s_end_x; +	int32_t			s_end_y;  	int32_t			s_tex_y;  	double			sprite_transform_x;  	double			sprite_transform_y; 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++;  	} | 
