author | celestar |
Sun, 11 Mar 2007 09:28:58 +0000 | |
branch | custombridgeheads |
changeset 5651 | 335d9bd345b0 |
parent 5650 | aefc131bf5ce |
child 5860 | 7fdc9b423ba1 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1309
diff
changeset
|
4 |
#include "openttd.h" |
1299
0a6510cc889b
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
923
diff
changeset
|
5 |
#include "debug.h" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
6 |
#include "functions.h" |
1309
dab90d4cbf2d
(svn r1813) Declare functions implemented in strings.c in their own shiny new header (though i think some of these function don't belong into strings.c)
tron
parents:
1299
diff
changeset
|
7 |
#include "strings.h" |
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
430
diff
changeset
|
8 |
#include "table/strings.h" |
0 | 9 |
#include "gfx.h" |
3329
992d1f7cb747
(svn r4105) - Feature: Add proper ISO-8859-15 <> LOCALCODE conversion. As the mess that is makefile can't properly support it at the moment, it is only available for MACOSX. Windows doesn't need FS conversion and I have no idea about OS/2 so it's disabled for them.
Darkvater
parents:
3294
diff
changeset
|
10 |
#include "hal.h" |
0 | 11 |
#include "viewport.h" |
12 |
#include "player.h" |
|
430 | 13 |
#include "screenshot.h" |
2159
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2121
diff
changeset
|
14 |
#include "variables.h" |
4261
2ec8f5a9747b
(svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents:
4185
diff
changeset
|
15 |
#include "date.h" |
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
16 |
#include "helpers.hpp" |
0 | 17 |
|
2121
c86a863485ee
(svn r2631) Move screenshot related variables from variables.h to screenshot.[ch]
tron
parents:
2120
diff
changeset
|
18 |
char _screenshot_format_name[8]; |
c86a863485ee
(svn r2631) Move screenshot related variables from variables.h to screenshot.[ch]
tron
parents:
2120
diff
changeset
|
19 |
uint _num_screenshot_formats; |
c86a863485ee
(svn r2631) Move screenshot related variables from variables.h to screenshot.[ch]
tron
parents:
2120
diff
changeset
|
20 |
uint _cur_screenshot_format; |
4184
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
21 |
ScreenshotType current_screenshot_type; |
2121
c86a863485ee
(svn r2631) Move screenshot related variables from variables.h to screenshot.[ch]
tron
parents:
2120
diff
changeset
|
22 |
|
0 | 23 |
// called by the ScreenShot proc to generate screenshot lines. |
2120 | 24 |
typedef void ScreenshotCallback(void *userdata, Pixel *buf, uint y, uint pitch, uint n); |
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
25 |
typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette); |
0 | 26 |
|
27 |
typedef struct { |
|
28 |
const char *name; |
|
29 |
const char *extension; |
|
30 |
ScreenshotHandlerProc *proc; |
|
31 |
} ScreenshotFormat; |
|
32 |
||
33 |
//************************************************ |
|
34 |
//*** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP) |
|
35 |
//************************************************ |
|
923
865f9716f7f5
(svn r1411) -Fix: structure packing in the OS/2 version (eg, with the old loader).
truelight
parents:
534
diff
changeset
|
36 |
#if defined(_MSC_VER) || defined(__WATCOMC__) |
0 | 37 |
#pragma pack(push, 1) |
38 |
#endif |
|
39 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
40 |
typedef struct BitmapFileHeader { |
0 | 41 |
uint16 type; |
42 |
uint32 size; |
|
43 |
uint32 reserved; |
|
44 |
uint32 off_bits; |
|
45 |
} GCC_PACK BitmapFileHeader; |
|
46 |
assert_compile(sizeof(BitmapFileHeader) == 14); |
|
47 |
||
923
865f9716f7f5
(svn r1411) -Fix: structure packing in the OS/2 version (eg, with the old loader).
truelight
parents:
534
diff
changeset
|
48 |
#if defined(_MSC_VER) || defined(__WATCOMC__) |
0 | 49 |
#pragma pack(pop) |
50 |
#endif |
|
51 |
||
52 |
typedef struct BitmapInfoHeader { |
|
53 |
uint32 size; |
|
54 |
int32 width, height; |
|
55 |
uint16 planes, bitcount; |
|
56 |
uint32 compression, sizeimage, xpels, ypels, clrused, clrimp; |
|
57 |
} BitmapInfoHeader; |
|
58 |
assert_compile(sizeof(BitmapInfoHeader) == 40); |
|
59 |
||
60 |
typedef struct RgbQuad { |
|
61 |
byte blue, green, red, reserved; |
|
62 |
} RgbQuad; |
|
63 |
assert_compile(sizeof(RgbQuad) == 4); |
|
64 |
||
65 |
// generic .BMP writer |
|
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
66 |
static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) |
0 | 67 |
{ |
68 |
BitmapFileHeader bfh; |
|
69 |
BitmapInfoHeader bih; |
|
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
315
diff
changeset
|
70 |
RgbQuad rq[256]; |
2062 | 71 |
Pixel *buff; |
0 | 72 |
FILE *f; |
73 |
uint i, padw; |
|
74 |
uint n, maxlines; |
|
75 |
||
76 |
// only implemented for 8bit images so far. |
|
77 |
if (pixelformat != 8) |
|
78 |
return false; |
|
79 |
||
5167
2b9d61386688
(svn r7274) -Codechange [utf8]: Add input/output unicode support. This actually only changes win32
Darkvater
parents:
4912
diff
changeset
|
80 |
f = fopen(name, "wb"); |
0 | 81 |
if (f == NULL) return false; |
82 |
||
83 |
// each scanline must be aligned on a 32bit boundary |
|
2398
70de6626d65f
(svn r2924) Introduce the ALIGN() macro which aligns values to multiples of a power of 2, for exact semantics see the commment in macros.h
tron
parents:
2186
diff
changeset
|
84 |
padw = ALIGN(w, 4); |
0 | 85 |
|
86 |
// setup the file header |
|
87 |
bfh.type = TO_LE16('MB'); |
|
88 |
bfh.size = TO_LE32(sizeof(bfh) + sizeof(bih) + sizeof(RgbQuad) * 256 + padw * h); |
|
89 |
bfh.reserved = 0; |
|
90 |
bfh.off_bits = TO_LE32(sizeof(bfh) + sizeof(bih) + sizeof(RgbQuad) * 256); |
|
91 |
||
92 |
// setup the info header |
|
93 |
bih.size = TO_LE32(sizeof(BitmapInfoHeader)); |
|
94 |
bih.width = TO_LE32(w); |
|
95 |
bih.height = TO_LE32(h); |
|
96 |
bih.planes = TO_LE16(1); |
|
97 |
bih.bitcount = TO_LE16(8); |
|
98 |
bih.compression = 0; |
|
99 |
bih.sizeimage = 0; |
|
100 |
bih.xpels = 0; |
|
101 |
bih.ypels = 0; |
|
102 |
bih.clrused = 0; |
|
103 |
bih.clrimp = 0; |
|
104 |
||
105 |
// convert the palette to the windows format |
|
430 | 106 |
for (i = 0; i != 256; i++) { |
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
107 |
rq[i].red = palette[i].r; |
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
108 |
rq[i].green = palette[i].g; |
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
109 |
rq[i].blue = palette[i].b; |
0 | 110 |
rq[i].reserved = 0; |
111 |
} |
|
112 |
||
113 |
// write file header and info header and palette |
|
430 | 114 |
if (fwrite(&bfh, sizeof(bfh), 1, f) != 1) return false; |
115 |
if (fwrite(&bih, sizeof(bih), 1, f) != 1) return false; |
|
116 |
if (fwrite(rq, sizeof(rq), 1, f) != 1) return false; |
|
0 | 117 |
|
118 |
// use by default 64k temp memory |
|
119 |
maxlines = clamp(65536 / padw, 16, 128); |
|
120 |
||
121 |
// now generate the bitmap bits |
|
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
122 |
MallocT(&buff, padw * maxlines); // by default generate 128 lines at a time. |
430 | 123 |
if (buff == NULL) { |
124 |
fclose(f); |
|
125 |
return false; |
|
126 |
} |
|
0 | 127 |
memset(buff, 0, padw * maxlines); // zero the buffer to have the padding bytes set to 0 |
128 |
||
129 |
// start at the bottom, since bitmaps are stored bottom up. |
|
130 |
do { |
|
131 |
// determine # lines |
|
132 |
n = min(h, maxlines); |
|
133 |
h -= n; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
134 |
|
0 | 135 |
// render the pixels |
136 |
callb(userdata, buff, h, padw, n); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
137 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
138 |
// write each line |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
139 |
while (n) |
430 | 140 |
if (fwrite(buff + (--n) * padw, padw, 1, f) != 1) { |
141 |
free(buff); |
|
142 |
fclose(f); |
|
143 |
return false; |
|
144 |
} |
|
145 |
} while (h != 0); |
|
0 | 146 |
|
430 | 147 |
free(buff); |
0 | 148 |
fclose(f); |
149 |
||
150 |
return true; |
|
151 |
} |
|
152 |
||
153 |
//******************************************************** |
|
154 |
//*** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG) |
|
155 |
//******************************************************** |
|
156 |
#if defined(WITH_PNG) |
|
157 |
#include <png.h> |
|
158 |
||
159 |
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message) |
|
160 |
{ |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5559
diff
changeset
|
161 |
DEBUG(misc, 0, "[libpng] error: %s - %s", message, (char *)png_get_error_ptr(png_ptr)); |
0 | 162 |
longjmp(png_ptr->jmpbuf, 1); |
163 |
} |
|
164 |
||
165 |
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message) |
|
166 |
{ |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5559
diff
changeset
|
167 |
DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (char *)png_get_error_ptr(png_ptr)); |
0 | 168 |
} |
169 |
||
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
170 |
static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) |
0 | 171 |
{ |
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
315
diff
changeset
|
172 |
png_color rq[256]; |
2062 | 173 |
Pixel *buff; |
0 | 174 |
FILE *f; |
175 |
uint i, y, n; |
|
176 |
uint maxlines; |
|
177 |
png_structp png_ptr; |
|
178 |
png_infop info_ptr; |
|
179 |
||
180 |
// only implemented for 8bit images so far. |
|
181 |
if (pixelformat != 8) |
|
182 |
return false; |
|
183 |
||
5167
2b9d61386688
(svn r7274) -Codechange [utf8]: Add input/output unicode support. This actually only changes win32
Darkvater
parents:
4912
diff
changeset
|
184 |
f = fopen(name, "wb"); |
0 | 185 |
if (f == NULL) return false; |
186 |
||
430 | 187 |
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (char *)name, png_my_error, png_my_warning); |
0 | 188 |
|
430 | 189 |
if (png_ptr == NULL) { |
0 | 190 |
fclose(f); |
191 |
return false; |
|
192 |
} |
|
193 |
||
194 |
info_ptr = png_create_info_struct(png_ptr); |
|
430 | 195 |
if (info_ptr == NULL) { |
0 | 196 |
png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
197 |
fclose(f); |
|
198 |
return false; |
|
199 |
} |
|
200 |
||
201 |
if (setjmp(png_jmpbuf(png_ptr))) { |
|
202 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
|
203 |
fclose(f); |
|
204 |
return false; |
|
205 |
} |
|
206 |
||
207 |
png_init_io(png_ptr, f); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
208 |
|
0 | 209 |
png_set_filter(png_ptr, 0, PNG_FILTER_NONE); |
210 |
||
430 | 211 |
png_set_IHDR(png_ptr, info_ptr, w, h, pixelformat, PNG_COLOR_TYPE_PALETTE, |
212 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
|
0 | 213 |
|
214 |
// convert the palette to the .PNG format. |
|
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
215 |
for (i = 0; i != 256; i++) { |
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
216 |
rq[i].red = palette[i].r; |
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
217 |
rq[i].green = palette[i].g; |
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
218 |
rq[i].blue = palette[i].b; |
0 | 219 |
} |
220 |
||
221 |
png_set_PLTE(png_ptr, info_ptr, rq, 256); |
|
222 |
png_write_info(png_ptr, info_ptr); |
|
223 |
png_set_flush(png_ptr, 512); |
|
224 |
||
225 |
// use by default 64k temp memory |
|
226 |
maxlines = clamp(65536 / w, 16, 128); |
|
227 |
||
228 |
// now generate the bitmap bits |
|
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
229 |
MallocT(&buff, w * maxlines); // by default generate 128 lines at a time. |
430 | 230 |
if (buff == NULL) { |
231 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
|
232 |
fclose(f); |
|
233 |
return false; |
|
234 |
} |
|
0 | 235 |
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0 |
236 |
||
237 |
y = 0; |
|
238 |
do { |
|
239 |
// determine # lines to write |
|
240 |
n = min(h - y, maxlines); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
241 |
|
0 | 242 |
// render the pixels into the buffer |
243 |
callb(userdata, buff, y, w, n); |
|
244 |
y += n; |
|
245 |
||
246 |
// write them to png |
|
430 | 247 |
for (i = 0; i != n; i++) |
0 | 248 |
png_write_row(png_ptr, buff + i * w); |
249 |
} while (y != h); |
|
250 |
||
251 |
png_write_end(png_ptr, info_ptr); |
|
252 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
|
253 |
||
430 | 254 |
free(buff); |
0 | 255 |
fclose(f); |
256 |
return true; |
|
257 |
} |
|
4300
687a17c9c557
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4261
diff
changeset
|
258 |
#endif /* WITH_PNG */ |
0 | 259 |
|
260 |
||
261 |
//************************************************ |
|
262 |
//*** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX) |
|
263 |
//************************************************ |
|
264 |
||
265 |
typedef struct { |
|
266 |
byte manufacturer; |
|
267 |
byte version; |
|
268 |
byte rle; |
|
269 |
byte bpp; |
|
270 |
uint32 unused; |
|
271 |
uint16 xmax, ymax; |
|
272 |
uint16 hdpi, vdpi; |
|
273 |
byte pal_small[16*3]; |
|
274 |
byte reserved; |
|
275 |
byte planes; |
|
276 |
uint16 pitch; |
|
277 |
uint16 cpal; |
|
278 |
uint16 width; |
|
279 |
uint16 height; |
|
280 |
byte filler[54]; |
|
281 |
} PcxHeader; |
|
282 |
assert_compile(sizeof(PcxHeader) == 128); |
|
283 |
||
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
284 |
static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) |
0 | 285 |
{ |
2062 | 286 |
Pixel *buff; |
0 | 287 |
FILE *f; |
288 |
uint maxlines; |
|
289 |
uint y; |
|
290 |
PcxHeader pcx; |
|
4019
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
291 |
bool success; |
0 | 292 |
|
293 |
if (pixelformat != 8 || w == 0) |
|
294 |
return false; |
|
295 |
||
5167
2b9d61386688
(svn r7274) -Codechange [utf8]: Add input/output unicode support. This actually only changes win32
Darkvater
parents:
4912
diff
changeset
|
296 |
f = fopen(name, "wb"); |
0 | 297 |
if (f == NULL) return false; |
298 |
||
299 |
memset(&pcx, 0, sizeof(pcx)); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
300 |
|
0 | 301 |
// setup pcx header |
302 |
pcx.manufacturer = 10; |
|
303 |
pcx.version = 5; |
|
304 |
pcx.rle = 1; |
|
305 |
pcx.bpp = 8; |
|
430 | 306 |
pcx.xmax = TO_LE16(w - 1); |
307 |
pcx.ymax = TO_LE16(h - 1); |
|
0 | 308 |
pcx.hdpi = TO_LE16(320); |
309 |
pcx.vdpi = TO_LE16(320); |
|
310 |
||
311 |
pcx.planes = 1; |
|
312 |
pcx.cpal = TO_LE16(1); |
|
313 |
pcx.width = pcx.pitch = TO_LE16(w); |
|
314 |
pcx.height = TO_LE16(h); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
315 |
|
0 | 316 |
// write pcx header |
430 | 317 |
if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) { |
318 |
fclose(f); |
|
319 |
return false; |
|
320 |
} |
|
0 | 321 |
|
322 |
// use by default 64k temp memory |
|
323 |
maxlines = clamp(65536 / w, 16, 128); |
|
324 |
||
325 |
// now generate the bitmap bits |
|
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
326 |
MallocT(&buff, w * maxlines); // by default generate 128 lines at a time. |
430 | 327 |
if (buff == NULL) { |
328 |
fclose(f); |
|
329 |
return false; |
|
330 |
} |
|
331 |
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0 |
|
0 | 332 |
|
333 |
y = 0; |
|
334 |
do { |
|
335 |
// determine # lines to write |
|
430 | 336 |
uint n = min(h - y, maxlines); |
337 |
uint i; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
338 |
|
0 | 339 |
// render the pixels into the buffer |
340 |
callb(userdata, buff, y, w, n); |
|
341 |
y += n; |
|
342 |
||
343 |
// write them to pcx |
|
430 | 344 |
for (i = 0; i != n; i++) { |
2062 | 345 |
const Pixel* bufp = buff + i * w; |
2004 | 346 |
byte runchar = bufp[0]; |
347 |
uint runcount = 1; |
|
348 |
uint j; |
|
0 | 349 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
350 |
// for each pixel... |
2004 | 351 |
for (j = 1; j < w; j++) { |
2062 | 352 |
Pixel ch = bufp[j]; |
2004 | 353 |
|
0 | 354 |
if (ch != runchar || runcount >= 0x3f) { |
430 | 355 |
if (runcount > 1 || (runchar & 0xC0) == 0xC0) |
356 |
if (fputc(0xC0 | runcount, f) == EOF) { |
|
357 |
free(buff); |
|
358 |
fclose(f); |
|
359 |
return false; |
|
360 |
} |
|
361 |
if (fputc(runchar, f) == EOF) { |
|
362 |
free(buff); |
|
363 |
fclose(f); |
|
364 |
return false; |
|
365 |
} |
|
0 | 366 |
runcount = 0; |
367 |
runchar = ch; |
|
368 |
} |
|
369 |
runcount++; |
|
370 |
} |
|
371 |
||
372 |
// write remaining bytes.. |
|
430 | 373 |
if (runcount > 1 || (runchar & 0xC0) == 0xC0) |
374 |
if (fputc(0xC0 | runcount, f) == EOF) { |
|
375 |
free(buff); |
|
376 |
fclose(f); |
|
377 |
return false; |
|
378 |
} |
|
379 |
if (fputc(runchar, f) == EOF) { |
|
380 |
free(buff); |
|
381 |
fclose(f); |
|
382 |
return false; |
|
383 |
} |
|
0 | 384 |
} |
385 |
} while (y != h); |
|
386 |
||
430 | 387 |
free(buff); |
388 |
||
0 | 389 |
// write 8-bit color palette |
430 | 390 |
if (fputc(12, f) == EOF) { |
391 |
fclose(f); |
|
392 |
return false; |
|
393 |
} |
|
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
394 |
|
4019
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
395 |
if (sizeof(*palette) == 3) { |
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
396 |
success = fwrite(palette, 256 * sizeof(*palette), 1, f) == 1; |
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
397 |
} else { |
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
398 |
/* If the palette is word-aligned, copy it to a temporary byte array */ |
4079
30cac8061fa2
(svn r5394) Allocate a small, fixed-size array on the stack instead of malloc()ing it
tron
parents:
4019
diff
changeset
|
399 |
byte tmp[256 * 3]; |
4019
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
400 |
uint i; |
4079
30cac8061fa2
(svn r5394) Allocate a small, fixed-size array on the stack instead of malloc()ing it
tron
parents:
4019
diff
changeset
|
401 |
|
4019
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
402 |
for (i = 0; i < 256; i++) { |
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
403 |
tmp[i * 3 + 0] = palette[i].r; |
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
404 |
tmp[i * 3 + 1] = palette[i].g; |
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
405 |
tmp[i * 3 + 2] = palette[i].b; |
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
406 |
} |
4079
30cac8061fa2
(svn r5394) Allocate a small, fixed-size array on the stack instead of malloc()ing it
tron
parents:
4019
diff
changeset
|
407 |
success = fwrite(tmp, sizeof(tmp), 1, f) == 1; |
430 | 408 |
} |
4019
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
409 |
|
0 | 410 |
fclose(f); |
411 |
||
4019
96c633a49ff4
(svn r5249) - Add code to copy the palette to a temporary byte aligned array when making a PCX screenshot, if the palette array is not byte aligned.
peter1138
parents:
3329
diff
changeset
|
412 |
return success; |
0 | 413 |
} |
414 |
||
415 |
//************************************************ |
|
416 |
//*** GENERIC SCREENSHOT CODE |
|
417 |
//************************************************ |
|
418 |
||
419 |
static const ScreenshotFormat _screenshot_formats[] = { |
|
420 |
#if defined(WITH_PNG) |
|
421 |
{"PNG", "png", &MakePNGImage}, |
|
422 |
#endif |
|
4360
07fb51363337
(svn r6068) -Codechange: Switch PNG and BMP priority for screenshots. This ensures PNG is used by default when available. (hylje)
matthijs
parents:
4300
diff
changeset
|
423 |
{"BMP", "bmp", &MakeBmpImage}, |
0 | 424 |
{"PCX", "pcx", &MakePCXImage}, |
425 |
}; |
|
426 |
||
430 | 427 |
void InitializeScreenshotFormats(void) |
0 | 428 |
{ |
430 | 429 |
int i, j; |
430 |
for (i = 0, j = 0; i != lengthof(_screenshot_formats); i++) |
|
431 |
if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) { |
|
432 |
j = i; |
|
433 |
break; |
|
434 |
} |
|
0 | 435 |
_cur_screenshot_format = j; |
436 |
_num_screenshot_formats = lengthof(_screenshot_formats); |
|
4185
50a4d6493690
(svn r5627) Fix (r5626) : Forgot to rename one instance make_screenshot to current_screenshot_type
belugas
parents:
4184
diff
changeset
|
437 |
current_screenshot_type = SC_NONE; |
0 | 438 |
} |
439 |
||
440 |
const char *GetScreenshotFormatDesc(int i) |
|
441 |
{ |
|
442 |
return _screenshot_formats[i].name; |
|
443 |
} |
|
444 |
||
445 |
void SetScreenshotFormat(int i) |
|
446 |
{ |
|
447 |
_cur_screenshot_format = i; |
|
448 |
strcpy(_screenshot_format_name, _screenshot_formats[i].extension); |
|
449 |
} |
|
450 |
||
451 |
// screenshot generator that dumps the current video buffer |
|
2062 | 452 |
static void CurrentScreenCallback(void *userdata, Pixel *buf, uint y, uint pitch, uint n) |
0 | 453 |
{ |
430 | 454 |
for (; n > 0; --n) { |
315
e008762a8689
(svn r321) -Fix: crash when making png screenshot with odd resolution (Tron)
truelight
parents:
193
diff
changeset
|
455 |
memcpy(buf, _screen.dst_ptr + y * _screen.pitch, _screen.width); |
e008762a8689
(svn r321) -Fix: crash when making png screenshot with odd resolution (Tron)
truelight
parents:
193
diff
changeset
|
456 |
++y; |
e008762a8689
(svn r321) -Fix: crash when making png screenshot with odd resolution (Tron)
truelight
parents:
193
diff
changeset
|
457 |
buf += pitch; |
e008762a8689
(svn r321) -Fix: crash when making png screenshot with odd resolution (Tron)
truelight
parents:
193
diff
changeset
|
458 |
} |
0 | 459 |
} |
460 |
||
461 |
// generate a large piece of the world |
|
2062 | 462 |
static void LargeWorldCallback(void *userdata, Pixel *buf, uint y, uint pitch, uint n) |
0 | 463 |
{ |
4522 | 464 |
ViewPort *vp = (ViewPort *)userdata; |
0 | 465 |
DrawPixelInfo dpi, *old_dpi; |
466 |
int wx, left; |
|
467 |
||
468 |
old_dpi = _cur_dpi; |
|
469 |
_cur_dpi = &dpi; |
|
470 |
||
471 |
dpi.dst_ptr = buf; |
|
472 |
dpi.height = n; |
|
473 |
dpi.width = vp->width; |
|
474 |
dpi.pitch = pitch; |
|
475 |
dpi.zoom = 0; |
|
476 |
dpi.left = 0; |
|
477 |
dpi.top = y; |
|
478 |
||
479 |
left = 0; |
|
480 |
while (vp->width - left != 0) { |
|
481 |
wx = min(vp->width - left, 1600); |
|
482 |
left += wx; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
483 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
484 |
ViewportDoDraw(vp, |
0 | 485 |
((left - wx - vp->left) << vp->zoom) + vp->virtual_left, |
486 |
((y - vp->top) << vp->zoom) + vp->virtual_top, |
|
487 |
((left - vp->left) << vp->zoom) + vp->virtual_left, |
|
430 | 488 |
(((y + n) - vp->top) << vp->zoom) + vp->virtual_top |
0 | 489 |
); |
490 |
} |
|
491 |
||
492 |
_cur_dpi = old_dpi; |
|
493 |
} |
|
494 |
||
495 |
static char *MakeScreenshotName(const char *ext) |
|
496 |
{ |
|
497 |
static char filename[256]; |
|
498 |
char *base; |
|
499 |
int serial; |
|
500 |
||
4848
56549aa3e234
(svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents:
4522
diff
changeset
|
501 |
if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_player == PLAYER_SPECTATOR) { |
0 | 502 |
sprintf(_screenshot_name, "screenshot"); |
503 |
} else { |
|
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2398
diff
changeset
|
504 |
const Player* p = GetPlayer(_local_player); |
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
505 |
SetDParam(0, p->name_1); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
506 |
SetDParam(1, p->name_2); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
507 |
SetDParam(2, _date); |
4912
d04b3f2bca70
(svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents:
4848
diff
changeset
|
508 |
GetString(_screenshot_name, STR_4004, lastof(_screenshot_name)); |
0 | 509 |
} |
510 |
||
511 |
base = strchr(_screenshot_name, 0); |
|
512 |
base[0] = '.'; strcpy(base + 1, ext); |
|
513 |
||
514 |
serial = 0; |
|
430 | 515 |
for (;;) { |
5296
6a4aaa66eed3
(svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents:
5167
diff
changeset
|
516 |
snprintf(filename, sizeof(filename), "%s%s", _paths.personal_dir, _screenshot_name); |
0 | 517 |
if (!FileExists(filename)) |
518 |
break; |
|
519 |
sprintf(base, " #%d.%s", ++serial, ext); |
|
520 |
} |
|
521 |
||
522 |
return filename; |
|
523 |
} |
|
524 |
||
4184
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
525 |
void SetScreenshotType(ScreenshotType t) |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
526 |
{ |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
527 |
current_screenshot_type = t; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
528 |
} |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
529 |
|
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
530 |
bool IsScreenshotRequested(void) |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
531 |
{ |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
532 |
return (current_screenshot_type != SC_NONE); |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
533 |
} |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
534 |
|
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
535 |
static bool MakeSmallScreenshot(void) |
0 | 536 |
{ |
537 |
const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
538 |
return sf->proc(MakeScreenshotName(sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height, 8, _cur_palette); |
0 | 539 |
} |
540 |
||
4184
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
541 |
static bool MakeWorldScreenshot(void) |
0 | 542 |
{ |
543 |
ViewPort vp; |
|
544 |
const ScreenshotFormat *sf; |
|
545 |
||
4184
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
546 |
vp.zoom = 0; |
0 | 547 |
vp.left = 0; |
548 |
vp.top = 0; |
|
4184
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
549 |
vp.virtual_left = -(int)MapMaxX() * TILE_PIXELS; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
550 |
vp.virtual_top = 0; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
551 |
vp.virtual_width = (MapMaxX() + MapMaxY()) * TILE_PIXELS; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
552 |
vp.width = vp.virtual_width; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
553 |
vp.virtual_height = (MapMaxX() + MapMaxY()) * TILE_PIXELS >> 1; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
554 |
vp.height = vp.virtual_height; |
0 | 555 |
|
556 |
sf = _screenshot_formats + _cur_screenshot_format; |
|
557 |
return sf->proc(MakeScreenshotName(sf->extension), LargeWorldCallback, &vp, vp.width, vp.height, 8, _cur_palette); |
|
558 |
} |
|
4184
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
559 |
|
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
560 |
bool MakeScreenshot(void) |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
561 |
{ |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
562 |
switch (current_screenshot_type) { |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
563 |
case SC_VIEWPORT: |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
564 |
UndrawMouseCursor(); |
5559
10610f5e6e38
(svn r7556) -Fix(r7153): By handling the KeyEvent sooner in the events loops, the console command "screenshot no_con" did not had time to actually repaint the screen without the console before the screenshot. Forcing a redraw before it ensure that the feature acts as required. (to be ported to 0.5.0)
belugas
parents:
5296
diff
changeset
|
565 |
DrawDirtyBlocks(); |
4184
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
566 |
current_screenshot_type = SC_NONE; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
567 |
return MakeSmallScreenshot(); |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
568 |
case SC_WORLD: |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
569 |
current_screenshot_type = SC_NONE; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
570 |
return MakeWorldScreenshot(); |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
571 |
default: return false; |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
572 |
} |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
573 |
} |
f38abcb8b18d
(svn r5626) CodeChange : Remove the global _make_screenshot and implement a more flexible mechanism
belugas
parents:
4079
diff
changeset
|
574 |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5559
diff
changeset
|
575 |
|
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5559
diff
changeset
|
576 |