From c661f8663a2ca6ff0debc222a9fbb4a1167dba5a Mon Sep 17 00:00:00 2001 From: Nipun Garg Date: Tue, 18 Jun 2019 05:47:12 +0530 Subject: HDB: Add the centerMapXY to center the map --- engines/hdb/map-loader.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++ engines/hdb/map-loader.h | 1 + 2 files changed, 64 insertions(+) (limited to 'engines') diff --git a/engines/hdb/map-loader.cpp b/engines/hdb/map-loader.cpp index c5d682c03a..f66783f14f 100644 --- a/engines/hdb/map-loader.cpp +++ b/engines/hdb/map-loader.cpp @@ -264,4 +264,67 @@ void Map::setMapXY(int x, int y) { _mapY = y; } +// Sets _mapX and _mapY and tries to center the map around X, Y +void Map::centerMapXY(int x, int y) { + int checkx = x / kTileWidth; + int checky = y / kTileHeight; + + int minx, miny, maxx, maxy; + + // Scan from centerX to right edge + maxx = (_width - (16/2)) * kTileWidth; + for (int i = checkx + 1; i <= checkx + (16 / 2); i++) { + if (!getMapBGTileIndex(i, checky)) { + maxx = (i - (16 / 2)) * kTileWidth; + break; + } + } + + // Scan from centerX to left edge + minx = 0; + for (int i = checkx - 1; i >= checkx - (16 / 2); i--) { + if (!getMapBGTileIndex(i, checky)) { + // +1 because we don't want to see one whole tile + minx = (1 + i + (16 / 2)) * kTileWidth; + break; + } + } + + // Scan from centerY to bottom edge + maxy = (_height - (16/2)) * kTileHeight; + for (int i = checky + 1; i <= checky + (16 / 2); i++) { + if (!getMapBGTileIndex(checkx, i)) { + maxy = (i - (16 / 2)) * kTileHeight; + break; + } + } + + // Scan from centerY to top edge + miny = 0; + for (int i = checky - 1; i >= checkx - (16 / 2); i--) { + if (!getMapBGTileIndex(checkx, i)) { + // +! because we don't want to see one whole tile + miny = (1 + i + (16 / 2)) * kTileHeight; + break; + } + } + + if (x < minx) { + x = minx; + } else if (x > maxx) { + x = maxx; + } + + if (y < miny) { + y = miny; + } else if (y > maxy) { + y = maxy; + } + + x -= (480 / 2); + y -= (480 / 2); + + setMapXY(x, y); +} + } diff --git a/engines/hdb/map-loader.h b/engines/hdb/map-loader.h index 35f1357ec3..d2266818b3 100644 --- a/engines/hdb/map-loader.h +++ b/engines/hdb/map-loader.h @@ -62,6 +62,7 @@ public: uint16 getMapFGTileIndex(int x, int y); void getMapXY(int *x, int *y); void setMapXY(int x, int y); + void centerMapXY(int x, int y); int _mapX, _mapY; // Coordinates of Map int _mapTileX, _mapTileY; // Tile Coordinates of Map -- cgit v1.2.3