# gfx_func.h File Reference

Functions related to the gfx engine. More...

`#include "gfx_type.h"`
`#include "strings_type.h"`

Go to the source code of this file.

## Defines
#define | ASCII_LETTERSTART 32
## Enumerations
enum | { DRAW_STRING_BUFFER = 2048 }
## Functions
void | GameLoop ()
void | CreateConsole ()
void | HandleKeypress (uint32 key)
| Handle keyboard input.
void | HandleCtrlChanged ()
| State of CONTROL key has changed.
void | HandleMouseEvents ()
| Handle a mouse event from the video driver.
void | CSleep (int milliseconds)
void | UpdateWindows ()
| Update the continuously changing contents of the windows, such as the viewports.
void | DrawMouseCursor ()
void | ScreenSizeChanged ()
void | GameSizeChanged ()
| Size of the application screen changed.
void | UndrawMouseCursor ()
void | RedrawScreenRect (int left, int top, int right, int bottom)
void | GfxScroll (int left, int top, int width, int height, int xo, int yo)
void | DrawSprite (SpriteID img, SpriteID pal, int x, int y, const SubSprite *sub=NULL)
| Draw a sprite.
int | DrawStringCentered (int x, int y, StringID str, TextColour colour)
| Draw string centered.
int | DrawStringCenteredTruncated (int xl, int xr, int y, StringID str, TextColour colour)
| Draw string centered, possibly truncated to fit in the assigned space.
int | DoDrawStringCentered (int x, int y, const char *str, TextColour colour)
| Draw string centered.
int | DrawString (int x, int y, StringID str, TextColour colour)
| Draw string starting at position (x,y).
int | DrawStringTruncated (int x, int y, StringID str, TextColour colour, uint maxw)
| Draw string, possibly truncated to make it fit in its allocated space.
int | DoDrawString (const char *string, int x, int y, TextColour colour, bool parse_string_also_when_clipped=false)
| Draw a string at the given coordinates with the given colour.
int | DoDrawStringTruncated (const char *str, int x, int y, TextColour colour, uint maxw)
| Draw the string of the character buffer, starting at position (x,y) with a given maximal width.
void | DrawStringCenterUnderline (int x, int y, StringID str, TextColour colour)
| Draw string centered, with additional line underneath it.
void | DrawStringCenterUnderlineTruncated (int xl, int xr, int y, StringID str, TextColour colour)
| Draw string centered possibly truncated, with additional line underneath it.
int | DrawStringRightAligned (int x, int y, StringID str, TextColour colour)
| Draw string right-aligned.
void | DrawStringRightAlignedTruncated (int x, int y, StringID str, TextColour colour, uint maxw)
| Draw string right-aligned, possibly truncated to make it fit in its allocated space.
void | DrawStringRightAlignedUnderline (int x, int y, StringID str, TextColour colour)
| Draw string right-aligned with a line underneath it.
void | DrawCharCentered (uint32 c, int x, int y, TextColour colour)
void | GfxFillRect (int left, int top, int right, int bottom, int colour, FillRectMode mode=FILLRECT_OPAQUE)
| Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
void | GfxDrawLine (int left, int top, int right, int bottom, int colour)
void | DrawBox (int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
| Draws the projection of a parallelepiped.
Dimension | GetStringBoundingBox (const char *str)
| Return the string dimension in pixels.
uint32 | FormatStringLinebreaks (char *str, int maxw)
| 'Correct' a string to a maximum length.
int | GetStringHeight (StringID str, int maxw)
| Calculates height of string (in pixels).
void | LoadStringWidthTable ()
| Initialize _stringwidth_table cache.
void | DrawStringMultiCenter (int x, int y, StringID str, int maxw)
| Draw a given string with the centre around the given (x,y) coordinates.
uint | DrawStringMultiLine (int x, int y, StringID str, int maxw, int maxh=-1)
void | DrawDirtyBlocks ()
| Let the dirty blocks repainting by the video driver.
void | SetDirtyBlocks (int left, int top, int right, int bottom)
| Set a new dirty block.
void | MarkWholeScreenDirty ()
| Marks the whole screen as dirty.
void | GfxInitPalettes ()
bool | FillDrawPixelInfo (DrawPixelInfo *n, int left, int top, int width, int height)
| Set up a clipping area for only drawing into a certain area.
void | DrawOverlappedWindowForAll (int left, int top, int right, int bottom)
| From a rectangle that needs redrawing, find the windows that intersect with the rectangle.
void | SetMouseCursor (SpriteID sprite, SpriteID pal)
void | SetAnimatedMouseCursor (const AnimCursor *table)
void | CursorTick ()
bool | ChangeResInGame (int w, int h)
void | SortResolutions (int count)
bool | ToggleFullScreen (bool fs)
byte | GetCharacterWidth (FontSize size, uint32 key)
static byte | GetCharacterHeight (FontSize size)
| Get height of a character for a given font size.
## Variables
byte | _dirkeys
| 1 = left, 2 = up, 4 = right, 8 = down
bool | _fullscreen
CursorVars | _cursor
bool | _ctrl_pressed
| Is Ctrl pressed?
bool | _shift_pressed
| Is Shift pressed?
byte | _fast_forward
bool | _left_button_down
| Is left mouse button pressed?
bool | _left_button_clicked
| Is left mouse button clicked?
bool | _right_button_down
| Is right mouse button pressed?
bool | _right_button_clicked
| Is right mouse button clicked?
DrawPixelInfo | _screen
bool | _screen_disable_anim
| Disable palette animation (important for 32bpp-anim blitter during giant screenshot).
int | _pal_first_dirty
int | _pal_count_dirty
int | _num_resolutions
Dimension | _resolutions [32]
Dimension | _cur_resolution
Colour | _cur_palette [256]
| Current palette. Entry 0 has to be always fully transparent!
FontSize | _cur_fontsize
DrawPixelInfo * | _cur_dpi
byte | _colour_gradient [COLOUR_END][8]
| All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7).
PaletteType | _use_palette
| The currently used palette.
bool | _palette_remap_grf []
| Whether the given NewGRFs must get a palette remap or not.
const byte * | _palette_remap
| Palette map to go from the !_use_palette to the _use_palette.
const byte * | _palette_reverse_remap
| Palette map to go from the _use_palette to the !_use_palette.

