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 "gui.h"
00024 #include "window_gui.h"
00025 #include "window_func.h"
00026 #include "tile_map.h"
00027 
00028 #include "table/strings.h"
00029 
00030 
00031 char _screenshot_format_name[8];      
00032 uint _num_screenshot_formats;         
00033 uint _cur_screenshot_format;          
00034 static char _screenshot_name[128];    
00035 char _full_screenshot_name[MAX_PATH]; 
00036 
00045 typedef void ScreenshotCallback(void *userdata, void *buf, uint y, uint pitch, uint n);
00046 
00058 typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette);
00059 
00061 struct ScreenshotFormat {
00062   const char *name;            
00063   const char *extension;       
00064   ScreenshotHandlerProc *proc; 
00065 };
00066 
00067 /*************************************************
00068  **** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP)
00069  *************************************************/
00070 #if defined(_MSC_VER) || defined(__WATCOMC__)
00071 #pragma pack(push, 1)
00072 #endif
00073 
00075 struct BitmapFileHeader {
00076   uint16 type;
00077   uint32 size;
00078   uint32 reserved;
00079   uint32 off_bits;
00080 } GCC_PACK;
00081 assert_compile(sizeof(BitmapFileHeader) == 14);
00082 
00083 #if defined(_MSC_VER) || defined(__WATCOMC__)
00084 #pragma pack(pop)
00085 #endif
00086 
00088 struct BitmapInfoHeader {
00089   uint32 size;
00090   int32 width, height;
00091   uint16 planes, bitcount;
00092   uint32 compression, sizeimage, xpels, ypels, clrused, clrimp;
00093 };
00094 assert_compile(sizeof(BitmapInfoHeader) == 40);
00095 
00097 struct RgbQuad {
00098   byte blue, green, red, reserved;
00099 };
00100 assert_compile(sizeof(RgbQuad) == 4);
00101 
00114 static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00115 {
00116   uint bpp; // bytes per pixel
00117   switch (pixelformat) {
00118     case 8:  bpp = 1; break;
00119     /* 32bpp mode is saved as 24bpp BMP */
00120     case 32: bpp = 3; break;
00121     /* Only implemented for 8bit and 32bit images so far */
00122     default: return false;
00123   }
00124 
00125   FILE *f = fopen(name, "wb");
00126   if (f == NULL) return false;
00127 
00128   /* Each scanline must be aligned on a 32bit boundary */
00129   uint bytewidth = Align(w * bpp, 4); // bytes per line in file
00130 
00131   /* Size of palette. Only present for 8bpp mode */
00132   uint pal_size = pixelformat == 8 ? sizeof(RgbQuad) * 256 : 0;
00133 
00134   /* Setup the file header */
00135   BitmapFileHeader bfh;
00136   bfh.type = TO_LE16('MB');
00137   bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + bytewidth * h);
00138   bfh.reserved = 0;
00139   bfh.off_bits = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size);
00140 
00141   /* Setup the info header */
00142   BitmapInfoHeader bih;
00143   bih.size = TO_LE32(sizeof(BitmapInfoHeader));
00144   bih.width = TO_LE32(w);
00145   bih.height = TO_LE32(h);
00146   bih.planes = TO_LE16(1);
00147   bih.bitcount = TO_LE16(bpp * 8);
00148   bih.compression = 0;
00149   bih.sizeimage = 0;
00150   bih.xpels = 0;
00151   bih.ypels = 0;
00152   bih.clrused = 0;
00153   bih.clrimp = 0;
00154 
00155   /* Write file header and info header */
00156   if (fwrite(&bfh, sizeof(bfh), 1, f) != 1 || fwrite(&bih, sizeof(bih), 1, f) != 1) {
00157     fclose(f);
00158     return false;
00159   }
00160 
00161   if (pixelformat == 8) {
00162     /* Convert the palette to the windows format */
00163     RgbQuad rq[256];
00164     for (uint i = 0; i < 256; i++) {
00165       rq[i].red   = palette[i].r;
00166       rq[i].green = palette[i].g;
00167       rq[i].blue  = palette[i].b;
00168       rq[i].reserved = 0;
00169     }
00170     /* Write the palette */
00171     if (fwrite(rq, sizeof(rq), 1, f) != 1) {
00172       fclose(f);
00173       return false;
00174     }
00175   }
00176 
00177   /* Try to use 64k of memory, store between 16 and 128 lines */
00178   uint maxlines = Clamp(65536 / (w * pixelformat / 8), 16, 128); // number of lines per iteration
00179 
00180   uint8 *buff = MallocT<uint8>(maxlines * w * pixelformat / 8); // buffer which is rendered to
00181   uint8 *line = AllocaM(uint8, bytewidth); // one line, stored to file
00182   memset(line, 0, bytewidth);
00183 
00184   /* Start at the bottom, since bitmaps are stored bottom up */
00185   do {
00186     uint n = min(h, maxlines);
00187     h -= n;
00188 
00189     /* Render the pixels */
00190     callb(userdata, buff, h, w, n);
00191 
00192     /* Write each line */
00193     while (n-- != 0) {
00194       if (pixelformat == 8) {
00195         /* Move to 'line', leave last few pixels in line zeroed */
00196         memcpy(line, buff + n * w, w);
00197       } else {
00198         /* Convert from 'native' 32bpp to BMP-like 24bpp.
00199          * Works for both big and little endian machines */
00200         Colour *src = ((Colour *)buff) + n * w;
00201         byte *dst = line;
00202         for (uint i = 0; i < w; i++) {
00203           dst[i * 3    ] = src[i].b;
00204           dst[i * 3 + 1] = src[i].g;
00205           dst[i * 3 + 2] = src[i].r;
00206         }
00207       }
00208       /* Write to file */
00209       if (fwrite(line, bytewidth, 1, f) != 1) {
00210         free(buff);
00211         fclose(f);
00212         return false;
00213       }
00214     }
00215   } while (h != 0);
00216 
00217   free(buff);
00218   fclose(f);
00219 
00220   return true;
00221 }
00222 
00223 /*********************************************************
00224  **** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG)
00225  *********************************************************/
00226 #if defined(WITH_PNG)
00227 #include <png.h>
00228 
00229 #ifdef PNG_TEXT_SUPPORTED
00230 #include "rev.h"
00231 #include "newgrf_config.h"
00232 #include "ai/ai_info.hpp"
00233 #include "company_base.h"
00234 #include "base_media_base.h"
00235 #endif /* PNG_TEXT_SUPPORTED */
00236 
00237 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
00238 {
00239   DEBUG(misc, 0, "[libpng] error: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
00240   longjmp(png_jmpbuf(png_ptr), 1);
00241 }
00242 
00243 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
00244 {
00245   DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
00246 }
00247 
00260 static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00261 {
00262   png_color rq[256];
00263   FILE *f;
00264   uint i, y, n;
00265   uint maxlines;
00266   uint bpp = pixelformat / 8;
00267   png_structp png_ptr;
00268   png_infop info_ptr;
00269 
00270   /* only implemented for 8bit and 32bit images so far. */
00271   if (pixelformat != 8 && pixelformat != 32) return false;
00272 
00273   f = fopen(name, "wb");
00274   if (f == NULL) return false;
00275 
00276   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (void *)name, png_my_error, png_my_warning);
00277 
00278   if (png_ptr == NULL) {
00279     fclose(f);
00280     return false;
00281   }
00282 
00283   info_ptr = png_create_info_struct(png_ptr);
00284   if (info_ptr == NULL) {
00285     png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
00286     fclose(f);
00287     return false;
00288   }
00289 
00290   if (setjmp(png_jmpbuf(png_ptr))) {
00291     png_destroy_write_struct(&png_ptr, &info_ptr);
00292     fclose(f);
00293     return false;
00294   }
00295 
00296   png_init_io(png_ptr, f);
00297 
00298   png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
00299 
00300   png_set_IHDR(png_ptr, info_ptr, w, h, 8, pixelformat == 8 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
00301     PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
00302 
00303 #ifdef PNG_TEXT_SUPPORTED
00304   /* Try to add some game metadata to the PNG screenshot so
00305    * it's more useful for debugging and archival purposes. */
00306   png_text_struct text[2];
00307   memset(text, 0, sizeof(text));
00308   text[0].key = const_cast<char *>("Software");
00309   text[0].text = const_cast<char *>(_openttd_revision);
00310   text[0].text_length = strlen(_openttd_revision);
00311   text[0].compression = PNG_TEXT_COMPRESSION_NONE;
00312 
00313   char buf[8192];
00314   char *p = buf;
00315   p += seprintf(p, lastof(buf), "Graphics set: %s (%u)\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
00316   p = strecpy(p, "NewGRFs:\n", lastof(buf));
00317   for (const GRFConfig *c = _game_mode == GM_MENU ? NULL : _grfconfig; c != NULL; c = c->next) {
00318     p += seprintf(p, lastof(buf), "%08X ", BSWAP32(c->ident.grfid));
00319     p = md5sumToString(p, lastof(buf), c->ident.md5sum);
00320     p += seprintf(p, lastof(buf), " %s\n", c->filename);
00321   }
00322   p = strecpy(p, "\nCompanies:\n", lastof(buf));
00323   const Company *c;
00324   FOR_ALL_COMPANIES(c) {
00325     if (c->ai_info == NULL) {
00326       p += seprintf(p, lastof(buf), "%2i: Human\n", (int)c->index);
00327     } else {
00328 #ifdef ENABLE_AI
00329       p += seprintf(p, lastof(buf), "%2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
00330 #endif /* ENABLE_AI */
00331     }
00332   }
00333   text[1].key = const_cast<char *>("Description");
00334   text[1].text = buf;
00335   text[1].text_length = p - buf;
00336   text[1].compression = PNG_TEXT_COMPRESSION_zTXt;
00337   png_set_text(png_ptr, info_ptr, text, 2);
00338 #endif /* PNG_TEXT_SUPPORTED */
00339 
00340   if (pixelformat == 8) {
00341     /* convert the palette to the .PNG format. */
00342     for (i = 0; i != 256; i++) {
00343       rq[i].red   = palette[i].r;
00344       rq[i].green = palette[i].g;
00345       rq[i].blue  = palette[i].b;
00346     }
00347 
00348     png_set_PLTE(png_ptr, info_ptr, rq, 256);
00349   }
00350 
00351   png_write_info(png_ptr, info_ptr);
00352   png_set_flush(png_ptr, 512);
00353 
00354   if (pixelformat == 32) {
00355     png_color_8 sig_bit;
00356 
00357     /* Save exact colour/alpha resolution */
00358     sig_bit.alpha = 0;
00359     sig_bit.blue  = 8;
00360     sig_bit.green = 8;
00361     sig_bit.red   = 8;
00362     sig_bit.gray  = 8;
00363     png_set_sBIT(png_ptr, info_ptr, &sig_bit);
00364 
00365 #if TTD_ENDIAN == TTD_LITTLE_ENDIAN
00366     png_set_bgr(png_ptr);
00367     png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
00368 #else
00369     png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
00370 #endif /* TTD_ENDIAN == TTD_LITTLE_ENDIAN */
00371   }
00372 
00373   /* use by default 64k temp memory */
00374   maxlines = Clamp(65536 / w, 16, 128);
00375 
00376   /* now generate the bitmap bits */
00377   void *buff = CallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
00378 
00379   y = 0;
00380   do {
00381     /* determine # lines to write */
00382     n = min(h - y, maxlines);
00383 
00384     /* render the pixels into the buffer */
00385     callb(userdata, buff, y, w, n);
00386     y += n;
00387 
00388     /* write them to png */
00389     for (i = 0; i != n; i++) {
00390       png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
00391     }
00392   } while (y != h);
00393 
00394   png_write_end(png_ptr, info_ptr);
00395   png_destroy_write_struct(&png_ptr, &info_ptr);
00396 
00397   free(buff);
00398   fclose(f);
00399   return true;
00400 }
00401 #endif /* WITH_PNG */
00402 
00403 
00404 /*************************************************
00405  **** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX)
00406  *************************************************/
00407 
00409 struct PcxHeader {
00410   byte manufacturer;
00411   byte version;
00412   byte rle;
00413   byte bpp;
00414   uint32 unused;
00415   uint16 xmax, ymax;
00416   uint16 hdpi, vdpi;
00417   byte pal_small[16 * 3];
00418   byte reserved;
00419   byte planes;
00420   uint16 pitch;
00421   uint16 cpal;
00422   uint16 width;
00423   uint16 height;
00424   byte filler[54];
00425 };
00426 assert_compile(sizeof(PcxHeader) == 128);
00427 
00440 static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
00441 {
00442   FILE *f;
00443   uint maxlines;
00444   uint y;
00445   PcxHeader pcx;
00446   bool success;
00447 
00448   if (pixelformat == 32) {
00449     DEBUG(misc, 0, "Can't convert a 32bpp screenshot to PCX format. Please pick another format.");
00450     return false;
00451   }
00452   if (pixelformat != 8 || w == 0) return false;
00453 
00454   f = fopen(name, "wb");
00455   if (f == NULL) return false;
00456 
00457   memset(&pcx, 0, sizeof(pcx));
00458 
00459   /* setup pcx header */
00460   pcx.manufacturer = 10;
00461   pcx.version = 5;
00462   pcx.rle = 1;
00463   pcx.bpp = 8;
00464   pcx.xmax = TO_LE16(w - 1);
00465   pcx.ymax = TO_LE16(h - 1);
00466   pcx.hdpi = TO_LE16(320);
00467   pcx.vdpi = TO_LE16(320);
00468 
00469   pcx.planes = 1;
00470   pcx.cpal = TO_LE16(1);
00471   pcx.width = pcx.pitch = TO_LE16(w);
00472   pcx.height = TO_LE16(h);
00473 
00474   /* write pcx header */
00475   if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
00476     fclose(f);
00477     return false;
00478   }
00479 
00480   /* use by default 64k temp memory */
00481   maxlines = Clamp(65536 / w, 16, 128);
00482 
00483   /* now generate the bitmap bits */
00484   uint8 *buff = CallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
00485 
00486   y = 0;
00487   do {
00488     /* determine # lines to write */
00489     uint n = min(h - y, maxlines);
00490     uint i;
00491 
00492     /* render the pixels into the buffer */
00493     callb(userdata, buff, y, w, n);
00494     y += n;
00495 
00496     /* write them to pcx */
00497     for (i = 0; i != n; i++) {
00498       const uint8 *bufp = buff + i * w;
00499       byte runchar = bufp[0];
00500       uint runcount = 1;
00501       uint j;
00502 
00503       /* for each pixel... */
00504       for (j = 1; j < w; j++) {
00505         uint8 ch = bufp[j];
00506 
00507         if (ch != runchar || runcount >= 0x3f) {
00508           if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
00509             if (fputc(0xC0 | runcount, f) == EOF) {
00510               free(buff);
00511               fclose(f);
00512               return false;
00513             }
00514           }
00515           if (fputc(runchar, f) == EOF) {
00516             free(buff);
00517             fclose(f);
00518             return false;
00519           }
00520           runcount = 0;
00521           runchar = ch;
00522         }
00523         runcount++;
00524       }
00525 
00526       /* write remaining bytes.. */
00527       if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
00528         if (fputc(0xC0 | runcount, f) == EOF) {
00529           free(buff);
00530           fclose(f);
00531           return false;
00532         }
00533       }
00534       if (fputc(runchar, f) == EOF) {
00535         free(buff);
00536         fclose(f);
00537         return false;
00538       }
00539     }
00540   } while (y != h);
00541 
00542   free(buff);
00543 
00544   /* write 8-bit colour palette */
00545   if (fputc(12, f) == EOF) {
00546     fclose(f);
00547     return false;
00548   }
00549 
00550   /* Palette is word-aligned, copy it to a temporary byte array */
00551   byte tmp[256 * 3];
00552 
00553   for (uint i = 0; i < 256; i++) {
00554     tmp[i * 3 + 0] = palette[i].r;
00555     tmp[i * 3 + 1] = palette[i].g;
00556     tmp[i * 3 + 2] = palette[i].b;
00557   }
00558   success = fwrite(tmp, sizeof(tmp), 1, f) == 1;
00559 
00560   fclose(f);
00561 
00562   return success;
00563 }
00564 
00565 /*************************************************
00566  **** GENERIC SCREENSHOT CODE
00567  *************************************************/
00568 
00570 static const ScreenshotFormat _screenshot_formats[] = {
00571 #if defined(WITH_PNG)
00572   {"PNG", "png", &MakePNGImage},
00573 #endif
00574   {"BMP", "bmp", &MakeBMPImage},
00575   {"PCX", "pcx", &MakePCXImage},
00576 };
00577 
00579 void InitializeScreenshotFormats()
00580 {
00581   uint j = 0;
00582   for (uint i = 0; i < lengthof(_screenshot_formats); i++) {
00583     if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) {
00584       j = i;
00585       break;
00586     }
00587   }
00588   _cur_screenshot_format = j;
00589   _num_screenshot_formats = lengthof(_screenshot_formats);
00590 }
00591 
00597 const char *GetScreenshotFormatDesc(int i)
00598 {
00599   return _screenshot_formats[i].name;
00600 }
00601 
00606 void SetScreenshotFormat(uint i)
00607 {
00608   assert(i < _num_screenshot_formats);
00609   _cur_screenshot_format = i;
00610   strecpy(_screenshot_format_name, _screenshot_formats[i].extension, lastof(_screenshot_format_name));
00611 }
00612 
00617 static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
00618 {
00619   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00620   void *src = blitter->MoveTo(_screen.dst_ptr, 0, y);
00621   blitter->CopyImageToBuffer(src, buf, _screen.width, n, pitch);
00622 }
00623 
00632 static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
00633 {
00634   ViewPort *vp = (ViewPort *)userdata;
00635   DrawPixelInfo dpi, *old_dpi;
00636   int wx, left;
00637 
00638   /* We are no longer rendering to the screen */
00639   DrawPixelInfo old_screen = _screen;
00640   bool old_disable_anim = _screen_disable_anim;
00641 
00642   _screen.dst_ptr = buf;
00643   _screen.width = pitch;
00644   _screen.height = n;
00645   _screen.pitch = pitch;
00646   _screen_disable_anim = true;
00647 
00648   old_dpi = _cur_dpi;
00649   _cur_dpi = &dpi;
00650 
00651   dpi.dst_ptr = buf;
00652   dpi.height = n;
00653   dpi.width = vp->width;
00654   dpi.pitch = pitch;
00655   dpi.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
00656   dpi.left = 0;
00657   dpi.top = y;
00658 
00659   /* Render viewport in blocks of 1600 pixels width */
00660   left = 0;
00661   while (vp->width - left != 0) {
00662     wx = min(vp->width - left, 1600);
00663     left += wx;
00664 
00665     ViewportDoDraw(vp,
00666       ScaleByZoom(left - wx - vp->left, vp->zoom) + vp->virtual_left,
00667       ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top,
00668       ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
00669       ScaleByZoom((y + n) - vp->top, vp->zoom) + vp->virtual_top
00670     );
00671   }
00672 
00673   _cur_dpi = old_dpi;
00674 
00675   /* Switch back to rendering to the screen */
00676   _screen = old_screen;
00677   _screen_disable_anim = old_disable_anim;
00678 }
00679 
00685 static const char *MakeScreenshotName(const char *ext)
00686 {
00687   bool generate = StrEmpty(_screenshot_name);
00688 
00689   if (generate) {
00690     if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) {
00691       strecpy(_screenshot_name, "screenshot", lastof(_screenshot_name));
00692     } else {
00693       GenerateDefaultSaveName(_screenshot_name, lastof(_screenshot_name));
00694     }
00695   }
00696 
00697   /* Add extension to screenshot file */
00698   size_t len = strlen(_screenshot_name);
00699   snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, ".%s", ext);
00700 
00701   for (uint serial = 1;; serial++) {
00702     if (snprintf(_full_screenshot_name, lengthof(_full_screenshot_name), "%s%s", _personal_dir, _screenshot_name) >= (int)lengthof(_full_screenshot_name)) {
00703       /* We need more characters than MAX_PATH -> end with error */
00704       _full_screenshot_name[0] = '\0';
00705       break;
00706     }
00707     if (!generate) break; // allow overwriting of non-automatic filenames
00708     if (!FileExists(_full_screenshot_name)) break;
00709     /* If file exists try another one with same name, but just with a higher index */
00710     snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, "#%u.%s", serial, ext);
00711   }
00712 
00713   return _full_screenshot_name;
00714 }
00715 
00717 static bool MakeSmallScreenshot()
00718 {
00719   const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
00720   return sf->proc(MakeScreenshotName(sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height, BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette);
00721 }
00722 
00724 static bool MakeZoomedInScreenshot()
00725 {
00726   Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
00727   ViewPort vp;
00728 
00729   vp.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
00730   vp.left = w->viewport->left;
00731   vp.top = w->viewport->top;
00732   vp.virtual_left = w->viewport->virtual_left;
00733   vp.virtual_top = w->viewport->virtual_top;
00734   vp.virtual_width = w->viewport->virtual_width;
00735   vp.width = vp.virtual_width;
00736   vp.virtual_height = w->viewport->virtual_height;
00737   vp.height = vp.virtual_height;
00738 
00739   const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
00740   return sf->proc(MakeScreenshotName(sf->extension), LargeWorldCallback, &vp, vp.width, vp.height, BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette);
00741 }
00742 
00744 static bool MakeWorldScreenshot()
00745 {
00746   ViewPort vp;
00747   const ScreenshotFormat *sf;
00748 
00749   /* We need to account for a hill or high building at tile 0,0. */
00750   int extra_height_top = TileHeight(0) * TILE_HEIGHT + 150;
00751   /* If there is a hill at the bottom don't create a large black area. */
00752   int reclaim_height_bottom = TileHeight(MapSize() - 1) * TILE_HEIGHT;
00753 
00754   vp.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
00755   vp.left = 0;
00756   vp.top = 0;
00757   vp.virtual_left = -(int)MapMaxX() * TILE_PIXELS;
00758   vp.virtual_top = -extra_height_top;
00759   vp.virtual_width = (MapMaxX() + MapMaxY()) * TILE_PIXELS;
00760   vp.width = vp.virtual_width;
00761   vp.virtual_height = ((MapMaxX() + MapMaxY()) * TILE_PIXELS >> 1) + extra_height_top - reclaim_height_bottom;
00762   vp.height = vp.virtual_height;
00763 
00764   sf = _screenshot_formats + _cur_screenshot_format;
00765   return sf->proc(MakeScreenshotName(sf->extension), LargeWorldCallback, &vp, vp.width, vp.height, BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette);
00766 }
00767 
00774 bool MakeScreenshot(ScreenshotType t, const char *name)
00775 {
00776   if (t == SC_VIEWPORT) {
00777     /* First draw the dirty parts of the screen and only then change the name
00778      * of the screenshot. This way the screenshot will always show the name
00779      * of the previous screenshot in the 'successful' message instead of the
00780      * name of the new screenshot (or an empty name). */
00781     UndrawMouseCursor();
00782     DrawDirtyBlocks();
00783   }
00784 
00785   _screenshot_name[0] = '\0';
00786   if (name != NULL) strecpy(_screenshot_name, name, lastof(_screenshot_name));
00787 
00788   bool ret;
00789   switch (t) {
00790     case SC_VIEWPORT:
00791     case SC_RAW:
00792       ret = MakeSmallScreenshot();
00793       break;
00794 
00795     case SC_ZOOMEDIN:
00796       ret = MakeZoomedInScreenshot();
00797       break;
00798 
00799     case SC_WORLD:
00800       ret = MakeWorldScreenshot();
00801       break;
00802 
00803     default:
00804       NOT_REACHED();
00805   }
00806 
00807   if (ret) {
00808     SetDParamStr(0, _screenshot_name);
00809     ShowErrorMessage(STR_MESSAGE_SCREENSHOT_SUCCESSFULLY, INVALID_STRING_ID, WL_WARNING);
00810   } else {
00811     ShowErrorMessage(STR_ERROR_SCREENSHOT_FAILED, INVALID_STRING_ID, WL_ERROR);
00812   }
00813 
00814   return ret;
00815 }

Generated on Fri May 27 04:19:48 2011 for OpenTTD by  doxygen 1.6.1