From 5ca553dd0821debfc7dd33b28996de4e31a6c869 Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Wed, 30 Oct 2019 20:24:13 +0000 Subject: [PATCH] Refactor viewport_coord_to_map_coord to return a CoordXY --- src/openrct2-ui/input/MouseInput.cpp | 5 ++-- .../interface/ViewportInteraction.cpp | 8 +++---- src/openrct2/interface/Viewport.cpp | 24 +++++++++---------- src/openrct2/interface/Viewport.h | 2 +- src/openrct2/world/Footpath.cpp | 18 +++++++------- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/openrct2-ui/input/MouseInput.cpp b/src/openrct2-ui/input/MouseInput.cpp index 4e650679a9..a5f1577d4b 100644 --- a/src/openrct2-ui/input/MouseInput.cpp +++ b/src/openrct2-ui/input/MouseInput.cpp @@ -1612,10 +1612,9 @@ void input_scroll_viewport(ScreenCoordsXY scrollScreenCoords) int32_t x = mainWindow->saved_view_x + viewport->view_width / 2 + dx; int32_t y = mainWindow->saved_view_y + viewport->view_height / 2; int32_t y_dy = mainWindow->saved_view_y + viewport->view_height / 2 + dy; - LocationXY16 mapCoord, mapCoord_dy; - mapCoord = viewport_coord_to_map_coord(x, y, 0); - mapCoord_dy = viewport_coord_to_map_coord(x, y_dy, 0); + auto mapCoord = viewport_coord_to_map_coord(x, y, 0); + auto mapCoord_dy = viewport_coord_to_map_coord(x, y_dy, 0); // Check if we're crossing the boundary // Clamp to the map minimum value diff --git a/src/openrct2-ui/interface/ViewportInteraction.cpp b/src/openrct2-ui/interface/ViewportInteraction.cpp index ab8a9b6c0a..de10bcfa34 100644 --- a/src/openrct2-ui/interface/ViewportInteraction.cpp +++ b/src/openrct2-ui/interface/ViewportInteraction.cpp @@ -684,18 +684,18 @@ void sub_68A15E(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y) } LocationXY16 initialVPPos = screen_coord_to_viewport_coord(viewport, screenX, screenY); - LocationXY16 mapPos = { (int16_t)(initialPos.x + 16), (int16_t)(initialPos.y + 16) }; + CoordsXY mapPos = initialPos + CoordsXY{ 16, 16 }; for (int32_t i = 0; i < 5; i++) { int16_t z = waterHeight; if (interactionType != VIEWPORT_INTERACTION_ITEM_WATER) { - z = tile_element_height({ mapPos.x, mapPos.y }); + z = tile_element_height(mapPos); } mapPos = viewport_coord_to_map_coord(initialVPPos.x, initialVPPos.y, z); - mapPos.x = std::clamp(mapPos.x, initialPos.x, initialPos.x + 31); - mapPos.y = std::clamp(mapPos.y, initialPos.y, initialPos.y + 31); + mapPos.x = std::clamp(mapPos.x, initialPos.x, initialPos.x + 31); + mapPos.y = std::clamp(mapPos.y, initialPos.y, initialPos.y + 31); } *x = mapPos.x & ~0x1F; diff --git a/src/openrct2/interface/Viewport.cpp b/src/openrct2/interface/Viewport.cpp index f66a4d7897..6ee90d6ecf 100644 --- a/src/openrct2/interface/Viewport.cpp +++ b/src/openrct2/interface/Viewport.cpp @@ -219,21 +219,20 @@ void viewport_adjust_for_map_height(int16_t* x, int16_t* y, int16_t* z) int16_t height = 0; uint32_t rotation = get_current_rotation(); - LocationXY16 pos; + CoordsXY pos{}; for (int32_t i = 0; i < 6; i++) { pos = viewport_coord_to_map_coord(start_x, start_y, height); - height = tile_element_height({ (0xFFFF) & pos.x, (0xFFFF) & pos.y }); + height = tile_element_height(pos); // HACK: This is to prevent the x and y values being set to values outside // of the map. This can happen when the height is larger than the map size. int16_t max = gMapSizeMinus2; if (pos.x > max && pos.y > max) { - int32_t x_corr[] = { -1, 1, 1, -1 }; - int32_t y_corr[] = { -1, -1, 1, 1 }; - pos.x += x_corr[rotation] * height; - pos.y += y_corr[rotation] * height; + const CoordsXY corr[] = { { -1, -1 }, { 1, -1 }, { 1, 1 }, { -1, 1 } }; + pos.x += corr[rotation].x * height; + pos.y += corr[rotation].y * height; } } @@ -557,9 +556,8 @@ void viewport_update_position(rct_window* window) int16_t x = window->saved_view_x + viewport->view_width / 2; int16_t y = window->saved_view_y + viewport->view_height / 2; - LocationXY16 mapCoord; - mapCoord = viewport_coord_to_map_coord(x, y, 0); + auto mapCoord = viewport_coord_to_map_coord(x, y, 0); // Clamp to the map minimum value int32_t at_map_edge = 0; @@ -1052,9 +1050,9 @@ LocationXY16 screen_coord_to_viewport_coord(rct_viewport* viewport, uint16_t x, return ret; } -LocationXY16 viewport_coord_to_map_coord(int32_t x, int32_t y, int32_t z) +CoordsXY viewport_coord_to_map_coord(int32_t x, int32_t y, int32_t z) { - LocationXY16 ret = {}; + CoordsXY ret{}; switch (get_current_rotation()) { case 0: @@ -1766,11 +1764,11 @@ void screen_get_map_xy(int32_t screenX, int32_t screenY, int16_t* x, int16_t* y, } LocationXY16 start_vp_pos = screen_coord_to_viewport_coord(myViewport, screenX, screenY); - LocationXY16 map_pos = { (int16_t)(my_x + 16), (int16_t)(my_y + 16) }; + CoordsXY map_pos = { my_x + 16, my_y + 16 }; for (int32_t i = 0; i < 5; i++) { - int32_t z = tile_element_height({ map_pos.x, map_pos.y }); + int32_t z = tile_element_height(map_pos); map_pos = viewport_coord_to_map_coord(start_vp_pos.x, start_vp_pos.y, z); map_pos.x = std::clamp(map_pos.x, my_x, my_x + 31); map_pos.y = std::clamp(map_pos.y, my_y, my_y + 31); @@ -1799,7 +1797,7 @@ void screen_get_map_xy_with_z(int16_t screenX, int16_t screenY, int16_t z, int16 screenX = viewport->view_x + ((screenX - viewport->x) << viewport->zoom); screenY = viewport->view_y + ((screenY - viewport->y) << viewport->zoom); - LocationXY16 mapPosition = viewport_coord_to_map_coord(screenX, screenY + z, 0); + auto mapPosition = viewport_coord_to_map_coord(screenX, screenY + z, 0); if (mapPosition.x < 0 || mapPosition.x >= (256 * 32) || mapPosition.y < 0 || mapPosition.y > (256 * 32)) { *mapX = LOCATION_NULL; diff --git a/src/openrct2/interface/Viewport.h b/src/openrct2/interface/Viewport.h index be69c929ac..48773ce720 100644 --- a/src/openrct2/interface/Viewport.h +++ b/src/openrct2/interface/Viewport.h @@ -140,7 +140,7 @@ void viewport_paint( void viewport_adjust_for_map_height(int16_t* x, int16_t* y, int16_t* z); LocationXY16 screen_coord_to_viewport_coord(rct_viewport* viewport, uint16_t x, uint16_t y); -LocationXY16 viewport_coord_to_map_coord(int32_t x, int32_t y, int32_t z); +CoordsXY viewport_coord_to_map_coord(int32_t x, int32_t y, int32_t z); void screen_pos_to_map_pos(int16_t* x, int16_t* y, int32_t* direction); void show_gridlines(); diff --git a/src/openrct2/world/Footpath.cpp b/src/openrct2/world/Footpath.cpp index 8b62b1b9ce..396f9f4288 100644 --- a/src/openrct2/world/Footpath.cpp +++ b/src/openrct2/world/Footpath.cpp @@ -252,17 +252,17 @@ void footpath_get_coordinates_from_pos( int32_t z = 0, interactionType; TileElement* myTileElement; rct_viewport* viewport; - LocationXY16 position = {}; + LocationXY16 position16 = {}; get_map_coordinates_from_pos( - screenX, screenY, VIEWPORT_INTERACTION_MASK_FOOTPATH, &position.x, &position.y, &interactionType, &myTileElement, + screenX, screenY, VIEWPORT_INTERACTION_MASK_FOOTPATH, &position16.x, &position16.y, &interactionType, &myTileElement, &viewport); if (interactionType != VIEWPORT_INTERACTION_ITEM_FOOTPATH || !(viewport->flags & (VIEWPORT_FLAG_UNDERGROUND_INSIDE | VIEWPORT_FLAG_HIDE_BASE | VIEWPORT_FLAG_HIDE_VERTICAL))) { get_map_coordinates_from_pos( - screenX, screenY, VIEWPORT_INTERACTION_MASK_FOOTPATH & VIEWPORT_INTERACTION_MASK_TERRAIN, &position.x, &position.y, - &interactionType, &myTileElement, &viewport); + screenX, screenY, VIEWPORT_INTERACTION_MASK_FOOTPATH & VIEWPORT_INTERACTION_MASK_TERRAIN, &position16.x, + &position16.y, &interactionType, &myTileElement, &viewport); if (interactionType == VIEWPORT_INTERACTION_ITEM_NONE) { if (x != nullptr) @@ -271,11 +271,11 @@ void footpath_get_coordinates_from_pos( } } - LocationXY16 minPosition = position; - LocationXY16 maxPosition = { int16_t(position.x + 31), int16_t(position.y + 31) }; + CoordsXY position = { position16.x, position16.y }; + auto minPosition = position; + auto maxPosition = position + CoordsXY{ 31, 31 }; - position.x += 16; - position.y += 16; + position += CoordsXY{ 16, 16 }; if (interactionType == VIEWPORT_INTERACTION_ITEM_FOOTPATH) { @@ -292,7 +292,7 @@ void footpath_get_coordinates_from_pos( { if (interactionType != VIEWPORT_INTERACTION_ITEM_FOOTPATH) { - z = tile_element_height({ position.x, position.y }); + z = tile_element_height(position); } position = viewport_coord_to_map_coord(start_vp_pos.x, start_vp_pos.y, z); position.x = std::clamp(position.x, minPosition.x, maxPosition.x);