OpenTTD
Functions | Variables
viewport_func.h File Reference

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

#include "gfx_type.h"
#include "viewport_type.h"
#include "window_type.h"
#include "tile_type.h"
#include "station_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. More...
 
ViewPortIsPtInWindowViewport (const Window *w, int x, int y)
 Is a xy position inside the viewport of the window? More...
 
Point TranslateXYToTileCoord (const ViewPort *vp, int x, int y, bool clamp_to_map=true)
 Translate screen coordinate in a viewport to a tile coordinate. More...
 
Point GetTileBelowCursor ()
 
void UpdateViewportPosition (Window *w)
 Update the viewport position being displayed. More...
 
void MarkAllViewportsDirty (int left, int top, int right, int bottom)
 Mark all viewports that display an area as dirty (in need of repaint). More...
 
bool DoZoomInOutWindow (ZoomStateChange how, Window *w)
 Zooms a viewport in a window in or out. More...
 
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. More...
 
static void MaxZoomInOut (ZoomStateChange how, Window *w)
 Zoom a viewport as far as possible in the given direction. More...
 
void OffsetGroundSprite (int x, int y)
 Called when a foundation has been drawn for the current tile. More...
 
void DrawGroundSprite (SpriteID image, PaletteID pal, const SubSprite *sub=NULL, int extra_offs_x=0, int extra_offs_y=0)
 Draws a ground sprite for the current tile. More...
 
void DrawGroundSpriteAt (SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub=NULL, int extra_offs_x=0, int extra_offs_y=0)
 Draws a ground sprite at a specific world-coordinate relative to the current tile. More...
 
