-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.c
63 lines (56 loc) · 2.19 KB
/
draw.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sadamant <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/03/08 14:37:35 by sadamant #+# #+# */
/* Updated: 2018/03/14 21:34:29 by sadamant ### ########.fr */
/* */
/* ************************************************************************** */
#include "wolf3d.h"
static int grab_color(t_texture *t)
{
return (t->textures[t->type]->bitmap[t->y * t->textures[t->type]->w +
t->x]);
}
static double grab_y_offset(t_env *e, t_ray *ray)
{
double perfect_distance;
double y_top_offset;
perfect_distance = e->p->c / WINDOW_H;
y_top_offset = (perfect_distance - ray->s) / (2 * perfect_distance);
return (y_top_offset);
}
static void setup_texture(t_texture *t, t_ray *ray)
{
t->type = set_wallpaper(ray->dir);
t->x_offset = (ray->dir == EAST || ray->dir == WEST) ? \
ray->y - (((int)ray->y / TILE) * TILE) : \
ray->x - (((int)ray->x / TILE) * TILE);
t->x = ((double)t->x_offset / TILE) * t->textures[t->type]->w;
}
void draw_wallpiece(t_env *e, t_texture *t, t_ray *ray, int x)
{
int wall_h;
int topmost_pixel;
int i;
double y_offset;
double y_increment;
i = 0;
if (ray->s == INT_MAX)
return ;
setup_texture(t, ray);
wall_h = (int)(e->p->c / ray->s) + 1;
wall_h = (wall_h > WINDOW_H) ? WINDOW_H : wall_h;
topmost_pixel = (wall_h == WINDOW_H) ? 0 : (WINDOW_H / 2) - (wall_h / 2);
y_offset = (wall_h == WINDOW_H) ? grab_y_offset(e, ray) : 0;
y_increment = (1 - (y_offset * 2)) / wall_h;
while (i <= wall_h)
{
t->y = (double)((i * y_increment) + y_offset) * t->textures[t->type]->h;
insert_bitmap(e->img, x, topmost_pixel + i, grab_color(t));
i++;
}
}