## Detailed Description

Functions related to the gfx engine.

Definition in file gfx_func.h.

## Function Documentation

int DoDrawString | ( | const char * | string,
|  | int | x,
|  | int | y,
|  | TextColour | colour,
|  | bool | parse_string_also_when_clipped |
| ) |  |  |

Draw a string at the given coordinates with the given colour.

While drawing the string, parse it in case some formatting is specified, like new colour, new size or even positionning.

Parameters:

  | string | The string to draw. This is not yet bidi reordered.
  | x | Offset from left side of the screen
  | y | Offset from top side of the screen
  | colour | Colour of the string, see _string_colourmap in table/palettes.h or docs/ottd-colourtext-palette.png or the enum TextColour in gfx_type.h
  | parse_string_also_when_clipped | By default, always test the available space where to draw the string. When in multipline drawing, it would already be done, so no need to re-perform the same kind (more or less) of verifications. It's not only an optimisation, it's also a way to ensures the string will be parsed (as there are certain side effects on global variables, which are important for the next line)

Returns:
  the x-coordinates where the drawing has finished. If nothing is drawn, the originally passed x-coordinate is returned

Definition at line 879 of file gfx.cpp.

References lastof, ReallyDoDrawString(), and strecpy().

Referenced by DoDrawStringTruncated(), Window::DrawSortButtonState(), Window::DrawWidgets(), and StationViewWindow::OnPaint().

int DoDrawStringCentered | ( | int | x,
|  | int | y,
|  | const char * | str,
|  | TextColour | colour |
| ) |  |  |

