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

Function UpdateViewportPosition

src/viewport.cpp:1988–2043  ·  view source on GitHub ↗

* Update the viewport position being displayed. * @param w %Window owning the viewport. * @param delta_ms Milliseconds since the last update. */

Source from the content-addressed store, hash-verified

1986 * @param delta_ms Milliseconds since the last update.
1987 */
1988void UpdateViewportPosition(Window *w, uint32_t delta_ms)
1989{
1990 ViewportData &vp = *w->viewport;
1991
1992 if (vp.follow_vehicle != VehicleID::Invalid()) {
1993 const Vehicle *veh = Vehicle::Get(vp.follow_vehicle);
1994 Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
1995
1996 vp.scrollpos_x = pt.x;
1997 vp.scrollpos_y = pt.y;
1998 SetViewportPosition(w, pt.x, pt.y);
1999 } else {
2000 /* Ensure the destination location is within the map */
2001 ClampViewportToMap(vp, &vp.dest_scrollpos_x, &vp.dest_scrollpos_y);
2002
2003 int delta_x = vp.dest_scrollpos_x - vp.scrollpos_x;
2004 int delta_y = vp.dest_scrollpos_y - vp.scrollpos_y;
2005
2006 int current_x = vp.scrollpos_x;
2007 int current_y = vp.scrollpos_y;
2008
2009 bool update_overlay = false;
2010 if (delta_x != 0 || delta_y != 0) {
2011 if (_settings_client.gui.smooth_scroll) {
2012 int delta_x_clamped;
2013 int delta_y_clamped;
2014
2015 if (abs(delta_x) > abs(delta_y)) {
2016 ClampSmoothScroll(delta_ms, delta_x, delta_y, delta_x_clamped, delta_y_clamped);
2017 } else {
2018 ClampSmoothScroll(delta_ms, delta_y, delta_x, delta_y_clamped, delta_x_clamped);
2019 }
2020
2021 vp.scrollpos_x += delta_x_clamped;
2022 vp.scrollpos_y += delta_y_clamped;
2023 } else {
2024 vp.scrollpos_x = vp.dest_scrollpos_x;
2025 vp.scrollpos_y = vp.dest_scrollpos_y;
2026 }
2027 update_overlay = (vp.scrollpos_x == vp.dest_scrollpos_x &&
2028 vp.scrollpos_y == vp.dest_scrollpos_y);
2029 }
2030
2031 ClampViewportToMap(vp, &vp.scrollpos_x, &vp.scrollpos_y);
2032
2033 /* When moving small amounts around the border we can get stuck, and
2034 * not actually move. In those cases, teleport to the destination. */
2035 if ((delta_x != 0 || delta_y != 0) && current_x == vp.scrollpos_x && current_y == vp.scrollpos_y) {
2036 vp.scrollpos_x = vp.dest_scrollpos_x;
2037 vp.scrollpos_y = vp.dest_scrollpos_y;
2038 }
2039
2040 SetViewportPosition(w, vp.scrollpos_x, vp.scrollpos_y);
2041 if (update_overlay) RebuildViewportOverlay(w);
2042 }
2043}
2044
2045/**

Callers 2

OnRealtimeTickMethod · 0.85
UpdateWindowsFunction · 0.85

Calls 7

InvalidFunction · 0.85
MapXYZToViewportFunction · 0.85
SetViewportPositionFunction · 0.85
ClampViewportToMapFunction · 0.85
absFunction · 0.85
ClampSmoothScrollFunction · 0.85
RebuildViewportOverlayFunction · 0.85

Tested by

no test coverage detected