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