Draw string centered.

Parameters:

  | x | X position of center of the string
  | y | Y position of center of the string
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details

Returns:
  Width of the drawn string in pixels

Definition at line 542 of file gfx.cpp.

References GetStringBoundingBox(), lastof, ReallyDoDrawString(), strecpy(), and Dimension::width.

int DoDrawStringTruncated | ( | const char * | str,
|  | int | x,
|  | int | y,
|  | TextColour | colour,
|  | uint | maxw |
| ) |  |  |

Draw the string of the character buffer, starting at position (x,y) with a given maximal width.

String is truncated if it is too long.

Parameters:

  | str | Character buffer containing the string
  | x | Left-most x coordinate to start drawing
  | y | Y coordinate to draw the string
  | colour | Colour to use, see DoDrawString() for details.
  | maxw | Maximal width in pixels that may be used for drawing

Returns:
  Right-most x position after drawing the (possibly truncated) string

Definition at line 985 of file gfx.cpp.

References DoDrawString(), lastof, strecpy(), and TruncateString().

Referenced by DrawNewsString(), NewGRFWindow::OnPaint(), NewGRFAddWindow::OnPaint(), AISettingsWindow::OnPaint(), and AIListWindow::OnPaint().

void DrawBox | ( | int | x,
|  | int | y,
|  | int | dx1,
|  | int | dy1,
|  | int | dx2,
|  | int | dy2,
|  | int | dx3,
|  | int | dy3 |
| ) |  |  |

Draws the projection of a parallelepiped.

This can be used to draw boxes in world coordinates.

Parameters:

  | x | Screen X-coordinate of top front corner.
  | y | Screen Y-coordinate of top front corner.
  | dx1 | Screen X-length of first edge.
  | dy1 | Screen Y-length of first edge.
  | dx2 | Screen X-length of second edge.
  | dy2 | Screen Y-length of second edge.
  | dx3 | Screen X-length of third edge.
  | dy3 | Screen Y-length of third edge.

Definition at line 207 of file gfx.cpp.

Referenced by ViewportDrawBoundingBoxes().

void DrawOverlappedWindowForAll | ( | int | left,
|  | int | top,
|  | int | right,
|  | int | bottom |
| ) |  |  |

From a rectangle that needs redrawing, find the windows that intersect with the rectangle.

These windows should be re-painted.

Parameters:

  | left | Left edge of the rectangle that should be repainted
  | top | Top edge of the rectangle that should be repainted
  | right | Right edge of the rectangle that should be repainted
  | bottom | Bottom edge of the rectangle that should be repainted

Definition at line 474 of file window.cpp.

References DrawOverlappedWindow(), Window::height, Window::left, Window::top, and Window::width.

void DrawSprite | ( | SpriteID | img,
|  | SpriteID | pal,
|  | int | x,
|  | int | y,
|  | const SubSprite * | sub |
| ) |  |  |

Draw a sprite.

Parameters:

  | img | Image number to draw
  | pal | Palette to use.
  | x | Left coordinate of image
  | y | Top coordinate of image
  | sub | If available, draw only specified part of the sprite

Definition at line 1001 of file gfx.cpp.

References GB(), HasBit(), PALETTE_MODIFIER_TRANSPARENT, PALETTE_WIDTH, SPRITE_WIDTH, ST_NORMAL, and ST_RECOLOUR.

Referenced by SettingEntry::Draw(), DrawCargoIcons(), DrawCompanyManagerFace(), DrawStationTile(), DrawVehicleProfitButton(), Window::DrawWidgets(), NewGRFWindow::OnPaint(), and CompanyWindow::OnPaint().

int DrawString | ( | int | x,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour |
| ) |  |  |

Draw string starting at position (x,y).

Parameters:

  | x | X position to start drawing
  | y | Y position to start drawing
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details

Returns:
  Horizontal coordinate after drawing the string

Definition at line 403 of file gfx.cpp.

