OpenTTD Source 20241224-master-gf74b0cf984
screenshot.cpp File Reference

The creation of screenshots! More...

#include "stdafx.h"
#include "core/backup_type.hpp"
#include "fileio_func.h"
#include "viewport_func.h"
#include "gfx_func.h"
#include "screenshot.h"
#include "screenshot_gui.h"
#include "blitter/factory.hpp"
#include "zoom_func.h"
#include "core/endian_func.hpp"
#include "saveload/saveload.h"
#include "company_base.h"
#include "company_func.h"
#include "strings_func.h"
#include "error.h"
#include "textbuf_gui.h"
#include "window_gui.h"
#include "window_func.h"
#include "tile_map.h"
#include "landscape.h"
#include "video/video_driver.hpp"
#include "smallmap_gui.h"
#include "table/strings.h"
#include "safeguards.h"
#include <png.h>

Go to the source code of this file.

Data Structures

struct  ScreenshotFormat
 Screenshot format information. More...
 
struct  BitmapInfoHeader
 BMP Info Header (stored in little endian) More...
 
struct  RgbQuad
 Format of palette data in BMP header. More...
 
struct  PcxHeader
 Definition of a PCX file header. More...
 

Macros

#define MKCOLOUR(x)   TO_LE32(x)
 

Typedefs

typedef 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.
 
typedef 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.
 

Functions

 PACK (struct BitmapFileHeader { uint16_t type;uint32_t size;uint32_t reserved;uint32_t off_bits;})
 BMP File Header (stored in little endian)
 
