aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_draw_sprite_extra.c
blob: 2545ec0f9d4e132a82018be9aca74505dcad1393 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_draw_sprite_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 <stdio.h>
#include <stdlib.h>
#include <math.h>

void
	ft_sprite_height(t_cub *cl, t_sprite *sprite)
{
	sprite->spriteheight = abs((int)(cl->wlist.y_size / (sprite->transformy)));
	sprite->drawstarty = -sprite->spriteheight / 2 + cl->wlist.y_size / 2;
	if (sprite->drawstarty < 0)
		sprite->drawstarty = 0;
	sprite->drawendy = sprite->spriteheight / 2 + cl->wlist.y_size / 2;
	if (sprite->drawendy >= (int)cl->wlist.y_size)
		sprite->drawendy = cl->wlist.y_size - 1;
}

void
	ft_sprite_width(t_cub *cl, t_sprite *sprite)
{
	sprite->spritewidth = abs((int)(cl->wlist.x_size / (sprite->transformy)));
	sprite->drawstartx = -sprite->spritewidth / 2 + sprite->spritescreenx;
	if (sprite->drawstartx < 0)
		sprite->drawstartx = 0;
	sprite->drawendx = sprite->spritewidth / 2 + sprite->spritescreenx;
	if (sprite->drawendx >= (int)cl->wlist.x_size)
		sprite->drawendx = cl->wlist.x_size - 1;
}

static void
	ft_init_sprite(t_cub *cl, t_sprite *sprite)
{
	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);
}

void
	ft_calc_sprite(t_cub *cl)
{
	t_sprite	sprite;
	int32_t		i;
	int32_t		j;

	i = 0;
	j = 0;
	while (j < cl->mlist.sprite_var)
	{
		while (i < cl->mlist.sprite_nbr)
		{
			sprite = cl->sprites[j][i];
			sprite.current_sprite = (j == 0) ? 4 : j + 7;
			sprite.spritey = sprite.s_pos_x - (cl->plist.pos_x - 0.5);
			sprite.spritex = sprite.s_pos_y - (cl->plist.pos_y - 0.5);
			ft_init_sprite(cl, &sprite);
			ft_sprite_height(cl, &sprite);
			ft_sprite_width(cl, &sprite);
			ft_draw_sprite(cl, &sprite);
			i++;
		}
		i = 0;
		j++;
	}
}