diff options
Diffstat (limited to '')
| -rw-r--r-- | src/ft_floor_cast.c | 239 | ||||
| -rw-r--r-- | src/ft_get_fps_count.c | 6 | ||||
| -rw-r--r-- | src/ft_raycasting.c | 81 | 
3 files changed, 285 insertions, 41 deletions
| diff --git a/src/ft_floor_cast.c b/src/ft_floor_cast.c index aa89f99..b00792e 100644 --- a/src/ft_floor_cast.c +++ b/src/ft_floor_cast.c @@ -12,6 +12,7 @@  #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) @@ -32,41 +33,27 @@ static void  	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 -		* 4 + 4 * cl->tlist[tid].img_h * cl->tlist[tid].tex_y + 1]; -	rgb.r = (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x -		* 4 + 4 * cl->tlist[tid].img_h * cl->tlist[tid].tex_y + 2]; +	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);  }  static void -	ft_set_tex_xy(uint8_t tid, uint16_t x, t_ray *rl, t_cub *cl) -{ -	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_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) +	ft_floor_cast_loop(uint16_t y, uint16_t x, t_cub *cl)  {  	if (cl->mlist.isftex)  	{ -		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, x, rl, cl);  		ft_draw_extra_tex(7, cl->wlist.y_size - y - 1, x, cl);  	}  	else if (!cl->mlist.isctex && !cl->mlist.isskybox) @@ -74,24 +61,228 @@ static void  }  void -	*ft_floor_cast(void *vargp) +*ft_floor_one(void *vargp)  { +	uint16_t	x; +	uint16_t	y;  	t_cub		*cl; + +	cl = (t_cub *)vargp; +	y = (cl->wlist.y_size / 2); +	y += 0 * (cl->wlist.y_size / 2 / 8); +	while (y < (cl->wlist.y_size / 2) + (1 * (cl->wlist.y_size / 2 / 8))) +	{ +		x = 0; +		while (x < cl->wlist.x_size) +		{ +			if (cl->rlist.wall_bz[x] <= y) +				ft_floor_cast_loop(y, x, cl); +			x++; +		} +		y++; +	} +	pthread_exit(0x0); +	return (0x0); +} + +void +*ft_floor_two(void *vargp) +{  	uint16_t	x;  	uint16_t	y; +	t_cub		*cl;  	cl = (t_cub *)vargp;  	y = (cl->wlist.y_size / 2); -	while (y < cl->wlist.y_size) +	y += 1 * (cl->wlist.y_size / 2 / 8); +	while (y < (cl->wlist.y_size / 2) + (2 * (cl->wlist.y_size / 2 / 8)))  	{  		x = 0;  		while (x < cl->wlist.x_size)  		{  			if (cl->rlist.wall_bz[x] <= y) -				ft_floor_cast_loop(y, x, &cl->rlist, cl); +				ft_floor_cast_loop(y, x, cl);  			x++;  		}  		y++;  	} +	pthread_exit(0x0); +	return (0x0); +} + +void +*ft_floor_three(void *vargp) +{ +	uint16_t	x; +	uint16_t	y; +	t_cub		*cl; + +	cl = (t_cub *)vargp; +	y =  (cl->wlist.y_size / 2); +	y += (2 * (cl->wlist.y_size / 2 / 8)); +	while (y < (cl->wlist.y_size / 2) + (3 * (cl->wlist.y_size / 2 / 8))) +	{ +		x = 0; +		while (x < cl->wlist.x_size) +		{ +			if (cl->rlist.wall_bz[x] <= y) +				ft_floor_cast_loop(y, x, cl); +			x++; +		} +		y++; +	} +	pthread_exit(0x0); +	return (0x0); +} + +void +*ft_floor_four(void *vargp) +{ +	uint16_t	x; +	uint16_t	y; +	t_cub		*cl; + +	cl = (t_cub *)vargp; +	y =  (cl->wlist.y_size / 2); +	y += (3 * (cl->wlist.y_size / 2 / 8)); +	while (y < (cl->wlist.y_size / 2) + (4 * (cl->wlist.y_size / 2 / 8))) +	{ +		x = 0; +		while (x < cl->wlist.x_size) +		{ +			if (cl->rlist.wall_bz[x] <= y) +				ft_floor_cast_loop(y, x, cl); +			x++; +		} +		y++; +	} +	pthread_exit(0x0); +	return (0x0); +} + +void +*ft_floor_five(void *vargp) +{ +	uint16_t	x; +	uint16_t	y; +	t_cub		*cl; + +	cl = (t_cub *)vargp; +	y =  (cl->wlist.y_size / 2); +	y += (4 * (cl->wlist.y_size / 2 / 8)); +	while (y < (cl->wlist.y_size / 2) + (5 * (cl->wlist.y_size / 2 / 8))) +	{ +		x = 0; +		while (x < cl->wlist.x_size) +		{ +			if (cl->rlist.wall_bz[x] <= y) +				ft_floor_cast_loop(y, x, cl); +			x++; +		} +		y++; +	} +	pthread_exit(0x0); +	return (0x0); +} + +void +*ft_floor_six(void *vargp) +{ +	uint16_t	x; +	uint16_t	y; +	t_cub		*cl; + +	cl = (t_cub *)vargp; +	y =  (cl->wlist.y_size / 2); +	y += (5 * (cl->wlist.y_size / 2 / 8)); +	while (y < (cl->wlist.y_size / 2) + (6 * (cl->wlist.y_size / 2 / 8))) +	{ +		x = 0; +		while (x < cl->wlist.x_size) +		{ +			if (cl->rlist.wall_bz[x] <= y) +				ft_floor_cast_loop(y, x, cl); +			x++; +		} +		y++; +	} +	pthread_exit(0x0); +	return (0x0); +} + +void +*ft_floor_seven(void *vargp) +{ +	uint16_t	x; +	uint16_t	y; +	t_cub		*cl; + +	cl = (t_cub *)vargp; +	y =  (cl->wlist.y_size / 2); +	y += (6 * (cl->wlist.y_size / 2 / 8)); +	while (y < (cl->wlist.y_size / 2) + (7 * (cl->wlist.y_size / 2 / 8))) +	{ +		x = 0; +		while (x < cl->wlist.x_size) +		{ +			if (cl->rlist.wall_bz[x] <= y) +				ft_floor_cast_loop(y, x, cl); +			x++; +		} +		y++; +	} +	pthread_exit(0x0); +	return (0x0); +} + +void +*ft_floor_eight(void *vargp) +{ +	uint16_t	x; +	uint16_t	y; +	t_cub		*cl; + +	cl = (t_cub *)vargp; +	y =  (cl->wlist.y_size / 2); +	y += (7 * (cl->wlist.y_size / 2 / 8)); +	while (y < (cl->wlist.y_size / 2) + (8 * (cl->wlist.y_size / 2 / 8))) +	{ +		x = 0; +		while (x < cl->wlist.x_size) +		{ +			if (cl->rlist.wall_bz[x] <= y) +				ft_floor_cast_loop(y, x, cl); +			x++; +		} +		y++; +	} +	pthread_exit(0x0); +	return (0x0); +} + +void +	*ft_floor_cast(void *vargp) +{ +	pthread_t	tid[8]; +	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_create(&tid[5], 0x0, ft_floor_six, (void*)cl); +	pthread_create(&tid[6], 0x0, ft_floor_seven, (void*)cl); +	pthread_create(&tid[7], 0x0, ft_floor_eight, (void*)cl); +	pthread_join(tid[0], 0x0); +	pthread_join(tid[1], 0x0); +	pthread_join(tid[2], 0x0); +	pthread_join(tid[3], 0x0); +	pthread_join(tid[4], 0x0); +	pthread_join(tid[5], 0x0); +	pthread_join(tid[6], 0x0); +	pthread_join(tid[7], 0x0); +	pthread_exit(0x0);  	return (0x0);  } diff --git a/src/ft_get_fps_count.c b/src/ft_get_fps_count.c index 63bf08e..f7f0177 100644 --- a/src/ft_get_fps_count.c +++ b/src/ft_get_fps_count.c @@ -22,7 +22,6 @@ static float  	return ((ticks / (float)CLOCKS_PER_SEC) * 1000.0);  } -  static void  	ft_handle_firing(clock_t dt, t_cub *cl)  { @@ -40,13 +39,16 @@ void  	ft_get_fps_count(clock_t dt, t_cub *cl)  {  	static clock_t	curr_time = 0; +	static uint16_t	ticks = 0;  	curr_time += dt; +	ticks += 1;  	if (cl->plist.fire == 1)  		ft_handle_firing(dt, cl);  	if (dt > 0 && ft_clock_to_ms(curr_time) > 1000.0)  	{ -		ft_sprintf(cl->fps_count, "fps: %ld", (uint32_t)CLOCKS_PER_SEC / dt); +		ft_sprintf(cl->fps_count, "fps: %hu", ticks); +		ticks = 0;  		curr_time -= CLOCKS_PER_SEC;  	}  } diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 73aaf30..e83c8f7 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -87,6 +87,20 @@ static void  	cl->rlist.wall_dist_tab[i] = cl->rlist.wall_dist;  } +static void +	ft_set_fc_tex_xy(uint8_t tid, uint16_t x, uint16_t y, t_cub *cl) +{ +	const int32_t	x_cell = (int32_t)(cl->rlist.x_floor); +	const int32_t	y_cell = (int32_t)(cl->rlist.y_floor); + +	cl->tlist[tid].tex_x = (int32_t)(cl->tlist[tid].img_h +		* (cl->rlist.y_floor - y_cell)); +	cl->tlist[tid].tex_y = (int32_t)(cl->tlist[tid].img_w +		* (cl->rlist.x_floor - x_cell)); +	cl->rlist.fc_tex_x_tab[tid - 6][y][x] = cl->tlist[tid].tex_x; +	cl->rlist.fc_tex_y_tab[tid - 6][y][x] = cl->tlist[tid].tex_y; +} +  void  	ft_castray(t_cub *cl)  { @@ -106,13 +120,31 @@ void  			sizeof(int16_t))) ||  		!(cl->rlist.tex_x_tab = (int32_t*)malloc(cl->wlist.x_size *  			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.fc_tex_x_tab = (int32_t***)malloc(2 * sizeof(int32_t**))) || +		!(cl->rlist.fc_tex_y_tab = (int32_t***)malloc(2 * sizeof(int32_t**))) ||  		!(cl->rlist.row_dist_tab = (float*)malloc(cl->wlist.y_size *  			sizeof(float)))) -		return ; +			ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, cl); +	x = 0; +	while (x < 2) +	{ +		if (!(cl->rlist.fc_tex_x_tab[x] = (int**)malloc(cl->wlist.y_size * +				sizeof(int*))) || +			!(cl->rlist.fc_tex_y_tab[x] = (int**)malloc(cl->wlist.y_size * +				sizeof(int*)))) +			ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, cl); +		y = 0; +		while (y < cl->wlist.y_size) +		{ +			if (!(cl->rlist.fc_tex_x_tab[x][y] = (int*)malloc(cl->wlist.x_size * +					sizeof(int))) || +				!(cl->rlist.fc_tex_y_tab[x][y] = (int*)malloc(cl->wlist.x_size * +					sizeof(int)))) +				ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, cl); +			y++; +		} +		x++; +	}  	x = 0;  	while (x < cl->wlist.x_size)  	{ @@ -126,10 +158,15 @@ void  		ft_floor_cast_inits(y, &cl->rlist, cl);  		while (x < cl->wlist.x_size)  		{ +			if (cl->rlist.wall_bz[x] <= y) +			{ +				if (cl->mlist.isftex) +					ft_set_fc_tex_xy(6, x, y, cl); +				if (cl->mlist.isctex && !cl->mlist.isskybox) +					ft_set_fc_tex_xy(7, x, cl->wlist.y_size - y - 1, cl); +			}  			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; @@ -140,19 +177,33 @@ void  	pthread_create(&tid[1], 0x0, ft_floor_cast, (void*)cl);  	pthread_join(tid[0], 0x0);  	pthread_join(tid[1], 0x0); +	x = 0; +	y = 0; +	while (x < 2) +	{ +		while (y < cl->wlist.y_size) +		{ +			free(cl->rlist.fc_tex_x_tab[x][y]); +			free(cl->rlist.fc_tex_y_tab[x][y]); +			y++; +		} +		free(cl->rlist.fc_tex_x_tab[x]); +		free(cl->rlist.fc_tex_y_tab[x]); +		x++; +	} +	free(cl->rlist.fc_tex_x_tab); +	free(cl->rlist.fc_tex_y_tab); +	ft_memdel((void*)&cl->rlist.tex_x_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); +	ft_memdel((void*)&cl->rlist.wall_bz);  	ft_calc_sprite(cl);  	ft_calc_heal(cl);  	ft_calc_trap(cl);  	ft_calc_weaps(cl);  	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);  	ft_memdel((void*)&cl->rlist.wall_dist_tab); -	ft_memdel((void*)&cl->rlist.wall_bz);  } | 