void AddSortableSpriteToDraw (SpriteID image, PaletteID 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. More...
 
void AddChildSpriteScreen (SpriteID image, PaletteID pal, int x, int y, bool transparent=false, const SubSprite *sub=NULL, bool scale=true)
 Add a child sprite to a parent sprite. More...
 
void ViewportAddString (const DrawPixelInfo *dpi, ZoomLevel small_from, const ViewportSign *sign, StringID string_normal, StringID string_small, StringID string_small_shadow, uint64 params_1, uint64 params_2=0, Colours colour=INVALID_COLOUR)
 Add a string to draw in the viewport. More...
 
void StartSpriteCombine ()
 Starts a block of sprites, which are "combined" into a single bounding box. More...
 
void EndSpriteCombine ()
 Terminates a block of sprites started by StartSpriteCombine. More...
 
bool HandleViewportClicked (const ViewPort *vp, int x, int y)
 
void SetRedErrorSquare (TileIndex tile)
 Set a tile to display a red error square. More...
 
void SetTileSelectSize (int w, int h)
 Highlight w by h tiles at the cursor. More...
 
void SetTileSelectBigSize (int ox, int oy, int sx, int sy)
 
void ViewportDoDraw (const ViewPort *vp, int left, int top, int right, int bottom)
 
bool ScrollWindowToTile (TileIndex tile, Window *w, bool instant=false)
 Scrolls the viewport in a window to a given location. More...
 
bool ScrollWindowTo (int x, int y, int z, Window *w, bool instant=false)
 Scrolls the viewport in a window to a given location. More...
 
void RebuildViewportOverlay (Window *w)
 
bool ScrollMainWindowToTile (TileIndex tile, bool instant=false)
 Scrolls the viewport of the main window to a given location. More...
 
bool ScrollMainWindowTo (int x, int y, int z=-1, bool instant=false)
 Scrolls the main window to given coordinates. More...
 
void UpdateAllVirtCoords ()
 Update the viewport coordinates of all signs.
 
void MarkTileDirtyByTile (TileIndex tile, int bridge_level_offset=0)
 Mark a tile given by its index dirty for repaint. More...
 
void MarkTileDirtyByTileOutsideMap (int x, int y)
 Mark a (virtual) tile outside the map dirty for repaint. More...
 
Point GetViewportStationMiddle (const ViewPort *vp, const Station *st)
 

Variables

static const int TILE_HEIGHT_STEP = 50
 One Z unit tile height difference is displayed as 50m.
 
Point _tile_fract_coords
 

Detailed Description

Functions related to (drawing on) viewports.

Definition in file viewport_func.h.

Function Documentation

◆ AddChildSpriteScreen()

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

Add a child sprite to a parent sprite.

Parameters
imagethe image to draw.
palthe provided palette.
xsprite x-offset (screen coordinates) relative to parent sprite.
ysprite y-offset (screen coordinates) relative to parent sprite.
transparentif true, switch the palette between the provided palette and the transparent palette,
subOnly draw a part of the sprite.

Definition at line 843 of file viewport.cpp.

◆ AddSortableSpriteToDraw()

void AddSortableSpriteToDraw ( SpriteID  image,
PaletteID  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
imagethe image to combine and draw,
palthe provided palette,
xposition X (world) of the sprite,
yposition Y (world) of the sprite,
wbounding box extent towards positive X (world),
hbounding box extent towards positive Y (world),
dzbounding box extent towards positive Z (world),
zposition Z (world) of the sprite,
transparentif true, switch the palette between the provided palette and the transparent palette,
bb_offset_xbounding box extent towards negative X (world),
bb_offset_ybounding box extent towards negative Y (world),
bb_offset_zbounding box extent towards negative Z (world)
subOnly draw a part of the sprite.

Definition at line 688 of file viewport.cpp.

Referenced by DrawCommonTileSeq().

◆ DoZoomInOutWindow()

bool DoZoomInOutWindow ( ZoomStateChange  how,
Window w 
)

Zooms a viewport in a window in or out.

Parameters
howZooming direction.
wWindow owning the viewport.
Returns
Returns true if zooming step could be done, false if further zooming is not possible.
Note
No button handling or what so ever is done.

Definition at line 143 of file main_gui.cpp.

Referenced by MaxZoomInOut(), and ExtraViewportWindow::OnClick().

◆ DrawGroundSprite()

void DrawGroundSprite ( SpriteID  image,
PaletteID  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
imagethe image to draw.
palthe provided palette.
subOnly draw a part of the sprite.
extra_offs_xPixel X offset for the sprite position.
extra_offs_yPixel Y offset for the sprite position.

Definition at line 604 of file viewport.cpp.

References DrawGroundSpriteAt().

Referenced by DrawSeaWater().

◆ DrawGroundSpriteAt()

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

Draws a ground sprite at a specific world-coordinate relative to 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
imagethe image to draw.
palthe provided palette.
xposition x (world coordinates) of the sprite relative to current tile.
yposition y (world coordinates) of the sprite relative to current tile.
zposition z (world coordinates) of the sprite relative to current tile.
subOnly draw a part of the sprite.
extra_offs_xPixel X offset for the sprite position.
extra_offs_yPixel Y offset for the sprite position.

Definition at line 581 of file viewport.cpp.

References AddChildSpriteToFoundation(), ViewportDrawer::foundation, ViewportDrawer::foundation_part, FOUNDATION_PART_NONE, FOUNDATION_PART_NORMAL, and RemapCoords().

Referenced by DrawGroundSprite().

◆ EndSpriteCombine()

void EndSpriteCombine ( )

Terminates a block of sprites started by StartSpriteCombine.

Take a look there for details.

Definition at line 795 of file viewport.cpp.

◆ HandleZoomMessage()

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
wWindow pointer to the window that has the zoom buttons
vppointer to the viewport whose zoom-level the buttons represent
widget_zoom_inwidget index for window with zoom-in button
widget_zoom_outwidget index for window with zoom-out button

Definition at line 506 of file viewport.cpp.

References _settings_client, ClientSettings::gui, Window::SetWidgetDirty(), Window::SetWidgetDisabledState(), ViewPort::zoom, GUISettings::zoom_max, and GUISettings::zoom_min.

Referenced by ExtraViewportWindow::OnInvalidateData(), MainToolbarWindow::OnInvalidateData(), and ScenarioEditorToolbarWindow::OnInvalidateData().

◆ InitializeWindowViewport()

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
wWindow to use/display the viewport in
xOffset of left edge of viewport with respect to left edge window w
yOffset of top edge of viewport with respect to top edge window w
widthWidth of the viewport
heightHeight of the viewport
follow_flagsFlags controlling the viewport.
  • If bit 31 is set, the lower 20 bits are the vehicle that the viewport should follow.
  • If bit 31 is clear, it is a TileIndex.
zoomZoomlevel to display

Definition at line 212 of file viewport.cpp.

Referenced by NWidgetViewport::InitializeViewport().

◆ IsPtInWindowViewport()

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

Is a xy position inside the viewport of the window?

Parameters
wWindow to examine its viewport
xX coordinate of the xy position
yY 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 385 of file viewport.cpp.

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

◆ MaxZoomInOut()

static void MaxZoomInOut ( ZoomStateChange  how,
Window w 
)
inlinestatic

Zoom a viewport as far as possible in the given direction.

Parameters
howZooming direction.
wWindow owning the viewport.
Precondition
how should not be ZOOM_NONE.

Definition at line 45 of file viewport_func.h.

References DoZoomInOutWindow(), and OffsetGroundSprite().

◆ OffsetGroundSprite()

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
xsprite x-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.
ysprite y-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.

Definition at line 616 of file viewport.cpp.

References ViewportDrawer::foundation_part, FOUNDATION_PART_HALFTILE, FOUNDATION_PART_NONE, and FOUNDATION_PART_NORMAL.

Referenced by MaxZoomInOut().

◆ ScrollMainWindowTo()

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

Scrolls the main window to given coordinates.

Parameters
xx coordinate
yy coordinate
zz coordinate; -1 to scroll to terrain height
instantscroll instantly (meaningful only when smooth_scrolling is active)
Returns
did the viewport position change?

Definition at line 1861 of file smallmap_gui.cpp.

Referenced by NewsWindow::OnClick(), VehicleViewWindow::OnClick(), and ScrollMainWindowToTile().

◆ ScrollMainWindowToTile()

bool ScrollMainWindowToTile ( TileIndex  tile,
bool  instant 
)

Scrolls the viewport of the main window to a given location.

Parameters
tileDesired tile to center on.
instantJump to the location instead of slowly moving to it.
Returns
Destination of the viewport was changed (to activate other actions when the viewport is already at the desired position).

Definition at line 2268 of file viewport.cpp.

References ScrollMainWindowTo(), TILE_SIZE, TileX(), and TileY().

Referenced by DEF_CONSOLE_CMD(), WaypointWindow::OnClick(), SignListWindow::OnClick(), TownViewWindow::OnClick(), NewsWindow::OnClick(), SignWindow::OnClick(), IndustryDirectoryWindow::OnClick(), StationViewWindow::OnClick(), CompanyWindow::OnClick(), VehicleViewWindow::OnClick(), and StoryBookWindow::OnPageElementClick().

◆ ScrollWindowTo()

bool ScrollWindowTo ( int  x,
int  y,
int  z,
Window w,
bool  instant 
)

Scrolls the viewport in a window to a given location.

Parameters
xDesired x location of the map to scroll to (world coordinate).
yDesired y location of the map to scroll to (world coordinate).
zDesired z location of the map to scroll to (world coordinate). Use -1 to scroll to the height of the map at the x, y location.
wWindow containing the viewport.
instantJump to the location instead of slowly moving to it.
Returns
Destination of the viewport was changed (to activate other actions when the viewport is already at the desired position).

Definition at line 2222 of file viewport.cpp.

References MapSizeX(), MapSizeY(), and TILE_SIZE.

Referenced by SmallMapWindow::OnClick(), and ScrollWindowToTile().

◆ ScrollWindowToTile()

bool ScrollWindowToTile ( TileIndex  tile,
Window w,
bool  instant 
)

Scrolls the viewport in a window to a given location.

Parameters
tileDesired tile to center on.
wWindow containing the viewport.
instantJump to the location instead of slowly moving to it.
Returns
Destination of the viewport was changed (to activate other actions when the viewport is already at the desired position).

Definition at line 2257 of file viewport.cpp.

References ScrollWindowTo(), TILE_SIZE, TileX(), and TileY().

Referenced by WaypointWindow::OnInvalidateData(), WaypointWindow::OnResize(), TownViewWindow::OnResize(), and IndustryViewWindow::OnResize().

◆ SetRedErrorSquare()

void SetRedErrorSquare ( TileIndex  tile)

Set a tile to display a red error square.

Parameters
tileTile that should show the red error square.

Definition at line 2277 of file viewport.cpp.

References INVALID_TILE, MarkTileDirtyByTile(), and TileHighlightData::redsq.

Referenced by ErrmsgWindow::OnHundredthTick().

◆ SetTileSelectSize()

void SetTileSelectSize ( int  w,
int  h 
)

Highlight w by h tiles at the cursor.

Parameters
wWidth of the highlighted tiles rectangle.
hHeight of the highlighted tiles rectangle.

Definition at line 2295 of file viewport.cpp.

References TileHighlightData::new_outersize, TileHighlightData::new_size, and TILE_SIZE.

Referenced by ScenarioEditorLandscapeGenerationWindow::OnPaint(), BuildRailStationWindow::OnPaint(), and SetObjectToPlace().

◆ StartSpriteCombine()

void StartSpriteCombine ( )

Starts a block of sprites, which are "combined" into a single bounding box.

Subsequent calls to AddSortableSpriteToDraw will be drawn into the same bounding box. That is: The first sprite that is not clipped by the viewport defines the bounding box, and the following sprites will be child sprites to that one.

That implies:

  • The drawing order is definite. No other sprites will be sorted between those of the block.
  • You have to provide a valid bounding box for all sprites, as you won't know which one is the first non-clipped one. Preferable you use the same bounding box for all.
  • You cannot use AddChildSpriteScreen inside the block, as its result will be indefinite.

The block is terminated by EndSpriteCombine.

You cannot nest "combined" blocks.

Definition at line 785 of file viewport.cpp.

◆ TranslateXYToTileCoord()

Point TranslateXYToTileCoord ( const ViewPort vp,
int  x,
int  y,
bool  clamp_to_map 
)

Translate screen coordinate in a viewport to a tile coordinate.

Parameters
vpViewport that contains the (x, y) screen coordinate
xScreen x coordinate
yScreen y coordinate
clamp_to_mapClamp the coordinate outside of the map to the closest tile within the map.
Returns
Tile coordinate

Definition at line 405 of file viewport.cpp.

References ViewPort::height, ViewPort::left, ScaleByZoom(), ViewPort::top, ViewPort::virtual_left, ViewPort::width, and ViewPort::zoom.

Referenced by SmallMapWindow::DrawMapIndicators(), and SmallMapWindow::SmallMapCenterOnCurrentPos().

◆ UpdateViewportPosition()

void UpdateViewportPosition ( Window w)

Update the viewport position being displayed.

Parameters
wWindow owning the viewport.

Definition at line 1787 of file viewport.cpp.

References ViewportData::follow_vehicle, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Get(), INVALID_VEHICLE, and Window::viewport.

◆ ViewportAddString()

void ViewportAddString ( const DrawPixelInfo dpi,
ZoomLevel  small_from,
const ViewportSign sign,
StringID  string_normal,
StringID  string_small,
StringID  string_small_shadow,
uint64  params_1,
uint64  params_2,
Colours  colour 
)

Add a string to draw in the viewport.

Parameters
dpicurrent viewport area
small_fromZoomlevel from when the small font should be used
signsign position and dimension
string_normalString for normal and 2x zoom level
string_smallString for 4x and 8x zoom level
string_small_shadowShadow string for 4x and 8x zoom level; or #STR_NULL if no shadow
colourcolour of the sign background; or INVALID_COLOUR if transparent

Definition at line 1219 of file viewport.cpp.

References ViewportSign::center, FONT_HEIGHT_NORMAL, ScaleByZoom(), ViewportSign::top, VPSM_BOTTOM, VPSM_TOP, ViewportSign::width_normal, and ViewportSign::width_small.