# screenshot.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "fileio_func.h"
    00014 #include "viewport_func.h"
    00015 #include "gfx_func.h"
    00016 #include "screenshot.h"
    00017 #include "blitter/factory.hpp"
    00018 #include "zoom_func.h"
    00019 #include "core/endian_func.hpp"
    00020 #include "saveload/saveload.h"
    00021 #include "company_func.h"
    00022 #include "strings_func.h"
    00023 #include "error.h"
    00024 #include "window_gui.h"
    00025 #include "window_func.h"
    00026 #include "tile_map.h"
    00027 #include "landscape.h"
    00028
    00029 #include "table/strings.h"
    00030
    00031 static const char * const SCREENSHOT_NAME = "screenshot";
    00032 static const char * const HEIGHTMAP_NAME  = "heightmap";
    00033
    00034 char _screenshot_format_name[8];
    00035 uint _num_screenshot_formats;
    00036 uint _cur_screenshot_format;
    00037 static char _screenshot_name[128];
    00038 char _full_screenshot_name[MAX_PATH];
    00039
    00048 typedef void ScreenshotCallback(void *userdata, void *buf, uint y, uint pitch, uint n);
    00049
    00061 typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette);
    00062
    00064 struct ScreenshotFormat {
    00065   const char *name;
    00066   const char *extension;
    00067   ScreenshotHandlerProc *proc;
    00068   bool supports_32bpp;
    00069 };
    00070
    00071 /*************************************************
    00072  **** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP)
    00073  *************************************************/
    00074 #if defined(_MSC_VER) || defined(__WATCOMC__)
    00075 #pragma pack(push, 1)
    00076 #endif
    00077
    00079 struct BitmapFileHeader {
    00080   uint16 type;
    00081   uint32 size;
    00082   uint32 reserved;
    00083   uint32 off_bits;
    00084 } GCC_PACK;
    00085 assert_compile(sizeof(BitmapFileHeader) == 14);
    00086
    00087 #if defined(_MSC_VER) || defined(__WATCOMC__)
    00088 #pragma pack(pop)
    00089 #endif
    00090
    00092 struct BitmapInfoHeader {
    00093   uint32 size;
    00094   int32 width, height;
    00095   uint16 planes, bitcount;
    00096   uint32 compression, sizeimage, xpels, ypels, clrused, clrimp;
    00097 };
    00098 assert_compile(sizeof(BitmapInfoHeader) == 40);
    00099
    00101 struct RgbQuad {
    00102   byte blue, green, red, reserved;
    00103 };
    00104 assert_compile(sizeof(RgbQuad) == 4);
    00105
    00118 static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
    00119 {
    00120   uint bpp; // bytes per pixel
    00121   switch (pixelformat) {
    00122     case 8:  bpp = 1; break;
    00123     /* 32bpp mode is saved as 24bpp BMP */
    00124     case 32: bpp = 3; break;
    00125     /* Only implemented for 8bit and 32bit images so far */
    00126     default: return false;
    00127   }
    00128
    00129   FILE *f = fopen(name, "wb");
    00130   if (f == NULL) return false;
    00131
    00132   /* Each scanline must be aligned on a 32bit boundary */
    00133   uint bytewidth = Align(w * bpp, 4); // bytes per line in file
    00134
    00135   /* Size of palette. Only present for 8bpp mode */
    00136   uint pal_size = pixelformat == 8 ? sizeof(RgbQuad) * 256 : 0;
    00137
    00138   /* Setup the file header */
    00139   BitmapFileHeader bfh;
    00140   bfh.type = TO_LE16('MB');
    00141   bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + bytewidth * h);
    00142   bfh.reserved = 0;
    00143   bfh.off_bits = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size);
    00144
    00145   /* Setup the info header */
    00146   BitmapInfoHeader bih;
    00147   bih.size = TO_LE32(sizeof(BitmapInfoHeader));
    00148   bih.width = TO_LE32(w);
    00149   bih.height = TO_LE32(h);
    00150   bih.planes = TO_LE16(1);
    00151   bih.bitcount = TO_LE16(bpp * 8);
    00152   bih.compression = 0;
    00153   bih.sizeimage = 0;
    00154   bih.xpels = 0;
    00155   bih.ypels = 0;
    00156   bih.clrused = 0;
    00157   bih.clrimp = 0;
    00158
    00159   /* Write file header and info header */
    00160   if (fwrite(&bfh, sizeof(bfh), 1, f) != 1 || fwrite(&bih, sizeof(bih), 1, f) != 1) {
    00161     fclose(f);
    00162     return false;
    00163   }
    00164
    00165   if (pixelformat == 8) {
    00166     /* Convert the palette to the windows format */
    00167     RgbQuad rq[256];
    00168     for (uint i = 0; i < 256; i++) {
    00169       rq[i].red   = palette[i].r;
    00170       rq[i].green = palette[i].g;
    00171       rq[i].blue  = palette[i].b;
    00172       rq[i].reserved = 0;
    00173     }
    00174     /* Write the palette */
    00175     if (fwrite(rq, sizeof(rq), 1, f) != 1) {
    00176       fclose(f);
    00177       return false;
    00178     }
    00179   }
    00180
    00181   /* Try to use 64k of memory, store between 16 and 128 lines */
    00182   uint maxlines = Clamp(65536 / (w * pixelformat / 8), 16, 128); // number of lines per iteration
    00183
    00184   uint8 *buff = MallocT<uint8>(maxlines * w * pixelformat / 8); // buffer which is rendered to
    00185   uint8 *line = AllocaM(uint8, bytewidth); // one line, stored to file
    00186   memset(line, 0, bytewidth);
    00187
    00188   /* Start at the bottom, since bitmaps are stored bottom up */
    00189   do {
    00190     uint n = min(h, maxlines);
    00191     h -= n;
    00192
    00193     /* Render the pixels */
    00194     callb(userdata, buff, h, w, n);
    00195
    00196     /* Write each line */
    00197     while (n-- != 0) {
    00198       if (pixelformat == 8) {
    00199         /* Move to 'line', leave last few pixels in line zeroed */
    00200         memcpy(line, buff + n * w, w);
    00201       } else {
    00202         /* Convert from 'native' 32bpp to BMP-like 24bpp.
    00203          * Works for both big and little endian machines */
    00204         Colour *src = ((Colour *)buff) + n * w;
    00205         byte *dst = line;
    00206         for (uint i = 0; i < w; i++) {
    00207           dst[i * 3    ] = src[i].b;
    00208           dst[i * 3 + 1] = src[i].g;
    00209           dst[i * 3 + 2] = src[i].r;
    00210         }
    00211       }
    00212       /* Write to file */
    00213       if (fwrite(line, bytewidth, 1, f) != 1) {
    00214         free(buff);
    00215         fclose(f);
    00216         return false;
    00217       }
    00218     }
    00219   } while (h != 0);
    00220
    00221   free(buff);
    00222   fclose(f);
    00223
    00224   return true;
    00225 }
    00226
    00227 /*********************************************************
    00228  **** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG)
    00229  *********************************************************/
    00230 #if defined(WITH_PNG)
    00231 #include <png.h>
    00232
    00233 #ifdef PNG_TEXT_SUPPORTED
    00234 #include "rev.h"
    00235 #include "newgrf_config.h"
    00236 #include "ai/ai_info.hpp"
    00237 #include "company_base.h"
    00238 #include "base_media_base.h"
    00239 #endif /* PNG_TEXT_SUPPORTED */
    00240
    00241 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
    00242 {
    00243   DEBUG(misc, 0, "[libpng] error: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
    00244   longjmp(png_jmpbuf(png_ptr), 1);
    00245 }
    00246
    00247 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
    00248 {
    00249   DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
    00250 }
    00251
    00264 static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
    00265 {
    00266   png_color rq[256];
    00267   FILE *f;
    00268   uint i, y, n;
    00269   uint maxlines;
    00270   uint bpp = pixelformat / 8;
    00271   png_structp png_ptr;
    00272   png_infop info_ptr;
    00273
    00274   /* only implemented for 8bit and 32bit images so far. */
    00275   if (pixelformat != 8 && pixelformat != 32) return false;
    00276
    00277   f = fopen(name, "wb");
    00278   if (f == NULL) return false;
    00279
    00280   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, const_cast<char *>(name), png_my_error, png_my_warning);
    00281
    00282   if (png_ptr == NULL) {
    00283     fclose(f);
    00284     return false;
    00285   }
    00286
    00287   info_ptr = png_create_info_struct(png_ptr);
    00288   if (info_ptr == NULL) {
    00289     png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
    00290     fclose(f);
    00291     return false;
    00292   }
    00293
    00294   if (setjmp(png_jmpbuf(png_ptr))) {
    00295     png_destroy_write_struct(&png_ptr, &info_ptr);
    00296     fclose(f);
    00297     return false;
    00298   }
    00299
    00300   png_init_io(png_ptr, f);
    00301
    00302   png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
    00303
    00304   png_set_IHDR(png_ptr, info_ptr, w, h, 8, pixelformat == 8 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
    00305     PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
    00306
    00307 #ifdef PNG_TEXT_SUPPORTED
    00308   /* Try to add some game metadata to the PNG screenshot so
    00309    * it's more useful for debugging and archival purposes. */
    00310   png_text_struct text[2];
    00311   memset(text, 0, sizeof(text));
    00312   text[0].key = const_cast<char *>("Software");
    00313   text[0].text = const_cast<char *>(_openttd_revision);
    00314   text[0].text_length = strlen(_openttd_revision);
    00315   text[0].compression = PNG_TEXT_COMPRESSION_NONE;
    00316
    00317   char buf[8192];
    00318   char *p = buf;
    00319   p += seprintf(p, lastof(buf), "Graphics set: %s (%u)\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
    00320   p = strecpy(p, "NewGRFs:\n", lastof(buf));
    00321   for (const GRFConfig *c = _game_mode == GM_MENU ? NULL : _grfconfig; c != NULL; c = c->next) {
    00322     p += seprintf(p, lastof(buf), "%08X ", BSWAP32(c->ident.grfid));
    00323     p = md5sumToString(p, lastof(buf), c->ident.md5sum);
    00324     p += seprintf(p, lastof(buf), " %s\n", c->filename);
    00325   }
    00326   p = strecpy(p, "\nCompanies:\n", lastof(buf));
    00327   const Company *c;
    00328   FOR_ALL_COMPANIES(c) {
    00329     if (c->ai_info == NULL) {
    00330       p += seprintf(p, lastof(buf), "%2i: Human\n", (int)c->index);
    00331     } else {
    00332       p += seprintf(p, lastof(buf), "%2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
    00333     }
    00334   }
    00335   text[1].key = const_cast<char *>("Description");
    00336   text[1].text = buf;
    00337   text[1].text_length = p - buf;
    00338   text[1].compression = PNG_TEXT_COMPRESSION_zTXt;
    00339   png_set_text(png_ptr, info_ptr, text, 2);
    00340 #endif /* PNG_TEXT_SUPPORTED */
    00341
    00342   if (pixelformat == 8) {
    00343     /* convert the palette to the .PNG format. */
    00344     for (i = 0; i != 256; i++) {
    00345       rq[i].red   = palette[i].r;
    00346       rq[i].green = palette[i].g;
    00347       rq[i].blue  = palette[i].b;
    00348     }
    00349
    00350     png_set_PLTE(png_ptr, info_ptr, rq, 256);
    00351   }
    00352
    00353   png_write_info(png_ptr, info_ptr);
    00354   png_set_flush(png_ptr, 512);
    00355
    00356   if (pixelformat == 32) {
    00357     png_color_8 sig_bit;
    00358
    00359     /* Save exact colour/alpha resolution */
    00360     sig_bit.alpha = 0;
    00361     sig_bit.blue  = 8;
    00362     sig_bit.green = 8;
    00363     sig_bit.red   = 8;
    00364     sig_bit.gray  = 8;
    00365     png_set_sBIT(png_ptr, info_ptr, &sig_bit);
    00366
    00367 #if TTD_ENDIAN == TTD_LITTLE_ENDIAN
    00368     png_set_bgr(png_ptr);
    00369     png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
    00370 #else
    00371     png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
    00372 #endif /* TTD_ENDIAN == TTD_LITTLE_ENDIAN */
    00373   }
    00374
    00375   /* use by default 64k temp memory */
    00376   maxlines = Clamp(65536 / w, 16, 128);
    00377
    00378   /* now generate the bitmap bits */
    00379   void *buff = CallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
    00380
    00381   y = 0;
    00382   do {
    00383     /* determine # lines to write */
    00384     n = min(h - y, maxlines);
    00385
    00386     /* render the pixels into the buffer */
    00387     callb(userdata, buff, y, w, n);
    00388     y += n;
    00389
    00390     /* write them to png */
    00391     for (i = 0; i != n; i++) {
    00392       png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
    00393     }
    00394   } while (y != h);
    00395
    00396   png_write_end(png_ptr, info_ptr);
    00397   png_destroy_write_struct(&png_ptr, &info_ptr);
    00398
    00399   free(buff);
    00400   fclose(f);
    00401   return true;
    00402 }
    00403 #endif /* WITH_PNG */
    00404
    00405
    00406 /*************************************************
    00407  **** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX)
    00408  *************************************************/
    00409
    00411 struct PcxHeader {
    00412   byte manufacturer;
    00413   byte version;
    00414   byte rle;
    00415   byte bpp;
    00416   uint32 unused;
    00417   uint16 xmax, ymax;
    00418   uint16 hdpi, vdpi;
    00419   byte pal_small[16 * 3];
    00420   byte reserved;
    00421   byte planes;
    00422   uint16 pitch;
    00423   uint16 cpal;
    00424   uint16 width;
    00425   uint16 height;
    00426   byte filler[54];
    00427 };
    00428 assert_compile(sizeof(PcxHeader) == 128);
    00429
    00442 static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
    00443 {
    00444   FILE *f;
    00445   uint maxlines;
    00446   uint y;
    00447   PcxHeader pcx;
    00448   bool success;
    00449
    00450   if (pixelformat == 32) {
    00451     DEBUG(misc, 0, "Can't convert a 32bpp screenshot to PCX format. Please pick another format.");
    00452     return false;
    00453   }
    00454   if (pixelformat != 8 || w == 0) return false;
    00455
    00456   f = fopen(name, "wb");
    00457   if (f == NULL) return false;
    00458
    00459   memset(&pcx, 0, sizeof(pcx));
    00460
    00461   /* setup pcx header */
    00462   pcx.manufacturer = 10;
    00463   pcx.version = 5;
    00464   pcx.rle = 1;
    00465   pcx.bpp = 8;
    00466   pcx.xmax = TO_LE16(w - 1);
    00467   pcx.ymax = TO_LE16(h - 1);
    00468   pcx.hdpi = TO_LE16(320);
    00469   pcx.vdpi = TO_LE16(320);
    00470
    00471   pcx.planes = 1;
    00472   pcx.cpal = TO_LE16(1);
    00473   pcx.width = pcx.pitch = TO_LE16(w);
    00474   pcx.height = TO_LE16(h);
    00475
    00476   /* write pcx header */
    00477   if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
    00478     fclose(f);
    00479     return false;
    00480   }
    00481
    00482   /* use by default 64k temp memory */
    00483   maxlines = Clamp(65536 / w, 16, 128);
    00484
    00485   /* now generate the bitmap bits */
    00486   uint8 *buff = CallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
    00487
    00488   y = 0;
    00489   do {
    00490     /* determine # lines to write */
    00491     uint n = min(h - y, maxlines);
    00492     uint i;
    00493
    00494     /* render the pixels into the buffer */
    00495     callb(userdata, buff, y, w, n);
    00496     y += n;
    00497
    00498     /* write them to pcx */
    00499     for (i = 0; i != n; i++) {
    00500       const uint8 *bufp = buff + i * w;
    00501       byte runchar = bufp[0];
    00502       uint runcount = 1;
    00503       uint j;
    00504
    00505       /* for each pixel... */
    00506       for (j = 1; j < w; j++) {
    00507         uint8 ch = bufp[j];
    00508
    00509         if (ch != runchar || runcount >= 0x3f) {
    00510           if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
    00511             if (fputc(0xC0 | runcount, f) == EOF) {
    00512               free(buff);
    00513               fclose(f);
    00514               return false;
    00515             }
    00516           }
    00517           if (fputc(runchar, f) == EOF) {
    00518             free(buff);
    00519             fclose(f);
    00520             return false;
    00521           }
    00522           runcount = 0;
    00523           runchar = ch;
    00524         }
    00525         runcount++;
    00526       }
    00527
    00528       /* write remaining bytes.. */
    00529       if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
    00530         if (fputc(0xC0 | runcount, f) == EOF) {
    00531           free(buff);
    00532           fclose(f);
    00533           return false;
    00534         }
    00535       }
    00536       if (fputc(runchar, f) == EOF) {
    00537         free(buff);
    00538         fclose(f);
    00539         return false;
    00540       }
    00541     }
    00542   } while (y != h);
    00543
    00544   free(buff);
    00545
    00546   /* write 8-bit colour palette */
    00547   if (fputc(12, f) == EOF) {
    00548     fclose(f);
    00549     return false;
    00550   }
    00551
    00552   /* Palette is word-aligned, copy it to a temporary byte array */
    00553   byte tmp[256 * 3];
    00554
    00555   for (uint i = 0; i < 256; i++) {
    00556     tmp[i * 3 + 0] = palette[i].r;
    00557     tmp[i * 3 + 1] = palette[i].g;
    00558     tmp[i * 3 + 2] = palette[i].b;
    00559   }
    00560   success = fwrite(tmp, sizeof(tmp), 1, f) == 1;
    00561
    00562   fclose(f);
    00563
    00564   return success;
    00565 }
    00566
    00567 /*************************************************
    00568  **** GENERIC SCREENSHOT CODE
    00569  *************************************************/
    00570
    00572 static const ScreenshotFormat _screenshot_formats[] = {
    00573 #if defined(WITH_PNG)
    00574   {"PNG", "png", &MakePNGImage, true},
    00575 #endif
    00576   {"BMP", "bmp", &MakeBMPImage, true},
    00577   {"PCX", "pcx", &MakePCXImage, false},
    00578 };
    00579
    00581 const char *GetCurrentScreenshotExtension()
    00582 {
    00583   return _screenshot_formats[_cur_screenshot_format].extension;
    00584 }
    00585
    00587 void InitializeScreenshotFormats()
    00588 {
    00589   uint j = 0;
    00590   for (uint i = 0; i < lengthof(_screenshot_formats); i++) {
    00591     if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) {
    00592       j = i;
    00593       break;
    00594     }
    00595   }
    00596   _cur_screenshot_format = j;
    00597   _num_screenshot_formats = lengthof(_screenshot_formats);
    00598 }
    00599
    00605 const char *GetScreenshotFormatDesc(int i)
    00606 {
    00607   return _screenshot_formats[i].name;
    00608 }
    00609
    00615 bool GetScreenshotFormatSupports_32bpp(int i)
    00616 {
    00617   return _screenshot_formats[i].supports_32bpp;
    00618 }
    00619
    00624 void SetScreenshotFormat(uint i)
    00625 {
    00626   assert(i < _num_screenshot_formats);
    00627   _cur_screenshot_format = i;
    00628   strecpy(_screenshot_format_name, _screenshot_formats[i].extension, lastof(_screenshot_format_name));
    00629 }
    00630
    00635 static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
    00636 {
    00637   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00638   void *src = blitter->MoveTo(_screen.dst_ptr, 0, y);
    00639   blitter->CopyImageToBuffer(src, buf, _screen.width, n, pitch);
    00640 }
    00641
    00650 static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
    00651 {
    00652   ViewPort *vp = (ViewPort *)userdata;
    00653   DrawPixelInfo dpi, *old_dpi;
    00654   int wx, left;
    00655
    00656   /* We are no longer rendering to the screen */
    00657   DrawPixelInfo old_screen = _screen;
    00658   bool old_disable_anim = _screen_disable_anim;
    00659
    00660   _screen.dst_ptr = buf;
    00661   _screen.width = pitch;
    00662   _screen.height = n;
    00663   _screen.pitch = pitch;
    00664   _screen_disable_anim = true;
    00665
    00666   old_dpi = _cur_dpi;
    00667   _cur_dpi = &dpi;
    00668
    00669   dpi.dst_ptr = buf;
    00670   dpi.height = n;
    00671   dpi.width = vp->width;
    00672   dpi.pitch = pitch;
    00673   dpi.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
    00674   dpi.left = 0;
    00675   dpi.top = y;
    00676
    00677   /* Render viewport in blocks of 1600 pixels width */
    00678   left = 0;
    00679   while (vp->width - left != 0) {
    00680     wx = min(vp->width - left, 1600);
    00681     left += wx;
    00682
    00683     ViewportDoDraw(vp,
    00684       ScaleByZoom(left - wx - vp->left, vp->zoom) + vp->virtual_left,
    00685       ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top,
    00686       ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
    00687       ScaleByZoom((y + n) - vp->top, vp->zoom) + vp->virtual_top
    00688     );
    00689   }
    00690
    00691   _cur_dpi = old_dpi;
    00692
    00693   /* Switch back to rendering to the screen */
    00694   _screen = old_screen;
    00695   _screen_disable_anim = old_disable_anim;
    00696 }
    00697
    00705 static const char *MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog = false)
    00706 {
    00707   bool generate = StrEmpty(_screenshot_name);
    00708
    00709   if (generate) {
    00710     if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) {
    00711       strecpy(_screenshot_name, default_fn, lastof(_screenshot_name));
    00712     } else {
    00713       GenerateDefaultSaveName(_screenshot_name, lastof(_screenshot_name));
    00714     }
    00715   }
    00716
    00717   /* Add extension to screenshot file */
    00718   size_t len = strlen(_screenshot_name);
    00719   snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, ".%s", ext);
    00720
    00721   const char *screenshot_dir = crashlog ? _personal_dir : FiosGetScreenshotDir();
    00722
    00723   for (uint serial = 1;; serial++) {
    00724     if (snprintf(_full_screenshot_name, lengthof(_full_screenshot_name), "%s%s", screenshot_dir, _screenshot_name) >= (int)lengthof(_full_screenshot_name)) {
    00725       /* We need more characters than MAX_PATH -> end with error */
    00726       _full_screenshot_name[0] = '\0';
    00727       break;
    00728     }
    00729     if (!generate) break; // allow overwriting of non-automatic filenames
    00730     if (!FileExists(_full_screenshot_name)) break;
    00731     /* If file exists try another one with same name, but just with a higher index */
    00732     snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, "#%u.%s", serial, ext);
    00733   }
    00734
    00735   return _full_screenshot_name;
    00736 }
    00737
    00739 static bool MakeSmallScreenshot(bool crashlog)
    00740 {
    00741   const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
    00742   return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension, crashlog), CurrentScreenCallback, NULL, _screen.width, _screen.height,
    00743       BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette);
    00744 }
    00745
    00751 void SetupScreenshotViewport(ScreenshotType t, ViewPort *vp)
    00752 {
    00753   /* Determine world coordinates of screenshot */
    00754   if (t == SC_WORLD) {
    00755     vp->zoom = ZOOM_LVL_WORLD_SCREENSHOT;
    00756
    00757     TileIndex north_tile = _settings_game.construction.freeform_edges ? TileXY(1, 1) : TileXY(0, 0);
    00758     TileIndex south_tile = MapSize() - 1;
    00759
    00760     /* We need to account for a hill or high building at tile 0,0. */
    00761     int extra_height_top = TilePixelHeight(north_tile) + 150;
    00762     /* If there is a hill at the bottom don't create a large black area. */
    00763     int reclaim_height_bottom = TilePixelHeight(south_tile);
    00764
    00765     vp->virtual_left   = RemapCoords(TileX(south_tile) * TILE_SIZE, TileY(north_tile) * TILE_SIZE, 0).x;
    00766     vp->virtual_top    = RemapCoords(TileX(north_tile) * TILE_SIZE, TileY(north_tile) * TILE_SIZE, extra_height_top).y;
    00767     vp->virtual_width  = RemapCoords(TileX(north_tile) * TILE_SIZE, TileY(south_tile) * TILE_SIZE, 0).x                     - vp->virtual_left + 1;
    00768     vp->virtual_height = RemapCoords(TileX(south_tile) * TILE_SIZE, TileY(south_tile) * TILE_SIZE, reclaim_height_bottom).y - vp->virtual_top  + 1;
    00769   } else {
    00770     vp->zoom = (t == SC_ZOOMEDIN) ? _settings_client.gui.zoom_min : ZOOM_LVL_VIEWPORT;
    00771
    00772     Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
    00773     vp->virtual_left   = w->viewport->virtual_left;
    00774     vp->virtual_top    = w->viewport->virtual_top;
    00775     vp->virtual_width  = w->viewport->virtual_width;
    00776     vp->virtual_height = w->viewport->virtual_height;
    00777   }
    00778
    00779   /* Compute pixel coordinates */
    00780   vp->left = 0;
    00781   vp->top = 0;
    00782   vp->width  = UnScaleByZoom(vp->virtual_width,  vp->zoom);
    00783   vp->height = UnScaleByZoom(vp->virtual_height, vp->zoom);
    00784   vp->overlay = NULL;
    00785 }
    00786
    00792 static bool MakeLargeWorldScreenshot(ScreenshotType t)
    00793 {
    00794   ViewPort vp;
    00795   SetupScreenshotViewport(t, &vp);
    00796
    00797   const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
    00798   return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension), LargeWorldCallback, &vp, vp.width, vp.height,
    00799       BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette);
    00800 }
    00801
    00811 static void HeightmapCallback(void *userdata, void *buffer, uint y, uint pitch, uint n)
    00812 {
    00813   byte *buf = (byte *)buffer;
    00814   while (n > 0) {
    00815     TileIndex ti = TileXY(MapMaxX(), y);
    00816     for (uint x = MapMaxX(); true; x--) {
    00817       *buf = 16 * TileHeight(ti);
    00818       buf++;
    00819       if (x == 0) break;
    00820       ti = TILE_ADDXY(ti, -1, 0);
    00821     }
    00822     y++;
    00823     n--;
    00824   }
    00825 }
    00826
    00831 bool MakeHeightmapScreenshot(const char *filename)
    00832 {
    00833   Colour palette[256];
    00834   for (uint i = 0; i < lengthof(palette); i++) {
    00835     palette[i].a = 0xff;
    00836     palette[i].r = i;
    00837     palette[i].g = i;
    00838     palette[i].b = i;
    00839   }
    00840   const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
    00841   return sf->proc(filename, HeightmapCallback, NULL, MapSizeX(), MapSizeY(), 8, palette);
    00842 }
    00843
    00850 bool MakeScreenshot(ScreenshotType t, const char *name)
    00851 {
    00852   if (t == SC_VIEWPORT) {
    00853     /* First draw the dirty parts of the screen and only then change the name
    00854      * of the screenshot. This way the screenshot will always show the name
    00855      * of the previous screenshot in the 'successful' message instead of the
    00856      * name of the new screenshot (or an empty name). */
    00857     UndrawMouseCursor();
    00858     DrawDirtyBlocks();
    00859   }
    00860
    00861   _screenshot_name[0] = '\0';
    00862   if (name != NULL) strecpy(_screenshot_name, name, lastof(_screenshot_name));
    00863
    00864   bool ret;
    00865   switch (t) {
    00866     case SC_VIEWPORT:
    00867       ret = MakeSmallScreenshot(false);
    00868       break;
    00869
    00870     case SC_CRASHLOG:
    00871       ret = MakeSmallScreenshot(true);
    00872       break;
    00873
    00874     case SC_ZOOMEDIN:
    00875     case SC_DEFAULTZOOM:
    00876     case SC_WORLD:
    00877       ret = MakeLargeWorldScreenshot(t);
    00878       break;
    00879
    00880     case SC_HEIGHTMAP: {
    00881       const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
    00882       ret = MakeHeightmapScreenshot(MakeScreenshotName(HEIGHTMAP_NAME, sf->extension));
    00883       break;
    00884     }
    00885
    00886     default:
    00887       NOT_REACHED();
    00888   }
    00889
    00890   if (ret) {
    00891     SetDParamStr(0, _screenshot_name);
    00892     ShowErrorMessage(STR_MESSAGE_SCREENSHOT_SUCCESSFULLY, INVALID_STRING_ID, WL_WARNING);
    00893   } else {
    00894     ShowErrorMessage(STR_ERROR_SCREENSHOT_FAILED, INVALID_STRING_ID, WL_ERROR);
    00895   }
    00896
    00897   return ret;
    00898 }