aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-03-24 00:25:18 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-03-24 00:25:18 +0100
commita2dc982a92d8f1745831f0ded8e082c445030ff0 (patch)
treee1e79ff1bfb45959701f4ba548a8b5a988bd4f11 /src
parentgot work to do (diff)
download42-cub3d-a2dc982a92d8f1745831f0ded8e082c445030ff0.tar.gz
42-cub3d-a2dc982a92d8f1745831f0ded8e082c445030ff0.tar.bz2
42-cub3d-a2dc982a92d8f1745831f0ded8e082c445030ff0.tar.xz
42-cub3d-a2dc982a92d8f1745831f0ded8e082c445030ff0.tar.zst
42-cub3d-a2dc982a92d8f1745831f0ded8e082c445030ff0.zip
Doesn't look too bad
Diffstat (limited to 'src')
-rw-r--r--src/ft_floor_cast.c30
-rw-r--r--src/ft_raycasting.c39
-rw-r--r--src/ft_rgb_to_hex.c7
3 files changed, 51 insertions, 25 deletions
diff --git a/src/ft_floor_cast.c b/src/ft_floor_cast.c
index 180f401..aa89f99 100644
--- a/src/ft_floor_cast.c
+++ b/src/ft_floor_cast.c
@@ -18,7 +18,8 @@ static void
{
float dist;
- dist = (cl->rlist.row_dist > 0) ? (cl->rlist.row_dist) : (0.0001);
+ if ((dist = cl->rlist.row_dist_tab[y]) <= 0)
+ dist = 0.0001;
*(int*)(cl->img.ptr +
(x * 4 + (y * cl->img.sizeline))) = ft_rgb_to_hex(dist, rgb, cl);
}
@@ -29,7 +30,8 @@ static void
float dist;
t_rgb rgb;
- dist = (cl->rlist.row_dist > 0) ? (cl->rlist.row_dist) : (0.0001);
+ if ((dist = cl->rlist.row_dist_tab[y]) <= 0)
+ dist = 0.0001;
rgb.b = (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x
* 4 + 4 * cl->tlist[tid].img_h * cl->tlist[tid].tex_y];
rgb.g = (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x
@@ -41,33 +43,30 @@ static void
}
static void
- ft_set_tex_xy(uint8_t tid, t_ray *rl, t_cub *cl)
+ ft_set_tex_xy(uint8_t tid, uint16_t x, t_ray *rl, t_cub *cl)
{
- cl->tlist[tid].tex_y = (int32_t)(cl->tlist[tid].img_w
- * (rl->x_floor - rl->x_cell));
+ const int32_t x_cell = (int32_t)(rl->x_floor_tab[x]);
+ const int32_t y_cell = (int32_t)(rl->y_floor_tab[x]);
+
cl->tlist[tid].tex_x = (int32_t)(cl->tlist[tid].img_h
- * (rl->y_floor - rl->y_cell));
- cl->tlist[tid].tex_x = (cl->tlist[tid].tex_x > 0)
- ? (cl->tlist[tid].tex_x) : (-cl->tlist[tid].tex_x);
- cl->tlist[tid].tex_y = (cl->tlist[tid].tex_y > 0)
- ? (cl->tlist[tid].tex_y) : (-cl->tlist[tid].tex_y);
+ * (rl->y_floor_tab[x] - y_cell));
+ cl->tlist[tid].tex_y = (int32_t)(cl->tlist[tid].img_w
+ * (rl->x_floor_tab[x] - x_cell));
}
static void
ft_floor_cast_loop(uint16_t y, uint16_t x, t_ray *rl, t_cub *cl)
{
- rl->x_cell = (int32_t)(rl->x_floor);
- rl->y_cell = (int32_t)(rl->y_floor);
if (cl->mlist.isftex)
{
- ft_set_tex_xy(6, rl, cl);
+ ft_set_tex_xy(6, x, rl, cl);
ft_draw_extra_tex(6, y, x, cl);
}
else
ft_draw_plain_horizontal(cl->f_rgb, cl, y, x);
if (cl->mlist.isctex && !cl->mlist.isskybox)
{
- ft_set_tex_xy(7, rl, cl);
+ ft_set_tex_xy(7, x, rl, cl);
ft_draw_extra_tex(7, cl->wlist.y_size - y - 1, x, cl);
}
else if (!cl->mlist.isctex && !cl->mlist.isskybox)
@@ -85,14 +84,11 @@ void
y = (cl->wlist.y_size / 2);
while (y < cl->wlist.y_size)
{
- ft_floor_cast_inits(y, &cl->rlist, cl);
x = 0;
while (x < cl->wlist.x_size)
{
if (cl->rlist.wall_bz[x] <= y)
ft_floor_cast_loop(y, x, &cl->rlist, cl);
- cl->rlist.x_floor += cl->mlist.x_floor_step;
- cl->rlist.y_floor += cl->mlist.y_floor_step;
x++;
}
y++;
diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c
index 89cc61f..73aaf30 100644
--- a/src/ft_raycasting.c
+++ b/src/ft_raycasting.c
@@ -91,7 +91,8 @@ void
ft_castray(t_cub *cl)
{
pthread_t tid[2];
- uint16_t i;
+ uint16_t y;
+ uint16_t x;
if (!(cl->rlist.wall_dist_tab = (float*)malloc(cl->wlist.x_size *
sizeof(float))) ||
@@ -104,13 +105,36 @@ void
!(cl->rlist.wall_t_tab = (int16_t*)malloc(cl->wlist.x_size *
sizeof(int16_t))) ||
!(cl->rlist.tex_x_tab = (int32_t*)malloc(cl->wlist.x_size *
- sizeof(int32_t))))
+ sizeof(int32_t))) ||
+ !(cl->rlist.y_floor_tab = (float*)malloc(cl->wlist.x_size *
+ sizeof(float))) ||
+ !(cl->rlist.x_floor_tab = (float*)malloc(cl->wlist.x_size *
+ sizeof(float))) ||
+ !(cl->rlist.row_dist_tab = (float*)malloc(cl->wlist.y_size *
+ sizeof(float))))
return ;
- i = 0;
- while (i < cl->wlist.x_size)
+ x = 0;
+ while (x < cl->wlist.x_size)
+ {
+ ft_castray_loop(x, &cl->wlist, cl);
+ x++;
+ }
+ y = cl->wlist.y_size / 2;
+ while (y < cl->wlist.y_size)
{
- ft_castray_loop(i, &cl->wlist, cl);
- i++;
+ x = 0;
+ ft_floor_cast_inits(y, &cl->rlist, cl);
+ while (x < cl->wlist.x_size)
+ {
+ cl->rlist.x_floor += cl->mlist.x_floor_step;
+ cl->rlist.y_floor += cl->mlist.y_floor_step;
+ cl->rlist.x_floor_tab[x] = cl->rlist.x_floor;
+ cl->rlist.y_floor_tab[x] = cl->rlist.y_floor;
+ x++;
+ }
+ cl->rlist.row_dist_tab[y] = cl->rlist.row_dist;
+ cl->rlist.row_dist_tab[cl->wlist.y_size - y] = cl->rlist.row_dist;
+ y++;
}
pthread_create(&tid[0], 0x0, ft_wall_cast, (void*)cl);
pthread_create(&tid[1], 0x0, ft_floor_cast, (void*)cl);
@@ -123,6 +147,9 @@ void
if (cl->plist.handles_weapon > -1)
ft_draw_handweap(cl);
ft_memdel((void*)&cl->rlist.tex_x_tab);
+ ft_memdel((void*)&cl->rlist.y_floor_tab);
+ ft_memdel((void*)&cl->rlist.x_floor_tab);
+ ft_memdel((void*)&cl->rlist.row_dist_tab);
ft_memdel((void*)&cl->rlist.wall_t_tab);
ft_memdel((void*)&cl->rlist.w_side_tab);
ft_memdel((void*)&cl->rlist.line_h_tab);
diff --git a/src/ft_rgb_to_hex.c b/src/ft_rgb_to_hex.c
index 2b72287..2ef1862 100644
--- a/src/ft_rgb_to_hex.c
+++ b/src/ft_rgb_to_hex.c
@@ -29,7 +29,10 @@ uint32_t
rgb.g = (rgb.g < 0) ? (0) : (rgb.g);
rgb.b = (rgb.b < 0) ? (0) : (rgb.b);
res = 0;
- res += (((uint8_t)(rgb.r / calc) << 16) + ((uint8_t)(rgb.g / calc) << 8)
- + (uint8_t)(rgb.b / calc));
+ if (calc <= 1)
+ res += ((rgb.r << 16) + (rgb.g << 8) + rgb.b);
+ else
+ res += (((uint8_t)(rgb.r / calc) << 16) + ((uint8_t)(rgb.g / calc) << 8)
+ + (uint8_t)(rgb.b / calc));
return (res);
}