aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Makefile2
-rw-r--r--inc/cub3d.h2
-rw-r--r--inc/cub3d_structs.h1
-rw-r--r--map/map_one.cub2
-rw-r--r--src/ft_draw_weapons.c116
-rw-r--r--src/ft_draw_weapons_extra.c43
-rw-r--r--src/ft_exit.c16
-rw-r--r--src/ft_get_weapon_spawn.c14
-rw-r--r--src/ft_init_map.c1
-rw-r--r--src/ft_raycasting.c1
10 files changed, 185 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index cad4b1b..43e4283 100644
--- a/Makefile
+++ b/Makefile
@@ -107,6 +107,8 @@ SRCS_NAME += ft_draw_heals_extra.c
SRCS_NAME += ft_find_item.c
SRCS_NAME += ft_tex_weap_init.c
SRCS_NAME += ft_get_weapon_spawn.c
+SRCS_NAME += ft_draw_weapons.c
+SRCS_NAME += ft_draw_weapons_extra.c
#--------------------------------------------------------------------------------------------------#
SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME})
#--------------------------------------------------------------------------------------------------#
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 33c8bba..79079e6 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -87,6 +87,8 @@ void ft_calc_trap(t_cub *cl);
void ft_draw_traps(t_cub *cl, t_sprite *sprite);
void ft_calc_heal(t_cub *cl);
void ft_draw_heals(t_cub *cl, t_sprite *sprite);
+void ft_calc_weaps(t_cub *cl);
+void ft_draw_weapon(t_cub *cl, t_sprite *sprite);
void ft_draw_skybox(t_cub *cl);
void ft_macos_suffer_animation(t_cub *cl);
void ft_linux_suffer_animation(t_cub *cl);
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index 174493d..8e38e36 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -204,6 +204,7 @@ typedef struct s_map
size_t mapl_len;
int32_t sprite_nbr[4096];
uint8_t sprite_var;
+ uint8_t weapon_var;
int32_t sprite_order[8][4096];
int32_t traps_nbr;
int32_t heals_nbr;
diff --git a/map/map_one.cub b/map/map_one.cub
index a4ee8ea..899a006 100644
--- a/map/map_one.cub
+++ b/map/map_one.cub
@@ -22,7 +22,7 @@ SH 2
10000E000T00333301
100000000000444401
100100000+00000001
-101100000111000001
+10110!0@0111000001
111111111111110011
1000001 100001
1001001 100001
diff --git a/src/ft_draw_weapons.c b/src/ft_draw_weapons.c
new file mode 100644
index 0000000..4a75c9f
--- /dev/null
+++ b/src/ft_draw_weapons.c
@@ -0,0 +1,116 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_draw_weapons.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>
+
+static void
+ ft_sort_weapons_norme(float *dist_tab, int16_t *i, uint16_t j, t_cub *cl)
+{
+ uint32_t tmp;
+ uint32_t it;
+
+ it = *i;
+ 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.weaps_order[j][it];
+ cl->mlist.weaps_order[j][it] = cl->mlist.weaps_order[j][it + 1];
+ cl->mlist.weaps_order[j][it + 1] = tmp;
+ *i = -1;
+ }
+}
+
+void
+ ft_sort_weapons(t_cub *cl, int16_t it, int16_t jt)
+{
+ float dist_tab[2][1];
+
+ while (++jt <= cl->mlist.weapon_var)
+ {
+ while (++it <= cl->mlist.weaps_nbr[jt])
+ {
+ dist_tab[jt][it] =
+ ((cl->plist.pos_x - cl->weaps[jt][it].s_pos_x) *
+ (cl->plist.pos_x - cl->weaps[jt][it].s_pos_x) +
+ (cl->plist.pos_y - cl->weaps[jt][it].s_pos_y) *
+ (cl->plist.pos_y - cl->weaps[jt][it].s_pos_y));
+ cl->mlist.weaps_order[jt][it] = it;
+ }
+ it = -1;
+ }
+ it = -1;
+ jt = -1;
+ while (++jt < cl->mlist.weapon_var)
+ {
+ while (++it < cl->mlist.weaps_nbr[jt] - 1)
+ ft_sort_weapons_norme(dist_tab[jt], &it, jt, cl);
+ it = -1;
+ }
+}
+
+static void
+ ft_put_weapon(t_sprite *sprite, t_cub *cl)
+{
+ float dist;
+ float calc;
+
+ 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[sprite->current_sprite].ptr[sprite->tex_x * 4 + 4 *
+ cl->tlist[sprite->current_sprite].img_h * sprite->tex_y] / calc;
+ cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline) + 1] =
+ (uint8_t)cl->tlist[sprite->current_sprite].ptr[sprite->tex_x * 4 + 4 *
+ cl->tlist[sprite->current_sprite].img_h * sprite->tex_y + 1] / calc;
+ cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline) + 2] =
+ (uint8_t)cl->tlist[sprite->current_sprite].ptr[sprite->tex_x * 4 + 4 *
+ cl->tlist[sprite->current_sprite].img_h * sprite->tex_y + 2] / calc;
+}
+
+void
+ ft_draw_weapon(t_cub *cl, t_sprite *sprite)
+{
+ 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[sprite->current_sprite].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[sprite->current_sprite].img_h / 2) /
+ sprite->spriteheight) / 128;
+ if (sprite->transformy > 0 &&
+ cl->tlist[sprite->current_sprite].ptr[sprite->tex_x * 4 + 4 *
+ cl->tlist[sprite->current_sprite].img_h * sprite->tex_y]
+ && cl->rlist.wall_dist_tab[sprite->x] > sprite->transformy)
+ ft_put_weapon(sprite, cl);
+ sprite->y++;
+ }
+ sprite->x++;
+ }
+}
diff --git a/src/ft_draw_weapons_extra.c b/src/ft_draw_weapons_extra.c
new file mode 100644
index 0000000..04b61ea
--- /dev/null
+++ b/src/ft_draw_weapons_extra.c
@@ -0,0 +1,43 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_draw_weapons_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>
+
+static void
+ ft_calc_weapon(uint8_t id, t_cub *cl)
+{
+ t_sprite sprite;
+
+ sprite = cl->weaps[id][0];
+ 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_h_w(cl, &sprite);
+ ft_draw_weapon(cl, &sprite);
+}
+
+void
+ ft_calc_weaps(t_cub *cl)
+{
+ ft_calc_weapon(0, cl);
+ ft_calc_weapon(1, cl);
+}
diff --git a/src/ft_exit.c b/src/ft_exit.c
index 1f47f02..91f1c4f 100644
--- a/src/ft_exit.c
+++ b/src/ft_exit.c
@@ -92,14 +92,14 @@ static void
mlx_destroy_image(clist->wlist.wlx, clist->tlist[18].img);
if (clist->mlist.isweapon[1] && clist->tlist[19].img)
mlx_destroy_image(clist->wlist.wlx, clist->tlist[19].img);
- if (cl->tweap[0].img)
- mlx_destroy_image(cl->wlist.wlx, cl->tweap[0].img);
- if (cl->tweap[1].img)
- mlx_destroy_image(cl->wlist.wlx, cl->tweap[1].img);
- if (cl->tweap[2].img)
- mlx_destroy_image(cl->wlist.wlx, cl->tweap[2].img);
- if (cl->tweap[3].img)
- mlx_destroy_image(cl->wlist.wlx, cl->tweap[3].img);
+ if (clist->tweap[0].img)
+ mlx_destroy_image(clist->wlist.wlx, clist->tweap[0].img);
+ if (clist->tweap[1].img)
+ mlx_destroy_image(clist->wlist.wlx, clist->tweap[1].img);
+ if (clist->tweap[2].img)
+ mlx_destroy_image(clist->wlist.wlx, clist->tweap[2].img);
+ if (clist->tweap[3].img)
+ mlx_destroy_image(clist->wlist.wlx, clist->tweap[3].img);
ft_del_extra_sprites(clist);
}
diff --git a/src/ft_get_weapon_spawn.c b/src/ft_get_weapon_spawn.c
index d9b7d52..a54d2b6 100644
--- a/src/ft_get_weapon_spawn.c
+++ b/src/ft_get_weapon_spawn.c
@@ -19,9 +19,16 @@ static void
{
clist->mlist.weaps_nbr[id]++;
if (clist->mlist.weaps_nbr[id] > 1)
- ft_map_error(FT_ERR_TOO_MUCH_W_ONE, clist);
+ {
+ if (id == 0)
+ ft_map_error(FT_ERR_TOO_MUCH_W_ONE, clist);
+ else
+ ft_map_error(FT_ERR_TOO_MUCH_W_TWO, clist);
+ }
clist->weaps[id][0].s_pos_x = x;
clist->weaps[id][0].s_pos_y = y;
+ clist->weaps[id][0].current_sprite = 18 + id;
+ clist->mlist.weapon_var++;
}
void
@@ -32,15 +39,14 @@ void
x = 1;
y = 1;
- i = 0;
while (clist->mlist.map[y])
{
while (clist->mlist.map[y][x])
{
if (clist->mlist.map[y][x] == '!')
- ft_get_weapon_one_spawn(0, y, x, clist);
+ ft_get_weapon_id_spawn(0, y, x, clist);
else if (clist->mlist.map[y][x] == '@')
- ft_get_weapon_two_spawn(1, y, x, clist);
+ ft_get_weapon_id_spawn(1, y, x, clist);
x++;
}
x = 1;
diff --git a/src/ft_init_map.c b/src/ft_init_map.c
index f740fd8..717506c 100644
--- a/src/ft_init_map.c
+++ b/src/ft_init_map.c
@@ -62,6 +62,7 @@ int8_t
mlist->nlx = 0;
mlist->nly = 0;
mlist->sprite_var = 0;
+ mlist->weapon_var = 0;
mlist->topsp = 0;
ft_bzero(mlist->sprite_nbr, 4096);
ft_bzero(mlist->sprite_order, 4096);
diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c
index 2d7df79..aa33eb5 100644
--- a/src/ft_raycasting.c
+++ b/src/ft_raycasting.c
@@ -108,6 +108,7 @@ void
ft_calc_sprite(cl);
ft_calc_heal(cl);
ft_calc_trap(cl);
+ ft_calc_weaps(cl);
ft_memdel((void*)&cl->rlist.wall_dist_tab);
ft_memdel((void*)&cl->rlist.wall_bz);
}