References lastof, and ReallyDoDrawString().

Referenced by DrawAircraftDetails(), DrawCompanyOwnerText(), DrawCompanyVehiclesAmount(), DrawShipDetails(), DrawVehiclePurchaseInfo(), DrawVehicleRefitWindow(), Window::DrawWidgets(), VehicleListWindow::OnPaint(), StationViewWindow::OnPaint(), CompanyStationsWindow::OnPaint(), CompanyWindow::OnPaint(), AIConfigWindow::OnPaint(), AIListWindow::OnPaint(), and StationsWndShowStationRating().

int DrawStringCentered | ( | int | x,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour |
| ) |  |  |

Draw string centered.

Parameters:

  | x | X position of center of the string
  | y | Y position of center of the string
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details

Returns:
  Width of the drawn string in pixels

Definition at line 497 of file gfx.cpp.

References GetStringBoundingBox(), lastof, ReallyDoDrawString(), and Dimension::width.

Referenced by DrawArrowButtons(), DrawStringCenterUnderline(), Window::DrawWidgets(), and CompanyStationsWindow::OnPaint().

int DrawStringCenteredTruncated | ( | int | xl,
|  | int | xr,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour |
| ) |  |  |

Draw string centered, possibly truncated to fit in the assigned space.

Parameters:

  | xl | Left-most x position
  | xr | Right-most x position
  | y | Y position of the string
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details

Returns:
  Right-most coordinate of the (possibly truncated) drawn string

Definition at line 522 of file gfx.cpp.

References GetStringBoundingBox(), lastof, ReallyDoDrawString(), TruncateStringID(), and Dimension::width.

Referenced by DrawStringCenterUnderlineTruncated(), and Window::DrawWidgets().

void DrawStringCenterUnderline | ( | int | x,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour |
| ) |  |  |

Draw string centered, with additional line underneath it.

Parameters:

  | x | X position of center of the string
  | y | Y position of center of the string
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details

Definition at line 561 of file gfx.cpp.

References DrawStringCentered(), and GfxFillRect().

void DrawStringCenterUnderlineTruncated | ( | int | xl,
|  | int | xr,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour |
| ) |  |  |

Draw string centered possibly truncated, with additional line underneath it.

Parameters:

  | xl | Left x position of the string
  | xr | Right x position of the string
  | y | Y position of center of the string
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details

Definition at line 576 of file gfx.cpp.

References DrawStringCenteredTruncated(), and GfxFillRect().

void DrawStringMultiCenter | ( | int | x,
|  | int | y,
|  | StringID | str,
|  | int | maxw |
| ) |  |  |

Draw a given string with the centre around the given (x,y) coordinates.

Parameters:

  | x | Centre the string around this pixel width
  | y | Centre the string around this pixel height
  | str | String to draw
  | maxw | Maximum width the string can have before it is wrapped

Definition at line 708 of file gfx.cpp.

References FormatStringLinebreaks(), GB(), GetCharacterHeight(), GetStringBoundingBox(), lastof, ReallyDoDrawString(), strecpy(), and Dimension::width.

Referenced by QueryWindow::OnPaint(), EndGameWindow::OnPaint(), and CompanyWindow::OnPaint().

int DrawStringRightAligned | ( | int | x,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour |
| ) |  |  |

Draw string right-aligned.

Parameters:

  | x | Right-most x position of the string
  | y | Y position of the string
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details

Returns:
  Width of drawn string in pixels

Definition at line 441 of file gfx.cpp.

References GetStringBoundingBox(), lastof, ReallyDoDrawString(), and Dimension::width.

Referenced by DrawEngineList(), DrawStringRightAlignedUnderline(), and StationViewWindow::OnPaint().

void DrawStringRightAlignedTruncated | ( | int | x,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour,
|  | uint | maxw |
| ) |  |  |

Draw string right-aligned, possibly truncated to make it fit in its allocated space.

