OpenTTD
screenshot.cpp
Go to the documentation of this file.
1 /* $Id: screenshot.cpp 26983 2014-10-09 19:52:07Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "fileio_func.h"
14 #include "viewport_func.h"
15 #include "gfx_func.h"
16 #include "screenshot.h"
17 #include "blitter/factory.hpp"
18 #include "zoom_func.h"
19 #include "core/endian_func.hpp"
20 #include "saveload/saveload.h"
21 #include "company_func.h"
22 #include "strings_func.h"
23 #include "error.h"
24 #include "window_gui.h"
25 #include "window_func.h"
26 #include "tile_map.h"
27 #include "landscape.h"
28 
29 #include "table/strings.h"
30 
31 #include "safeguards.h"
32 
33 static const char * const SCREENSHOT_NAME = "screenshot";
34 static const char * const HEIGHTMAP_NAME = "heightmap";
35 
39 static char _screenshot_name[128];
40 char _full_screenshot_name[MAX_PATH];
41 
50 typedef void ScreenshotCallback(void *userdata, void *buf, uint y, uint pitch, uint n);
51 
63 typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette);
64 
67  const char *extension;
69 };
70 
71 /*************************************************
72  **** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP)
73  *************************************************/
74 #if defined(_MSC_VER) || defined(__WATCOMC__)
75 #pragma pack(push, 1)
76 #endif
77 
80  uint16 type;
81  uint32 size;
82  uint32 reserved;
83  uint32 off_bits;
84 } GCC_PACK;
85 assert_compile(sizeof(BitmapFileHeader) == 14);
86 
87 #if defined(_MSC_VER) || defined(__WATCOMC__)
88 #pragma pack(pop)
89 #endif
90 
93  uint32 size;
94  int32 width, height;
95  uint16 planes, bitcount;
96  uint32 compression, sizeimage, xpels, ypels, clrused, clrimp;
97 };
98 assert_compile(sizeof(BitmapInfoHeader) == 40);
99 
101 struct RgbQuad {
102  byte blue, green, red, reserved;
103 };
104 assert_compile(sizeof(RgbQuad) == 4);
105 
118 static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
119 {
120  uint bpp; // bytes per pixel
121  switch (pixelformat) {
122  case 8: bpp = 1; break;
123  /* 32bpp mode is saved as 24bpp BMP */
124  case 32: bpp = 3; break;
125  /* Only implemented for 8bit and 32bit images so far */
126  default: return false;
127  }
128 
129  FILE *f = fopen(name, "wb");
130  if (f == NULL) return false;
131 
132  /* Each scanline must be aligned on a 32bit boundary */
133  uint bytewidth = Align(w * bpp, 4); // bytes per line in file
134 
135  /* Size of palette. Only present for 8bpp mode */
136  uint pal_size = pixelformat == 8 ? sizeof(RgbQuad) * 256 : 0;
137 
138  /* Setup the file header */
139  BitmapFileHeader bfh;
140  bfh.type = TO_LE16('MB');
141  bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + bytewidth * h);
142  bfh.reserved = 0;
143  bfh.off_bits = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size);
144 
145  /* Setup the info header */
146  BitmapInfoHeader bih;
147  bih.size = TO_LE32(sizeof(BitmapInfoHeader));
148  bih.width = TO_LE32(w);
149  bih.height = TO_LE32(h);
150  bih.planes = TO_LE16(1);
151  bih.bitcount = TO_LE16(bpp * 8);
152  bih.compression = 0;
153  bih.sizeimage = 0;
154  bih.xpels = 0;
155  bih.ypels = 0;
156  bih.clrused = 0;
157  bih.clrimp = 0;
158 
159  /* Write file header and info header */
160  if (fwrite(&bfh, sizeof(bfh), 1, f) != 1 || fwrite(&bih, sizeof(bih), 1, f) != 1) {
161  fclose(f);
162  return false;
163  }
164 
165  if (pixelformat == 8) {
166  /* Convert the palette to the windows format */
167  RgbQuad rq[256];
168  for (uint i = 0; i < 256; i++) {
169  rq[i].red = palette[i].r;
170  rq[i].green = palette[i].g;
171  rq[i].blue = palette[i].b;
172  rq[i].reserved = 0;
173  }
174  /* Write the palette */
175  if (fwrite(rq, sizeof(rq), 1, f) != 1) {
176  fclose(f);
177  return false;
178  }
179  }
180 
181  /* Try to use 64k of memory, store between 16 and 128 lines */
182  uint maxlines = Clamp(65536 / (w * pixelformat / 8), 16, 128); // number of lines per iteration
183 
184  uint8 *buff = MallocT<uint8>(maxlines * w * pixelformat / 8); // buffer which is rendered to
185  uint8 *line = AllocaM(uint8, bytewidth); // one line, stored to file
186  memset(line, 0, bytewidth);
187 
188  /* Start at the bottom, since bitmaps are stored bottom up */
189  do {
190  uint n = min(h, maxlines);
191  h -= n;
192 
193  /* Render the pixels */
194  callb(userdata, buff, h, w, n);
195 
196  /* Write each line */
197  while (n-- != 0) {
198  if (pixelformat == 8) {
199  /* Move to 'line', leave last few pixels in line zeroed */
200  memcpy(line, buff + n * w, w);
201  } else {
202  /* Convert from 'native' 32bpp to BMP-like 24bpp.
203  * Works for both big and little endian machines */
204  Colour *src = ((Colour *)buff) + n * w;
205  byte *dst = line;
206  for (uint i = 0; i < w; i++) {
207  dst[i * 3 ] = src[i].b;
208  dst[i * 3 + 1] = src[i].g;
209  dst[i * 3 + 2] = src[i].r;
210  }
211  }
212  /* Write to file */
213  if (fwrite(line, bytewidth, 1, f) != 1) {
214  free(buff);
215  fclose(f);
216  return false;
217  }
218  }
219  } while (h != 0);
220 
221  free(buff);
222  fclose(f);
223 
224  return true;
225 }
226 
227 /*********************************************************
228  **** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG)
229  *********************************************************/
230 #if defined(WITH_PNG)
231 #include <png.h>
232 
233 #ifdef PNG_TEXT_SUPPORTED
234 #include "rev.h"
235 #include "newgrf_config.h"
236 #include "ai/ai_info.hpp"
237 #include "company_base.h"
238 #include "base_media_base.h"
239 #endif /* PNG_TEXT_SUPPORTED */
240 
241 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
242 {
243  DEBUG(misc, 0, "[libpng] error: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
244  longjmp(png_jmpbuf(png_ptr), 1);
245 }
246 
247 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
248 {
249  DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
250 }
251 
264 static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
265 {
266  png_color rq[256];
267  FILE *f;
268  uint i, y, n;
269  uint maxlines;
270  uint bpp = pixelformat / 8;
271  png_structp png_ptr;
272  png_infop info_ptr;
273 
274  /* only implemented for 8bit and 32bit images so far. */
275  if (pixelformat != 8 && pixelformat != 32) return false;
276 
277  f = fopen(name, "wb");
278  if (f == NULL) return false;
279 
280  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, const_cast<char *>(name), png_my_error, png_my_warning);
281 
282  if (png_ptr == NULL) {
283  fclose(f);
284  return false;
285  }
286 
287  info_ptr = png_create_info_struct(png_ptr);
288  if (info_ptr == NULL) {
289  png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
290  fclose(f);
291  return false;
292  }
293 
294  if (setjmp(png_jmpbuf(png_ptr))) {
295  png_destroy_write_struct(&png_ptr, &info_ptr);
296  fclose(f);
297  return false;
298  }
299 
300  png_init_io(png_ptr, f);
301 
302  png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
303 
304  png_set_IHDR(png_ptr, info_ptr, w, h, 8, pixelformat == 8 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
305  PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
306 
307 #ifdef PNG_TEXT_SUPPORTED
308  /* Try to add some game metadata to the PNG screenshot so
309  * it's more useful for debugging and archival purposes. */
310  png_text_struct text[2];
311  memset(text, 0, sizeof(text));
312  text[0].key = const_cast<char *>("Software");
313  text[0].text = const_cast<char *>(_openttd_revision);
314  text[0].text_length = strlen(_openttd_revision);
315  text[0].compression = PNG_TEXT_COMPRESSION_NONE;
316 
317  char buf[8192];
318  char *p = buf;
319  p += seprintf(p, lastof(buf), "Graphics set: %s (%u)\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
320  p = strecpy(p, "NewGRFs:\n", lastof(buf));
321  for (const GRFConfig *c = _game_mode == GM_MENU ? NULL : _grfconfig; c != NULL; c = c->next) {
322  p += seprintf(p, lastof(buf), "%08X ", BSWAP32(c->ident.grfid));
323  p = md5sumToString(p, lastof(buf), c->ident.md5sum);
324  p += seprintf(p, lastof(buf), " %s\n", c->filename);
325  }
326  p = strecpy(p, "\nCompanies:\n", lastof(buf));
327  const Company *c;
328  FOR_ALL_COMPANIES(c) {
329  if (c->ai_info == NULL) {
330  p += seprintf(p, lastof(buf), "%2i: Human\n", (int)c->index);
331  } else {
332  p += seprintf(p, lastof(buf), "%2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
333  }
334  }
335  text[1].key = const_cast<char *>("Description");
336  text[1].text = buf;
337  text[1].text_length = p - buf;
338  text[1].compression = PNG_TEXT_COMPRESSION_zTXt;
339  png_set_text(png_ptr, info_ptr, text, 2);
340 #endif /* PNG_TEXT_SUPPORTED */
341 
342  if (pixelformat == 8) {
343  /* convert the palette to the .PNG format. */
344  for (i = 0; i != 256; i++) {
345  rq[i].red = palette[i].r;
346  rq[i].green = palette[i].g;
347  rq[i].blue = palette[i].b;
348  }
349 
350  png_set_PLTE(png_ptr, info_ptr, rq, 256);
351  }
352 
353  png_write_info(png_ptr, info_ptr);
354  png_set_flush(png_ptr, 512);
355 
356  if (pixelformat == 32) {
357  png_color_8 sig_bit;
358 
359  /* Save exact colour/alpha resolution */
360  sig_bit.alpha = 0;
361  sig_bit.blue = 8;
362  sig_bit.green = 8;
363  sig_bit.red = 8;
364  sig_bit.gray = 8;
365  png_set_sBIT(png_ptr, info_ptr, &sig_bit);
366 
367 #if TTD_ENDIAN == TTD_LITTLE_ENDIAN
368  png_set_bgr(png_ptr);
369  png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
370 #else
371  png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
372 #endif /* TTD_ENDIAN == TTD_LITTLE_ENDIAN */
373  }
374 
375  /* use by default 64k temp memory */
376  maxlines = Clamp(65536 / w, 16, 128);
377 
378  /* now generate the bitmap bits */
379  void *buff = CallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
380 
381  y = 0;
382  do {
383  /* determine # lines to write */
384  n = min(h - y, maxlines);
385 
386  /* render the pixels into the buffer */
387  callb(userdata, buff, y, w, n);
388  y += n;
389 
390  /* write them to png */
391  for (i = 0; i != n; i++) {
392  png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
393  }
394  } while (y != h);
395 
396  png_write_end(png_ptr, info_ptr);
397  png_destroy_write_struct(&png_ptr, &info_ptr);
398 
399  free(buff);
400  fclose(f);
401  return true;
402 }
403 #endif /* WITH_PNG */
404 
405 
406 /*************************************************
407  **** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX)
408  *************************************************/
409 
411 struct PcxHeader {
412  byte manufacturer;
413  byte version;
414  byte rle;
415  byte bpp;
416  uint32 unused;
417  uint16 xmax, ymax;
418  uint16 hdpi, vdpi;
419  byte pal_small[16 * 3];
420  byte reserved;
421  byte planes;
422  uint16 pitch;
423  uint16 cpal;
424  uint16 width;
425  uint16 height;
426  byte filler[54];
427 };
428 assert_compile(sizeof(PcxHeader) == 128);
429 
442 static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
443 {
444  FILE *f;
445  uint maxlines;
446  uint y;
447  PcxHeader pcx;
448  bool success;
449 
450  if (pixelformat == 32) {
451  DEBUG(misc, 0, "Can't convert a 32bpp screenshot to PCX format. Please pick another format.");
452  return false;
453  }
454  if (pixelformat != 8 || w == 0) return false;
455 
456  f = fopen(name, "wb");
457  if (f == NULL) return false;
458 
459  memset(&pcx, 0, sizeof(pcx));
460 
461  /* setup pcx header */
462  pcx.manufacturer = 10;
463  pcx.version = 5;
464  pcx.rle = 1;
465  pcx.bpp = 8;
466  pcx.xmax = TO_LE16(w - 1);
467  pcx.ymax = TO_LE16(h - 1);
468  pcx.hdpi = TO_LE16(320);
469  pcx.vdpi = TO_LE16(320);
470 
471  pcx.planes = 1;
472  pcx.cpal = TO_LE16(1);
473  pcx.width = pcx.pitch = TO_LE16(w);
474  pcx.height = TO_LE16(h);
475 
476  /* write pcx header */
477  if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
478  fclose(f);
479  return false;
480  }
481 
482  /* use by default 64k temp memory */
483  maxlines = Clamp(65536 / w, 16, 128);
484 
485  /* now generate the bitmap bits */
486  uint8 *buff = CallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
487 
488  y = 0;
489  do {
490  /* determine # lines to write */
491  uint n = min(h - y, maxlines);
492  uint i;
493 
494  /* render the pixels into the buffer */
495  callb(userdata, buff, y, w, n);
496  y += n;
497 
498  /* write them to pcx */
499  for (i = 0; i != n; i++) {
500  const uint8 *bufp = buff + i * w;
501  byte runchar = bufp[0];
502  uint runcount = 1;
503  uint j;
504 
505  /* for each pixel... */
506  for (j = 1; j < w; j++) {
507  uint8 ch = bufp[j];
508 
509  if (ch != runchar || runcount >= 0x3f) {
510  if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
511  if (fputc(0xC0 | runcount, f) == EOF) {
512  free(buff);
513  fclose(f);
514  return false;
515  }
516  }
517  if (fputc(runchar, f) == EOF) {
518  free(buff);
519  fclose(f);
520  return false;
521  }
522  runcount = 0;
523  runchar = ch;
524  }
525  runcount++;
526  }
527 
528  /* write remaining bytes.. */
529  if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
530  if (fputc(0xC0 | runcount, f) == EOF) {
531  free(buff);
532  fclose(f);
533  return false;
534  }
535  }
536  if (fputc(runchar, f) == EOF) {
537  free(buff);
538  fclose(f);
539  return false;
540  }
541  }
542  } while (y != h);
543 
544  free(buff);
545 
546  /* write 8-bit colour palette */
547  if (fputc(12, f) == EOF) {
548  fclose(f);
549  return false;
550  }
551 
552  /* Palette is word-aligned, copy it to a temporary byte array */
553  byte tmp[256 * 3];
554 
555  for (uint i = 0; i < 256; i++) {
556  tmp[i * 3 + 0] = palette[i].r;
557  tmp[i * 3 + 1] = palette[i].g;
558  tmp[i * 3 + 2] = palette[i].b;
559  }
560  success = fwrite(tmp, sizeof(tmp), 1, f) == 1;
561 
562  fclose(f);
563 
564  return success;
565 }
566 
567 /*************************************************
568  **** GENERIC SCREENSHOT CODE
569  *************************************************/
570 
573 #if defined(WITH_PNG)
574  {"png", &MakePNGImage},
575 #endif
576  {"bmp", &MakeBMPImage},
577  {"pcx", &MakePCXImage},
578 };
579 
582 {
583  return _screenshot_formats[_cur_screenshot_format].extension;
584 }
585 
588 {
589  uint j = 0;
590  for (uint i = 0; i < lengthof(_screenshot_formats); i++) {
591  if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) {
592  j = i;
593  break;
594  }
595  }
597  _num_screenshot_formats = lengthof(_screenshot_formats);
598 }
599 
604 static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
605 {
607  void *src = blitter->MoveTo(_screen.dst_ptr, 0, y);
608  blitter->CopyImageToBuffer(src, buf, _screen.width, n, pitch);
609 }
610 
619 static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
620 {
621  ViewPort *vp = (ViewPort *)userdata;
622  DrawPixelInfo dpi, *old_dpi;
623  int wx, left;
624 
625  /* We are no longer rendering to the screen */
626  DrawPixelInfo old_screen = _screen;
627  bool old_disable_anim = _screen_disable_anim;
628 
629  _screen.dst_ptr = buf;
630  _screen.width = pitch;
631  _screen.height = n;
632  _screen.pitch = pitch;
633  _screen_disable_anim = true;
634 
635  old_dpi = _cur_dpi;
636  _cur_dpi = &dpi;
637 
638  dpi.dst_ptr = buf;
639  dpi.height = n;
640  dpi.width = vp->width;
641  dpi.pitch = pitch;
642  dpi.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
643  dpi.left = 0;
644  dpi.top = y;
645 
646  /* Render viewport in blocks of 1600 pixels width */
647  left = 0;
648  while (vp->width - left != 0) {
649  wx = min(vp->width - left, 1600);
650  left += wx;
651 
652  ViewportDoDraw(vp,
653  ScaleByZoom(left - wx - vp->left, vp->zoom) + vp->virtual_left,
654  ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top,
655  ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
656  ScaleByZoom((y + n) - vp->top, vp->zoom) + vp->virtual_top
657  );
658  }
659 
660  _cur_dpi = old_dpi;
661 
662  /* Switch back to rendering to the screen */
663  _screen = old_screen;
664  _screen_disable_anim = old_disable_anim;
665 }
666 
674 static const char *MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog = false)
675 {
676  bool generate = StrEmpty(_screenshot_name);
677 
678  if (generate) {
679  if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) {
681  } else {
683  }
684  }
685 
686  /* Add extension to screenshot file */
687  size_t len = strlen(_screenshot_name);
688  seprintf(&_screenshot_name[len], lastof(_screenshot_name), ".%s", ext);
689 
690  const char *screenshot_dir = crashlog ? _personal_dir : FiosGetScreenshotDir();
691 
692  for (uint serial = 1;; serial++) {
694  /* We need more characters than MAX_PATH -> end with error */
695  _full_screenshot_name[0] = '\0';
696  break;
697  }
698  if (!generate) break; // allow overwriting of non-automatic filenames
699  if (!FileExists(_full_screenshot_name)) break;
700  /* If file exists try another one with same name, but just with a higher index */
701  seprintf(&_screenshot_name[len], lastof(_screenshot_name) - len, "#%u.%s", serial, ext);
702  }
703 
704  return _full_screenshot_name;
705 }
706 
708 static bool MakeSmallScreenshot(bool crashlog)
709 {
710  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
711  return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension, crashlog), CurrentScreenCallback, NULL, _screen.width, _screen.height,
713 }
714 
721 {
722  /* Determine world coordinates of screenshot */
723  if (t == SC_WORLD) {
725 
726  TileIndex north_tile = _settings_game.construction.freeform_edges ? TileXY(1, 1) : TileXY(0, 0);
727  TileIndex south_tile = MapSize() - 1;
728 
729  /* We need to account for a hill or high building at tile 0,0. */
730  int extra_height_top = TilePixelHeight(north_tile) + 150;
731  /* If there is a hill at the bottom don't create a large black area. */
732  int reclaim_height_bottom = TilePixelHeight(south_tile);
733 
734  vp->virtual_left = RemapCoords(TileX(south_tile) * TILE_SIZE, TileY(north_tile) * TILE_SIZE, 0).x;
735  vp->virtual_top = RemapCoords(TileX(north_tile) * TILE_SIZE, TileY(north_tile) * TILE_SIZE, extra_height_top).y;
736  vp->virtual_width = RemapCoords(TileX(north_tile) * TILE_SIZE, TileY(south_tile) * TILE_SIZE, 0).x - vp->virtual_left + 1;
737  vp->virtual_height = RemapCoords(TileX(south_tile) * TILE_SIZE, TileY(south_tile) * TILE_SIZE, reclaim_height_bottom).y - vp->virtual_top + 1;
738  } else {
740 
742  vp->virtual_left = w->viewport->virtual_left;
743  vp->virtual_top = w->viewport->virtual_top;
744  vp->virtual_width = w->viewport->virtual_width;
745  vp->virtual_height = w->viewport->virtual_height;
746  }
747 
748  /* Compute pixel coordinates */
749  vp->left = 0;
750  vp->top = 0;
751  vp->width = UnScaleByZoom(vp->virtual_width, vp->zoom);
752  vp->height = UnScaleByZoom(vp->virtual_height, vp->zoom);
753  vp->overlay = NULL;
754 }
755 
762 {
763  ViewPort vp;
764  SetupScreenshotViewport(t, &vp);
765 
766  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
769 }
770 
780 static void HeightmapCallback(void *userdata, void *buffer, uint y, uint pitch, uint n)
781 {
782  byte *buf = (byte *)buffer;
783  while (n > 0) {
784  TileIndex ti = TileXY(MapMaxX(), y);
785  for (uint x = MapMaxX(); true; x--) {
786  *buf = 256 * TileHeight(ti) / (1 + _settings_game.construction.max_heightlevel);
787  buf++;
788  if (x == 0) break;
789  ti = TILE_ADDXY(ti, -1, 0);
790  }
791  y++;
792  n--;
793  }
794 }
795 
800 bool MakeHeightmapScreenshot(const char *filename)
801 {
802  Colour palette[256];
803  for (uint i = 0; i < lengthof(palette); i++) {
804  palette[i].a = 0xff;
805  palette[i].r = i;
806  palette[i].g = i;
807  palette[i].b = i;
808  }
809  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
810  return sf->proc(filename, HeightmapCallback, NULL, MapSizeX(), MapSizeY(), 8, palette);
811 }
812 
819 bool MakeScreenshot(ScreenshotType t, const char *name)
820 {
821  if (t == SC_VIEWPORT) {
822  /* First draw the dirty parts of the screen and only then change the name
823  * of the screenshot. This way the screenshot will always show the name
824  * of the previous screenshot in the 'successful' message instead of the
825  * name of the new screenshot (or an empty name). */
826  UndrawMouseCursor();
827  DrawDirtyBlocks();
828  }
829 
830  _screenshot_name[0] = '\0';
831  if (name != NULL) strecpy(_screenshot_name, name, lastof(_screenshot_name));
832 
833  bool ret;
834  switch (t) {
835  case SC_VIEWPORT:
836  ret = MakeSmallScreenshot(false);
837  break;
838 
839  case SC_CRASHLOG:
840  ret = MakeSmallScreenshot(true);
841  break;
842 
843  case SC_ZOOMEDIN:
844  case SC_DEFAULTZOOM:
845  case SC_WORLD:
846  ret = MakeLargeWorldScreenshot(t);
847  break;
848 
849  case SC_HEIGHTMAP: {
850  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
852  break;
853  }
854 
855  default:
856  NOT_REACHED();
857  }
858 
859  if (ret) {
861  ShowErrorMessage(STR_MESSAGE_SCREENSHOT_SUCCESSFULLY, INVALID_STRING_ID, WL_WARNING);
862  } else {
863  ShowErrorMessage(STR_ERROR_SCREENSHOT_FAILED, INVALID_STRING_ID, WL_ERROR);
864  }
865 
866  return ret;
867 }
World screenshot.
Definition: screenshot.h:25
Functions related to OTTD&#39;s strings.
char _screenshot_format_name[8]
Extension of the current screenshot format (corresponds with _cur_screenshot_format).
Definition: screenshot.cpp:36
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:74
uint8 max_heightlevel
maximum allowed heightlevel
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:74
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
static bool MakeSmallScreenshot(bool crashlog)
Make a screenshot of the current screen.
Definition: screenshot.cpp:708
Definition of stuff that is very close to a company, like the company struct itself.
char _full_screenshot_name[MAX_PATH]
Pathname of the screenshot file.
Definition: screenshot.cpp:40
void InitializeScreenshotFormats()
Initialize screenshot format information on startup, with _screenshot_format_name filled from the loa...
Definition: screenshot.cpp:587
int virtual_left
Virtual left coordinate.
Definition: viewport_type.h:30
Data about how and where to blit pixels.
Definition: gfx_type.h:156
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:84
BMP Info Header (stored in little endian)
Definition: screenshot.cpp:92
GRFConfig * _grfconfig
First item in list of current GRF set up.
Heightmap of the world.
Definition: screenshot.h:26
void GenerateDefaultSaveName(char *buf, const char *last)
Fill the buffer with the default name for a savegame or screenshot.
Definition: saveload.cpp:2885
int height
Screen height of the viewport.
Definition: viewport_type.h:28
static Point RemapCoords(int x, int y, int z)
Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap...
Definition: landscape.h:83
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:398
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
Handle warning in pnglib.
Definition: splash.cpp:46
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:310
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1105
static int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL) When shifting right...
Definition: zoom_func.h:37
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:59
BMP File Header (stored in little endian)
Definition: screenshot.cpp:79
Generic functions for replacing base data (graphics, sounds).
Screenshot of viewport.
Definition: screenshot.h:21
uint8 a
colour channels in LE order
Definition: gfx_type.h:170
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:416
int virtual_height
height << zoom
Definition: viewport_type.h:33
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right...
Definition: zoom_func.h:24
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Functions for Standard In/Out file operations.
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
Handle pnglib error.
Definition: splash.cpp:34
bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Function signature for a screenshot generation routine for one of the available formats.
Definition: screenshot.cpp:63
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
The client is spectating.
Definition: company_type.h:37
Function to handling different endian machines.
Functions to make screenshots.
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
Definition: alloc_func.hpp:134
uint _num_screenshot_formats
Number of available screenshot formats.
Definition: screenshot.cpp:37
How all blitters should look like.
Definition: base.hpp:30
virtual void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)=0
Copy from the screen to a buffer in a palette format for 8bpp and RGBA format for 32bpp...
Fully zoomed in screenshot of the visible area.
Definition: screenshot.h:23
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Functions, definitions and such used only by the GUI.
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
Functions related to (drawing on) viewports.
Definition of a PCX file header.
Definition: screenshot.cpp:411
ScreenshotHandlerProc * proc
Function for writing the screenshot.
Definition: screenshot.cpp:68
void SetupScreenshotViewport(ScreenshotType t, ViewPort *vp)
Configure a ViewPort for rendering (a part of) the map into a screenshot.
Definition: screenshot.cpp:720
bool freeform_edges
allow terraforming the tiles at the map edges
Data structure for an opened window.
Definition: window_gui.h:271
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=NULL, uint textref_stack_size=0, const uint32 *textref_stack=NULL)
Display an error message in a window.
Definition: error_gui.cpp:378
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:279
Main window; Window numbers:
Definition: window_type.h:46
Functions/types related to saving and loading games.
Other information.
Definition: error.h:24
Functions related to errors.
static const char *const HEIGHTMAP_NAME
Default filename of a saved heightmap.
Definition: screenshot.cpp:34
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:97
void ScreenshotCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
Callback function signature for generating lines of pixel data to be written to the screenshot file...
Definition: screenshot.cpp:50
Information about GRF, used in the game and (part of it) in savegames.
const char * extension
File extension.
Definition: screenshot.cpp:67
Functions related to the gfx engine.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
bool FileExists(const char *filename)
Test whether the given filename exists.
Definition: fileio.cpp:326
Definition of base types and functions in a cross-platform compatible way.
static void HeightmapCallback(void *userdata, void *buffer, uint y, uint pitch, uint n)
Callback for generating a heightmap.
Definition: screenshot.cpp:780
#define TILE_ADDXY(tile, x, y)
Adds a given offset to a tile.
Definition: map_func.h:260
A number of safeguards to prevent using unsafe methods.
Screenshot format information.
Definition: screenshot.cpp:66
static const ScreenshotFormat _screenshot_formats[]
Available screenshot formats.
Definition: screenshot.cpp:572
static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
Callback of the screenshot generator that dumps the current video buffer.
Definition: screenshot.cpp:604
static const char * MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog=false)
Construct a pathname for a screenshot file.
Definition: screenshot.cpp:674
const char * _personal_dir
custom directory for personal settings, saves, newgrf, etc.
Definition: fileio.cpp:1163
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot) ...
Definition: gfx.cpp:44
static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .PCX file image writer.
Definition: screenshot.cpp:442
int virtual_width
width << zoom
Definition: viewport_type.h:32
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as &#39;dirty&#39;.
Definition: gfx.cpp:1307
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:147
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
Default zoom level for viewports.
Definition: zoom_type.h:35
Functions to find and configure NewGRFs.
static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .PNG file image writer.
Definition: screenshot.cpp:264
Palette _cur_palette
Current palette.
Definition: gfx.cpp:49
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
bool MakeScreenshot(ScreenshotType t, const char *name)
Make an actual screenshot.
Definition: screenshot.cpp:819
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
static const char *const SCREENSHOT_NAME
Default filename of a saved screenshot.
Definition: screenshot.cpp:33
int left
Screen coordinate left egde of the viewport.
Definition: viewport_type.h:25
ZoomLevelByte zoom_min
minimum zoom out level
Format of palette data in BMP header.
Definition: screenshot.cpp:101
uint _cur_screenshot_format
Index of the currently selected screenshot format in _screenshot_formats.
Definition: screenshot.cpp:38
Functions related to companies.
static uint MapSize()
Get the size of the map.
Definition: map_func.h:94
GUISettings gui
settings related to the GUI
static bool MakeLargeWorldScreenshot(ScreenshotType t)
Make a screenshot of the map.
Definition: screenshot.cpp:761
Data structure for viewport, display of a part of the world.
Definition: viewport_type.h:24
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
Zoomed to default zoom level screenshot of the visible area.
Definition: screenshot.h:24
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static char _screenshot_name[128]
Filename of the screenshot file.
Definition: screenshot.cpp:39
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
Functions related to zooming.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:581
virtual void * MoveTo(void *video, int x, int y)=0
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
Functions related to OTTD&#39;s landscape.
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:164
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:31
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
ConstructionSettings construction
construction of things in-game
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:19
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
declaration of OTTD revision dependent variables
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:35
int virtual_top
Virtual top coordinate.
Definition: viewport_type.h:31
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:644
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104
Map writing/reading functions for tiles.
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Window functions not directly related to making/drawing windows.
int top
Screen coordinate top edge of the viewport.
Definition: viewport_type.h:26
AIInfo keeps track of all information of an AI, like Author, Description, ...
static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .BMP writer.
Definition: screenshot.cpp:118
bool MakeHeightmapScreenshot(const char *filename)
Make a heightmap of the current map.
Definition: screenshot.cpp:800
Errors (eg. saving/loading failed)
Definition: error.h:25
Raw screenshot from blitter buffer.
Definition: screenshot.h:22
Factory to &#39;query&#39; all available blitters.
static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
generate a large piece of the world
Definition: screenshot.cpp:619
static const GraphicsSet * GetUsedSet()
Return the used set.
static uint TilePixelHeight(TileIndex tile)
Returns the height of a tile in pixels.
Definition: tile_map.h:64
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165
Default zoom level for the world screen shot.
Definition: zoom_type.h:43
ScreenshotType
Type of requested screenshot.
Definition: screenshot.h:20
int width
Screen width of the viewport.
Definition: viewport_type.h:27