OpenTTD Source  1.10.0-RC1
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_base.h"
20 #include "company_func.h"
21 #include "strings_func.h"
22 #include "error.h"
23 #include "window_gui.h"
24 #include "window_func.h"
25 #include "tile_map.h"
26 #include "landscape.h"
27 
28 #include "table/strings.h"
29 
30 #include "safeguards.h"
31 
32 static const char * const SCREENSHOT_NAME = "screenshot";
33 static const char * const HEIGHTMAP_NAME = "heightmap";
34 
38 static char _screenshot_name[128];
39 char _full_screenshot_name[MAX_PATH];
40 
49 typedef void ScreenshotCallback(void *userdata, void *buf, uint y, uint pitch, uint n);
50 
62 typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette);
63 
66  const char *extension;
68 };
69 
70 #define MKCOLOUR(x) TO_LE32X(x)
71 
72 /*************************************************
73  **** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP)
74  *************************************************/
75 
77 PACK(struct BitmapFileHeader {
78  uint16 type;
79  uint32 size;
80  uint32 reserved;
81  uint32 off_bits;
82 });
83 assert_compile(sizeof(BitmapFileHeader) == 14);
84 
87  uint32 size;
88  int32 width, height;
89  uint16 planes, bitcount;
90  uint32 compression, sizeimage, xpels, ypels, clrused, clrimp;
91 };
92 assert_compile(sizeof(BitmapInfoHeader) == 40);
93 
95 struct RgbQuad {
96  byte blue, green, red, reserved;
97 };
98 assert_compile(sizeof(RgbQuad) == 4);
99 
112 static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
113 {
114  uint bpp; // bytes per pixel
115  switch (pixelformat) {
116  case 8: bpp = 1; break;
117  /* 32bpp mode is saved as 24bpp BMP */
118  case 32: bpp = 3; break;
119  /* Only implemented for 8bit and 32bit images so far */
120  default: return false;
121  }
122 
123  FILE *f = fopen(name, "wb");
124  if (f == nullptr) return false;
125 
126  /* Each scanline must be aligned on a 32bit boundary */
127  uint bytewidth = Align(w * bpp, 4); // bytes per line in file
128 
129  /* Size of palette. Only present for 8bpp mode */
130  uint pal_size = pixelformat == 8 ? sizeof(RgbQuad) * 256 : 0;
131 
132  /* Setup the file header */
133  BitmapFileHeader bfh;
134  bfh.type = TO_LE16('MB');
135  bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + bytewidth * h);
136  bfh.reserved = 0;
137  bfh.off_bits = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size);
138 
139  /* Setup the info header */
140  BitmapInfoHeader bih;
141  bih.size = TO_LE32(sizeof(BitmapInfoHeader));
142  bih.width = TO_LE32(w);
143  bih.height = TO_LE32(h);
144  bih.planes = TO_LE16(1);
145  bih.bitcount = TO_LE16(bpp * 8);
146  bih.compression = 0;
147  bih.sizeimage = 0;
148  bih.xpels = 0;
149  bih.ypels = 0;
150  bih.clrused = 0;
151  bih.clrimp = 0;
152 
153  /* Write file header and info header */
154  if (fwrite(&bfh, sizeof(bfh), 1, f) != 1 || fwrite(&bih, sizeof(bih), 1, f) != 1) {
155  fclose(f);
156  return false;
157  }
158 
159  if (pixelformat == 8) {
160  /* Convert the palette to the windows format */
161  RgbQuad rq[256];
162  for (uint i = 0; i < 256; i++) {
163  rq[i].red = palette[i].r;
164  rq[i].green = palette[i].g;
165  rq[i].blue = palette[i].b;
166  rq[i].reserved = 0;
167  }
168  /* Write the palette */
169  if (fwrite(rq, sizeof(rq), 1, f) != 1) {
170  fclose(f);
171  return false;
172  }
173  }
174 
175  /* Try to use 64k of memory, store between 16 and 128 lines */
176  uint maxlines = Clamp(65536 / (w * pixelformat / 8), 16, 128); // number of lines per iteration
177 
178  uint8 *buff = MallocT<uint8>(maxlines * w * pixelformat / 8); // buffer which is rendered to
179  uint8 *line = AllocaM(uint8, bytewidth); // one line, stored to file
180  memset(line, 0, bytewidth);
181 
182  /* Start at the bottom, since bitmaps are stored bottom up */
183  do {
184  uint n = min(h, maxlines);
185  h -= n;
186 
187  /* Render the pixels */
188  callb(userdata, buff, h, w, n);
189 
190  /* Write each line */
191  while (n-- != 0) {
192  if (pixelformat == 8) {
193  /* Move to 'line', leave last few pixels in line zeroed */
194  memcpy(line, buff + n * w, w);
195  } else {
196  /* Convert from 'native' 32bpp to BMP-like 24bpp.
197  * Works for both big and little endian machines */
198  Colour *src = ((Colour *)buff) + n * w;
199  byte *dst = line;
200  for (uint i = 0; i < w; i++) {
201  dst[i * 3 ] = src[i].b;
202  dst[i * 3 + 1] = src[i].g;
203  dst[i * 3 + 2] = src[i].r;
204  }
205  }
206  /* Write to file */
207  if (fwrite(line, bytewidth, 1, f) != 1) {
208  free(buff);
209  fclose(f);
210  return false;
211  }
212  }
213  } while (h != 0);
214 
215  free(buff);
216  fclose(f);
217 
218  return true;
219 }
220 
221 /*********************************************************
222  **** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG)
223  *********************************************************/
224 #if defined(WITH_PNG)
225 #include <png.h>
226 
227 #ifdef PNG_TEXT_SUPPORTED
228 #include "rev.h"
229 #include "newgrf_config.h"
230 #include "ai/ai_info.hpp"
231 #include "company_base.h"
232 #include "base_media_base.h"
233 #endif /* PNG_TEXT_SUPPORTED */
234 
235 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
236 {
237  DEBUG(misc, 0, "[libpng] error: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
238  longjmp(png_jmpbuf(png_ptr), 1);
239 }
240 
241 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
242 {
243  DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
244 }
245 
258 static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
259 {
260  png_color rq[256];
261  FILE *f;
262  uint i, y, n;
263  uint maxlines;
264  uint bpp = pixelformat / 8;
265  png_structp png_ptr;
266  png_infop info_ptr;
267 
268  /* only implemented for 8bit and 32bit images so far. */
269  if (pixelformat != 8 && pixelformat != 32) return false;
270 
271  f = fopen(name, "wb");
272  if (f == nullptr) return false;
273 
274  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, const_cast<char *>(name), png_my_error, png_my_warning);
275 
276  if (png_ptr == nullptr) {
277  fclose(f);
278  return false;
279  }
280 
281  info_ptr = png_create_info_struct(png_ptr);
282  if (info_ptr == nullptr) {
283  png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
284  fclose(f);
285  return false;
286  }
287 
288  if (setjmp(png_jmpbuf(png_ptr))) {
289  png_destroy_write_struct(&png_ptr, &info_ptr);
290  fclose(f);
291  return false;
292  }
293 
294  png_init_io(png_ptr, f);
295 
296  png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
297 
298  png_set_IHDR(png_ptr, info_ptr, w, h, 8, pixelformat == 8 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
299  PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
300 
301 #ifdef PNG_TEXT_SUPPORTED
302  /* Try to add some game metadata to the PNG screenshot so
303  * it's more useful for debugging and archival purposes. */
304  png_text_struct text[2];
305  memset(text, 0, sizeof(text));
306  text[0].key = const_cast<char *>("Software");
307  text[0].text = const_cast<char *>(_openttd_revision);
308  text[0].text_length = strlen(_openttd_revision);
309  text[0].compression = PNG_TEXT_COMPRESSION_NONE;
310 
311  char buf[8192];
312  char *p = buf;
313  p += seprintf(p, lastof(buf), "Graphics set: %s (%u)\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
314  p = strecpy(p, "NewGRFs:\n", lastof(buf));
315  for (const GRFConfig *c = _game_mode == GM_MENU ? nullptr : _grfconfig; c != nullptr; c = c->next) {
316  p += seprintf(p, lastof(buf), "%08X ", BSWAP32(c->ident.grfid));
317  p = md5sumToString(p, lastof(buf), c->ident.md5sum);
318  p += seprintf(p, lastof(buf), " %s\n", c->filename);
319  }
320  p = strecpy(p, "\nCompanies:\n", lastof(buf));
321  for (const Company *c : Company::Iterate()) {
322  if (c->ai_info == nullptr) {
323  p += seprintf(p, lastof(buf), "%2i: Human\n", (int)c->index);
324  } else {
325  p += seprintf(p, lastof(buf), "%2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
326  }
327  }
328  text[1].key = const_cast<char *>("Description");
329  text[1].text = buf;
330  text[1].text_length = p - buf;
331  text[1].compression = PNG_TEXT_COMPRESSION_zTXt;
332  png_set_text(png_ptr, info_ptr, text, 2);
333 #endif /* PNG_TEXT_SUPPORTED */
334 
335  if (pixelformat == 8) {
336  /* convert the palette to the .PNG format. */
337  for (i = 0; i != 256; i++) {
338  rq[i].red = palette[i].r;
339  rq[i].green = palette[i].g;
340  rq[i].blue = palette[i].b;
341  }
342 
343  png_set_PLTE(png_ptr, info_ptr, rq, 256);
344  }
345 
346  png_write_info(png_ptr, info_ptr);
347  png_set_flush(png_ptr, 512);
348 
349  if (pixelformat == 32) {
350  png_color_8 sig_bit;
351 
352  /* Save exact colour/alpha resolution */
353  sig_bit.alpha = 0;
354  sig_bit.blue = 8;
355  sig_bit.green = 8;
356  sig_bit.red = 8;
357  sig_bit.gray = 8;
358  png_set_sBIT(png_ptr, info_ptr, &sig_bit);
359 
360 #if TTD_ENDIAN == TTD_LITTLE_ENDIAN
361  png_set_bgr(png_ptr);
362  png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
363 #else
364  png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
365 #endif /* TTD_ENDIAN == TTD_LITTLE_ENDIAN */
366  }
367 
368  /* use by default 64k temp memory */
369  maxlines = Clamp(65536 / w, 16, 128);
370 
371  /* now generate the bitmap bits */
372  void *buff = CallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
373 
374  y = 0;
375  do {
376  /* determine # lines to write */
377  n = min(h - y, maxlines);
378 
379  /* render the pixels into the buffer */
380  callb(userdata, buff, y, w, n);
381  y += n;
382 
383  /* write them to png */
384  for (i = 0; i != n; i++) {
385  png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
386  }
387  } while (y != h);
388 
389  png_write_end(png_ptr, info_ptr);
390  png_destroy_write_struct(&png_ptr, &info_ptr);
391 
392  free(buff);
393  fclose(f);
394  return true;
395 }
396 #endif /* WITH_PNG */
397 
398 
399 /*************************************************
400  **** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX)
401  *************************************************/
402 
404 struct PcxHeader {
405  byte manufacturer;
406  byte version;
407  byte rle;
408  byte bpp;
409  uint32 unused;
410  uint16 xmax, ymax;
411  uint16 hdpi, vdpi;
412  byte pal_small[16 * 3];
413  byte reserved;
414  byte planes;
415  uint16 pitch;
416  uint16 cpal;
417  uint16 width;
418  uint16 height;
419  byte filler[54];
420 };
421 assert_compile(sizeof(PcxHeader) == 128);
422 
435 static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
436 {
437  FILE *f;
438  uint maxlines;
439  uint y;
440  PcxHeader pcx;
441  bool success;
442 
443  if (pixelformat == 32) {
444  DEBUG(misc, 0, "Can't convert a 32bpp screenshot to PCX format. Please pick another format.");
445  return false;
446  }
447  if (pixelformat != 8 || w == 0) return false;
448 
449  f = fopen(name, "wb");
450  if (f == nullptr) return false;
451 
452  memset(&pcx, 0, sizeof(pcx));
453 
454  /* setup pcx header */
455  pcx.manufacturer = 10;
456  pcx.version = 5;
457  pcx.rle = 1;
458  pcx.bpp = 8;
459  pcx.xmax = TO_LE16(w - 1);
460  pcx.ymax = TO_LE16(h - 1);
461  pcx.hdpi = TO_LE16(320);
462  pcx.vdpi = TO_LE16(320);
463 
464  pcx.planes = 1;
465  pcx.cpal = TO_LE16(1);
466  pcx.width = pcx.pitch = TO_LE16(w);
467  pcx.height = TO_LE16(h);
468 
469  /* write pcx header */
470  if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
471  fclose(f);
472  return false;
473  }
474 
475  /* use by default 64k temp memory */
476  maxlines = Clamp(65536 / w, 16, 128);
477 
478  /* now generate the bitmap bits */
479  uint8 *buff = CallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
480 
481  y = 0;
482  do {
483  /* determine # lines to write */
484  uint n = min(h - y, maxlines);
485  uint i;
486 
487  /* render the pixels into the buffer */
488  callb(userdata, buff, y, w, n);
489  y += n;
490 
491  /* write them to pcx */
492  for (i = 0; i != n; i++) {
493  const uint8 *bufp = buff + i * w;
494  byte runchar = bufp[0];
495  uint runcount = 1;
496  uint j;
497 
498  /* for each pixel... */
499  for (j = 1; j < w; j++) {
500  uint8 ch = bufp[j];
501 
502  if (ch != runchar || runcount >= 0x3f) {
503  if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
504  if (fputc(0xC0 | runcount, f) == EOF) {
505  free(buff);
506  fclose(f);
507  return false;
508  }
509  }
510  if (fputc(runchar, f) == EOF) {
511  free(buff);
512  fclose(f);
513  return false;
514  }
515  runcount = 0;
516  runchar = ch;
517  }
518  runcount++;
519  }
520 
521  /* write remaining bytes.. */
522  if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
523  if (fputc(0xC0 | runcount, f) == EOF) {
524  free(buff);
525  fclose(f);
526  return false;
527  }
528  }
529  if (fputc(runchar, f) == EOF) {
530  free(buff);
531  fclose(f);
532  return false;
533  }
534  }
535  } while (y != h);
536 
537  free(buff);
538 
539  /* write 8-bit colour palette */
540  if (fputc(12, f) == EOF) {
541  fclose(f);
542  return false;
543  }
544 
545  /* Palette is word-aligned, copy it to a temporary byte array */
546  byte tmp[256 * 3];
547 
548  for (uint i = 0; i < 256; i++) {
549  tmp[i * 3 + 0] = palette[i].r;
550  tmp[i * 3 + 1] = palette[i].g;
551  tmp[i * 3 + 2] = palette[i].b;
552  }
553  success = fwrite(tmp, sizeof(tmp), 1, f) == 1;
554 
555  fclose(f);
556 
557  return success;
558 }
559 
560 /*************************************************
561  **** GENERIC SCREENSHOT CODE
562  *************************************************/
563 
566 #if defined(WITH_PNG)
567  {"png", &MakePNGImage},
568 #endif
569  {"bmp", &MakeBMPImage},
570  {"pcx", &MakePCXImage},
571 };
572 
575 {
576  return _screenshot_formats[_cur_screenshot_format].extension;
577 }
578 
581 {
582  uint j = 0;
583  for (uint i = 0; i < lengthof(_screenshot_formats); i++) {
584  if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) {
585  j = i;
586  break;
587  }
588  }
590  _num_screenshot_formats = lengthof(_screenshot_formats);
591 }
592 
597 static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
598 {
600  void *src = blitter->MoveTo(_screen.dst_ptr, 0, y);
601  blitter->CopyImageToBuffer(src, buf, _screen.width, n, pitch);
602 }
603 
612 static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
613 {
614  ViewPort *vp = (ViewPort *)userdata;
615  DrawPixelInfo dpi, *old_dpi;
616  int wx, left;
617 
618  /* We are no longer rendering to the screen */
619  DrawPixelInfo old_screen = _screen;
620  bool old_disable_anim = _screen_disable_anim;
621 
622  _screen.dst_ptr = buf;
623  _screen.width = pitch;
624  _screen.height = n;
625  _screen.pitch = pitch;
626  _screen_disable_anim = true;
627 
628  old_dpi = _cur_dpi;
629  _cur_dpi = &dpi;
630 
631  dpi.dst_ptr = buf;
632  dpi.height = n;
633  dpi.width = vp->width;
634  dpi.pitch = pitch;
635  dpi.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
636  dpi.left = 0;
637  dpi.top = y;
638 
639  /* Render viewport in blocks of 1600 pixels width */
640  left = 0;
641  while (vp->width - left != 0) {
642  wx = min(vp->width - left, 1600);
643  left += wx;
644 
645  ViewportDoDraw(vp,
646  ScaleByZoom(left - wx - vp->left, vp->zoom) + vp->virtual_left,
647  ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top,
648  ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
649  ScaleByZoom((y + n) - vp->top, vp->zoom) + vp->virtual_top
650  );
651  }
652 
653  _cur_dpi = old_dpi;
654 
655  /* Switch back to rendering to the screen */
656  _screen = old_screen;
657  _screen_disable_anim = old_disable_anim;
658 }
659 
667 static const char *MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog = false)
668 {
669  bool generate = StrEmpty(_screenshot_name);
670 
671  if (generate) {
672  if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) {
674  } else {
676  }
677  }
678 
679  /* Add extension to screenshot file */
680  size_t len = strlen(_screenshot_name);
681  seprintf(&_screenshot_name[len], lastof(_screenshot_name), ".%s", ext);
682 
683  const char *screenshot_dir = crashlog ? _personal_dir : FiosGetScreenshotDir();
684 
685  for (uint serial = 1;; serial++) {
687  /* We need more characters than MAX_PATH -> end with error */
688  _full_screenshot_name[0] = '\0';
689  break;
690  }
691  if (!generate) break; // allow overwriting of non-automatic filenames
692  if (!FileExists(_full_screenshot_name)) break;
693  /* If file exists try another one with same name, but just with a higher index */
694  seprintf(&_screenshot_name[len], lastof(_screenshot_name) - len, "#%u.%s", serial, ext);
695  }
696 
697  return _full_screenshot_name;
698 }
699 
701 static bool MakeSmallScreenshot(bool crashlog)
702 {
703  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
704  return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension, crashlog), CurrentScreenCallback, nullptr, _screen.width, _screen.height,
706 }
707 
714 {
715  switch(t) {
716  case SC_VIEWPORT:
717  case SC_CRASHLOG: {
720  vp->virtual_top = w->viewport->virtual_top;
723 
724  /* Compute pixel coordinates */
725  vp->left = 0;
726  vp->top = 0;
727  vp->width = _screen.width;
728  vp->height = _screen.height;
729  vp->overlay = w->viewport->overlay;
730  break;
731  }
732  case SC_WORLD: {
733  /* Determine world coordinates of screenshot */
735 
736  TileIndex north_tile = _settings_game.construction.freeform_edges ? TileXY(1, 1) : TileXY(0, 0);
737  TileIndex south_tile = MapSize() - 1;
738 
739  /* We need to account for a hill or high building at tile 0,0. */
740  int extra_height_top = TilePixelHeight(north_tile) + 150;
741  /* If there is a hill at the bottom don't create a large black area. */
742  int reclaim_height_bottom = TilePixelHeight(south_tile);
743 
744  vp->virtual_left = RemapCoords(TileX(south_tile) * TILE_SIZE, TileY(north_tile) * TILE_SIZE, 0).x;
745  vp->virtual_top = RemapCoords(TileX(north_tile) * TILE_SIZE, TileY(north_tile) * TILE_SIZE, extra_height_top).y;
746  vp->virtual_width = RemapCoords(TileX(north_tile) * TILE_SIZE, TileY(south_tile) * TILE_SIZE, 0).x - vp->virtual_left + 1;
747  vp->virtual_height = RemapCoords(TileX(south_tile) * TILE_SIZE, TileY(south_tile) * TILE_SIZE, reclaim_height_bottom).y - vp->virtual_top + 1;
748 
749  /* Compute pixel coordinates */
750  vp->left = 0;
751  vp->top = 0;
752  vp->width = UnScaleByZoom(vp->virtual_width, vp->zoom);
753  vp->height = UnScaleByZoom(vp->virtual_height, vp->zoom);
754  vp->overlay = nullptr;
755  break;
756  }
757  default: {
759 
761  vp->virtual_left = w->viewport->virtual_left;
762  vp->virtual_top = w->viewport->virtual_top;
763  vp->virtual_width = w->viewport->virtual_width;
764  vp->virtual_height = w->viewport->virtual_height;
765 
766  /* Compute pixel coordinates */
767  vp->left = 0;
768  vp->top = 0;
769  vp->width = UnScaleByZoom(vp->virtual_width, vp->zoom);
770  vp->height = UnScaleByZoom(vp->virtual_height, vp->zoom);
771  vp->overlay = nullptr;
772  break;
773  }
774  }
775 }
776 
783 {
784  ViewPort vp;
785  SetupScreenshotViewport(t, &vp);
786 
787  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
790 }
791 
801 static void HeightmapCallback(void *userdata, void *buffer, uint y, uint pitch, uint n)
802 {
803  byte *buf = (byte *)buffer;
804  while (n > 0) {
805  TileIndex ti = TileXY(MapMaxX(), y);
806  for (uint x = MapMaxX(); true; x--) {
807  *buf = 256 * TileHeight(ti) / (1 + _settings_game.construction.max_heightlevel);
808  buf++;
809  if (x == 0) break;
810  ti = TILE_ADDXY(ti, -1, 0);
811  }
812  y++;
813  n--;
814  }
815 }
816 
821 bool MakeHeightmapScreenshot(const char *filename)
822 {
823  Colour palette[256];
824  for (uint i = 0; i < lengthof(palette); i++) {
825  palette[i].a = 0xff;
826  palette[i].r = i;
827  palette[i].g = i;
828  palette[i].b = i;
829  }
830  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
831  return sf->proc(filename, HeightmapCallback, nullptr, MapSizeX(), MapSizeY(), 8, palette);
832 }
833 
840 bool MakeScreenshot(ScreenshotType t, const char *name)
841 {
842  if (t == SC_VIEWPORT) {
843  /* First draw the dirty parts of the screen and only then change the name
844  * of the screenshot. This way the screenshot will always show the name
845  * of the previous screenshot in the 'successful' message instead of the
846  * name of the new screenshot (or an empty name). */
847  UndrawMouseCursor();
848  DrawDirtyBlocks();
849  }
850 
851  _screenshot_name[0] = '\0';
852  if (name != nullptr) strecpy(_screenshot_name, name, lastof(_screenshot_name));
853 
854  bool ret;
855  switch (t) {
856  case SC_VIEWPORT:
857  ret = MakeSmallScreenshot(false);
858  break;
859 
860  case SC_CRASHLOG:
861  ret = MakeSmallScreenshot(true);
862  break;
863 
864  case SC_ZOOMEDIN:
865  case SC_DEFAULTZOOM:
866  case SC_WORLD:
867  ret = MakeLargeWorldScreenshot(t);
868  break;
869 
870  case SC_HEIGHTMAP: {
871  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
873  break;
874  }
875 
876  case SC_MINIMAP:
878  break;
879 
880  default:
881  NOT_REACHED();
882  }
883 
884  if (ret) {
886  ShowErrorMessage(STR_MESSAGE_SCREENSHOT_SUCCESSFULLY, INVALID_STRING_ID, WL_WARNING);
887  } else {
888  ShowErrorMessage(STR_ERROR_SCREENSHOT_FAILED, INVALID_STRING_ID, WL_ERROR);
889  }
890 
891  return ret;
892 }
893 
894 
902 {
903  Owner o;
904 
905  if (IsTileType(tile, MP_VOID)) {
906  return OWNER_END;
907  } else {
908  switch (GetTileType(tile)) {
909  case MP_INDUSTRY: o = OWNER_DEITY; break;
910  case MP_HOUSE: o = OWNER_TOWN; break;
911  default: o = GetTileOwner(tile); break;
912  /* FIXME: For MP_ROAD there are multiple owners.
913  * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
914  * even if there are no ROADTYPE_ROAD bits on the tile.
915  */
916  }
917 
918  return o;
919  }
920 }
921 
922 static void MinimapScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
923 {
924  /* Fill with the company colours */
925  byte owner_colours[OWNER_END + 1];
926  for (const Company *c : Company::Iterate()) {
927  owner_colours[c->index] = MKCOLOUR(_colour_gradient[c->colour][5]);
928  }
929 
930  /* Fill with some special colours */
931  owner_colours[OWNER_TOWN] = PC_DARK_RED;
932  owner_colours[OWNER_NONE] = PC_GRASS_LAND;
933  owner_colours[OWNER_WATER] = PC_WATER;
934  owner_colours[OWNER_DEITY] = PC_DARK_GREY; // industry
935  owner_colours[OWNER_END] = PC_BLACK;
936 
937  uint32 *ubuf = (uint32 *)buf;
938  uint num = (pitch * n);
939  for (uint i = 0; i < num; i++) {
940  uint row = y + (int)(i / pitch);
941  uint col = (MapSizeX() - 1) - (i % pitch);
942 
943  TileIndex tile = TileXY(col, row);
944  Owner o = GetMinimapOwner(tile);
945  byte val = owner_colours[o];
946 
947  uint32 colour_buf = 0;
948  colour_buf = (_cur_palette.palette[val].b << 0);
949  colour_buf |= (_cur_palette.palette[val].g << 8);
950  colour_buf |= (_cur_palette.palette[val].r << 16);
951 
952  *ubuf = colour_buf;
953  ubuf++; // Skip alpha
954  }
955 }
956 
961 {
962  const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
963  return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension), MinimapScreenCallback, nullptr, MapSizeX(), MapSizeY(), 32, _cur_palette.palette);
964 }
World screenshot.
Definition: screenshot.h:23
Functions related to OTTD&#39;s strings.
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
Owner
Enum for all companies/owners.
Definition: company_type.h:18
char _screenshot_format_name[8]
Extension of the current screenshot format (corresponds with _cur_screenshot_format).
Definition: screenshot.cpp:35
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:80
static bool MakeSmallScreenshot(bool crashlog)
Make a screenshot of the current screen.
Definition: screenshot.cpp:701
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:39
void InitializeScreenshotFormats()
Initialize screenshot format information on startup, with _screenshot_format_name filled from the loa...
Definition: screenshot.cpp:580
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:86
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:2826
Part of an industry.
Definition: tile_type.h:49
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:1133
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:52
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).
A town owns the tile, or a town is expanding.
Definition: company_type.h:24
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:62
#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:36
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.
static const uint8 PC_GRASS_LAND
Dark green palette colour for grass land.
Definition: gfx_func.h:228
Functions, definitions and such used only by the GUI.
Functions related to (drawing on) viewports.
Definition of a PCX file header.
Definition: screenshot.cpp:404
ScreenshotHandlerProc * proc
Function for writing the screenshot.
Definition: screenshot.cpp:67
void SetupScreenshotViewport(ScreenshotType t, ViewPort *vp)
Configure a ViewPort for rendering (a part of) the map into a screenshot.
Definition: screenshot.cpp:713
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
The tile has no ownership.
Definition: company_type.h:25
Functions/types related to saving and loading games.
static const uint8 PC_DARK_GREY
Dark grey palette colour.
Definition: gfx_func.h:205
Other information.
Definition: error.h:22
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
Functions related to errors.
static const char *const HEIGHTMAP_NAME
Default filename of a saved heightmap.
Definition: screenshot.cpp:33
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:49
Information about GRF, used in the game and (part of it) in savegames.
static Owner GetMinimapOwner(TileIndex tile)
Return the owner of a tile to display it with in the small map in mode "Owner".
Definition: screenshot.cpp:901
bool MakeMinimapWorldScreenshot()
Make a minimap screenshot.
Definition: screenshot.cpp:960
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
const char * extension
File extension.
Definition: screenshot.cpp:66
Functions related to the gfx engine.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
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:801
#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:65
static const ScreenshotFormat _screenshot_formats[]
Available screenshot formats.
Definition: screenshot.cpp:565
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:597
static const char * MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog=false)
Construct a pathname for a screenshot file.
Definition: screenshot.cpp:667
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:435
int virtual_width
width << zoom
Definition: viewport_type.h:30
Last + 1 owner.
Definition: company_type.h:28
#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:1442
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
static const uint8 PC_BLACK
Black palette colour.
Definition: gfx_func.h:204
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:258
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:840
#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:32
int left
Screen coordinate left edge of the viewport.
Definition: viewport_type.h:23
Format of palette data in BMP header.
Definition: screenshot.cpp:95
static const uint8 PC_DARK_RED
Dark red palette colour.
Definition: gfx_func.h:210
The tile/execution is done by "water".
Definition: company_type.h:26
uint _cur_screenshot_format
Index of the currently selected screenshot format in _screenshot_formats.
Definition: screenshot.cpp:37
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:378
Invisible tiles at the SW and SE border.
Definition: tile_type.h:48
static bool MakeLargeWorldScreenshot(ScreenshotType t)
Make a screenshot of the map.
Definition: screenshot.cpp:782
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:38
Minimap screenshot.
Definition: screenshot.h:25
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:574
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.
static const uint8 PC_WATER
Dark blue palette colour for water.
Definition: gfx_func.h:232
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
A house by a town.
Definition: tile_type.h:44
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:112
ZoomLevel zoom_min
minimum zoom out level
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:326
bool MakeHeightmapScreenshot(const char *filename)
Make a heightmap of the current map.
Definition: screenshot.cpp:821
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
The object is owned by a superuser / goal script.
Definition: company_type.h:27
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:612
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