aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsalaaad2 <arthudurant263@gmail.com>2020-03-06 18:40:56 +0100
committersalaaad2 <arthudurant263@gmail.com>2020-03-06 18:40:56 +0100
commit04658c5e9a019917c3f8ffeb84562c328da0a885 (patch)
tree5a57411c8968aa57f4827aade5f1640fb68124fe
parentfuck (diff)
download42-cub3d-04658c5e9a019917c3f8ffeb84562c328da0a885.tar.gz
42-cub3d-04658c5e9a019917c3f8ffeb84562c328da0a885.tar.bz2
42-cub3d-04658c5e9a019917c3f8ffeb84562c328da0a885.tar.xz
42-cub3d-04658c5e9a019917c3f8ffeb84562c328da0a885.tar.zst
42-cub3d-04658c5e9a019917c3f8ffeb84562c328da0a885.zip
ils se cachent (mal)
Diffstat (limited to '')
-rw-r--r--inc/cub3d_structs.h5
-rw-r--r--map/map_one.cub2
-rw-r--r--src/ft_draw_sprite.c86
-rw-r--r--src/ft_draw_verline.c9
-rw-r--r--src/ft_get_sprite.c9
-rw-r--r--src/ft_raycasting.c56
6 files changed, 102 insertions, 65 deletions
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index 102b985..7b86d09 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -89,6 +89,7 @@ typedef struct s_ray
{
uint16_t line_h;
float wall_dist;
+ float wall_dist_tab[4096];
float x_ray_pos;
float y_ray_pos;
float x_ray_dir;
@@ -116,6 +117,8 @@ typedef struct s_map
char *sprite_path;
char *mapl;
char **map;
+ int32_t sprite_nbr;
+ int32_t sprite_order[12];
int8_t x_step;
int8_t y_step;
size_t map_w;
@@ -142,7 +145,7 @@ typedef struct s_cub
struct s_rgb f_rgb;
struct s_rgb c_rgb;
struct s_img tlist[5];
- struct s_sprite sprites;
+ struct s_sprite sprites[12];
} t_cub;
# endif
diff --git a/map/map_one.cub b/map/map_one.cub
index ab1ae76..c7b0f71 100644
--- a/map/map_one.cub
+++ b/map/map_one.cub
@@ -10,7 +10,7 @@ C 50,100,200
F 50,190,124
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
-1 2 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1
+1 2 2 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 N 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c
index 82984d2..29a8a46 100644
--- a/src/ft_draw_sprite.c
+++ b/src/ft_draw_sprite.c
@@ -18,6 +18,90 @@
#include <math.h>
void
+ ft_sort_sprites(t_cub *cl)
+{
+ 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;
+}
+
+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;
+}
+
+void
+ ft_calc_sprite(t_cub *cl)
+{
+ t_sprite sprite;
+ int i = 0;
+
+ while (i < cl->mlist->sprite_nbr)
+ {
+ sprite = cl->sprites[i];
+ sprite.spritex = sprite.s_pos_x - (cl->plist->pos_x - 0.5);
+ sprite.spritey = 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.spritey - cl->plist->dir_x * sprite.spritex);
+ sprite.transformy = sprite.invdet *
+ (-cl->plist->plane_y * sprite.spritex + cl->plist->plane_x * sprite.spritey);
+ printf("[%f] [%f] x [%f] y [%f] [%d]\n", sprite.spritex, sprite.spritey, sprite.transformx, sprite.transformy, i);
+ sprite.spritescreenx = (int)((cl->wlist->y_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, t_sprite *sprite)
{
int32_t d;
@@ -37,7 +121,7 @@ void
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])
+ 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] > cl->rlist.wall_dist)
{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 *
diff --git a/src/ft_draw_verline.c b/src/ft_draw_verline.c
index 2b684eb..220badb 100644
--- a/src/ft_draw_verline.c
+++ b/src/ft_draw_verline.c
@@ -19,11 +19,12 @@ uint32_t
(void)it;
res = 0;
- if (it < 255)
+ while (it < 500)
{
- rgb.r += it;
- rgb.g += it;
- rgb.b += it;
+ rgb.r += 1;
+ rgb.g += 1;
+ rgb.b += 1;
+ it++;
}
res += ((rgb.r << 16) + (rgb.g << 8) + rgb.b);
return (res);
diff --git a/src/ft_get_sprite.c b/src/ft_get_sprite.c
index 5f9a0bc..84feeff 100644
--- a/src/ft_get_sprite.c
+++ b/src/ft_get_sprite.c
@@ -19,18 +19,21 @@ void
{
size_t x;
size_t y;
+ uint8_t i;
x = 1;
y = 1;
+ i = 0;
while (clist->mlist->map[y])
{
while (clist->mlist->map[y][x])
{
if (ft_ischarset("2", clist->mlist->map[y][x]))
{
- clist->sprites.spritex = x;
- clist->sprites.spritey = y;
- return ;
+ clist->mlist->sprite_nbr++;
+ clist->sprites[i].s_pos_x = x;
+ clist->sprites[i].s_pos_y = y;
+ i++;
}
x++;
}
diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c
index 925711f..2c4b05c 100644
--- a/src/ft_raycasting.c
+++ b/src/ft_raycasting.c
@@ -17,61 +17,6 @@
#include <stdlib.h>
#include <math.h>
-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;
-}
-
-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;
-}
-
-void
- ft_calc_sprite(t_cub *cl)
-{
- t_sprite sprite;
- static int i = 0;
- float tmp_a;
- float tmp_b;
-
- sprite = cl->sprites;
- tmp_a = cl->plist->dir_x;
- tmp_b = cl->plist->dir_y;
- sprite.spritex = sprite.spritex - (cl->plist->pos_x - 0.5);
- sprite.spritey = sprite.spritey - (cl->plist->pos_y - 0.5);
- /* sprite.transformx */
- /* sprite.transformy */
- printf("[%f] [%f] x [%f] y [%f] [%d]\n", sprite.spritex, sprite.spritey, sprite.transformx, sprite.transformy, i);
- 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.spritey - cl->plist->dir_x * sprite.spritex);
- sprite.transformy = sprite.invdet *
- (-cl->plist->plane_y * sprite.spritex + cl->plist->plane_x * sprite.spritey);
- sprite.spritescreenx = (int)((cl->wlist->y_size / 2) *
- (1 + sprite.transformx / sprite.transformy));
- ft_sprite_height(cl, &sprite);
- ft_sprite_width(cl, &sprite);
- ft_draw_sprite(cl, &sprite);
- cl->plist->dir_x = tmp_a;
- cl->plist->dir_y = tmp_b;
- i++;
-}
void
ft_calc_tex(t_cub *clist)
@@ -142,6 +87,7 @@ ft_castray(t_cub *cl)
ft_choose_tex(cl);
ft_calc_tex(cl);
ft_draw_verline(cl, i, cl->rlist.wall_t, cl->rlist.wall_b);
+ cl->rlist.wall_dist_tab[i] = cl->rlist.wall_dist;
i++;
}
}