-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.c
93 lines (83 loc) · 2.34 KB
/
bitmap.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bitmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dalba-de <dalba-de@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 11:18:47 by dalba-de #+# #+# */
/* Updated: 2020/03/09 16:04:35 by dalba-de ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/cub3d.h"
void write_color(int fd, unsigned char buffer[3])
{
write(fd, &buffer[0], 1);
write(fd, &buffer[1], 1);
write(fd, &buffer[2], 1);
}
void ft_zeropad(int fd, int len)
{
int i;
unsigned char c;
c = 0x00;
i = 0;
while (i < len)
{
write(fd, &c, 1);
i++;
}
}
void ft_data(t_cub *cub, int fd)
{
int x;
int y;
unsigned char buffer[3];
unsigned int color;
char *image;
y = cub->height - 1;
while (y >= 0)
{
x = 0;
while (x < cub->width)
{
image = cub->data;
image += (int)(y * cub->sizeline + (x * (cub->bpp / 8)));
color = *(unsigned int *)image;
buffer[0] = color % 256;
buffer[1] = color / 256 % 256;
buffer[2] = color / 256 / 256 % 256;
write_color(fd, buffer);
x++;
}
y--;
}
}
void header_write(t_cub *cub, int fd)
{
unsigned int width;
unsigned int height;
unsigned int size;
width = (unsigned int)cub->width;
height = (unsigned int)cub->height;
size = (3 * (width * height));
size += ((width * 3) % 4) ? height * (4 - ((width * 3) % 4)) : 0;
write(fd, "BM\x00\x00\x00\x00\x00\x00\x00\x00", 10);
write(fd, "\x36\x00\x00\x00\x28\x00\x00\x00", 8);
write(fd, &width, 4);
write(fd, &height, 4);
write(fd, "\x01\x00\x18\x00\x00\x00\x00\x00", 8);
write(fd, &size, 4);
ft_zeropad(fd, 16);
}
int bitmap(t_cub *cub)
{
int fd;
raycast(cub);
fd = open("screenshot.bmp", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
header_write(cub, fd);
ft_data(cub, fd);
close(fd);
ft_close(cub);
return (0);
}