static bool MakeBMPImage (const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
 Generic .BMP writer.
 
static void PNGAPI png_my_error (png_structp png_ptr, png_const_charp message)
 
static void PNGAPI png_my_warning (png_structp png_ptr, png_const_charp message)
 
static bool MakePNGImage (const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
 Generic .PNG file image writer.
 
static bool MakePCXImage (const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
 Generic .PCX file image writer.
 
const char * GetCurrentScreenshotExtension ()
 Get filename extension of current screenshot file format.
 
void InitializeScreenshotFormats ()
 Initialize screenshot format information on startup, with _screenshot_format_name filled from the loadsave code.
 
static void CurrentScreenCallback (void *, void *buf, uint y, uint pitch, uint n)
 Callback of the screenshot generator that dumps the current video buffer.
 
static void LargeWorldCallback (void *userdata, void *buf, uint y, uint pitch, uint n)
 generate a large piece of the world
 
static const char * MakeScreenshotName (const char *default_fn, const char *ext, bool crashlog=false)
 Construct a pathname for a screenshot file.
 
static bool MakeSmallScreenshot (bool crashlog)
 Make a screenshot of the current screen.
 
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.
 
static bool MakeLargeWorldScreenshot (ScreenshotType t, uint32_t width=0, uint32_t height=0)
 Make a screenshot of the map.
 
static void HeightmapCallback (void *, void *buffer, uint y, uint, uint n)
 Callback for generating a heightmap.
 
bool MakeHeightmapScreenshot (const char *filename)
 Make a heightmap of the current map.
 
static void ScreenshotConfirmationCallback (Window *, bool confirmed)
 Callback on the confirmation window for huge screenshots.
 
void MakeScreenshotWithConfirm (ScreenshotType t)
 Make a screenshot.
 
static bool RealMakeScreenshot (ScreenshotType t, std::string name, uint32_t width, uint32_t height)
 Make a screenshot.
 
bool MakeScreenshot (ScreenshotType t, std::string name, uint32_t width, uint32_t height)
 Schedule making a screenshot.
 
static void MinimapScreenCallback (void *, void *buf, uint y, uint pitch, uint n)
 
bool MakeMinimapWorldScreenshot ()
 Make a minimap screenshot.
 

Variables

static const char *const SCREENSHOT_NAME = "screenshot"
 Default filename of a saved screenshot.
 
static const char *const HEIGHTMAP_NAME = "heightmap"
 Default filename of a saved heightmap.
 
std::string _screenshot_format_name
 Extension of the current screenshot format (corresponds with _cur_screenshot_format).
 
static std::string _screenshot_name
 Filename of the screenshot file.
 
std::string _full_screenshot_path
 Pathname of the screenshot file.
 
uint _heightmap_highest_peak
 When saving a heightmap, this contains the highest peak on the map.
 
static const ScreenshotFormat _screenshot_formats []
 Available screenshot formats.
 
static const ScreenshotFormat_cur_screenshot_format = std::begin(_screenshot_formats)
 
static ScreenshotType _confirmed_screenshot_type
 Screenshot type the current query is about to confirm.
 

Detailed Description

The creation of screenshots!

Definition in file screenshot.cpp.

Macro Definition Documentation

◆ MKCOLOUR

#define MKCOLOUR (   x)    TO_LE32(x)

Definition at line 74 of file screenshot.cpp.

Typedef Documentation

◆ ScreenshotCallback

typedef 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.

Parameters
userdataPointer to user data.
bufDestination buffer.
yLine number of the first line to write.
pitchNumber of pixels to write (1 byte for 8bpp, 4 bytes for 32bpp).
See also
Colour
Parameters
nNumber of lines to write.

Definition at line 53 of file screenshot.cpp.

◆ ScreenshotHandlerProc

typedef 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.

Parameters
nameFilename, including extension.
callbCallback function for generating lines of pixels.
userdataUser data, passed on to callb.
wWidth of the image in pixels.
hHeight of the image in pixels.
pixelformatBits per pixel (bpp), either 8 or 32.
paletteColour palette (for 8bpp images).
Returns
File was written successfully.

Definition at line 66 of file screenshot.cpp.

Function Documentation

◆ CurrentScreenCallback()

static void CurrentScreenCallback ( void *  ,
void *  buf,
uint  y,
uint  pitch,
uint  n 
)
static

Callback of the screenshot generator that dumps the current video buffer.

See also
ScreenshotCallback

Definition at line 576 of file screenshot.cpp.

References Blitter::CopyImageToBuffer(), BlitterFactory::GetCurrentBlitter(), and Blitter::MoveTo().

Referenced by MakeSmallScreenshot().

◆ GetCurrentScreenshotExtension()

const char * GetCurrentScreenshotExtension ( )

Get filename extension of current screenshot file format.

Definition at line 554 of file screenshot.cpp.

References ScreenshotFormat::extension.

Referenced by FiosMakeHeightmapName().

◆ HeightmapCallback()

static void HeightmapCallback ( void *  ,
void *  buffer,
uint  y,
uint  ,
uint  n 
)
static

Callback for generating a heightmap.

Supports 8bpp grayscale only.

Parameters
bufferDestination buffer.
yLine number of the first line to write.
nNumber of lines to write.
See also
ScreenshotCallback

Definition at line 791 of file screenshot.cpp.

References _heightmap_highest_peak, Map::MaxX(), TileAddXY(), TileHeight(), and TileXY().

Referenced by MakeHeightmapScreenshot().

◆ InitializeScreenshotFormats()

void InitializeScreenshotFormats ( )

Initialize screenshot format information on startup, with _screenshot_format_name filled from the loadsave code.

Definition at line 560 of file screenshot.cpp.

References _screenshot_format_name, and _screenshot_formats.

Referenced by openttd_main().

◆ LargeWorldCallback()

static void LargeWorldCallback ( void *  userdata,
void *  buf,
uint  y,
uint  pitch,
uint  n 
)
static

generate a large piece of the world

Parameters
userdataViewport area to draw
bufVideobuffer with same bitdepth as current blitter
yFirst line to render
pitchPitch of the videobuffer
nNumber of lines to render

Definition at line 591 of file screenshot.cpp.

References _screen_disable_anim, Viewport::left, ScaleByZoom(), Viewport::top, Viewport::virtual_left, Viewport::virtual_top, Viewport::width, Viewport::zoom, and ZOOM_LVL_WORLD_SCREENSHOT.

Referenced by MakeLargeWorldScreenshot().

◆ MakeBMPImage()

static bool MakeBMPImage ( const char *  name,
ScreenshotCallback callb,
void *  userdata,
uint  w,
uint  h,
int  pixelformat,
const Colour palette 
)
static

Generic .BMP writer.

Parameters
namefile name including extension
callbcallback used for gathering rendered image
userdataparameters forwarded to callb
wwidth in pixels
hheight in pixels
pixelformatbits per pixel
palettecolour palette (for 8bpp mode)
Returns
was everything ok?
See also
ScreenshotHandlerProc

Definition at line 116 of file screenshot.cpp.

References Align(), Colour::b, Clamp(), Colour::data, and FileHandle::Open().

◆ MakeHeightmapScreenshot()

bool MakeHeightmapScreenshot ( const char *  filename)

Make a heightmap of the current map.

Parameters
filenameFilename to use for saving.

Definition at line 811 of file screenshot.cpp.

References _heightmap_highest_peak, Colour::b, HeightmapCallback(), Map::Iterate(), lengthof, ScreenshotFormat::proc, Map::SizeX(), Map::SizeY(), and TileHeight().

Referenced by RealMakeScreenshot().

◆ MakeLargeWorldScreenshot()

static bool MakeLargeWorldScreenshot ( ScreenshotType  t,
uint32_t  width = 0,
uint32_t  height = 0 
)
static

Make a screenshot of the map.

Parameters
tScreenshot type: World or viewport screenshot
widththe width of the screenshot of, or 0 for current viewport width.
heightthe height of the screenshot of, or 0 for current viewport height.
Returns
true on success

Definition at line 775 of file screenshot.cpp.

References _cur_palette, ScreenshotFormat::extension, BlitterFactory::GetCurrentBlitter(), Blitter::GetScreenDepth(), Viewport::height, LargeWorldCallback(), MakeScreenshotName(), Palette::palette, ScreenshotFormat::proc, SCREENSHOT_NAME, SetupScreenshotViewport(), and Viewport::width.

Referenced by RealMakeScreenshot().

◆ MakeMinimapWorldScreenshot()

bool MakeMinimapWorldScreenshot ( )

◆ MakePCXImage()

static bool MakePCXImage ( const char *  name,
ScreenshotCallback callb,
void *  userdata,
uint  w,
uint  h,
int  pixelformat,
const Colour palette 
)
static

Generic .PCX file image writer.

Parameters
nameFilename, including extension.
callbCallback function for generating lines of pixels.
userdataUser data, passed on to callb.
wWidth of the image in pixels.
hHeight of the image in pixels.
pixelformatBits per pixel (bpp), either 8 or 32.
paletteColour palette (for 8bpp images).
Returns
File was written successfully.
See also
ScreenshotHandlerProc

Definition at line 426 of file screenshot.cpp.

References Colour::b, Clamp(), Debug, and FileHandle::Open().

◆ MakePNGImage()

static bool MakePNGImage ( const char *  name,
ScreenshotCallback callb,
void *  userdata,
uint  w,
uint  h,
int  pixelformat,
const Colour palette 
)
static

Generic .PNG file image writer.

Parameters
nameFilename, including extension.
callbCallback function for generating lines of pixels.
userdataUser data, passed on to callb.
wWidth of the image in pixels.
hHeight of the image in pixels.
pixelformatBits per pixel (bpp), either 8 or 32.
paletteColour palette (for 8bpp images).
Returns
File was written successfully.
See also
ScreenshotHandlerProc

Definition at line 256 of file screenshot.cpp.

References _grfconfig, Colour::b, BSWAP32(), Clamp(), FormatArrayAsHex(), BaseMedia< GraphicsSet >::GetUsedSet(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Iterate(), GRFConfig::next, and FileHandle::Open().

◆ MakeScreenshot()

bool MakeScreenshot ( ScreenshotType  t,
std::string  name,
uint32_t  width,
uint32_t  height 
)

Schedule making a screenshot.

Unconditionally take a screenshot of the requested type.

Parameters
tthe type of screenshot to make.
namethe name to give to the screenshot.
widththe width of the screenshot of, or 0 for current viewport width (only works for SC_ZOOMEDIN and SC_DEFAULTZOOM).
heightthe height of the screenshot of, or 0 for current viewport height (only works for SC_ZOOMEDIN and SC_DEFAULTZOOM).
Returns
true iff the screenshot was successfully made.
See also
MakeScreenshotWithConfirm

Definition at line 949 of file screenshot.cpp.

References VideoDriver::GetInstance(), lock, VideoDriver::QueueOnMainThread(), RealMakeScreenshot(), and SC_CRASHLOG.

Referenced by MakeScreenshotWithConfirm(), ScreenshotConfirmationCallback(), and CrashLog::WriteScreenshot().

◆ MakeScreenshotName()

static const char * MakeScreenshotName ( const char *  default_fn,
const char *  ext,
bool  crashlog = false 
)
static

Construct a pathname for a screenshot file.

Parameters
default_fnDefault filename.
extExtension to use.
crashlogCreate path for crash.png
Returns
Pathname for a screenshot file.

Definition at line 643 of file screenshot.cpp.

References _full_screenshot_path, _local_company, _personal_dir, _screenshot_name, COMPANY_SPECTATOR, FileExists(), FiosGetScreenshotDir(), and GenerateDefaultSaveName().

Referenced by MakeLargeWorldScreenshot(), MakeMinimapWorldScreenshot(), MakeSmallScreenshot(), and RealMakeScreenshot().

◆ MakeScreenshotWithConfirm()

void MakeScreenshotWithConfirm ( ScreenshotType  t)

Make a screenshot.

Ask for confirmation first if the screenshot will be huge.

Parameters
tScreenshot type: World, defaultzoom, heightmap or viewport screenshot
See also
MakeScreenshot

Definition at line 847 of file screenshot.cpp.

References _confirmed_screenshot_type, Viewport::height, MakeScreenshot(), SC_HEIGHTMAP, SC_MINIMAP, ScreenshotConfirmationCallback(), SetDParam(), SetupScreenshotViewport(), ShowQuery(), Map::SizeX(), Map::SizeY(), and Viewport::width.

Referenced by ScreenshotWindow::OnClick(), MainToolbarWindow::OnHotkey(), and ScenarioEditorToolbarWindow::OnHotkey().

◆ MakeSmallScreenshot()

static bool MakeSmallScreenshot ( bool  crashlog)
static

◆ MinimapScreenCallback()

static void MinimapScreenCallback ( void *  ,
void *  buf,
uint  y,
uint  pitch,
uint  n 
)
static

Definition at line 966 of file screenshot.cpp.

◆ png_my_error()

static void PNGAPI png_my_error ( png_structp  png_ptr,
png_const_charp  message 
)
static

Definition at line 233 of file screenshot.cpp.

◆ png_my_warning()

static void PNGAPI png_my_warning ( png_structp  png_ptr,
png_const_charp  message 
)
static

Definition at line 239 of file screenshot.cpp.

◆ RealMakeScreenshot()

static bool RealMakeScreenshot ( ScreenshotType  t,
std::string  name,
uint32_t  width,
uint32_t  height 
)
static

Make a screenshot.

Parameters
tthe type of screenshot to make.
namethe name to give to the screenshot.
widththe width of the screenshot of, or 0 for current viewport width (only works for SC_ZOOMEDIN and SC_DEFAULTZOOM).
heightthe height of the screenshot of, or 0 for current viewport height (only works for SC_ZOOMEDIN and SC_DEFAULTZOOM).
Returns
true iff the screenshot was made successfully

Definition at line 876 of file screenshot.cpp.

References _heightmap_highest_peak, _screenshot_name, DrawDirtyBlocks(), ScreenshotFormat::extension, HEIGHTMAP_NAME, INVALID_STRING_ID, MakeHeightmapScreenshot(), MakeLargeWorldScreenshot(), MakeMinimapWorldScreenshot(), MakeScreenshotName(), MakeSmallScreenshot(), SC_CRASHLOG, SC_DEFAULTZOOM, SC_HEIGHTMAP, SC_MINIMAP, SC_VIEWPORT, SC_WORLD, SC_ZOOMEDIN, SetDParam(), SetDParamStr(), SetScreenshotWindowVisibility(), ShowErrorMessage(), WL_ERROR, and WL_WARNING.

Referenced by MakeScreenshot().

◆ ScreenshotConfirmationCallback()

static void ScreenshotConfirmationCallback ( Window ,
bool  confirmed 
)
static

Callback on the confirmation window for huge screenshots.

Parameters
confirmedtrue on confirmation

Definition at line 836 of file screenshot.cpp.

References _confirmed_screenshot_type, and MakeScreenshot().

Referenced by MakeScreenshotWithConfirm().

◆ SetupScreenshotViewport()

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.

Parameters
tScreenshot type
widththe width of the screenshot, or 0 for current viewport width (needs to be 0 with SC_VIEWPORT, SC_CRASHLOG, and SC_WORLD).
heightthe height of the screenshot, or 0 for current viewport height (needs to be 0 with SC_VIEWPORT, SC_CRASHLOG, and SC_WORLD).
[out]vpResult viewport

Definition at line 694 of file screenshot.cpp.

References _settings_client, _settings_game, GameSettings::construction, ConstructionSettings::freeform_edges, GetMainWindow(), ClientSettings::gui, Viewport::height, Viewport::left, RemapCoords(), SC_CRASHLOG, SC_VIEWPORT, SC_WORLD, SC_ZOOMEDIN, Map::Size(), TILE_SIZE, TilePixelHeight(), TileX(), TileXY(), TileY(), Viewport::top, UnScaleByZoom(), Window::viewport, Viewport::virtual_height, Viewport::virtual_left, Viewport::virtual_top, Viewport::virtual_width, Viewport::width, Viewport::zoom, ZOOM_LVL_VIEWPORT, ZOOM_LVL_WORLD_SCREENSHOT, and GUISettings::zoom_min.

Referenced by MakeLargeWorldScreenshot(), and MakeScreenshotWithConfirm().

Variable Documentation

◆ _confirmed_screenshot_type

ScreenshotType _confirmed_screenshot_type
static

Screenshot type the current query is about to confirm.

Definition at line 830 of file screenshot.cpp.

Referenced by MakeScreenshotWithConfirm(), and ScreenshotConfirmationCallback().

◆ _cur_screenshot_format

const ScreenshotFormat* _cur_screenshot_format = std::begin(_screenshot_formats)
static

Definition at line 551 of file screenshot.cpp.

◆ _full_screenshot_path

std::string _full_screenshot_path

Pathname of the screenshot file.

Definition at line 42 of file screenshot.cpp.

Referenced by MakeScreenshotName(), and CrashLog::WriteScreenshot().

◆ _heightmap_highest_peak

uint _heightmap_highest_peak

When saving a heightmap, this contains the highest peak on the map.

Definition at line 43 of file screenshot.cpp.

Referenced by HeightmapCallback(), MakeHeightmapScreenshot(), and RealMakeScreenshot().

◆ _screenshot_format_name

std::string _screenshot_format_name

Extension of the current screenshot format (corresponds with _cur_screenshot_format).

Definition at line 40 of file screenshot.cpp.

Referenced by InitializeScreenshotFormats().

◆ _screenshot_formats

const ScreenshotFormat _screenshot_formats[]
static
Initial value:
= {
{"png", &MakePNGImage},
{"bmp", &MakeBMPImage},
{"pcx", &MakePCXImage},
}
static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .BMP writer.
static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .PNG file image writer.
static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
Generic .PCX file image writer.

Available screenshot formats.

Definition at line 542 of file screenshot.cpp.

Referenced by InitializeScreenshotFormats().

◆ _screenshot_name

std::string _screenshot_name
static

Filename of the screenshot file.

Definition at line 41 of file screenshot.cpp.

Referenced by MakeScreenshotName(), and RealMakeScreenshot().

◆ HEIGHTMAP_NAME

const char* const HEIGHTMAP_NAME = "heightmap"
static

Default filename of a saved heightmap.

Definition at line 38 of file screenshot.cpp.

Referenced by RealMakeScreenshot().

◆ SCREENSHOT_NAME

const char* const SCREENSHOT_NAME = "screenshot"
static

Default filename of a saved screenshot.

Definition at line 37 of file screenshot.cpp.

Referenced by MakeLargeWorldScreenshot(), MakeMinimapWorldScreenshot(), and MakeSmallScreenshot().