Parameters:

  | x | Right-most x position to start drawing
  | y | Y position to start drawing
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details
  | maxw | Maximal width of the string

Definition at line 464 of file gfx.cpp.

References GetStringBoundingBox(), lastof, ReallyDoDrawString(), and TruncateStringID().

Referenced by StationViewWindow::OnPaint().

void DrawStringRightAlignedUnderline | ( | int | x,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour |
| ) |  |  |

Draw string right-aligned with a line underneath it.

Parameters:

  | x | Right-most x position of the string
  | y | Y position of the string
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details

Definition at line 481 of file gfx.cpp.

References DrawStringRightAligned(), and GfxFillRect().

int DrawStringTruncated | ( | int | x,
|  | int | y,
|  | StringID | str,
|  | TextColour | colour,
|  | uint | maxw |
| ) |  |  |

Draw string, possibly truncated to make it fit in its allocated space.

Parameters:

  | x | X position to start drawing
  | y | Y position to start drawing
  | str | String to draw
  | colour | Colour used for drawing the string, see DoDrawString() for details
  | maxw | Maximal width of the string

Returns:
  Horizontal coordinate after drawing the (possibly truncated) string

Definition at line 423 of file gfx.cpp.

References lastof, ReallyDoDrawString(), and TruncateStringID().

Referenced by SettingEntry::Draw(), DrawEngineList(), SettingEntry::DrawSetting(), Window::DrawWidgets(), IndustryDirectoryWindow::OnPaint(), ReplaceVehicleWindow::OnPaint(), AIConfigWindow::OnPaint(), AISettingsWindow::OnPaint(), and AIListWindow::OnPaint().

bool FillDrawPixelInfo | ( | DrawPixelInfo * | n,
|  | int | left,
|  | int | top,
|  | int | width,
|  | int | height |
| ) |  |  |

Set up a clipping area for only drawing into a certain area.

To do this, Fill a DrawPixelInfo object with the supplied relative rectangle, backup the original (calling) _cur_dpi and assign the just returned DrawPixelInfo _cur_dpi. When you are done, give restore _cur_dpi's original value

Parameters:

  | *n | the DrawPixelInfo that will be the clipping rectangle box allowed for drawing
  | left,top,width,height | the relative coordinates of the clipping rectangle relative to the current _cur_dpi. This will most likely be the offset from the calling window coordinates

Returns:
  return false if the requested rectangle is not possible with the current dpi pointer. Only continue of the return value is true, or you'll get some nasty results

Definition at line 1539 of file gfx.cpp.

References BlitterFactoryBase::GetCurrentBlitter(), and Blitter::MoveTo().

uint32 FormatStringLinebreaks | ( | char * | str,
|  | int | maxw |
| ) |  |  |

'Correct' a string to a maximum length.

Longer strings will be cut into additional lines at whitespace characters if possible. The string parameter is modified with terminating characters mid-string which are the placeholders for the newlines. The string WILL be truncated if there was no whitespace for the current line's maximum width.

Note:
  To know if the terminating '' is the string end or just a newline, the returned 'num' value should be consulted. The num'th '', starting with index 0 is the real string end.

Parameters:

  | str | string to check and correct for length restrictions
  | maxw | the maximum width the string can have on one line

Returns:
  return a 32bit wide number consisting of 2 packed values: 0 - 15 the number of lines ADDED to the string 16 - 31 the fontsize in which the length calculation was done at

Definition at line 600 of file gfx.cpp.

References GetCharacterWidth(), IsWhitespace(), SCC_BIGFONT, SCC_TINYFONT, and Utf8PrevChar().

Referenced by DrawStringMultiCenter(), and GetStringHeight().

void GameSizeChanged | ( |  | ) |

Size of the application screen changed.

Adapt the game screen-size, re-allocate the open windows, and repaint everything

Definition at line 408 of file main_gui.cpp.

References Dimension::height, MarkWholeScreenDirty(), RelocateAllWindows(), and Dimension::width.

