OpenTTD
Data Structures | Typedefs | Functions | Variables
screenshot.cpp File Reference

The creation of screenshots! More...

#include "stdafx.h"
#include "fileio_func.h"
#include "viewport_func.h"
#include "gfx_func.h"
#include "screenshot.h"
#include "blitter/factory.hpp"
#include "zoom_func.h"
#include "core/endian_func.hpp"
#include "saveload/saveload.h"
#include "company_func.h"
#include "strings_func.h"
#include "error.h"
#include "window_gui.h"
#include "window_func.h"
#include "tile_map.h"
#include "landscape.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  BitmapFileHeader
 BMP File Header (stored in little endian) 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...
 

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

Functions

 assert_compile (sizeof(BitmapFileHeader)==14)
 
 assert_compile (sizeof(BitmapInfoHeader)==40)
 
 assert_compile (sizeof(RgbQuad)==4)
 
static bool MakeBMPImage (const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
 Generic .BMP writer. More...
 
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. More...
 
 assert_compile (sizeof(PcxHeader)==128)
 
static bool MakePCXImage (const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
 Generic .PCX file image writer. More...
 
const char * GetCurrentScreenshotExtension ()
 Get filename extension of current screenshot file format. More...
 
void InitializeScreenshotFormats ()
 Initialize screenshot format information on startup, with _screenshot_format_name filled from the loadsave code. More...
 
static void CurrentScreenCallback (void *userdata, void *buf, uint y, uint pitch, uint n)
 Callback of the screenshot generator that dumps the current video buffer. More...
 
static void LargeWorldCallback (void *userdata, void *buf, uint y, uint pitch, uint n)
 generate a large piece of the world More...
 
static const char * MakeScreenshotName (const char *default_fn, const char *ext, bool crashlog=false)
 Construct a pathname for a screenshot file. More...
 
static bool MakeSmallScreenshot (bool crashlog)
 Make a screenshot of the current screen. More...
 
void SetupScreenshotViewport (ScreenshotType t, ViewPort *vp)
 Configure a ViewPort for rendering (a part of) the map into a screenshot. More...
 
static bool MakeLargeWorldScreenshot (ScreenshotType t)
 Make a screenshot of the map. More...
 
static void HeightmapCallback (void *userdata, void *buffer, uint y, uint pitch, uint n)
 Callback for generating a heightmap. More...
 
bool MakeHeightmapScreenshot (const char *filename)
 Make a heightmap of the current map. More...
 
bool MakeScreenshot (ScreenshotType t, const char *name)
 Make an actual screenshot. More...
 

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.
 
char _screenshot_format_name [8]
 Extension of the current screenshot format (corresponds with _cur_screenshot_format).
 
uint _num_screenshot_formats
 Number of available screenshot formats.
 
uint _cur_screenshot_format
 Index of the currently selected screenshot format in _screenshot_formats.
 
static char _screenshot_name [128]
 Filename of the screenshot file.
 
char _full_screenshot_name [MAX_PATH]
 Pathname of the screenshot file.
 
struct BitmapFileHeader GCC_PACK
 
static const ScreenshotFormat _screenshot_formats []
 Available screenshot formats. More...
 

Detailed Description

The creation of screenshots!

Definition in 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 50 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 63 of file screenshot.cpp.

Function Documentation

◆ CurrentScreenCallback()

static void CurrentScreenCallback ( void *  userdata,
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 604 of file screenshot.cpp.

Referenced by MakeSmallScreenshot().

◆ GetCurrentScreenshotExtension()

const char* GetCurrentScreenshotExtension ( )

Get filename extension of current screenshot file format.

Definition at line 581 of file screenshot.cpp.

References _cur_screenshot_format, and ScreenshotFormat::extension.

Referenced by FiosMakeHeightmapName().

◆ HeightmapCallback()

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

Callback for generating a heightmap.

Supports 8bpp grayscale only.

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.
See also
ScreenshotCallback

Definition at line 780 of file screenshot.cpp.

Referenced by MakeHeightmapScreenshot().

◆ InitializeScreenshotFormats()

void InitializeScreenshotFormats ( )

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

Definition at line 587 of file screenshot.cpp.

References _cur_screenshot_format, _num_screenshot_formats, _screenshot_format_name, ScreenshotFormat::extension, and lengthof.

◆ 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 619 of file screenshot.cpp.

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 118 of file screenshot.cpp.

◆ MakeHeightmapScreenshot()

bool MakeHeightmapScreenshot ( const char *  filename)

Make a heightmap of the current map.

Parameters
filenameFilename to use for saving.

Definition at line 800 of file screenshot.cpp.

References _cur_screenshot_format, Colour::a, HeightmapCallback(), lengthof, MapSizeX(), MapSizeY(), and ScreenshotFormat::proc.

◆ MakeLargeWorldScreenshot()

static bool MakeLargeWorldScreenshot ( ScreenshotType  t)
static

◆ 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 442 of file screenshot.cpp.

◆ 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 264 of file screenshot.cpp.

◆ MakeScreenshot()

bool MakeScreenshot ( ScreenshotType  t,
const char *  name 
)

Make an actual screenshot.

Parameters
tthe type of screenshot to make.
namethe name to give to the screenshot.
Returns
true iff the screenshot was made successfully

Definition at line 819 of file screenshot.cpp.

References SC_VIEWPORT.

Referenced by MenuClickLargeWorldScreenshot(), and ScreenshotConfirmCallback().

◆ 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 674 of file screenshot.cpp.

References _screenshot_name, and StrEmpty().

Referenced by MakeLargeWorldScreenshot(), and MakeSmallScreenshot().

◆ MakeSmallScreenshot()

static bool MakeSmallScreenshot ( bool  crashlog)
static

Make a screenshot of the current screen.

Definition at line 708 of file screenshot.cpp.

References _cur_screenshot_format, CurrentScreenCallback(), ScreenshotFormat::extension, MakeScreenshotName(), ScreenshotFormat::proc, and SCREENSHOT_NAME.

◆ SetupScreenshotViewport()

void SetupScreenshotViewport ( ScreenshotType  t,
ViewPort vp 
)

Variable Documentation

◆ _screenshot_formats

const ScreenshotFormat _screenshot_formats[]
static
Initial value:
= {
{"png", &MakePNGImage},
{"bmp", &MakeBMPImage},
{"pcx", &MakePCXImage},
}
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:442
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:264
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:118

Available screenshot formats.

Definition at line 572 of file screenshot.cpp.