aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--inc/cub3d.h2
-rw-r--r--inc/cub3d_structs.h26
-rw-r--r--src/ft_draw_sprite.c44
-rw-r--r--src/ft_get_sprite.c4
-rw-r--r--src/ft_raycasting.c85
5 files changed, 82 insertions, 79 deletions
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 24ea92a..310e00d 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -92,7 +92,7 @@ void ft_draw_life_bar(size_t map_w,
void ft_music(t_cub *cl);
void ft_calc_sprite(t_cub *cl);
void ft_calc_sprite_norme(t_cub *cl);
-void ft_draw_sprite(t_cub *cl);
+void ft_draw_sprite(t_cub *cl, t_sprite *sprite);
void ft_get_sprite_spawn(t_cub *cl);
# endif
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index 91a1ee9..a8ae385 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -49,24 +49,26 @@ typedef struct s_rgb
typedef struct s_sprite
{
- int32_t s_screen_x;
+ int32_t spritescreenx;
int32_t x;
int32_t y;
+ int32_t tex_x;
+ int32_t tex_y;
int32_t s_pos_x;
int32_t s_pos_y;
- double s_x;
- double s_y;
- int32_t s_h;
- int32_t s_w;
- int32_t s_start_x;
- int32_t s_start_y;
- int32_t s_end_x;
- int32_t s_end_y;
+ double spritex;
+ double spritey;
+ int32_t spriteheight;
+ int32_t spritewidth;
+ int32_t drawstartx;
+ int32_t drawstarty;
+ int32_t drawendx;
+ int32_t drawendy;
int32_t s_tex_y;
- double sprite_transform_x;
- double sprite_transform_y;
+ double transformx;
+ double transformy;
double sprite_dist;
- double inv_c_m;
+ double invdet;
} t_sprite;
typedef struct s_player
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++;
}