static byte GetCharacterHeight | ( | FontSize | size | ) | `[inline, static]`

Get height of a character for a given font size.

Parameters:

  | size | Font size to get height of

Returns:
  Height of characters in the given font (pixels)

Definition at line 164 of file gfx_func.h.

Referenced by DrawCompanyOwnerText(), DrawStringMultiCenter(), GetMultilineStringHeight(), GetStringBoundingBox(), and ReallyDoDrawString().

Dimension GetStringBoundingBox | ( | const char * | str | ) |

Return the string dimension in pixels.

The height and width are returned in a single Dimension value. TINYFONT, BIGFONT modifiers are only supported as the first character of the string. The returned dimensions are therefore a rough estimation correct for all the current strings but not every possible combination

Parameters:

  | str | string to calculate pixel-width

Returns:
  string width and height in pixels

Definition at line 813 of file gfx.cpp.

References GetCharacterHeight(), GetCharacterWidth(), Dimension::height, max(), SCC_BIGFONT, SCC_TINYFONT, and Dimension::width.

Referenced by DoDrawStringCentered(), DrawStringCentered(), DrawStringCenteredTruncated(), DrawStringMultiCenter(), DrawStringRightAligned(), DrawStringRightAlignedTruncated(), and GuiShowTooltips().

int GetStringHeight | ( | StringID | str,
|  | int | maxw |
| ) |  |  |

Calculates height of string (in pixels).

The string is changed to a multiline string if needed.

Parameters:

  | str | string to check
  | maxw | maximum string width

Returns:
  height of pixels of string when it is drawn

Definition at line 691 of file gfx.cpp.

References FormatStringLinebreaks(), GB(), GetMultilineStringHeight(), and lastof.

void GfxFillRect | ( | int | left,
|  | int | top,
|  | int | right,
|  | int | bottom,
|  | int | colour,
|  | FillRectMode | mode |
| ) |  |  |

Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.

Precondition:
  dpi->zoom == ZOOM_LVL_NORMAL, right >= left, bottom >= top

Parameters:

  | left | Minimum X (inclusive)
  | top | Minimum Y (inclusive)
  | right | Maximum X (inclusive)
  | bottom | Maximum Y (inclusive)
  | colour | A 8 bit palette index (FILLRECT_OPAQUE and FILLRECT_CHECKER) or a recolour spritenumber (FILLRECT_RECOLOUR)
  | mode | FILLRECT_OPAQUE: Fill the rectangle with the specified colour FILLRECT_CHECKER: Like FILLRECT_OPAQUE, but only draw every second pixel (used to grey out things) FILLRECT_RECOLOUR: Apply a recolour sprite to every pixel in the rectangle currently on screen

Definition at line 107 of file gfx.cpp.

References Blitter::DrawColourMappingRect(), Blitter::DrawRect(), FILLRECT_CHECKER, FILLRECT_RECOLOUR, GB(), BlitterFactoryBase::GetCurrentBlitter(), Blitter::MoveTo(), PALETTE_WIDTH, and Blitter::SetPixel().

Referenced by DrawArrowButtons(), DrawFrameRect(), DrawStringCenterUnderline(), DrawStringCenterUnderlineTruncated(), DrawStringRightAlignedUnderline(), Window::DrawWidgets(), CompanyStationsWindow::OnPaint(), NewGRFAddWindow::OnPaint(), and StationsWndShowStationRating().

void HandleKeypress | ( | uint32 | raw_key | ) |

Handle keyboard input.

Parameters:

  | raw_key | Lower 8 bits contain the ASCII character, the higher 16 bits the keycode

Definition at line 1736 of file window.cpp.

References EditBoxInGlobalFocus(), Window::ES_HANDLED, FindWindowById(), GB(), IsGeneratingWorld(), and Window::OnKeyPress().

## Variable Documentation

bool _palette_remap_grf[]

Whether the given NewGRFs must get a palette remap or not.

Definition at line 24 of file gfxinit.cpp.