27 #include "table/strings.h" 74 PACK(
struct BitmapFileHeader {
80 assert_compile(
sizeof(BitmapFileHeader) == 14);
86 uint16 planes, bitcount;
87 uint32 compression, sizeimage, xpels, ypels, clrused, clrimp;
93 byte blue, green, red, reserved;
95 assert_compile(
sizeof(
RgbQuad) == 4);
112 switch (pixelformat) {
113 case 8: bpp = 1;
break;
115 case 32: bpp = 3;
break;
117 default:
return false;
120 FILE *f = fopen(name,
"wb");
121 if (f ==
nullptr)
return false;
124 uint bytewidth =
Align(w * bpp, 4);
127 uint pal_size = pixelformat == 8 ?
sizeof(
RgbQuad) * 256 : 0;
130 BitmapFileHeader bfh;
131 bfh.type = TO_LE16(
'MB');
132 bfh.size = TO_LE32(
sizeof(BitmapFileHeader) +
sizeof(
BitmapInfoHeader) + pal_size + bytewidth * h);
134 bfh.off_bits = TO_LE32(
sizeof(BitmapFileHeader) +
sizeof(
BitmapInfoHeader) + pal_size);
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);
151 if (fwrite(&bfh,
sizeof(bfh), 1, f) != 1 || fwrite(&bih,
sizeof(bih), 1, f) != 1) {
156 if (pixelformat == 8) {
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;
166 if (fwrite(rq,
sizeof(rq), 1, f) != 1) {
173 uint maxlines =
Clamp(65536 / (w * pixelformat / 8), 16, 128);
175 uint8 *buff = MallocT<uint8>(maxlines * w * pixelformat / 8);
176 uint8 *line =
AllocaM(uint8, bytewidth);
177 memset(line, 0, bytewidth);
181 uint n =
min(h, maxlines);
185 callb(userdata, buff, h, w, n);
189 if (pixelformat == 8) {
191 memcpy(line, buff + n * w, w);
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;
204 if (fwrite(line, bytewidth, 1, f) != 1) {
221 #if defined(WITH_PNG) 224 #ifdef PNG_TEXT_SUPPORTED 232 static void PNGAPI
png_my_error(png_structp png_ptr, png_const_charp message)
234 DEBUG(misc, 0,
"[libpng] error: %s - %s", message, (
const char *)png_get_error_ptr(png_ptr));
235 longjmp(png_jmpbuf(png_ptr), 1);
238 static void PNGAPI
png_my_warning(png_structp png_ptr, png_const_charp message)
240 DEBUG(misc, 1,
"[libpng] warning: %s - %s", message, (
const char *)png_get_error_ptr(png_ptr));
261 uint bpp = pixelformat / 8;
266 if (pixelformat != 8 && pixelformat != 32)
return false;
268 f = fopen(name,
"wb");
269 if (f ==
nullptr)
return false;
273 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);
285 if (setjmp(png_jmpbuf(png_ptr))) {
286 png_destroy_write_struct(&png_ptr, &info_ptr);
291 png_init_io(png_ptr, f);
293 png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
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);
298 #ifdef PNG_TEXT_SUPPORTED 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;
319 if (c->ai_info ==
nullptr) {
322 p +=
seprintf(p,
lastof(buf),
"%2i: %s (v%d)\n", (
int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
325 text[1].key =
const_cast<char *
>(
"Description");
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);
332 if (pixelformat == 8) {
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;
340 png_set_PLTE(png_ptr, info_ptr, rq, 256);
343 png_write_info(png_ptr, info_ptr);
344 png_set_flush(png_ptr, 512);
346 if (pixelformat == 32) {
355 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
357 #if TTD_ENDIAN == TTD_LITTLE_ENDIAN 358 png_set_bgr(png_ptr);
359 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
361 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
366 maxlines =
Clamp(65536 / w, 16, 128);
369 void *buff = CallocT<uint8>(w * maxlines * bpp);
374 n =
min(h - y, maxlines);
377 callb(userdata, buff, y, w, n);
381 for (i = 0; i != n; i++) {
382 png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
386 png_write_end(png_ptr, info_ptr);
387 png_destroy_write_struct(&png_ptr, &info_ptr);
409 byte pal_small[16 * 3];
418 assert_compile(
sizeof(
PcxHeader) == 128);
440 if (pixelformat == 32) {
441 DEBUG(misc, 0,
"Can't convert a 32bpp screenshot to PCX format. Please pick another format.");
444 if (pixelformat != 8 || w == 0)
return false;
446 f = fopen(name,
"wb");
447 if (f ==
nullptr)
return false;
449 memset(&pcx, 0,
sizeof(pcx));
452 pcx.manufacturer = 10;
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);
462 pcx.cpal = TO_LE16(1);
463 pcx.width = pcx.pitch = TO_LE16(w);
464 pcx.height = TO_LE16(h);
467 if (fwrite(&pcx,
sizeof(pcx), 1, f) != 1) {
473 maxlines =
Clamp(65536 / w, 16, 128);
476 uint8 *buff = CallocT<uint8>(w * maxlines);
481 uint n =
min(h - y, maxlines);
485 callb(userdata, buff, y, w, n);
489 for (i = 0; i != n; i++) {
490 const uint8 *bufp = buff + i * w;
491 byte runchar = bufp[0];
496 for (j = 1; j < w; j++) {
499 if (ch != runchar || runcount >= 0x3f) {
500 if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
501 if (fputc(0xC0 | runcount, f) == EOF) {
507 if (fputc(runchar, f) == EOF) {
519 if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
520 if (fputc(0xC0 | runcount, f) == EOF) {
526 if (fputc(runchar, f) == EOF) {
537 if (fputc(12, f) == EOF) {
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;
550 success = fwrite(tmp,
sizeof(tmp), 1, f) == 1;
563 #if defined(WITH_PNG) 580 for (uint i = 0; i <
lengthof(_screenshot_formats); i++) {
597 void *src = blitter->
MoveTo(_screen.dst_ptr, 0, y);
619 _screen.dst_ptr = buf;
620 _screen.width = pitch;
622 _screen.pitch = pitch;
630 dpi.width = vp->
width;
638 while (vp->
width - left != 0) {
653 _screen = old_screen;
664 static const char *
MakeScreenshotName(
const char *default_fn,
const char *ext,
bool crashlog =
false)
682 for (uint serial = 1;; serial++) {
688 if (!generate)
break;
743 vp->overlay =
nullptr;
772 byte *buf = (byte *)buffer;
775 for (uint x =
MapMaxX();
true; x--) {
793 for (uint i = 0; i <
lengthof(palette); i++) {
Functions related to OTTD's strings.
char _screenshot_format_name[8]
Extension of the current screenshot format (corresponds with _cur_screenshot_format).
uint8 max_heightlevel
maximum allowed heightlevel
static uint MapSizeX()
Get the size of the map along the X.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static bool MakeSmallScreenshot(bool crashlog)
Make a screenshot of the current screen.
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.
void InitializeScreenshotFormats()
Initialize screenshot format information on startup, with _screenshot_format_name filled from the loa...
int virtual_left
Virtual left coordinate.
Data about how and where to blit pixels.
static uint MapSizeY()
Get the size of the map along the Y.
GRFConfig * _grfconfig
First item in list of current GRF set up.
void GenerateDefaultSaveName(char *buf, const char *last)
Fill the buffer with the default name for a savegame or screenshot.
int height
Screen height of the viewport.
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...
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
Handle warning in pnglib.
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
static int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL) When shifting right...
uint8 a
colour channels in LE order
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
int virtual_height
height << zoom
static uint TileX(TileIndex tile)
Get the X component of a tile.
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right...
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.
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.
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 lastof(x)
Get the last element of an fixed size array.
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
uint _num_screenshot_formats
Number of available screenshot formats.
How all blitters should look like.
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.
static const uint TILE_SIZE
Tile size in world coordinates.
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.
void SetupScreenshotViewport(ScreenshotType t, ViewPort *vp)
Configure a ViewPort for rendering (a part of) the map into a screenshot.
bool freeform_edges
allow terraforming the tiles at the map edges
Data structure for an opened window.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Main window; Window numbers:
Functions/types related to saving and loading games.
Functions related to errors.
static const char *const HEIGHTMAP_NAME
Default filename of a saved heightmap.
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
The client is spectating.
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...
Information about GRF, used in the game and (part of it) in savegames.
Functions related to the gfx engine.
ClientSettings _settings_client
The current settings for this game.
bool FileExists(const char *filename)
Test whether the given filename exists.
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.
#define TILE_ADDXY(tile, x, y)
Adds a given offset to a tile.
A number of safeguards to prevent using unsafe methods.
Default zoom level for the world screen shot.
static const ScreenshotFormat _screenshot_formats[]
Available screenshot formats.
static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
Callback of the screenshot generator that dumps the current video buffer.
static const char * MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog=false)
Construct a pathname for a screenshot file.
const char * _personal_dir
custom directory for personal settings, saves, newgrf, etc.
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant 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.
int virtual_width
width << zoom
#define lengthof(x)
Return the length of an fixed size array.
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
static T min(const T a, const T b)
Returns the minimum of two values.
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.
Palette _cur_palette
Current palette.
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
bool MakeScreenshot(ScreenshotType t, const char *name)
Make an actual screenshot.
#define DEBUG(name, level,...)
Output a line of debugging information.
static const char *const SCREENSHOT_NAME
Default filename of a saved screenshot.
int left
Screen coordinate left edge of the viewport.
Format of palette data in BMP header.
uint _cur_screenshot_format
Index of the currently selected screenshot format in _screenshot_formats.
Functions related to companies.
static uint MapSize()
Get the size of the map.
GUISettings gui
settings related to the GUI
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static bool MakeLargeWorldScreenshot(ScreenshotType t)
Make a screenshot of the map.
Data structure for viewport, display of a part of the world.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Default zoom level for viewports.
Zoomed to default zoom level screenshot of the visible area.
uint32 TileIndex
The index/ID of a Tile.
static char _screenshot_name[128]
Filename of the screenshot file.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Functions related to zooming.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
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's landscape.
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
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)
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
declaration of OTTD revision dependent variables
ZoomLevel zoom
The zoom level of the viewport.
int virtual_top
Virtual top coordinate.
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
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.
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.
ZoomLevel zoom_min
minimum zoom out level
bool MakeHeightmapScreenshot(const char *filename)
Make a heightmap of the current map.
Errors (eg. saving/loading failed)
Raw screenshot from blitter buffer.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Factory to 'query' all available blitters.
static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
generate a large piece of the world
static uint TilePixelHeight(TileIndex tile)
Returns the height of a tile in pixels.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
ScreenshotType
Type of requested screenshot.
int width
Screen width of the viewport.