-
Notifications
You must be signed in to change notification settings - Fork 9
/
bmp.c
107 lines (97 loc) · 2.88 KB
/
bmp.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ncolomer <ncolomer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/10 18:13:15 by ncolomer #+# #+# */
/* Updated: 2019/11/12 22:09:07 by ncolomer ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
static void
set_int_in_char(unsigned char *start, int value)
{
start[0] = (unsigned char)(value);
start[1] = (unsigned char)(value >> 8);
start[2] = (unsigned char)(value >> 16);
start[3] = (unsigned char)(value >> 24);
}
static int
write_bmp_header(int fd, int filesize, t_game *game)
{
int i;
int tmp;
unsigned char bmpfileheader[54];
i = 0;
while (i < 54)
bmpfileheader[i++] = (unsigned char)(0);
bmpfileheader[0] = (unsigned char)('B');
bmpfileheader[1] = (unsigned char)('M');
set_int_in_char(bmpfileheader + 2, filesize);
bmpfileheader[10] = (unsigned char)(54);
bmpfileheader[14] = (unsigned char)(40);
tmp = game->window.size.x;
set_int_in_char(bmpfileheader + 18, tmp);
tmp = game->window.size.y;
set_int_in_char(bmpfileheader + 22, tmp);
bmpfileheader[27] = (unsigned char)(1);
bmpfileheader[28] = (unsigned char)(24);
return (!(write(fd, bmpfileheader, 54) < 0));
}
static int
get_color(t_window *w, int x, int y)
{
int rgb;
int color;
color = *(int*)(w->screen.ptr
+ (4 * (int)w->size.x * ((int)w->size.y - 1 - y))
+ (4 * x));
rgb = (color & 0xFF0000) | (color & 0x00FF00) | (color & 0x0000FF);
return (rgb);
}
static int
write_bmp_data(int file, t_window *w, int pad)
{
const unsigned char zero[3] = {0, 0, 0};
int i;
int j;
int color;
i = 0;
while (i < (int)w->size.y)
{
j = 0;
while (j < (int)w->size.x)
{
color = get_color(w, j, i);
if (write(file, &color, 3) < 0)
return (0);
if (pad > 0 && write(file, &zero, pad) < 0)
return (0);
j++;
}
i++;
}
return (1);
}
int
save_bmp(t_game *game)
{
t_window *w;
int filesize;
int file;
int pad;
w = &game->window;
pad = (4 - ((int)w->size.x * 3) % 4) % 4;
filesize = 54 + (3 * ((int)w->size.x + pad) * (int)w->size.y);
if ((file = open("screenshot.bmp", O_WRONLY | O_CREAT
| O_TRUNC | O_APPEND)) < 0)
return (0);
if (!write_bmp_header(file, filesize, game))
return (0);
if (!write_bmp_data(file, w, pad))
return (0);
close(file);
return (1);
}