forked from AndreIglesias/fdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.c
More file actions
131 lines (118 loc) · 2.81 KB
/
render.c
File metadata and controls
131 lines (118 loc) · 2.81 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ksoto <ksoto@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/11 01:27:49 by ciglesia #+# #+# */
/* Updated: 2021/10/14 12:01:41 by ksoto ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static void set_color_ptr(unsigned char *line, t_layer *l, int color, int x)
{
int byte;
int dec;
int opp;
unsigned char *ucolor;
t_fdf *fdf;
fdf = ft_fdf(NULL);
ucolor = (unsigned char *)&color;
dec = l->bpp / 8;
opp = dec;
byte = x * l->bpp / 8;
while (dec--)
{
*(line + byte + dec) = ((ucolor))[3 - dec];
if (l->endian)
*(line + byte + dec) = ((ucolor))[opp - 1 - dec];
if (l->endian == fdf->local_endian)
{
*(line + byte + dec) = ((ucolor))[dec];
if (l->endian)
*(line + byte + dec) = ((ucolor))[4 - opp + dec];
}
}
}
static void fill_img(t_layer *l, int w, int h, int **bmp)
{
int x;
int y;
int color;
unsigned char *ptr;
y = 0;
while (y < h)
{
ptr = (unsigned char *)l->data + y * l->bpl;
x = 0;
while (x < w)
{
color = bmp[y][x];
set_color_ptr(ptr, l, color, x);
x++;
}
y++;
}
}
void ft_plot(int **bmp)
{
t_layer l;
t_fdf *fdf;
fdf = ft_fdf(NULL);
if (fdf->l.img)
mlx_destroy_image(fdf->mlx, fdf->l.img);
l.img = mlx_new_image(fdf->mlx, fdf->res[0], fdf->res[1]);
if (!l.img)
exit_win(ft_fdf(NULL));
l.data = mlx_get_data_addr(l.img, &l.bpp, &l.bpl, &l.endian);
fill_img(&l, fdf->res[0], fdf->res[1], bmp);
mlx_put_image_to_window(fdf->mlx, fdf->win, l.img, 0, 0);
fdf->l = l;
}
/*
** bresenham_loop: bresenham loop
** fdf: fdf project
*/
void bresenham_loop(t_fdf *fdf)
{
int x;
int y;
y = -1;
while (++y < fdf->mapy)
{
x = -1;
while (++x < fdf->mapx)
{
if (x < fdf->mapx - 1)
{
set_vertical(fdf, x, y);
bresenham_line(fdf, fdf->bmp);
}
if (y < fdf->mapy - 1)
{
set_horizontal(fdf, x, y);
bresenham_line(fdf, fdf->bmp);
}
}
}
}
/*
** plot_map: plot map
** fdf: fdf project
*/
void plot_map(t_fdf *fdf)
{
int x;
int y;
y = -1;
while (++y < fdf->res[1])
{
x = -1;
while (++x < fdf->res[0])
fdf->bmp[y][x] = 0;
}
mlx_clear_window(fdf->mlx, fdf->win);
bresenham_loop(fdf);
ft_plot(fdf->bmp);
}