Skip to content

Commit

Permalink
Correctly calculate the screen dimensions when the resolution is not …
Browse files Browse the repository at this point in the history
…dividable by the tile size.

Rename the screen variables in the Parallax code as the same name (var shadowing) is confusing.

Fixes incorrect scrolling of the map in Beloved Rapture
  • Loading branch information
Ghabry committed Nov 8, 2024
1 parent e0a5863 commit 70a3047
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/game_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ void Game_Map::OnContinueFromBattle() {
static Game_Map::Parallax::Params GetParallaxParams();

void Game_Map::Init() {
screen_width = (Player::screen_width / 16) * SCREEN_TILE_SIZE;
screen_height = (Player::screen_height / 16) * SCREEN_TILE_SIZE;

Dispose();

map_info = {};
Expand Down Expand Up @@ -156,8 +153,8 @@ int Game_Map::GetMapSaveCount() {
void Game_Map::Setup(std::unique_ptr<lcf::rpg::Map> map_in) {
Dispose();

screen_width = (Player::screen_width / 16) * SCREEN_TILE_SIZE;
screen_height = (Player::screen_height / 16) * SCREEN_TILE_SIZE;
screen_width = (Player::screen_width / 16.0) * SCREEN_TILE_SIZE;
screen_height = (Player::screen_height / 16.0) * SCREEN_TILE_SIZE;

map = std::move(map_in);

Expand Down Expand Up @@ -637,7 +634,7 @@ void Game_Map::Scroll(int dx, int dy) {
// that acc changed by.
static void ClampingAdd(int low, int high, int& acc, int& inc) {
int original_acc = acc;
acc = std::max(low, std::min(high, acc + inc));
acc = std::clamp(acc + inc, low, high);
inc = acc - original_acc;
}

Expand Down Expand Up @@ -1651,7 +1648,7 @@ void Game_Map::SetPositionX(int x, bool reset_panorama) {
if (LoopHorizontal()) {
x = Utils::PositiveModulo(x, map_width);
} else {
x = std::max(0, std::min(map_width - screen_width, x));
x = std::clamp(x, 0, map_width - screen_width);
}
map_info.position_x = x;
if (reset_panorama) {
Expand All @@ -1673,7 +1670,7 @@ void Game_Map::SetPositionY(int y, bool reset_panorama) {
if (LoopVertical()) {
y = Utils::PositiveModulo(y, map_height);
} else {
y = std::max(0, std::min(map_height - screen_height, y));
y = std::clamp(y, 0, map_height - screen_height);
}
map_info.position_y = y;
if (reset_panorama) {
Expand Down Expand Up @@ -2037,19 +2034,19 @@ void Game_Map::Parallax::ResetPositionX() {
parallax_fake_x = false;

if (!params.scroll_horz && !LoopHorizontal()) {
int screen_width = Player::screen_width;
int pan_screen_width = Player::screen_width;
if (Player::game_config.fake_resolution.Get()) {
screen_width = SCREEN_TARGET_WIDTH;
pan_screen_width = SCREEN_TARGET_WIDTH;
}

int tiles_per_screen = screen_width / TILE_SIZE;
if (screen_width % TILE_SIZE != 0) {
int tiles_per_screen = pan_screen_width / TILE_SIZE;
if (pan_screen_width % TILE_SIZE != 0) {
++tiles_per_screen;
}

if (GetTilesX() > tiles_per_screen && parallax_width > screen_width) {
if (GetTilesX() > tiles_per_screen && parallax_width > pan_screen_width) {
const int w = (GetTilesX() - tiles_per_screen) * TILE_SIZE;
const int ph = 2 * std::min(w, parallax_width - screen_width) * map_info.position_x / w;
const int ph = 2 * std::min(w, parallax_width - pan_screen_width) * map_info.position_x / w;
if (Player::IsRPG2k()) {
SetPositionX(ph);
} else {
Expand All @@ -2075,19 +2072,19 @@ void Game_Map::Parallax::ResetPositionY() {
parallax_fake_y = false;

if (!params.scroll_vert && !Game_Map::LoopVertical()) {
int screen_height = Player::screen_height;
int pan_screen_height = Player::screen_height;
if (Player::game_config.fake_resolution.Get()) {
screen_height = SCREEN_TARGET_HEIGHT;
pan_screen_height = SCREEN_TARGET_HEIGHT;
}

int tiles_per_screen = screen_height / TILE_SIZE;
if (screen_height % TILE_SIZE != 0) {
int tiles_per_screen = pan_screen_height / TILE_SIZE;
if (pan_screen_height % TILE_SIZE != 0) {
++tiles_per_screen;
}

if (GetTilesY() > tiles_per_screen && parallax_height > screen_height) {
if (GetTilesY() > tiles_per_screen && parallax_height > pan_screen_height) {
const int h = (GetTilesY() - tiles_per_screen) * TILE_SIZE;
const int pv = 2 * std::min(h, parallax_height - screen_height) * map_info.position_y / h;
const int pv = 2 * std::min(h, parallax_height - pan_screen_height) * map_info.position_y / h;
SetPositionY(pv);
} else {
panorama.pan_y = 0;
Expand Down

0 comments on commit 70a3047

Please sign in to comment.