-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenshot.c
95 lines (86 loc) · 2.83 KB
/
screenshot.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* screenshot.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahamdaou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/09 20:39:01 by ahamdaou #+# #+# */
/* Updated: 2021/01/21 12:21:27 by ahamdaou ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
char *make_bmp_header(t_bitmapheader *header)
{
char *buf;
buf = ft_calloc(54, 1);
header->bit_count = 24;
header->width_in_bytes = ((g_game.win_width * header->bit_count + 31)
/ 32) * 4;
header->image_size = header->width_in_bytes * g_game.win_height;
header->size = 54 + header->image_size;
header->off_bits = 54;
header->info_size = 40;
header->planes = 1;
header->width = g_game.win_width;
header->height = g_game.win_height;
ft_memcpy(buf, "BM", 2);
ft_memcpy(buf + 2, &(header->size), 4);
ft_memcpy(buf + 10, &(header->off_bits), 4);
ft_memcpy(buf + 14, &(header->info_size), 4);
ft_memcpy(buf + 18, &(header->width), 4);
ft_memcpy(buf + 22, &(header->height), 4);
ft_memcpy(buf + 26, &(header->planes), 2);
ft_memcpy(buf + 28, &(header->bit_count), 2);
ft_memcpy(buf + 34, &(header->image_size), 4);
return (buf);
}
int *get_colors(int color)
{
int *colors;
colors = malloc(3 * sizeof(int));
colors[0] = ((color >> 16) & 0xFF);
colors[1] = ((color >> 8) & 0xFF);
colors[2] = ((color) & 0xFF);
return (colors);
}
char *make_img_buff(t_bitmapheader *header)
{
char *buf;
int i;
int j;
int *colors;
buf = malloc(header->image_size);
i = header->height - 1;
while (i > 0)
{
j = 0;
while (j < header->width)
{
colors = get_colors(pixel_get(j, (g_game.win_height - i)));
buf[i * header->width_in_bytes + j * 3 + 2] = colors[0];
buf[i * header->width_in_bytes + j * 3 + 1] = colors[1];
buf[i * header->width_in_bytes + j * 3 + 0] = colors[2];
free(colors);
j++;
}
i--;
}
return (buf);
}
void take_screenshot(const char *map_name)
{
t_bitmapheader header;
char *header_str;
char *img_buf;
setup(map_name);
header.fd = open("./screenshot.bmp", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
render();
header_str = make_bmp_header(&header);
img_buf = make_img_buff(&header);
write(header.fd, header_str, 54);
write(header.fd, img_buf, header.image_size);
free(header_str);
free(img_buf);
game_exit(0);
}