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

Function ClampViewportToMap

src/viewport.cpp:1928–1946  ·  view source on GitHub ↗

* Ensure that a given viewport has a valid scroll position. * * There must be a visible piece of the map in the center of the viewport. * If there isn't, the viewport will be scrolled to nearest such location. * * @param vp The viewport. * @param[in,out] scroll_x Viewport X scroll. * @param[in,out] scroll_y Viewport Y scroll. */

Source from the content-addressed store, hash-verified

1926 * @param[in,out] scroll_y Viewport Y scroll.
1927 */
1928static inline void ClampViewportToMap(const Viewport &vp, int *scroll_x, int *scroll_y)
1929{
1930 /* Centre of the viewport is hot spot. */
1931 Point pt = {
1932 *scroll_x + vp.virtual_width / 2,
1933 *scroll_y + vp.virtual_height / 2
1934 };
1935
1936 /* Find nearest tile that is within borders of the map. */
1937 bool clamped;
1938 pt = InverseRemapCoords2(pt.x, pt.y, true, &clamped);
1939
1940 if (clamped) {
1941 /* Convert back to viewport coordinates and remove centering. */
1942 pt = RemapCoords2(pt.x, pt.y);
1943 *scroll_x = pt.x - vp.virtual_width / 2;
1944 *scroll_y = pt.y - vp.virtual_height / 2;
1945 }
1946}
1947
1948/**
1949 * Clamp the smooth scroll to a maxmimum speed and distance based on time elapsed.

Callers 1

UpdateViewportPositionFunction · 0.85

Calls 2

InverseRemapCoords2Function · 0.85
RemapCoords2Function · 0.85

Tested by

no test coverage detected