aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-03-08 02:47:01 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-03-08 02:47:01 +0100
commitde7d432e18f4363e73b11f23885d7afd002a99f0 (patch)
treeb2321f6c77d19147892541b2de619681372beeb0
parentNot great (diff)
download42-cub3d-de7d432e18f4363e73b11f23885d7afd002a99f0.tar.gz
42-cub3d-de7d432e18f4363e73b11f23885d7afd002a99f0.tar.bz2
42-cub3d-de7d432e18f4363e73b11f23885d7afd002a99f0.tar.xz
42-cub3d-de7d432e18f4363e73b11f23885d7afd002a99f0.tar.zst
42-cub3d-de7d432e18f4363e73b11f23885d7afd002a99f0.zip
I'm close af
Diffstat (limited to '')
-rw-r--r--inc/cub3d.h6
-rw-r--r--inc/cub3d_structs.h6
-rw-r--r--src/ft_darken_rgb.c9
-rw-r--r--src/ft_draw_textures.c6
-rw-r--r--src/ft_draw_verline.c53
-rw-r--r--src/ft_floor_cast.c48
-rw-r--r--src/ft_raycasting.c7
-rw-r--r--src/ft_tex_init.c10
8 files changed, 95 insertions, 50 deletions
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 3875adc..e97af59 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -120,7 +120,7 @@ uint8_t ft_use_args(int argc, const char *argv[],
*/
void ft_castray(t_cub *cl);
-void ft_floor_cast(uint16_t y, t_cub *cl);
+void ft_floor_cast(t_cub *cl);
void ft_detect(t_cub *cl);
/*
@@ -136,8 +136,8 @@ uint8_t ft_free_words(char **words);
int8_t ft_warp_level(t_cub *cl);
int ft_exit(uint8_t exit_code, t_cub *clist);
uint32_t ft_rgb_to_hex(t_rgb rgb);
-uint32_t ft_darken_ceil(t_rgb rgb, int32_t y, t_cub *cl);
-uint32_t ft_darken_floor(t_rgb rgb, int32_t y, t_cub *cl);
t_bmp_rgb ft_hex_to_rgb(uint32_t color);
+uint32_t ft_darken_ceil(t_rgb rgb, int32_t y, t_cub *cl);
+uint32_t ft_darken_floor(t_rgb rgb, float dist, t_cub *cl);
# endif
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index d59c25e..b8a61ac 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -135,8 +135,8 @@ typedef struct s_ray
float y_side_dist;
float x_delta_dist;
float y_delta_dist;
- float floor_x;
- float floor_y;
+ double floor_x_wall;
+ double floor_y_wall;
int16_t cell_x;
int16_t cell_y;
int16_t wall_t;
@@ -202,7 +202,7 @@ typedef struct s_cub
struct s_img img;
struct s_rgb f_rgb;
struct s_rgb c_rgb;
- struct s_img tlist[6];
+ struct s_img tlist[7];
struct s_sprite sp_list;
} t_cub;
diff --git a/src/ft_darken_rgb.c b/src/ft_darken_rgb.c
index 5edfd89..90188b4 100644
--- a/src/ft_darken_rgb.c
+++ b/src/ft_darken_rgb.c
@@ -12,8 +12,8 @@
#include <cub3d.h>
#include <stdint.h>
-#include <stdio.h>
+#include <stdio.h>
uint32_t
ft_darken_ceil(t_rgb rgb, int32_t y, t_cub *cl)
{
@@ -31,7 +31,7 @@ uint32_t
}
uint32_t
- ft_darken_floor(t_rgb rgb, int32_t y, t_cub *cl)
+ ft_darken_floor(t_rgb rgb, float dist, t_cub *cl)
{
t_rgb darker;
float calc;
@@ -39,8 +39,9 @@ uint32_t
/* calc = ((float)(y + (75000.0 / cl->wlist.y_size)) */
/* / (cl->wlist.y_size / 2)) - 1.0; */
/* calc = (calc < 0) ? (0) : (calc); */
- (void)y;
- calc = (cl->rlist.row_dist * 0.4);
+ printf("%f\n", dist);
+ (void)cl;
+ calc = (dist * 0.4);
calc = (calc >= 255) ? (255) : (calc);
calc = (calc < 1) ? (1) : (calc);
darker = rgb;
diff --git a/src/ft_draw_textures.c b/src/ft_draw_textures.c
index c8eacfc..7893ffe 100644
--- a/src/ft_draw_textures.c
+++ b/src/ft_draw_textures.c
@@ -33,13 +33,13 @@ void ft_draw_texture(t_cub *cl, int x, int y, int tex_y)
calc = (calc >= 255) ? (255) : (calc);
calc = (calc < 1) ? (1) : (calc);
cl->img.ptr[x * 4 + (cl->img.sizeline * y)] =
- (unsigned char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 *
+ (uint8_t)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 *
cl->tlist[cl->w_side].img_h * tex_y] / calc;
cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 1] =
- (unsigned char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 *
+ (uint8_t)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] / calc;
cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] =
- (unsigned char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 *
+ (uint8_t)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]
/ calc;
cl->img.ptr[x * 4 + cl->wlist.x_size * y + 3] = (char)0;
diff --git a/src/ft_draw_verline.c b/src/ft_draw_verline.c
index d32ac38..b7084cc 100644
--- a/src/ft_draw_verline.c
+++ b/src/ft_draw_verline.c
@@ -14,13 +14,50 @@
#include <stdint.h>
static void
+ ft_draw_floor_tex(t_cub *cl, int32_t y, int32_t x, int32_t tex_y)
+{
+ cl->img.ptr[x * 4 + (cl->img.sizeline * y)] =
+ (uint8_t)cl->tlist[6].ptr[cl->tlist[6].tex_x * 4 + 4 *
+ cl->tlist[6].img_h * tex_y];
+ cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 1] =
+ (uint8_t)cl->tlist[6].ptr[cl->tlist[6].tex_x * 4 + 4 *
+ cl->tlist[6].img_h * tex_y + 1];
+ cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] =
+ (uint8_t)cl->tlist[6].ptr[cl->tlist[6].tex_x * 4 + 4 *
+ cl->tlist[6].img_h * tex_y + 2];
+ cl->img.ptr[x * 4 + cl->wlist.x_size * y + 3] = (int8_t)0;
+}
+
+#include <libft.h>
+static void
ft_draw_floor(t_cub *cl, int32_t y, int32_t x)
{
+ float dist_player;
+ float curr_dist;
+ float weight;
+ float curr_floor_x;
+ float curr_floor_y;
+
+ dist_player = 0.0;
while ((uint32_t)y < cl->wlist.y_size)
{
- *(int*)(cl->img.ptr +
- (x * 4 + (y * cl->img.sizeline)))
- = ft_darken_floor(cl->f_rgb, y, cl);
+ curr_dist = cl->wlist.y_size / (2.0 * y - cl->wlist.y_size);
+ weight = (curr_dist - dist_player)
+ / (cl->rlist.wall_dist - dist_player);
+ curr_floor_x = weight * cl->rlist.floor_x_wall
+ + (1.0 - weight) * cl->plist.pos_x;
+ curr_floor_y = weight * cl->rlist.floor_y_wall
+ + (1.0 - weight) * cl->plist.pos_y;
+ cl->tlist[6].tex_x = (int32_t)(curr_floor_x
+ * cl->tlist[6].img_w) % cl->tlist[6].img_w;
+ cl->tlist[6].tex_y = (int32_t)(curr_floor_y
+ * cl->tlist[6].img_h) % cl->tlist[6].img_h;
+ cl->tlist[6].tex_x = (cl->tlist[6].tex_x > 0) ? (cl->tlist[6].tex_x) : (-cl->tlist[6].tex_x);
+ cl->tlist[6].tex_y = (cl->tlist[6].tex_y > 0) ? (cl->tlist[6].tex_y) : (-cl->tlist[6].tex_y);
+ ft_draw_floor_tex(cl, y, x, cl->tlist[6].tex_y);
+ /* *(int*)(cl->img.ptr + */
+ /* (x * 4 + (y * cl->img.sizeline))) */
+ /* = ft_darken_floor(cl->f_rgb, weight, cl); */
y++;
}
}
@@ -33,7 +70,6 @@ static void
i = 0;
while (i <= y)
{
- ft_floor_cast(i, cl);
*(int*)(cl->img.ptr +
(x * 4 + (i * cl->img.sizeline)))
= ft_darken_ceil(cl->c_rgb, i, cl);
@@ -41,15 +77,6 @@ static void
}
}
-/* #include <stdio.h> */
-/* static void */
-/* ft_calc_ceil_dist(int32_t y, t_ray *rl, t_cub *cl) */
-/* { */
-/* int16_t p; */
-
-/* printf("%f\n", rl->ceil_dist); */
-/* } */
-
int8_t
ft_draw_verline(t_cub *cl, int32_t x, int32_t y, int32_t y2)
{
diff --git a/src/ft_floor_cast.c b/src/ft_floor_cast.c
index 482ee8a..9077b9b 100644
--- a/src/ft_floor_cast.c
+++ b/src/ft_floor_cast.c
@@ -13,28 +13,32 @@
#include <cub3d.h>
#include <stdint.h>
-static void
- ft_initray(uint16_t y, t_ray *rl, t_player *pl, t_cub *cl)
-{
- int16_t p;
-
- rl->x_ray_dir_f = pl->dir_x - pl->plane_x;
- rl->y_ray_dir_f = pl->dir_y - pl->plane_y;
- rl->x_ray_dir_f_bis = pl->dir_x + pl->plane_x;
- rl->y_ray_dir_f_bis = pl->dir_y + pl->plane_y;
- p = y - cl->wlist.y_size / 2;
- rl->row_dist = pl->pos_z / p;
- cl->mlist.x_floor_step = rl->row_dist *
- (rl->x_ray_dir_f_bis - rl->x_ray_dir_f) / cl->wlist.x_size;
- cl->mlist.y_floor_step = rl->row_dist *
- (rl->y_ray_dir_f_bis - rl->y_ray_dir_f) / cl->wlist.x_size;
- rl->floor_x = pl->pos_x + rl->row_dist * rl->x_ray_dir_f;
- rl->floor_y = pl->pos_y + rl->row_dist * rl->y_ray_dir_f;
-}
-
void
- ft_floor_cast(uint16_t y, t_cub *cl)
+ ft_floor_cast(t_cub *cl)
{
- cl->plist.pos_z = 0.5 * cl->wlist.y_size;
- ft_initray(y, &cl->rlist, &cl->plist, cl);
+ t_ray *rl;
+ const uint32_t map_x = (uint32_t)&cl->plist.pos_x;
+ const uint32_t map_y = (uint32_t)&cl->plist.pos_y;
+
+ rl = &cl->rlist;
+ if (rl->side == 0 && rl->x_ray_dir > 0)
+ {
+ rl->floor_x_wall = (double)map_x;
+ rl->floor_y_wall = (double)map_y + rl->wall_hit_x;
+ }
+ else if (rl->side == 0 && rl->x_ray_dir < 0)
+ {
+ rl->floor_x_wall = (double)map_x + 1.0;
+ rl->floor_y_wall = (double)map_y + rl->wall_hit_x;
+ }
+ else if (rl->side == 1 && rl->y_ray_dir > 0)
+ {
+ rl->floor_x_wall = (double)map_x + rl->wall_hit_x;
+ rl->floor_y_wall = (double)map_y;
+ }
+ else
+ {
+ rl->floor_x_wall = (double)map_x + rl->wall_hit_x;
+ rl->floor_y_wall = (double)map_y + 1.0;
+ }
}
diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c
index 50bdbf9..0a4b8b5 100644
--- a/src/ft_raycasting.c
+++ b/src/ft_raycasting.c
@@ -77,7 +77,7 @@ void
}
static void
- ft_initray(t_cub *cl, uint16_t i)
+ ft_initray(uint16_t i, t_cub *cl)
{
t_win *wl;
t_player *pl;
@@ -100,8 +100,10 @@ static void
(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;
+ }
}
void
@@ -114,7 +116,8 @@ void
wl = &cl->wlist;
while (i < wl->x_size)
{
- ft_initray(cl, i);
+ ft_initray(i, cl);
+ ft_floor_cast(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)
diff --git a/src/ft_tex_init.c b/src/ft_tex_init.c
index c19bd20..116f537 100644
--- a/src/ft_tex_init.c
+++ b/src/ft_tex_init.c
@@ -33,6 +33,15 @@ static void
&cl->tlist[5].bpp, &cl->tlist[5].sizeline, &cl->tlist[5].endian);
}
+static void
+ ft_get_floor_tex(t_cub *cl)
+{
+ cl->tlist[6].img = mlx_xpm_file_to_image(cl->wlist.wlx,
+ cl->mlist.no_tex_path, &cl->tlist[6].img_w, &cl->tlist[6].img_h);
+ cl->tlist[6].ptr = mlx_get_data_addr(cl->tlist[6].img,
+ &cl->tlist[6].bpp, &cl->tlist[6].sizeline, &cl->tlist[6].endian);
+}
+
void
ft_wall_tex_init(t_cub *cl)
{
@@ -58,5 +67,6 @@ void
&cl->tlist[4].bpp, &cl->tlist[4].sizeline, &cl->tlist[4].endian);
if (cl->mlist.isnlvl)
ft_get_nlvl_img(cl);
+ ft_get_floor_tex(cl);
cl->walltexgood = 1;
}