# viewport_func.h File Reference

Functions related to (drawing on) viewports. More...

`#include "gfx_type.h"`
`#include "viewport_type.h"`
`#include "vehicle_type.h"`
`#include "strings_type.h"`
`#include "window_type.h"`
`#include "tile_type.h"`

Go to the source code of this file.

## Functions
void | SetSelectionRed (bool)
void | DeleteWindowViewport (Window *w)
void | InitializeWindowViewport (Window *w, int x, int y, int width, int height, uint32 follow_flags, ZoomLevel zoom)
| Initialize viewport of the window for use.
ViewPort * | IsPtInWindowViewport (const Window *w, int x, int y)
| Is a xy position inside the viewport of the window?
Point | GetTileBelowCursor ()
void | UpdateViewportPosition (Window *w)
void | UpdateViewportSignPos (ViewportSign *sign, int left, int top, StringID str)
bool | DoZoomInOutWindow (int how, Window *w)
void | ZoomInOrOutToCursorWindow (bool in, Window *w)
Point | GetTileZoomCenterWindow (bool in, Window *w)
void | HandleZoomMessage (Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
| Update the status of the zoom-buttons according to the zoom-level of the viewport.
static void | MaxZoomInOut (int how, Window *w)
void | OffsetGroundSprite (int x, int y)
| Called when a foundation has been drawn for the current tile.
void | DrawGroundSprite (SpriteID image, SpriteID pal, const SubSprite *sub=NULL, int extra_offs_x=0, int extra_offs_y=0)
| Draws a ground sprite for the current tile.
void | DrawGroundSpriteAt (SpriteID image, SpriteID pal, int32 x, int32 y, byte z, const SubSprite *sub=NULL, int extra_offs_x=0, int extra_offs_y=0)
| Draws a ground sprite at a specific world-coordinate.
void | AddSortableSpriteToDraw (SpriteID image, SpriteID pal, int x, int y, int w, int h, int dz, int z, bool transparent=false, int bb_offset_x=0, int bb_offset_y=0, int bb_offset_z=0, const SubSprite *sub=NULL)
| Draw a (transparent) sprite at given coordinates with a given bounding box.
void | AddStringToDraw (int x, int y, StringID string, uint64 params_1, uint64 params_2, uint16 colour=0, uint16 width=0)
void | AddChildSpriteScreen (SpriteID image, SpriteID pal, int x, int y, bool transparent=false, const SubSprite *sub=NULL)
| Add a child sprite to a parent sprite.
void | StartSpriteCombine ()
void | EndSpriteCombine ()
bool | HandleViewportClicked (const ViewPort *vp, int x, int y)
void | PlaceObject ()
void | SetRedErrorSquare (TileIndex tile)
void | SetTileSelectSize (int w, int h)
void | SetTileSelectBigSize (int ox, int oy, int sx, int sy)
Vehicle * | CheckMouseOverVehicle ()
void | ViewportDoDraw (const ViewPort *vp, int left, int top, int right, int bottom)
bool | ScrollWindowTo (int x, int y, int z, Window *w, bool instant=false)
bool | ScrollMainWindowToTile (TileIndex tile, bool instant=false)
bool | ScrollMainWindowTo (int x, int y, int z=-1, bool instant=false)
| Scrolls the main window to given coordinates.
## Variables
Point | _tile_fract_coords

## Detailed Description

Functions related to (drawing on) viewports.

Definition in file viewport_func.h.

## Function Documentation

void AddChildSpriteScreen | ( | SpriteID | image,
|  | SpriteID | pal,
|  | int | x,
|  | int | y,
|  | bool | transparent,
|  | const SubSprite * | sub |
| ) |  |  |

Add a child sprite to a parent sprite.

Parameters:

  | image | the image to draw.
  | pal | the provided palette.
  | x | sprite x-offset (screen coordinates) relative to parent sprite.
  | y | sprite y-offset (screen coordinates) relative to parent sprite.
  | transparent | if true, switch the palette between the provided palette and the transparent palette,
  | sub | Only draw a part of the sprite.

Definition at line 706 of file viewport.cpp.

References SmallVector< T, S >::Append(), ViewportDrawer::child_screen_sprites_to_draw, ViewportDrawer::last_child, ViewportDrawer::last_foundation_child, SmallVector< T, S >::Length(), MAX_SPRITES, PALETTE_MODIFIER_TRANSPARENT, SetBit(), and SPRITE_MASK.

Referenced by AddChildSpriteToFoundation(), and AddCombinedSprite().

void AddSortableSpriteToDraw | ( | SpriteID | image,
|  | SpriteID | pal,
|  | int | x,
|  | int | y,
|  | int | w,
|  | int | h,
|  | int | dz,
|  | int | z,
|  | bool | transparent,
|  | int | bb_offset_x,
|  | int | bb_offset_y,
|  | int | bb_offset_z,
|  | const SubSprite * | sub |
| ) |  |  |

Draw a (transparent) sprite at given coordinates with a given bounding box.

The bounding box extends from (x + bb_offset_x, y + bb_offset_y, z + bb_offset_z) to (x + w - 1, y + h - 1, z + dz - 1), both corners included. Bounding boxes with bb_offset_x == w or bb_offset_y == h or bb_offset_z == dz are allowed and produce thin slices.

Note:
  Bounding boxes are normally specified with bb_offset_x = bb_offset_y = bb_offset_z = 0. The extent of the bounding box in negative direction is defined by the sprite offset in the grf file. However if modifying the sprite offsets is not suitable (e.g. when using existing graphics), the bounding box can be tuned by bb_offset.

Precondition:
  w >= bb_offset_x, h >= bb_offset_y, dz >= bb_offset_z. Else w, h or dz are ignored.

Parameters:

  | image | the image to combine and draw,
  | pal | the provided palette,
  | x | position X (world) of the sprite,
  | y | position Y (world) of the sprite,
  | w | bounding box extent towards positive X (world),
  | h | bounding box extent towards positive Y (world),
  | dz | bounding box extent towards positive Z (world),
  | z | position Z (world) of the sprite,
  | transparent | if true, switch the palette between the provided palette and the transparent palette,
  | bb_offset_x | bounding box extent towards negative X (world),
  | bb_offset_y | bounding box extent towards negative Y (world),
  | bb_offset_z | bounding box extent towards negative Z (world)
  | sub | Only draw a part of the sprite.

Definition at line 607 of file viewport.cpp.

References AddCombinedSprite(), SmallVector< T, S >::Append(), ViewportDrawer::combine_sprites, ParentSpriteToDraw::comparison_done, ViewportDrawer::dpi, ParentSpriteToDraw::first_child, ParentSpriteToDraw::image, ViewportDrawer::last_child, ParentSpriteToDraw::left, max(), MAX_SPRITES, min(), ParentSpriteToDraw::pal, PALETTE_MODIFIER_TRANSPARENT, ViewportDrawer::parent_sprites_to_draw, SetBit(), SPRITE_MASK, ST_NORMAL, ParentSpriteToDraw::sub, ParentSpriteToDraw::top, ParentSpriteToDraw::x, Point::x, ParentSpriteToDraw::xmax, ParentSpriteToDraw::xmin, ParentSpriteToDraw::y, Point::y, ParentSpriteToDraw::ymax, ParentSpriteToDraw::ymin, ParentSpriteToDraw::zmax, and ParentSpriteToDraw::zmin.

Referenced by DrawBridgePillars(), DrawBridgeTramBits(), DrawCatenary(), DrawCatenaryOnTunnel(), DrawCatenaryRailway(), DrawFoundation(), DrawRoadDetail(), DrawTile_Road(), DrawTile_Town(), DrawTile_TunnelBridge(), DrawTrackFence_NS_1(), DrawTrackFence_NS_2(), DrawTrackFence_WE_1(), DrawTrackFence_WE_2(), and DrawTramCatenary().

void DrawGroundSprite | ( | SpriteID | image,
|  | SpriteID | pal,
|  | const SubSprite * | sub,
|  | int | extra_offs_x,
|  | int | extra_offs_y |
| ) |  |  |

Draws a ground sprite for the current tile.

If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite.

Parameters:

  | image | the image to draw.
  | pal | the provided palette.
  | sub | Only draw a part of the sprite.
  | extra_offs_x | Pixel X offset for the sprite position.
  | extra_offs_y | Pixel Y offset for the sprite position.

Definition at line 516 of file viewport.cpp.

References AddChildSpriteToFoundation(), DrawGroundSpriteAt(), ViewportDrawer::foundation, ViewportDrawer::foundation_part, FOUNDATION_PART_NONE, FOUNDATION_PART_NORMAL, TileInfo::x, TileInfo::y, and TileInfo::z.

Referenced by DrawCanalWater(), DrawRoadBits(), DrawSeaWater(), DrawTile_Road(), DrawTile_Town(), DrawTile_TunnelBridge(), and DrawTrackBits().

void DrawGroundSpriteAt | ( | SpriteID | image,
|  | SpriteID | pal,
|  | int32 | x,
|  | int32 | y,
|  | byte | z,
|  | const SubSprite * | sub,
|  | int | extra_offs_x,
|  | int | extra_offs_y |
| ) |  |  |

Draws a ground sprite at a specific world-coordinate.

Parameters:

  | image | the image to draw.
  | pal | the provided palette.
  | x | position x (world coordinates) of the sprite.
  | y | position y (world coordinates) of the sprite.
  | z | position z (world coordinates) of the sprite.
  | sub | Only draw a part of the sprite.
  | extra_offs_x | Pixel X offset for the sprite position.
  | extra_offs_y | Pixel Y offset for the sprite position.

Definition at line 465 of file viewport.cpp.

References SmallVector< T, S >::Append(), MAX_SPRITES, SPRITE_MASK, ViewportDrawer::tile_sprites_to_draw, Point::x, and Point::y.

Referenced by DrawGroundSprite(), and DrawSelectionSprite().

void HandleZoomMessage | ( | Window * | w,
|  | const ViewPort * | vp,
|  | byte | widget_zoom_in,
|  | byte | widget_zoom_out |
| ) |  |  |

Update the status of the zoom-buttons according to the zoom-level of the viewport.

This will update their status and invalidate accordingly

Parameters:

  | w | Window pointer to the window that has the zoom buttons
  | vp | pointer to the viewport whose zoom-level the buttons represent
  | widget_zoom_in | widget index for window with zoom-in button
  | widget_zoom_out | widget index for window with zoom-out button

Definition at line 443 of file viewport.cpp.

References Window::InvalidateWidget(), Window::SetWidgetDisabledState(), and ViewPort::zoom.

void InitializeWindowViewport | ( | Window * | w,
|  | int | x,
|  | int | y,
|  | int | width,
|  | int | height,
|  | uint32 | follow_flags,
|  | ZoomLevel | zoom |
| ) |  |  |

Initialize viewport of the window for use.

Parameters:

  | w | Window to use/display the viewport in
  | x | Offset of left edge of viewport with respect to left edge window w
  | y | Offset of top edge of viewport with respect to top edge window w
  | width | Width of the viewport
  | height | Height of the viewport
  | follow_flags | Flags controlling the viewport. If bit 31 is set, the lower 16 bits are the vehicle that the viewport should follow. If bit 31 is clear, it is a tile position.
  | zoom | Zoomlevel to display

Definition at line 164 of file viewport.cpp.

References ViewportData::dest_scrollpos_x, ViewportData::dest_scrollpos_y, ViewportData::follow_vehicle, ViewPort::height, Window::left, ViewPort::left, ScaleByZoom(), ViewportData::scrollpos_x, ViewportData::scrollpos_y, TILE_SIZE, TileX(), TileY(), Window::top, ViewPort::top, Window::viewport, ViewPort::virtual_height, ViewPort::virtual_left, ViewPort::virtual_top, ViewPort::virtual_width, ViewPort::width, Point::x, Point::y, and ViewPort::zoom.

Referenced by ShowNewspaper().

ViewPort* IsPtInWindowViewport | ( | const Window * | w,
|  | int | x,
|  | int | y |
| ) |  |  |

Is a xy position inside the viewport of the window?

Parameters:

  | w | Window to examine its viewport
  | x | X coordinate of the xy position
  | y | Y coordinate of the xy position

Returns:
  Pointer to the viewport if the xy position is in the viewport of the window, otherwise `NULL` is returned.

Definition at line 335 of file viewport.cpp.

References ViewPort::height, IsInsideMM(), ViewPort::left, ViewPort::top, Window::viewport, and ViewPort::width.

Referenced by HandleAutoscroll().

void OffsetGroundSprite | ( | int | x,
|  | int | y |
| ) |  |  |

Called when a foundation has been drawn for the current tile.

Successive ground sprites for the current tile will be drawn as child sprites of the "foundation"-ParentSprite, not as TileSprites.

Parameters:

  | x | sprite x-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.
  | y | sprite y-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.

Definition at line 536 of file viewport.cpp.

References ViewportDrawer::foundation, ViewportDrawer::foundation_offset, ViewportDrawer::foundation_part, FOUNDATION_PART_HALFTILE, FOUNDATION_PART_NONE, FOUNDATION_PART_NORMAL, ViewportDrawer::last_child, ViewportDrawer::last_foundation_child, SmallVector< T, S >::Length(), ViewportDrawer::parent_sprites_to_draw, Point::x, and Point::y.

Referenced by DrawFoundation().

bool ScrollMainWindowTo | ( | int | x,
|  | int | y,
|  | int | z,
|  | bool | instant |
| ) |  |  |

Scrolls the main window to given coordinates.

Parameters:

  | x | x coordinate
  | y | y coordinate
  | z | z coordinate; -1 to scroll to terrain height
  | instant | scroll instantly (meaningful only when smooth_scrolling is active)

Returns:
  did the viewport position change?

Definition at line 1223 of file smallmap_gui.cpp.

References FindWindowById().