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