OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
33 #include "table/strings.h"
74 #define MKCOLOUR(x) TO_LE32(x)
81 PACK(
struct BitmapFileHeader {
87 static_assert(
sizeof(BitmapFileHeader) == 14);
92 int32_t width, height;
93 uint16_t planes, bitcount;
94 uint32_t compression, sizeimage, xpels, ypels, clrused, clrimp;
100 uint8_t blue, green, red, reserved;
102 static_assert(
sizeof(
RgbQuad) == 4);
119 switch (pixelformat) {
120 case 8: bpp = 1;
break;
122 case 32: bpp = 3;
break;
124 default:
return false;
128 if (!of.has_value())
return false;
132 uint bytewidth =
Align(w * bpp, 4);
135 uint pal_size = pixelformat == 8 ?
sizeof(
RgbQuad) * 256 : 0;
138 BitmapFileHeader bfh;
139 bfh.type = TO_LE16(
'MB');
140 bfh.size = TO_LE32(
sizeof(BitmapFileHeader) +
sizeof(
BitmapInfoHeader) + pal_size +
static_cast<size_t>(bytewidth) * h);
142 bfh.off_bits = TO_LE32(
sizeof(BitmapFileHeader) +
sizeof(
BitmapInfoHeader) + pal_size);
147 bih.width = TO_LE32(w);
148 bih.height = TO_LE32(h);
149 bih.planes = TO_LE16(1);
150 bih.bitcount = TO_LE16(bpp * 8);
159 if (fwrite(&bfh,
sizeof(bfh), 1, f) != 1 || fwrite(&bih,
sizeof(bih), 1, f) != 1) {
163 if (pixelformat == 8) {
166 for (uint i = 0; i < 256; i++) {
167 rq[i].red = palette[i].r;
168 rq[i].green = palette[i].g;
169 rq[i].blue = palette[i].b;
173 if (fwrite(rq,
sizeof(rq), 1, f) != 1) {
179 uint maxlines =
Clamp(65536 / (w * pixelformat / 8), 16, 128);
181 std::vector<uint8_t> buff(maxlines * w * pixelformat / 8);
182 std::vector<uint8_t> line(bytewidth);
186 uint n = std::min(h, maxlines);
190 callb(userdata, buff.data(), h, w, n);
194 if (pixelformat == 8) {
196 memcpy(line.data(), buff.data() + n * w, w);
201 uint8_t *dst = line.
data();
202 for (uint i = 0; i < w; i++) {
203 dst[i * 3 ] = src[i].b;
204 dst[i * 3 + 1] = src[i].g;
205 dst[i * 3 + 2] = src[i].r;
209 if (fwrite(line.data(), bytewidth, 1, f) != 1) {
222 #if defined(WITH_PNG)
225 #ifdef PNG_TEXT_SUPPORTED
233 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
235 Debug(misc, 0,
"[libpng] error: {} - {}", message, (
const char *)png_get_error_ptr(png_ptr));
236 longjmp(png_jmpbuf(png_ptr), 1);
239 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
241 Debug(misc, 1,
"[libpng] warning: {} - {}", message, (
const char *)png_get_error_ptr(png_ptr));
261 uint bpp = pixelformat / 8;
266 if (pixelformat != 8 && pixelformat != 32)
return false;
269 if (!of.has_value())
return false;
272 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
const_cast<char *
>(name), png_my_error, png_my_warning);
274 if (png_ptr ==
nullptr) {
278 info_ptr = png_create_info_struct(png_ptr);
279 if (info_ptr ==
nullptr) {
280 png_destroy_write_struct(&png_ptr, (png_infopp)
nullptr);
284 if (setjmp(png_jmpbuf(png_ptr))) {
285 png_destroy_write_struct(&png_ptr, &info_ptr);
289 png_init_io(png_ptr, f);
291 png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
293 png_set_IHDR(png_ptr, info_ptr, w, h, 8, pixelformat == 8 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
294 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
296 #ifdef PNG_TEXT_SUPPORTED
299 png_text_struct text[2];
300 memset(text, 0,
sizeof(text));
301 text[0].key =
const_cast<char *
>(
"Software");
302 text[0].text =
const_cast<char *
>(_openttd_revision);
303 text[0].text_length = strlen(_openttd_revision);
304 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
307 message.reserve(1024);
309 message +=
"NewGRFs:\n";
311 fmt::format_to(std::back_inserter(message),
"{:08X} {} {}\n",
BSWAP32(c->ident.grfid),
FormatArrayAsHex(c->ident.md5sum), c->filename);
313 message +=
"\nCompanies:\n";
315 if (c->ai_info ==
nullptr) {
316 fmt::format_to(std::back_inserter(message),
"{:2d}: Human\n", (
int)c->index);
318 fmt::format_to(std::back_inserter(message),
"{:2d}: {} (v{})\n", (
int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
321 text[1].key =
const_cast<char *
>(
"Description");
322 text[1].text =
const_cast<char *
>(message.c_str());
323 text[1].text_length = message.size();
324 text[1].compression = PNG_TEXT_COMPRESSION_zTXt;
325 png_set_text(png_ptr, info_ptr, text, 2);
328 if (pixelformat == 8) {
330 for (i = 0; i != 256; i++) {
331 rq[i].red = palette[i].r;
332 rq[i].green = palette[i].g;
333 rq[i].blue = palette[i].b;
336 png_set_PLTE(png_ptr, info_ptr, rq, 256);
339 png_write_info(png_ptr, info_ptr);
340 png_set_flush(png_ptr, 512);
342 if (pixelformat == 32) {
351 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
353 if constexpr (std::endian::native == std::endian::little) {
354 png_set_bgr(png_ptr);
355 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
357 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
362 maxlines =
Clamp(65536 / w, 16, 128);
365 std::vector<uint8_t> buff(
static_cast<size_t>(w) * maxlines * bpp);
370 n = std::min(h - y, maxlines);
373 callb(userdata, buff.data(), y, w, n);
377 for (i = 0; i != n; i++) {
378 png_write_row(png_ptr, (png_bytep)buff.data() + i * w * bpp);
382 png_write_end(png_ptr, info_ptr);
383 png_destroy_write_struct(&png_ptr, &info_ptr);
396 uint8_t manufacturer;
403 uint8_t pal_small[16 * 3];
433 if (pixelformat == 32) {
434 Debug(misc, 0,
"Can't convert a 32bpp screenshot to PCX format. Please pick another format.");
437 if (pixelformat != 8 || w == 0)
return false;
440 if (!of.has_value())
return false;
443 memset(&pcx, 0,
sizeof(pcx));
446 pcx.manufacturer = 10;
450 pcx.xmax = TO_LE16(w - 1);
451 pcx.ymax = TO_LE16(h - 1);
452 pcx.hdpi = TO_LE16(320);
453 pcx.vdpi = TO_LE16(320);
456 pcx.cpal = TO_LE16(1);
457 pcx.width = pcx.pitch = TO_LE16(w);
458 pcx.height = TO_LE16(h);
461 if (fwrite(&pcx,
sizeof(pcx), 1, f) != 1) {
466 maxlines =
Clamp(65536 / w, 16, 128);
469 std::vector<uint8_t> buff(
static_cast<size_t>(w) * maxlines);
474 uint n = std::min(h - y, maxlines);
478 callb(userdata, buff.data(), y, w, n);
482 for (i = 0; i != n; i++) {
483 const uint8_t *bufp = buff.data() + i * w;
484 uint8_t runchar = bufp[0];
489 for (j = 1; j < w; j++) {
490 uint8_t ch = bufp[j];
492 if (ch != runchar || runcount >= 0x3f) {
493 if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
494 if (fputc(0xC0 | runcount, f) == EOF) {
498 if (fputc(runchar, f) == EOF) {
508 if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
509 if (fputc(0xC0 | runcount, f) == EOF) {
513 if (fputc(runchar, f) == EOF) {
520 if (fputc(12, f) == EOF) {
525 uint8_t tmp[256 * 3];
527 for (uint i = 0; i < 256; i++) {
528 tmp[i * 3 + 0] = palette[i].r;
529 tmp[i * 3 + 1] = palette[i].g;
530 tmp[i * 3 + 2] = palette[i].b;
532 success = fwrite(tmp,
sizeof(tmp), 1, f) == 1;
543 #if defined(WITH_PNG)
556 return _cur_screenshot_format->
extension;
564 _cur_screenshot_format = &format;
579 void *src = blitter->
MoveTo(_screen.dst_ptr, 0, y);
601 _screen.dst_ptr = buf;
602 _screen.width = pitch;
604 _screen.pitch = pitch;
611 dpi.width = vp->
width;
619 while (vp->
width - left != 0) {
620 wx = std::min(vp->
width - left, 1600);
632 _screen = old_screen;
643 static const char *
MakeScreenshotName(
const char *default_fn,
const char *ext,
bool crashlog =
false)
667 for (uint serial = 1;; serial++) {
670 if (!generate)
break;
699 assert(width == 0 && height == 0);
710 vp->
width = _screen.width;
711 vp->
height = _screen.height;
716 assert(width == 0 && height == 0);
739 vp->overlay =
nullptr;
749 if (width == 0 || height == 0) {
762 vp->overlay =
nullptr;
793 uint8_t *buf = (uint8_t *)buffer;
814 for (uint i = 0; i <
lengthof(palette); i++) {
856 if (width * height > 8192 * 8192) {
966 static void MinimapScreenCallback(
void *,
void *buf, uint y, uint pitch, uint n)
968 uint32_t *ubuf = (uint32_t *)buf;
969 uint num = (pitch * n);
970 for (uint i = 0; i < num; i++) {
971 uint row = y + (int)(i / pitch);
977 uint32_t colour_buf = 0;
Palette _cur_palette
Current palette.
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Format of palette data in BMP header.
void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback, bool focus)
Show a confirmation window with standard 'yes' and 'no' buttons The window is aligned to the centre o...
ScreenshotType
Type of requested screenshot.
std::string _personal_dir
custom directory for personal settings, saves, newgrf, etc.
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
@ WL_WARNING
Other information.
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
std::mutex lock
synchronization for playback status fields
How all blitters should look like.
int width
Screen width of the viewport.
@ SC_HEIGHTMAP
Heightmap of the world.
static void HeightmapCallback(void *, void *buffer, uint y, uint, uint n)
Callback for generating a heightmap.
ViewportData * viewport
Pointer to viewport data, if present.
int height
Screen height of the viewport.
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
int top
Screen coordinate top edge of the viewport.
std::string _screenshot_format_name
Extension of the current screenshot format (corresponds with #_cur_screenshot_format).
static const uint TILE_SIZE
Tile size in world coordinates.
ClientSettings _settings_client
The current settings for this game.
static void ScreenshotConfirmationCallback(Window *, bool confirmed)
Callback on the confirmation window for huge screenshots.
@ SC_ZOOMEDIN
Fully zoomed in screenshot of the visible area.
static std::optional< FileHandle > Open(const std::string &filename, const std::string &mode)
Open an RAII file handle if possible.
int virtual_top
Virtual top coordinate.
bool MakeHeightmapScreenshot(const char *filename)
Make a heightmap of the current map.
void InitializeScreenshotFormats()
Initialize screenshot format information on startup, with _screenshot_format_name filled from the loa...
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
static ScreenshotType _confirmed_screenshot_type
Screenshot type the current query is about to confirm.
Data structure for viewport, display of a part of the world.
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
Information about GRF, used in the game and (part of it) in savegames.
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
int virtual_left
Virtual left coordinate.
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
int left
Screen coordinate left edge of the viewport.
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
bool FileExists(const std::string &filename)
Test whether the given filename exists.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
std::string GenerateDefaultSaveName()
Get the default name for a savegame or screenshot.
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.
#define lengthof(array)
Return the length of an fixed size array.
bool freeform_edges
allow terraforming the tiles at the map edges
PACK(struct BitmapFileHeader { uint16_t type;uint32_t size;uint32_t reserved;uint32_t off_bits;})
BMP File Header (stored in little endian)
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
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.
static const char * MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog=false)
Construct a pathname for a screenshot file.
int virtual_width
width << zoom
void QueueOnMainThread(std::function< void()> &&func)
Queue a function to be called on the main thread with game state lock held and video buffer locked.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
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.
uint32_t data
Conversion of the channel information to a 32 bit number.
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
ZoomLevel zoom_min
minimum zoom out level
static debug_inline uint SizeX()
Get the size of the map along the X.
static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
generate a large piece of the world
static const ScreenshotFormat _screenshot_formats[]
Available screenshot formats.
uint TilePixelHeight(Tile tile)
Returns the height of a tile in pixels.
void SetScreenshotWindowVisibility(bool hide)
Set the visibility of the screenshot window when taking a screenshot.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .BMP writer.
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN) When shifting right,...
@ SC_WORLD
World screenshot.
static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .PCX file image writer.
static const char *const HEIGHTMAP_NAME
Default filename of a saved heightmap.
@ ZOOM_LVL_VIEWPORT
Default zoom level for viewports.
void SetupScreenshotViewport(ScreenshotType t, Viewport *vp, uint32_t width, uint32_t height)
Configure a Viewport for rendering (a part of) the map into a screenshot.
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...
static debug_inline uint Size()
Get the size of the map.
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Window * GetMainWindow()
Get the main window, i.e.
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
static bool RealMakeScreenshot(ScreenshotType t, std::string name, uint32_t width, uint32_t height)
Make a screenshot.
static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .PNG file image writer.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
@ SC_DEFAULTZOOM
Zoomed to default zoom level screenshot of the visible area.
static bool MakeSmallScreenshot(bool crashlog)
Make a screenshot of the current screen.
@ WL_ERROR
Errors (eg. saving/loading failed)
bool MakeMinimapWorldScreenshot()
Make a minimap screenshot.
GRFConfig * _grfconfig
First item in list of current GRF set up.
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
ZoomLevel zoom
The zoom level of the viewport.
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
@ COMPANY_SPECTATOR
The client is spectating.
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
@ SC_VIEWPORT
Screenshot of viewport.
void MakeScreenshotWithConfirm(ScreenshotType t)
Make a screenshot.
static std::string _screenshot_name
Filename of the screenshot file.
ConstructionSettings construction
construction of things in-game
Data structure for an opened window.
@ SC_CRASHLOG
Raw screenshot from blitter buffer.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
@ Never
Never include the heightmap.
int virtual_height
height << zoom
bool MakeScreenshot(ScreenshotType t, std::string name, uint32_t width, uint32_t height)
Schedule making a screenshot.
@ SC_MINIMAP
Minimap screenshot.
static const char *const SCREENSHOT_NAME
Default filename of a saved screenshot.
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_MIN) When shifting right,...
static bool MakeLargeWorldScreenshot(ScreenshotType t, uint32_t width=0, uint32_t height=0)
Make a screenshot of the map.
uint _heightmap_highest_peak
When saving a heightmap, this contains the highest peak on the map.
Helper struct to ensure the video buffer is locked and ready for drawing.
uint32_t GetSmallMapOwnerPixels(TileIndex tile, TileType t, IncludeHeightmap include_heightmap)
Return the colour a tile would be displayed with in the small map in mode "Owner".
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
uint8_t a
colour channels in LE order
@ ZOOM_LVL_WORLD_SCREENSHOT
Default zoom level for the world screen shot.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
static uint SizeY()
Get the size of the map along the Y.
GUISettings gui
settings related to the GUI
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.
static void CurrentScreenCallback(void *, void *buf, uint y, uint pitch, uint n)
Callback of the screenshot generator that dumps the current video buffer.
std::string _full_screenshot_path
Pathname of the screenshot file.
Data about how and where to blit pixels.
const char * FiosGetScreenshotDir()
Get the directory for screenshots.