From 1d6a92329849e2b33f52e47f8d336ffda1924b28 Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Thu, 14 Nov 2019 20:53:24 +0000 Subject: [PATCH] Refactor screen_get_map_xy_with_z to use CoordsXY structs --- src/openrct2-ui/windows/RideConstruction.cpp | 14 +++---- src/openrct2-ui/windows/TopToolbar.cpp | 9 +++-- src/openrct2/interface/Viewport.cpp | 40 +++++++++----------- src/openrct2/interface/Viewport.h | 2 +- src/openrct2/ride/Ride.cpp | 21 +++++----- 5 files changed, 41 insertions(+), 45 deletions(-) diff --git a/src/openrct2-ui/windows/RideConstruction.cpp b/src/openrct2-ui/windows/RideConstruction.cpp index cba0c71a41..bbd9856056 100644 --- a/src/openrct2-ui/windows/RideConstruction.cpp +++ b/src/openrct2-ui/windows/RideConstruction.cpp @@ -2111,15 +2111,14 @@ static void window_ride_construction_update(rct_window* w) static std::optional ride_get_place_position_from_screen_position(ScreenCoordsXY screenCoords) { CoordsXY mapCoords; - int16_t mapZ; - int32_t interactionType; - rct_viewport* viewport = nullptr; if (!_trackPlaceCtrlState) { if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_COPY_Z) { TileElement* tileElement; + rct_viewport* viewport = nullptr; + int32_t interactionType; get_map_coordinates_from_pos(screenCoords, 0xFCCA, mapCoords, &interactionType, &tileElement, &viewport); if (interactionType != 0) { @@ -2184,7 +2183,7 @@ static std::optional ride_get_place_position_from_screen_position(Scre auto surfaceElement = map_get_surface_element_at(mapCoords.x >> 5, mapCoords.y >> 5); if (surfaceElement == nullptr) return std::nullopt; - mapZ = floor2(surfaceElement->base_height * 8, 16); + auto mapZ = floor2(surfaceElement->base_height * 8, 16); mapZ += _trackPlaceShiftZ; mapZ = std::max(mapZ, 16); _trackPlaceZ = mapZ; @@ -2192,10 +2191,9 @@ static std::optional ride_get_place_position_from_screen_position(Scre } else { - int16_t mapX, mapY; - mapZ = _trackPlaceCtrlZ; - screen_get_map_xy_with_z(screenCoords.x, screenCoords.y, mapZ, &mapX, &mapY); - mapCoords = { mapX, mapY }; + auto mapZ = _trackPlaceCtrlZ; + mapCoords = screen_get_map_xy_with_z(screenCoords, mapZ); + if (_trackPlaceShiftState != 0) { mapZ += _trackPlaceShiftZ; diff --git a/src/openrct2-ui/windows/TopToolbar.cpp b/src/openrct2-ui/windows/TopToolbar.cpp index b02d3e4279..55a5e3dc5a 100644 --- a/src/openrct2-ui/windows/TopToolbar.cpp +++ b/src/openrct2-ui/windows/TopToolbar.cpp @@ -1456,8 +1456,9 @@ static void sub_6E1F34( else { int16_t z = gSceneryCtrlPressZ; - screen_get_map_xy_with_z(x, y, z, grid_x, grid_y); - + auto coords = screen_get_map_xy_with_z({ x, y }, z); + *grid_x = coords.x; + *grid_y = coords.y; // If SHIFT pressed if (gSceneryShiftPressed) { @@ -1616,7 +1617,9 @@ static void sub_6E1F34( else { int16_t z = gSceneryCtrlPressZ; - screen_get_map_xy_with_z(x, y, z, grid_x, grid_y); + auto coords = screen_get_map_xy_with_z({ x, y }, z); + *grid_x = coords.x; + *grid_y = coords.y; // If SHIFT pressed if (gSceneryShiftPressed) diff --git a/src/openrct2/interface/Viewport.cpp b/src/openrct2/interface/Viewport.cpp index 991743cefd..882ffacba2 100644 --- a/src/openrct2/interface/Viewport.cpp +++ b/src/openrct2/interface/Viewport.cpp @@ -1806,27 +1806,23 @@ CoordsXY screen_get_map_xy(ScreenCoordsXY screenCoords, rct_viewport** viewport) * * rct2: 0x006894D4 */ -void screen_get_map_xy_with_z(int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY) +CoordsXY screen_get_map_xy_with_z(ScreenCoordsXY screenCoords, int16_t z) { - rct_viewport* viewport = viewport_find_from_point(screenX, screenY); + CoordsXY ret{ LOCATION_NULL, 0 }; + rct_viewport* viewport = viewport_find_from_point(screenCoords.x, screenCoords.y); if (viewport == nullptr) { - *mapX = LOCATION_NULL; - return; + return ret; } - screenX = viewport->view_x + ((screenX - viewport->x) << viewport->zoom); - screenY = viewport->view_y + ((screenY - viewport->y) << viewport->zoom); - - auto mapPosition = viewport_coord_to_map_coord(screenX, screenY + z, 0); + auto vpCoords = screen_coord_to_viewport_coord(viewport, screenCoords); + auto mapPosition = viewport_coord_to_map_coord(vpCoords.x, vpCoords.y, z); if (mapPosition.x < 0 || mapPosition.x >= (256 * 32) || mapPosition.y < 0 || mapPosition.y > (256 * 32)) { - *mapX = LOCATION_NULL; - return; + return ret; } - *mapX = mapPosition.x; - *mapY = mapPosition.y; + return mapPosition; } /** @@ -1850,13 +1846,13 @@ CoordsXY screen_get_map_xy_quadrant(ScreenCoordsXY screenCoords, uint8_t* quadra void screen_get_map_xy_quadrant_with_z( int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* quadrant) { - screen_get_map_xy_with_z(screenX, screenY, z, mapX, mapY); - if (*mapX == LOCATION_NULL) + auto coords = screen_get_map_xy_with_z({ screenX, screenY }, z); + if (coords.x == LOCATION_NULL) return; - *quadrant = map_get_tile_quadrant(*mapX, *mapY); - *mapX = floor2(*mapX, 32); - *mapY = floor2(*mapY, 32); + *quadrant = map_get_tile_quadrant(coords.x, coords.y); + *mapX = floor2(coords.x, 32); + *mapY = floor2(coords.y, 32); } /** @@ -1879,13 +1875,13 @@ CoordsXY screen_get_map_xy_side(ScreenCoordsXY screenCoords, uint8_t* side) */ void screen_get_map_xy_side_with_z(int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* side) { - screen_get_map_xy_with_z(screenX, screenY, z, mapX, mapY); - if (*mapX == LOCATION_NULL) + auto coords = screen_get_map_xy_with_z({ screenX, screenY }, z); + if (coords.x == LOCATION_NULL) return; - *side = map_get_tile_side(*mapX, *mapY); - *mapX = floor2(*mapX, 32); - *mapY = floor2(*mapY, 32); + *side = map_get_tile_side(coords.x, coords.y); + *mapX = floor2(coords.x, 32); + *mapY = floor2(coords.y, 32); } /** diff --git a/src/openrct2/interface/Viewport.h b/src/openrct2/interface/Viewport.h index 5fb6890a46..02ed412c7f 100644 --- a/src/openrct2/interface/Viewport.h +++ b/src/openrct2/interface/Viewport.h @@ -174,7 +174,7 @@ void sub_68B2B7(paint_session* session, CoordsXY mapCoords); void viewport_invalidate(rct_viewport* viewport, int32_t left, int32_t top, int32_t right, int32_t bottom); CoordsXY screen_get_map_xy(ScreenCoordsXY screenCoords, rct_viewport** viewport); -void screen_get_map_xy_with_z(int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY); +CoordsXY screen_get_map_xy_with_z(ScreenCoordsXY screenCoords, int16_t z); CoordsXY screen_get_map_xy_quadrant(ScreenCoordsXY screenCoords, uint8_t* quadrant); void screen_get_map_xy_quadrant_with_z( int16_t screenX, int16_t screenY, int16_t z, int16_t* mapX, int16_t* mapY, uint8_t* quadrant); diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index db3eced204..fedee9e735 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -6176,7 +6176,6 @@ static int32_t loc_6CD18E( */ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsXY screenCoords) { - int16_t mapX, mapY; int16_t entranceMinX, entranceMinY, entranceMaxX, entranceMaxY, word_F4418C, word_F4418E; int32_t interactionType, stationDirection; uint8_t stationHeight; @@ -6187,7 +6186,7 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX gRideEntranceExitPlaceDirection = 255; get_map_coordinates_from_pos( - screenCoords.x, screenCoords.y, 0xFFFB, &mapX, &mapY, &interactionType, &tileElement, &viewport); + screenCoords.x, screenCoords.y, 0xFFFB, nullptr, nullptr, &interactionType, &tileElement, &viewport); if (interactionType != 0) { if (tileElement->GetType() == TILE_ELEMENT_TYPE_TRACK) @@ -6218,17 +6217,17 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX stationHeight = ride->stations[gRideEntranceExitPlaceStationIndex].Height; - screen_get_map_xy_with_z(screenCoords.x, screenCoords.y, stationHeight * 8, &mapX, &mapY); - if (mapX == LOCATION_NULL) + auto coords = screen_get_map_xy_with_z(screenCoords, stationHeight * 8); + if (coords.x == LOCATION_NULL) { entranceExitCoords.x = LOCATION_NULL; return entranceExitCoords; } - word_F4418C = mapX; - word_F4418E = mapY; + word_F4418C = coords.x; + word_F4418E = coords.y; - entranceExitCoords = { floor2(mapX, 32), floor2(mapY, 32), stationHeight, INVALID_DIRECTION }; + entranceExitCoords = { floor2(coords.x, 32), floor2(coords.y, 32), stationHeight, INVALID_DIRECTION }; if (ride->type == RIDE_TYPE_NULL) { @@ -6245,8 +6244,8 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_3)) { - mapX = (word_F4418C & 0x1F) - 16; - mapY = (word_F4418E & 0x1F) - 16; + auto mapX = (word_F4418C & 0x1F) - 16; + auto mapY = (word_F4418E & 0x1F) - 16; if (std::abs(mapX) < std::abs(mapY)) { entranceExitCoords.direction = mapY < 0 ? 3 : 1; @@ -6300,8 +6299,8 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(ScreenCoordsX } else { - mapX = stationStart.x * 32; - mapY = stationStart.y * 32; + auto mapX = stationStart.x * 32; + auto mapY = stationStart.y * 32; entranceMinX = mapX; entranceMinY = mapY;