Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panorama fixes #1551

Merged
merged 2 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 27 additions & 57 deletions src/game_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ void Game_Map::ScrollRight(int distance) {
int x = map_info.position_x;
AddScreenX(x, distance);
map_info.position_x = x;
Parallax::Scroll(distance, 0);
// Unused, except compatibility with RPG_RT
auto& pan_x = Main_Data::game_data.screen.pan_x;
const auto pan_limit_x = 20 * SCREEN_TILE_WIDTH;
Expand All @@ -373,7 +372,6 @@ void Game_Map::ScrollDown(int distance) {
int y = map_info.position_y;
AddScreenY(y, distance);
map_info.position_y = y;
Parallax::Scroll(0, distance);
// Unused, except compatibility with RPG_RT
auto& pan_y = Main_Data::game_data.screen.pan_y;
const auto pan_limit_y = 10 * SCREEN_TILE_WIDTH;
Expand Down Expand Up @@ -1161,7 +1159,6 @@ void Game_Map::SetPositionX(int x) {
x = std::max(0, std::min(map_width - SCREEN_WIDTH, x));
}
map_info.position_x = x;
Parallax::ResetPosition();
}

int Game_Map::GetPositionY() {
Expand All @@ -1180,7 +1177,6 @@ void Game_Map::SetPositionY(int y) {
y = std::max(0, std::min(map_height - SCREEN_HEIGHT, y));
}
map_info.position_y = y;
Parallax::ResetPosition();
}

Game_Map::RefreshMode Game_Map::GetNeedRefresh() {
Expand Down Expand Up @@ -1516,27 +1512,36 @@ void Game_Map::Parallax::Initialize(int width, int height) {
parallax_width = width;
parallax_height = height;

ResetPosition();
UpdatePosition(true);
}

void Game_Map::Parallax::ResetPosition() {
void Game_Map::Parallax::UpdatePosition(bool init) {
Params params = GetParallaxParams();

if (params.scroll_horz)
parallax_x = -map_info.position_x / 2;
else if (GetWidth() > 20 && parallax_width > SCREEN_TARGET_WIDTH)
parallax_x = -std::min(map_info.position_x,
(map_info.position_x / (SCREEN_TILE_WIDTH / TILE_SIZE)) * (parallax_width - SCREEN_TARGET_WIDTH) / (GetWidth() - 20));
else
parallax_x = 0;
// RPG_RT bug:
// On loaded save games, RPG_RT will always reset
// parallax_x/y to 0 when the map is looping.
// We don't emulate this.

if (params.scroll_vert)
parallax_y = -map_info.position_y / 2;
else if (GetHeight() > 15 && parallax_height > SCREEN_TARGET_HEIGHT)
parallax_y = -std::min(map_info.position_y,
(map_info.position_y / (SCREEN_TILE_WIDTH / TILE_SIZE)) * (parallax_height - SCREEN_TARGET_HEIGHT) / (GetHeight() - 15));
else
parallax_y = 0;
if (init || !Game_Map::LoopHorizontal()) {
if (params.scroll_horz || LoopHorizontal())
parallax_x = -map_info.position_x / 2;
else if (GetWidth() > 20 && parallax_width > SCREEN_TARGET_WIDTH)
parallax_x = -std::min(map_info.position_x,
(map_info.position_x / (SCREEN_TILE_WIDTH / TILE_SIZE)) * (parallax_width - SCREEN_TARGET_WIDTH) / (GetWidth() - 20));
else
parallax_x = 0;
}

if (init || !Game_Map::LoopVertical()) {
if (params.scroll_vert || Game_Map::LoopVertical())
parallax_y = -map_info.position_y / 2;
else if (GetHeight() > 15 && parallax_height > SCREEN_TARGET_HEIGHT)
parallax_y = -std::min(map_info.position_y,
(map_info.position_y / (SCREEN_TILE_WIDTH / TILE_SIZE)) * (parallax_height - SCREEN_TARGET_HEIGHT) / (GetHeight() - 15));
else
parallax_y = 0;
}
}

void Game_Map::Parallax::Update() {
Expand All @@ -1545,6 +1550,8 @@ void Game_Map::Parallax::Update() {
if (params.name.empty())
return;

UpdatePosition();

auto scroll_amt = [](int speed) {
if (speed > 0) return 1 << (speed - 1);
if (speed < 0) return -(1 << (-speed - 1));
Expand All @@ -1564,43 +1571,6 @@ void Game_Map::Parallax::Update() {
}
}

/** Return the argument that is closer to zero. */
static int closer_to_zero(int x, int y) {
return (std::abs(x) < std::abs(y)) ? x : y;
}

void Game_Map::Parallax::Scroll(int distance_right, int distance_down) {
Params params = GetParallaxParams();

// TODO: understand and then doc this function :)

if (params.scroll_vert) {
parallax_y -= distance_down / 2;
} else if (
!LoopVertical() &&
GetHeight() > 15 && parallax_height > SCREEN_TARGET_HEIGHT
) {
parallax_y -=
closer_to_zero(
distance_down,
distance_down * (parallax_height - SCREEN_TARGET_HEIGHT) / (GetHeight() - 15) / (SCREEN_TILE_WIDTH / TILE_SIZE)
);
}

if (params.scroll_horz) {
parallax_x -= distance_right / 2;
} else if (
!LoopHorizontal() &&
GetWidth() > 20 && parallax_width > SCREEN_TARGET_WIDTH
) {
parallax_x -=
closer_to_zero(
distance_right,
distance_right * (parallax_width - SCREEN_TARGET_WIDTH) / (GetWidth() - 20) / (SCREEN_TILE_WIDTH / TILE_SIZE)
);
}
}

void Game_Map::Parallax::ChangeBG(const Params& params) {
map_info.parallax_name = params.name;
map_info.parallax_horz = params.scroll_horz;
Expand Down
14 changes: 6 additions & 8 deletions src/game_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,18 +652,16 @@ namespace Game_Map {
/** Call this when you find out the width and height of the BG. */
void Initialize(int width, int height);

/** Reset the x- and y- position of the BG (eg. after a teleport). */
void ResetPosition();
/**
* Reset the x- and y- position of the BG.
*
* @param init if true will always reset position, even on looping maps
*/
void UpdatePosition(bool init = false);

/** Update autoscrolling BG (call every frame). */
void Update();

/**
* Scrolls the BG by the correct amount when the screen scrolls
* by the given distances.
*/
void Scroll(int distance_right, int distance_down);

/** Change BG (eg. with a "Change Parallax BG" command). */
void ChangeBG(const Params& params);

Expand Down