aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--inc/cub3d.h2
-rw-r--r--inc/cub3d_structs.h46
-rw-r--r--libft/inc/libft.h1
-rw-r--r--map/map_three.cub17
-rw-r--r--src/ft_draw_sprite.c161
-rw-r--r--src/ft_draw_sprite_extra.c68
-rw-r--r--src/ft_raycasting.c22
-rw-r--r--src/ft_tex_init.c2
9 files changed, 194 insertions, 126 deletions
diff --git a/Makefile b/Makefile
index 3a0f6c1..f8fb71e 100644
--- a/Makefile
+++ b/Makefile
@@ -72,6 +72,7 @@ SRCS_NAME += ft_key_loop.c
SRCS_NAME += ft_draw_hud.c
SRCS_NAME += ft_music.c
SRCS_NAME += ft_draw_sprite.c
+SRCS_NAME += ft_draw_sprite_extra.c
SRCS_NAME += ft_warp_level.c
SRCS_NAME += ft_save_to_bmp.c
SRCS_NAME += ft_treat_args.c
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 615112b..f255b00 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -72,6 +72,8 @@ void ft_draw_circle(float a, float b,
int32_t color, t_cub *cl);
int8_t ft_draw_verline(t_cub *cl, int32_t x,
int32_t y1, int32_t y2);
+void ft_sprite_width(t_cub *cl, t_sprite *sprite);
+void ft_sprite_height(t_cub *cl, t_sprite *sprite);
void ft_calc_sprite(t_cub *cl);
void ft_draw_sprite(t_cub *cl, t_sprite *sprite);
void ft_get_sprite_spawn(t_cub *cl);
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index 3c7ae28..d650110 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -86,29 +86,29 @@ typedef struct s_rgb
typedef struct s_sprite
{
- 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 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 transformx;
- double transformy;
- double sprite_dist;
- double invdet;
-} t_sprite;
-
-typedef struct s_player
+ 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 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 transformx;
+ double transformy;
+ double sprite_dist;
+ double invdet;
+} t_sprite;
+
+typedef struct s_player
{
float pos_x;
float pos_y;
diff --git a/libft/inc/libft.h b/libft/inc/libft.h
index e3dae43..843e97c 100644
--- a/libft/inc/libft.h
+++ b/libft/inc/libft.h
@@ -30,7 +30,6 @@ typedef struct s_list
struct s_list *next;
} t_list;
-
typedef struct s_gnl
{
int fd;
diff --git a/map/map_three.cub b/map/map_three.cub
index f2bc3bb..b8c6ea3 100644
--- a/map/map_three.cub
+++ b/map/map_three.cub
@@ -2,15 +2,26 @@ R 1000 720
NO ./media/img/BRIQUASSES.xpm
SO ./media/img/BRIQUASSES.xpm
-EA ./media/img/hey.xpm
+EA ./media/img/BRIQUASSES.xpm
WE ./media/img/left_arrow.xpm
-S ./media/img/segfot.xpm
+S ./media/img/hey.xpm
SH 0
C ./media/img/BRIQUASSES.xpm
F ./media/img/BRIQUASSES.xpm
- 1111
+SH 4
+
+
+ 111111
+ 120001
+ 120001
+ 120001
+ 120001
+ 120001
+ 120001
+ 120001
+1111111111111111001
1111111111111111001
1E00000000000000001
1111111111111111111
diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c
index 1393d38..203c48d 100644
--- a/src/ft_draw_sprite.c
+++ b/src/ft_draw_sprite.c
@@ -6,126 +6,105 @@
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 20:22:45 by rbousset #+# #+# */
-/* Updated: 2020/02/24 20:22:47 by rbousset ### ########lyon.fr */
+/* Updated: 2020/03/09 18:56:01 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <cub3d.h>
#include <stdint.h>
-#include <stdio.h>
#include <stdlib.h>
#include <math.h>
-void
- ft_sort_sprites(t_cub *cl)
+static void
+ ft_sort_sprites_norme(float *dist_tab, int32_t it, t_cub *cl)
{
- float dist_tab[4096];
- uint32_t tmp;
- int32_t it;
+ uint32_t tmp;
- 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;
- }
- }
+ tmp = 0;
+ 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)
+ ft_sort_sprites(t_cub *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 - 1;
-}
+ float dist_tab[4096];
+ int32_t it;
-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;
+ 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)
+ {
+ ft_sort_sprites_norme(dist_tab, it, cl);
+ }
}
-void
- ft_calc_sprite(t_cub *cl)
+static void
+ ft_put_sprite(t_sprite *sprite, t_cub *cl)
{
- t_sprite sprite;
- int i = 0;
+ float dist;
+ float calc;
- 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++;
- }
+ if ((dist = cl->rlist.wall_dist_tab[sprite->x]) <= 0)
+ dist = 0.0001;
+ calc = (dist * 0.1 * cl->mlist.darklvl);
+ calc = (calc >= 255) ? (255) : (calc);
+ calc = (calc < 1) ? (1) : (calc);
+ cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline)] =
+ (uint8_t)cl->tlist[4].ptr[sprite->tex_x * 4 + 4 *
+ cl->tlist[4].img_h * sprite->tex_y] / calc;
+ cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline) + 1] =
+ (uint8_t)cl->tlist[4].ptr[sprite->tex_x * 4 + 4 *
+ cl->tlist[4].img_h * sprite->tex_y + 1] / calc;
+ cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline) + 2] =
+ (uint8_t)cl->tlist[4].ptr[sprite->tex_x * 4 + 4 *
+ cl->tlist[4].img_h * sprite->tex_y + 2] / calc;
+ cl->img.ptr[sprite->x * 4 + cl->wlist.x_size * sprite->y + 3] = (char)0;
}
void
ft_draw_sprite(t_cub *cl, t_sprite *sprite)
{
- int32_t d;
+ int32_t d;
sprite->x = sprite->drawstartx;
while (sprite->x < sprite->drawendx)
{
sprite->tex_x = (int32_t)((sprite->x - (-sprite->spritewidth / 2 +
- sprite->spritescreenx)) * cl->tlist[4].img_w / sprite->spritewidth);
+ 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++;
- }
+ 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)
+ {
+ ft_put_sprite(sprite, cl);
+ }
+ sprite->y++;
+ }
+ sprite->x++;
+ }
}
diff --git a/src/ft_draw_sprite_extra.c b/src/ft_draw_sprite_extra.c
new file mode 100644
index 0000000..73b8959
--- /dev/null
+++ b/src/ft_draw_sprite_extra.c
@@ -0,0 +1,68 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_draw_sprite_extra.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/24 20:22:45 by rbousset #+# #+# */
+/* Updated: 2020/03/09 18:56:01 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <cub3d.h>
+#include <stdint.h>
+#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 - 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;
+ int32_t i;
+
+ 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++;
+ }
+}
diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c
index 9657b18..3cbd368 100644
--- a/src/ft_raycasting.c
+++ b/src/ft_raycasting.c
@@ -67,6 +67,19 @@ static void
}
}
+static void
+ ft_castray_norme(uint16_t i, t_win *wl, t_cub *cl)
+{
+ ft_initray(i, cl);
+ cl->rlist.line_h = (int16_t)(wl->y_size / cl->rlist.wall_dist);
+ cl->rlist.wall_t = -cl->rlist.line_h / 2 + wl->y_size / 2;
+ if (cl->rlist.wall_t < 0)
+ cl->rlist.wall_t = 0;
+ cl->rlist.wall_b = cl->rlist.line_h / 2 + wl->y_size / 2;
+ if (cl->rlist.wall_b >= (float)wl->y_size)
+ cl->rlist.wall_b = wl->y_size - 1;
+}
+
void
ft_castray(t_cub *cl)
{
@@ -85,14 +98,7 @@ void
i = 0;
while (i < wl->x_size)
{
- ft_initray(i, cl);
- cl->rlist.line_h = (int16_t)(wl->y_size / cl->rlist.wall_dist);
- cl->rlist.wall_t = -cl->rlist.line_h / 2 + wl->y_size / 2;
- if (cl->rlist.wall_t < 0)
- cl->rlist.wall_t = 0;
- cl->rlist.wall_b = cl->rlist.line_h / 2 + wl->y_size / 2;
- if (cl->rlist.wall_b >= (float)wl->y_size)
- cl->rlist.wall_b = wl->y_size - 1;
+ ft_castray_norme(i, wl, cl);
ft_choose_tex(cl);
ft_calc_tex(cl);
ft_draw_verline(cl, i, cl->rlist.wall_t, cl->rlist.wall_b);
diff --git a/src/ft_tex_init.c b/src/ft_tex_init.c
index adc7fd0..38e4c77 100644
--- a/src/ft_tex_init.c
+++ b/src/ft_tex_init.c
@@ -22,6 +22,8 @@
** 3 : we
** 4 : sprite
** 5 : new level door
+** 6 : floor
+** 7 : ceil
*/
static void