forked from diasurgical/devilution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture.cpp
More file actions
208 lines (178 loc) · 4.8 KB
/
capture.cpp
File metadata and controls
208 lines (178 loc) · 4.8 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* @file capture.cpp
*
* Implementation of the screenshot function.
*/
#include "all.h"
/**
* @brief Write the PCX-file header
* @param hFile File handler for the PCX file.
* @param width Image width
* @param height Image height
* @return True on success
*/
static BOOL CaptureHdr(HANDLE hFile, short width, short height)
{
DWORD lpNumBytes;
PCXHEADER Buffer;
memset(&Buffer, 0, sizeof(Buffer));
Buffer.Manufacturer = 10;
Buffer.Version = 5;
Buffer.Encoding = 1;
Buffer.BitsPerPixel = 8;
Buffer.Xmax = width - 1;
Buffer.Ymax = height - 1;
Buffer.HDpi = width;
Buffer.VDpi = height;
Buffer.NPlanes = 1;
Buffer.BytesPerLine = width;
return WriteFile(hFile, &Buffer, sizeof(Buffer), &lpNumBytes, NULL) && lpNumBytes == sizeof(Buffer);
}
/**
* @brief Write the current ingame palette to the PCX file
* @param hFile File handler for the PCX file.
* @param palette Current palette
* @return True if successful, else false
*/
static BOOL CapturePal(HANDLE hFile, PALETTEENTRY *palette)
{
DWORD NumberOfBytesWritten;
BYTE pcx_palette[1 + 256 * 3];
int i;
pcx_palette[0] = 12;
for (i = 0; i < 256; i++) {
pcx_palette[1 + 3 * i + 0] = palette[i].peRed;
pcx_palette[1 + 3 * i + 1] = palette[i].peGreen;
pcx_palette[1 + 3 * i + 2] = palette[i].peBlue;
}
return WriteFile(hFile, pcx_palette, sizeof(pcx_palette), &NumberOfBytesWritten, NULL) && NumberOfBytesWritten == sizeof(pcx_palette);
}
/**
* @brief RLE compress the pixel data
* @param src Raw pixel buffer
* @param dst Output buffer
* @param width Width of pixel buffer
* @return Output buffer
*/
static BYTE *CaptureEnc(BYTE *src, BYTE *dst, int width)
{
int rleLength;
do {
BYTE rlePixel = *src;
*src++;
rleLength = 1;
width--;
while (rlePixel == *src) {
if (rleLength >= 63)
break;
if (!width)
break;
rleLength++;
width--;
src++;
}
if (rleLength > 1 || rlePixel > 0xBF) {
*dst = rleLength | 0xC0;
*dst++;
}
*dst = rlePixel;
*dst++;
} while (width);
return dst;
}
/**
* @brief Write the pixel data to the PCX file
* @param hFile File handler for the PCX file.
* @param width Image width
* @param height Image height
* @param stride Buffer width
* @param pixels Raw pixel buffer
* @return True if successful, else false
*/
static BOOL CapturePix(HANDLE hFile, WORD width, WORD height, WORD stride, BYTE *pixels)
{
int writeSize;
DWORD lpNumBytes;
BYTE *pBuffer, *pBufferEnd;
pBuffer = (BYTE *)DiabloAllocPtr(2 * width);
while (height--) {
pBufferEnd = CaptureEnc(pixels, pBuffer, width);
pixels += stride;
writeSize = pBufferEnd - pBuffer;
if (!(WriteFile(hFile, pBuffer, writeSize, &lpNumBytes, NULL) && lpNumBytes == writeSize)) {
return FALSE;
}
}
mem_free_dbg(pBuffer);
return TRUE;
}
static HANDLE CaptureFile(char *dst_path)
{
BOOLEAN num_used[100];
int free_num, hFind;
struct _finddata_t finder;
memset(num_used, FALSE, sizeof(num_used));
hFind = _findfirst("screen??.PCX", &finder);
if (hFind != -1) {
do {
if (isdigit(finder.name[6]) && isdigit(finder.name[7])) {
free_num = 10 * (finder.name[6] - '0');
free_num += (finder.name[7] - '0');
num_used[free_num] = TRUE;
}
} while (_findnext(hFind, &finder) == 0);
}
for (free_num = 0; free_num < 100; free_num++) {
if (!num_used[free_num]) {
sprintf(dst_path, "screen%02d.PCX", free_num);
return CreateFile(dst_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
}
return INVALID_HANDLE_VALUE;
}
/**
* @brief Make a red version of the given palette and apply it to the screen.
* @param pal The original palette
*/
static void RedPalette(PALETTEENTRY *pal)
{
PALETTEENTRY red[256];
int i;
for (i = 0; i < 256; i++) {
red[i].peRed = pal[i].peRed;
red[i].peGreen = 0;
red[i].peBlue = 0;
red[i].peFlags = 0;
}
lpDDPalette->SetEntries(0, 0, 256, red);
}
/**
* @brief Save the current screen to a screen??.PCX (00-99) in file if available, then make the screen red for 200ms.
*/
void CaptureScreen()
{
HANDLE hObject;
PALETTEENTRY palette[256];
char FileName[MAX_PATH];
BOOL success;
hObject = CaptureFile(FileName);
if (hObject != INVALID_HANDLE_VALUE) {
DrawAndBlit();
lpDDPalette->GetEntries(0, 0, 256, palette);
RedPalette(palette);
lock_buf(2);
success = CaptureHdr(hObject, SCREEN_WIDTH, SCREEN_HEIGHT);
if (success) {
success = CapturePix(hObject, SCREEN_WIDTH, SCREEN_HEIGHT, BUFFER_WIDTH, &gpBuffer[SCREENXY(0, 0)]);
}
if (success) {
success = CapturePal(hObject, palette);
}
unlock_buf(2);
CloseHandle(hObject);
if (!success)
DeleteFile(FileName);
Sleep(300);
lpDDPalette->SetEntries(0, 0, 256, palette);
}
}