author | Darkvater |
Sat, 25 Mar 2006 09:22:10 +0000 | |
changeset 3329 | 992d1f7cb747 |
parent 3294 | be3b1e6b4b74 |
child 4019 | 96c633a49ff4 |
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" |
0 | 15 |
|
2121
c86a863485ee
(svn r2631) Move screenshot related variables from variables.h to screenshot.[ch]
tron
parents:
2120
diff
changeset
|
16 |
char _screenshot_format_name[8]; |
c86a863485ee
(svn r2631) Move screenshot related variables from variables.h to screenshot.[ch]
tron
parents:
2120
diff
changeset
|
17 |
uint _num_screenshot_formats; |
c86a863485ee
(svn r2631) Move screenshot related variables from variables.h to screenshot.[ch]
tron
parents:
2120
diff
changeset
|
18 |
uint _cur_screenshot_format; |
c86a863485ee
(svn r2631) Move screenshot related variables from variables.h to screenshot.[ch]
tron
parents:
2120
diff
changeset
|
19 |
|
0 | 20 |
// called by the ScreenShot proc to generate screenshot lines. |
2120 | 21 |
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
|
22 |
typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette); |
0 | 23 |
|
24 |
typedef struct { |
|
25 |
const char *name; |
|
26 |
const char *extension; |
|
27 |
ScreenshotHandlerProc *proc; |
|
28 |
} ScreenshotFormat; |
|
29 |
||
30 |
//************************************************ |
|
31 |
//*** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP) |
|
32 |
//************************************************ |
|
923
865f9716f7f5
(svn r1411) -Fix: structure packing in the OS/2 version (eg, with the old loader).
truelight
parents:
534
diff
changeset
|
33 |
#if defined(_MSC_VER) || defined(__WATCOMC__) |
0 | 34 |
#pragma pack(push, 1) |
35 |
#endif |
|
36 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
37 |
typedef struct BitmapFileHeader { |
0 | 38 |
uint16 type; |
39 |
uint32 size; |
|
40 |
uint32 reserved; |
|
41 |
uint32 off_bits; |
|
42 |
} GCC_PACK BitmapFileHeader; |
|
43 |
assert_compile(sizeof(BitmapFileHeader) == 14); |
|
44 |
||
923
865f9716f7f5
(svn r1411) -Fix: structure packing in the OS/2 version (eg, with the old loader).
truelight
parents:
534
diff
changeset
|
45 |
#if defined(_MSC_VER) || defined(__WATCOMC__) |
0 | 46 |
#pragma pack(pop) |
47 |
#endif |
|
48 |
||
49 |
typedef struct BitmapInfoHeader { |
|
50 |
uint32 size; |
|
51 |
int32 width, height; |
|
52 |
uint16 planes, bitcount; |
|
53 |
uint32 compression, sizeimage, xpels, ypels, clrused, clrimp; |
|
54 |
} BitmapInfoHeader; |
|
55 |
assert_compile(sizeof(BitmapInfoHeader) == 40); |
|
56 |
||
57 |
typedef struct RgbQuad { |
|
58 |
byte blue, green, red, reserved; |
|
59 |
} RgbQuad; |
|
60 |
assert_compile(sizeof(RgbQuad) == 4); |
|
61 |
||
62 |
// 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
|
63 |
static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) |
0 | 64 |
{ |
65 |
BitmapFileHeader bfh; |
|
66 |
BitmapInfoHeader bih; |
|
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
315
diff
changeset
|
67 |
RgbQuad rq[256]; |
2062 | 68 |
Pixel *buff; |
0 | 69 |
FILE *f; |
70 |
uint i, padw; |
|
71 |
uint n, maxlines; |
|
72 |
||
73 |
// only implemented for 8bit images so far. |
|
74 |
if (pixelformat != 8) |
|
75 |
return false; |
|
76 |
||
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
|
77 |
f = fopen(OTTD2FS(name), "wb"); |
0 | 78 |
if (f == NULL) return false; |
79 |
||
80 |
// 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
|
81 |
padw = ALIGN(w, 4); |
0 | 82 |
|
83 |
// setup the file header |
|
84 |
bfh.type = TO_LE16('MB'); |
|
85 |
bfh.size = TO_LE32(sizeof(bfh) + sizeof(bih) + sizeof(RgbQuad) * 256 + padw * h); |
|
86 |
bfh.reserved = 0; |
|
87 |
bfh.off_bits = TO_LE32(sizeof(bfh) + sizeof(bih) + sizeof(RgbQuad) * 256); |
|
88 |
||
89 |
// setup the info header |
|
90 |
bih.size = TO_LE32(sizeof(BitmapInfoHeader)); |
|
91 |
bih.width = TO_LE32(w); |
|
92 |
bih.height = TO_LE32(h); |
|
93 |
bih.planes = TO_LE16(1); |
|
94 |
bih.bitcount = TO_LE16(8); |
|
95 |
bih.compression = 0; |
|
96 |
bih.sizeimage = 0; |
|
97 |
bih.xpels = 0; |
|
98 |
bih.ypels = 0; |
|
99 |
bih.clrused = 0; |
|
100 |
bih.clrimp = 0; |
|
101 |
||
102 |
// convert the palette to the windows format |
|
430 | 103 |
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
|
104 |
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
|
105 |
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
|
106 |
rq[i].blue = palette[i].b; |
0 | 107 |
rq[i].reserved = 0; |
108 |
} |
|
109 |
||
110 |
// write file header and info header and palette |
|
430 | 111 |
if (fwrite(&bfh, sizeof(bfh), 1, f) != 1) return false; |
112 |
if (fwrite(&bih, sizeof(bih), 1, f) != 1) return false; |
|
113 |
if (fwrite(rq, sizeof(rq), 1, f) != 1) return false; |
|
0 | 114 |
|
115 |
// use by default 64k temp memory |
|
116 |
maxlines = clamp(65536 / padw, 16, 128); |
|
117 |
||
118 |
// now generate the bitmap bits |
|
430 | 119 |
buff = malloc(padw * maxlines); // by default generate 128 lines at a time. |
120 |
if (buff == NULL) { |
|
121 |
fclose(f); |
|
122 |
return false; |
|
123 |
} |
|
0 | 124 |
memset(buff, 0, padw * maxlines); // zero the buffer to have the padding bytes set to 0 |
125 |
||
126 |
// start at the bottom, since bitmaps are stored bottom up. |
|
127 |
do { |
|
128 |
// determine # lines |
|
129 |
n = min(h, maxlines); |
|
130 |
h -= n; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
131 |
|
0 | 132 |
// render the pixels |
133 |
callb(userdata, buff, h, padw, n); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
134 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
135 |
// write each line |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
136 |
while (n) |
430 | 137 |
if (fwrite(buff + (--n) * padw, padw, 1, f) != 1) { |
138 |
free(buff); |
|
139 |
fclose(f); |
|
140 |
return false; |
|
141 |
} |
|
142 |
} while (h != 0); |
|
0 | 143 |
|
430 | 144 |
free(buff); |
0 | 145 |
fclose(f); |
146 |
||
147 |
return true; |
|
148 |
} |
|
149 |
||
150 |
//******************************************************** |
|
151 |
//*** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG) |
|
152 |
//******************************************************** |
|
153 |
#if defined(WITH_PNG) |
|
154 |
#include <png.h> |
|
155 |
||
156 |
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message) |
|
157 |
{ |
|
2013
1b80954e9594
(svn r2521) -Codechange: Removed trailing "\n"s from DEBUG statements
celestar
parents:
2004
diff
changeset
|
158 |
DEBUG(misc, 0) ("ERROR(libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr)); |
0 | 159 |
longjmp(png_ptr->jmpbuf, 1); |
160 |
} |
|
161 |
||
162 |
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message) |
|
163 |
{ |
|
2013
1b80954e9594
(svn r2521) -Codechange: Removed trailing "\n"s from DEBUG statements
celestar
parents:
2004
diff
changeset
|
164 |
DEBUG(misc, 0) ("WARNING(libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr)); |
0 | 165 |
} |
166 |
||
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
167 |
static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) |
0 | 168 |
{ |
318
65ebd0cab39b
(svn r328) -Fix: remove some unlogical alloca()s (Tron)
darkvater
parents:
315
diff
changeset
|
169 |
png_color rq[256]; |
2062 | 170 |
Pixel *buff; |
0 | 171 |
FILE *f; |
172 |
uint i, y, n; |
|
173 |
uint maxlines; |
|
174 |
png_structp png_ptr; |
|
175 |
png_infop info_ptr; |
|
176 |
||
177 |
// only implemented for 8bit images so far. |
|
178 |
if (pixelformat != 8) |
|
179 |
return false; |
|
180 |
||
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
|
181 |
f = fopen(OTTD2FS(name), "wb"); |
0 | 182 |
if (f == NULL) return false; |
183 |
||
430 | 184 |
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (char *)name, png_my_error, png_my_warning); |
0 | 185 |
|
430 | 186 |
if (png_ptr == NULL) { |
0 | 187 |
fclose(f); |
188 |
return false; |
|
189 |
} |
|
190 |
||
191 |
info_ptr = png_create_info_struct(png_ptr); |
|
430 | 192 |
if (info_ptr == NULL) { |
0 | 193 |
png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
194 |
fclose(f); |
|
195 |
return false; |
|
196 |
} |
|
197 |
||
198 |
if (setjmp(png_jmpbuf(png_ptr))) { |
|
199 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
|
200 |
fclose(f); |
|
201 |
return false; |
|
202 |
} |
|
203 |
||
204 |
png_init_io(png_ptr, f); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
205 |
|
0 | 206 |
png_set_filter(png_ptr, 0, PNG_FILTER_NONE); |
207 |
||
430 | 208 |
png_set_IHDR(png_ptr, info_ptr, w, h, pixelformat, PNG_COLOR_TYPE_PALETTE, |
209 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
|
0 | 210 |
|
211 |
// 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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
rq[i].blue = palette[i].b; |
0 | 216 |
} |
217 |
||
218 |
png_set_PLTE(png_ptr, info_ptr, rq, 256); |
|
219 |
png_write_info(png_ptr, info_ptr); |
|
220 |
png_set_flush(png_ptr, 512); |
|
221 |
||
222 |
// use by default 64k temp memory |
|
223 |
maxlines = clamp(65536 / w, 16, 128); |
|
224 |
||
225 |
// now generate the bitmap bits |
|
430 | 226 |
buff = malloc(w * maxlines); // by default generate 128 lines at a time. |
227 |
if (buff == NULL) { |
|
228 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
|
229 |
fclose(f); |
|
230 |
return false; |
|
231 |
} |
|
0 | 232 |
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0 |
233 |
||
234 |
y = 0; |
|
235 |
do { |
|
236 |
// determine # lines to write |
|
237 |
n = min(h - y, maxlines); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
238 |
|
0 | 239 |
// render the pixels into the buffer |
240 |
callb(userdata, buff, y, w, n); |
|
241 |
y += n; |
|
242 |
||
243 |
// write them to png |
|
430 | 244 |
for (i = 0; i != n; i++) |
0 | 245 |
png_write_row(png_ptr, buff + i * w); |
246 |
} while (y != h); |
|
247 |
||
248 |
png_write_end(png_ptr, info_ptr); |
|
249 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
|
250 |
||
430 | 251 |
free(buff); |
0 | 252 |
fclose(f); |
253 |
return true; |
|
254 |
} |
|
255 |
#endif // WITH_PNG |
|
256 |
||
257 |
||
258 |
//************************************************ |
|
259 |
//*** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX) |
|
260 |
//************************************************ |
|
261 |
||
262 |
typedef struct { |
|
263 |
byte manufacturer; |
|
264 |
byte version; |
|
265 |
byte rle; |
|
266 |
byte bpp; |
|
267 |
uint32 unused; |
|
268 |
uint16 xmax, ymax; |
|
269 |
uint16 hdpi, vdpi; |
|
270 |
byte pal_small[16*3]; |
|
271 |
byte reserved; |
|
272 |
byte planes; |
|
273 |
uint16 pitch; |
|
274 |
uint16 cpal; |
|
275 |
uint16 width; |
|
276 |
uint16 height; |
|
277 |
byte filler[54]; |
|
278 |
} PcxHeader; |
|
279 |
assert_compile(sizeof(PcxHeader) == 128); |
|
280 |
||
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
281 |
static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) |
0 | 282 |
{ |
2062 | 283 |
Pixel *buff; |
0 | 284 |
FILE *f; |
285 |
uint maxlines; |
|
286 |
uint y; |
|
287 |
PcxHeader pcx; |
|
288 |
||
289 |
if (pixelformat != 8 || w == 0) |
|
290 |
return false; |
|
291 |
||
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
|
292 |
f = fopen(OTTD2FS(name), "wb"); |
0 | 293 |
if (f == NULL) return false; |
294 |
||
295 |
memset(&pcx, 0, sizeof(pcx)); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
296 |
|
0 | 297 |
// setup pcx header |
298 |
pcx.manufacturer = 10; |
|
299 |
pcx.version = 5; |
|
300 |
pcx.rle = 1; |
|
301 |
pcx.bpp = 8; |
|
430 | 302 |
pcx.xmax = TO_LE16(w - 1); |
303 |
pcx.ymax = TO_LE16(h - 1); |
|
0 | 304 |
pcx.hdpi = TO_LE16(320); |
305 |
pcx.vdpi = TO_LE16(320); |
|
306 |
||
307 |
pcx.planes = 1; |
|
308 |
pcx.cpal = TO_LE16(1); |
|
309 |
pcx.width = pcx.pitch = TO_LE16(w); |
|
310 |
pcx.height = TO_LE16(h); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
311 |
|
0 | 312 |
// write pcx header |
430 | 313 |
if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) { |
314 |
fclose(f); |
|
315 |
return false; |
|
316 |
} |
|
0 | 317 |
|
318 |
// use by default 64k temp memory |
|
319 |
maxlines = clamp(65536 / w, 16, 128); |
|
320 |
||
321 |
// now generate the bitmap bits |
|
430 | 322 |
buff = malloc(w * maxlines); // by default generate 128 lines at a time. |
323 |
if (buff == NULL) { |
|
324 |
fclose(f); |
|
325 |
return false; |
|
326 |
} |
|
327 |
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0 |
|
0 | 328 |
|
329 |
y = 0; |
|
330 |
do { |
|
331 |
// determine # lines to write |
|
430 | 332 |
uint n = min(h - y, maxlines); |
333 |
uint i; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
334 |
|
0 | 335 |
// render the pixels into the buffer |
336 |
callb(userdata, buff, y, w, n); |
|
337 |
y += n; |
|
338 |
||
339 |
// write them to pcx |
|
430 | 340 |
for (i = 0; i != n; i++) { |
2062 | 341 |
const Pixel* bufp = buff + i * w; |
2004 | 342 |
byte runchar = bufp[0]; |
343 |
uint runcount = 1; |
|
344 |
uint j; |
|
0 | 345 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
346 |
// for each pixel... |
2004 | 347 |
for (j = 1; j < w; j++) { |
2062 | 348 |
Pixel ch = bufp[j]; |
2004 | 349 |
|
0 | 350 |
if (ch != runchar || runcount >= 0x3f) { |
430 | 351 |
if (runcount > 1 || (runchar & 0xC0) == 0xC0) |
352 |
if (fputc(0xC0 | runcount, f) == EOF) { |
|
353 |
free(buff); |
|
354 |
fclose(f); |
|
355 |
return false; |
|
356 |
} |
|
357 |
if (fputc(runchar, f) == EOF) { |
|
358 |
free(buff); |
|
359 |
fclose(f); |
|
360 |
return false; |
|
361 |
} |
|
0 | 362 |
runcount = 0; |
363 |
runchar = ch; |
|
364 |
} |
|
365 |
runcount++; |
|
366 |
} |
|
367 |
||
368 |
// write remaining bytes.. |
|
430 | 369 |
if (runcount > 1 || (runchar & 0xC0) == 0xC0) |
370 |
if (fputc(0xC0 | runcount, f) == EOF) { |
|
371 |
free(buff); |
|
372 |
fclose(f); |
|
373 |
return false; |
|
374 |
} |
|
375 |
if (fputc(runchar, f) == EOF) { |
|
376 |
free(buff); |
|
377 |
fclose(f); |
|
378 |
return false; |
|
379 |
} |
|
0 | 380 |
} |
381 |
} while (y != h); |
|
382 |
||
430 | 383 |
free(buff); |
384 |
||
0 | 385 |
// write 8-bit color palette |
430 | 386 |
if (fputc(12, f) == EOF) { |
387 |
fclose(f); |
|
388 |
return false; |
|
389 |
} |
|
1991
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
390 |
|
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
391 |
{assert_compile(sizeof(*palette) == 3);} |
f3d5e35731a2
(svn r2497) Use a struct array for palette entries instead of a flat byte array
tron
parents:
1891
diff
changeset
|
392 |
if (fwrite(palette, 256 * sizeof(*palette), 1, f) != 1) { |
430 | 393 |
fclose(f); |
394 |
return false; |
|
395 |
} |
|
0 | 396 |
fclose(f); |
397 |
||
398 |
return true; |
|
399 |
} |
|
400 |
||
401 |
//************************************************ |
|
402 |
//*** GENERIC SCREENSHOT CODE |
|
403 |
//************************************************ |
|
404 |
||
405 |
static const ScreenshotFormat _screenshot_formats[] = { |
|
406 |
{"BMP", "bmp", &MakeBmpImage}, |
|
407 |
#if defined(WITH_PNG) |
|
408 |
{"PNG", "png", &MakePNGImage}, |
|
409 |
#endif |
|
410 |
{"PCX", "pcx", &MakePCXImage}, |
|
411 |
}; |
|
412 |
||
430 | 413 |
void InitializeScreenshotFormats(void) |
0 | 414 |
{ |
430 | 415 |
int i, j; |
416 |
for (i = 0, j = 0; i != lengthof(_screenshot_formats); i++) |
|
417 |
if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) { |
|
418 |
j = i; |
|
419 |
break; |
|
420 |
} |
|
0 | 421 |
_cur_screenshot_format = j; |
422 |
_num_screenshot_formats = lengthof(_screenshot_formats); |
|
423 |
} |
|
424 |
||
425 |
const char *GetScreenshotFormatDesc(int i) |
|
426 |
{ |
|
427 |
return _screenshot_formats[i].name; |
|
428 |
} |
|
429 |
||
430 |
void SetScreenshotFormat(int i) |
|
431 |
{ |
|
432 |
_cur_screenshot_format = i; |
|
433 |
strcpy(_screenshot_format_name, _screenshot_formats[i].extension); |
|
434 |
} |
|
435 |
||
436 |
// screenshot generator that dumps the current video buffer |
|
2062 | 437 |
static void CurrentScreenCallback(void *userdata, Pixel *buf, uint y, uint pitch, uint n) |
0 | 438 |
{ |
430 | 439 |
for (; n > 0; --n) { |
315
e008762a8689
(svn r321) -Fix: crash when making png screenshot with odd resolution (Tron)
truelight
parents:
193
diff
changeset
|
440 |
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
|
441 |
++y; |
e008762a8689
(svn r321) -Fix: crash when making png screenshot with odd resolution (Tron)
truelight
parents:
193
diff
changeset
|
442 |
buf += pitch; |
e008762a8689
(svn r321) -Fix: crash when making png screenshot with odd resolution (Tron)
truelight
parents:
193
diff
changeset
|
443 |
} |
0 | 444 |
} |
445 |
||
446 |
// generate a large piece of the world |
|
2062 | 447 |
static void LargeWorldCallback(void *userdata, Pixel *buf, uint y, uint pitch, uint n) |
0 | 448 |
{ |
449 |
ViewPort *vp = (ViewPort *)userdata; |
|
450 |
DrawPixelInfo dpi, *old_dpi; |
|
451 |
int wx, left; |
|
452 |
||
453 |
old_dpi = _cur_dpi; |
|
454 |
_cur_dpi = &dpi; |
|
455 |
||
456 |
dpi.dst_ptr = buf; |
|
457 |
dpi.height = n; |
|
458 |
dpi.width = vp->width; |
|
459 |
dpi.pitch = pitch; |
|
460 |
dpi.zoom = 0; |
|
461 |
dpi.left = 0; |
|
462 |
dpi.top = y; |
|
463 |
||
464 |
left = 0; |
|
465 |
while (vp->width - left != 0) { |
|
466 |
wx = min(vp->width - left, 1600); |
|
467 |
left += wx; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
468 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
469 |
ViewportDoDraw(vp, |
0 | 470 |
((left - wx - vp->left) << vp->zoom) + vp->virtual_left, |
471 |
((y - vp->top) << vp->zoom) + vp->virtual_top, |
|
472 |
((left - vp->left) << vp->zoom) + vp->virtual_left, |
|
430 | 473 |
(((y + n) - vp->top) << vp->zoom) + vp->virtual_top |
0 | 474 |
); |
475 |
} |
|
476 |
||
477 |
_cur_dpi = old_dpi; |
|
478 |
} |
|
479 |
||
480 |
static char *MakeScreenshotName(const char *ext) |
|
481 |
{ |
|
482 |
static char filename[256]; |
|
483 |
char *base; |
|
484 |
int serial; |
|
485 |
||
2693
4c9f68543198
(svn r3235) - Fix: fix two crashes, one with making a screenshot in the main menu without any game loaded, another when failing to load a scenario in 'New Game'
Darkvater
parents:
2548
diff
changeset
|
486 |
if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_player == OWNER_SPECTATOR) { |
0 | 487 |
sprintf(_screenshot_name, "screenshot"); |
488 |
} 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
|
489 |
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
|
490 |
SetDParam(0, p->name_1); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
491 |
SetDParam(1, p->name_2); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
492 |
SetDParam(2, _date); |
0 | 493 |
GetString(_screenshot_name, STR_4004); |
494 |
} |
|
495 |
||
496 |
base = strchr(_screenshot_name, 0); |
|
497 |
base[0] = '.'; strcpy(base + 1, ext); |
|
498 |
||
499 |
serial = 0; |
|
430 | 500 |
for (;;) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
1
diff
changeset
|
501 |
snprintf(filename, sizeof(filename), "%s%s", _path.personal_dir, _screenshot_name); |
0 | 502 |
if (!FileExists(filename)) |
503 |
break; |
|
504 |
sprintf(base, " #%d.%s", ++serial, ext); |
|
505 |
} |
|
506 |
||
507 |
return filename; |
|
508 |
} |
|
509 |
||
430 | 510 |
bool MakeScreenshot(void) |
0 | 511 |
{ |
512 |
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
|
513 |
return sf->proc(MakeScreenshotName(sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height, 8, _cur_palette); |
0 | 514 |
} |
515 |
||
516 |
bool MakeWorldScreenshot(int left, int top, int width, int height, int zoom) |
|
517 |
{ |
|
518 |
ViewPort vp; |
|
519 |
const ScreenshotFormat *sf; |
|
520 |
||
521 |
vp.zoom = zoom; |
|
522 |
vp.left = 0; |
|
523 |
vp.top = 0; |
|
524 |
vp.virtual_width = width; |
|
525 |
vp.width = width >> zoom; |
|
526 |
vp.virtual_height = height; |
|
527 |
vp.height = height >> zoom; |
|
528 |
vp.virtual_left = left; |
|
529 |
vp.virtual_top = top; |
|
530 |
||
531 |
sf = _screenshot_formats + _cur_screenshot_format; |
|
532 |
return sf->proc(MakeScreenshotName(sf->extension), LargeWorldCallback, &vp, vp.width, vp.height, 8, _cur_palette); |
|
533 |
} |