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