MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / PixelToTile

Method PixelToTile

src/smallmap_gui.cpp:1148–1169  ·  view source on GitHub ↗

* Determine the tile relative to the base tile of the smallmap, and the pixel position at * that tile for a point in the smallmap. * @param px Horizontal coordinate of the pixel. * @param py Vertical coordinate of the pixel. * @param[out] sub Pixel position at the tile (0..3). * @param add_sub Add current #subscroll to the position. * @return Tile being displayed at the gi

Source from the content-addressed store, hash-verified

1146 * @note The #subscroll offset is already accounted for.
1147 */
1148 Point PixelToTile(int px, int py, int *sub, bool add_sub = true) const
1149 {
1150 if (add_sub) px += this->subscroll; // Total horizontal offset.
1151
1152 /* For each two rows down, add a x and a y tile, and
1153 * For each four pixels to the right, move a tile to the right. */
1154 Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
1155 px &= 3;
1156
1157 if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
1158 if (px < 2) {
1159 pt.x += this->zoom;
1160 px += 2;
1161 } else {
1162 pt.y += this->zoom;
1163 px -= 2;
1164 }
1165 }
1166
1167 *sub = px;
1168 return pt;
1169 }
1170
1171 /**
1172 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).

Callers 4

DrawSmallMapMethod · 0.95
SetZoomLevelMethod · 0.95
OnClickMethod · 0.95
OnScrollMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected