aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_floor_cast.c
blob: 7c3de95c6bfac74520a219f180249afcce75d163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_floor_cast.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:22:57 by joelecle          #+#    #+#             */
/*   Updated: 2020/02/14 17:23:42 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <cub3d.h>
#include <stdint.h>
#include <pthread.h>

static void
	ft_draw_plain_horizontal(t_rgb rgb, t_cub *cl, int32_t y, int32_t x)
{
	float	dist;

	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);
}

static void
	ft_draw_extra_tex(uint8_t tid, uint16_t y, uint16_t x, t_cub *cl)
{
	float		dist;
	t_rgb		rgb;

	dist = cl->rlist.row_dist_tab[y];
	if (dist <= 0)
		dist = 0.0001;
	rgb.b = (uint8_t)cl->tlist[tid].ptr[cl->rlist.fc_tex_x_tab[tid - 6][y][x]
		* 4 + 4 * cl->tlist[tid].img_h *
		cl->rlist.fc_tex_y_tab[tid - 6][y][x]];
	rgb.g = (uint8_t)cl->tlist[tid].ptr[cl->rlist.fc_tex_x_tab[tid - 6][y][x]
		* 4 + 4 * cl->tlist[tid].img_h *
		cl->rlist.fc_tex_y_tab[tid - 6][y][x] + 1];
	rgb.r = (uint8_t)cl->tlist[tid].ptr[cl->rlist.fc_tex_x_tab[tid - 6][y][x]
		* 4 + 4 * cl->tlist[tid].img_h *
		cl->rlist.fc_tex_y_tab[tid - 6][y][x] + 2];
	*(int*)(cl->img.ptr + ((uint16_t)x * 4 +
		(y * cl->img.sizeline))) = ft_rgb_to_hex(dist, rgb, cl);
}

void
	ft_floor_cast_loop(uint16_t y, uint16_t x, t_cub *cl)
{
	if (cl->mlist.isftex)
	{
		ft_draw_extra_tex(6, y, x, cl);
	}
	else if (!cl->mlist.isftex)
		ft_draw_plain_horizontal(cl->f_rgb, cl, y, x);
	if (cl->mlist.isctex)
	{
		ft_draw_extra_tex(7, cl->wlist.y_size - y - 1, x, cl);
	}
	else if (!cl->mlist.isctex)
		ft_draw_plain_horizontal(cl->c_rgb, cl, cl->wlist.y_size - y - 1, x);
}

void
	*ft_floor_cast(void *vargp)
{
	pthread_t	tid[5];
	t_cub		*cl;

	cl = (t_cub *)vargp;
	pthread_create(&tid[0], 0x0, ft_floor_one, (void*)cl);
	pthread_create(&tid[1], 0x0, ft_floor_two, (void*)cl);
	pthread_create(&tid[2], 0x0, ft_floor_three, (void*)cl);
	pthread_create(&tid[3], 0x0, ft_floor_four, (void*)cl);
	pthread_create(&tid[4], 0x0, ft_floor_five, (void*)cl);
	pthread_join(tid[4], 0x0);
	pthread_join(tid[3], 0x0);
	pthread_join(tid[2], 0x0);
	pthread_join(tid[1], 0x0);
	pthread_join(tid[0], 0x0);
	pthread_exit(0x0);
	return (0x0);
}