From 98e0c2435bd69d6dd78fb0f84389a81b56278958 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Thu, 7 Mar 2024 20:33:38 +0100 Subject: Fixed ambient layers "keepratio" option I had broken this in 264be2108c51837fa92085f6c839e66aebbcfc9e by no longer reading out the property. Made some further simplifications and two more fixes: * When the original image's size was not 800x600, it wouldn't get scaled as expected due to the integer division being performed before the multiplication. * I had changed the default resolution to 1280x720, while mKeepRatio expects to scale the original image as if it was displayed on an 800x600 screen. So now it's hardcoded on 800x600 rather than using the defaultScreenWidth/Height variables. * Removed the confusing x/y parameters from AmbientLayer::draw, which were actually set to the graphics width/height. * Changed SDLGraphics::drawRescaledImagePattern to not repeatedly set the source origin. --- src/map.cpp | 3 ++- src/resources/ambientlayer.cpp | 27 +++++++++++++++++++-------- src/resources/ambientlayer.h | 2 +- src/resources/resource.h | 2 +- src/sdlgraphics.cpp | 8 ++++---- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/map.cpp b/src/map.cpp index 1de22456..368b4e2a 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -252,6 +252,7 @@ void Map::initializeAmbientLayers() ambientLayer->mSpeedX = getFloatProperty(name + "scrollX"); ambientLayer->mSpeedY = getFloatProperty(name + "scrollY"); ambientLayer->mMask = getIntProperty(name + "mask", 1); + ambientLayer->mKeepRatio = getBoolProperty(name + "keepratio"); list.push_back(ambientLayer); @@ -501,7 +502,7 @@ void Map::drawAmbientLayers(Graphics *graphics, LayerType type, if ((layer->mMask & mMask) == 0) continue; - layer->draw(graphics, graphics->getWidth(), graphics->getHeight()); + layer->draw(graphics); // Detail 1: only one overlay, higher: all overlays if (detail == 1) diff --git a/src/resources/ambientlayer.cpp b/src/resources/ambientlayer.cpp index 2fa522e5..b292070b 100644 --- a/src/resources/ambientlayer.cpp +++ b/src/resources/ambientlayer.cpp @@ -21,7 +21,6 @@ #include "resources/ambientlayer.h" #include "graphics.h" -#include "video.h" #include "resources/image.h" #include "resources/resourcemanager.h" @@ -30,8 +29,7 @@ AmbientLayer::AmbientLayer(Image *img) : mImage(img) {} -AmbientLayer::~AmbientLayer() -{} +AmbientLayer::~AmbientLayer() = default; void AmbientLayer::update(int timePassed, float dx, float dy) { @@ -58,14 +56,27 @@ void AmbientLayer::update(int timePassed, float dx, float dy) mPosY += imgH; } -void AmbientLayer::draw(Graphics *graphics, int x, int y) +void AmbientLayer::draw(Graphics *graphics) { + const auto screenWidth = graphics->getWidth(); + const auto screenHeight = graphics->getHeight(); + const auto x = static_cast(mPosX); + const auto y = static_cast(mPosY); + if (!mKeepRatio) + { graphics->drawImagePattern(mImage, - (int) -mPosX, (int) -mPosY, x + (int) mPosX, y + (int) mPosY); + -x, -y, + screenWidth + x, + screenHeight + y); + } else + { graphics->drawRescaledImagePattern(mImage, - (int) -mPosX, (int) -mPosY, x + (int) mPosX, y + (int) mPosY, - (int) mImage->getWidth() / defaultScreenWidth * graphics->getWidth(), - (int) mImage->getHeight() / defaultScreenHeight * graphics->getHeight()); + -x, -y, + screenWidth + x, + screenHeight + y, + mImage->getWidth() * screenWidth / 800, + mImage->getHeight() * screenHeight / 600); + } } diff --git a/src/resources/ambientlayer.h b/src/resources/ambientlayer.h index def2090b..e62af33f 100644 --- a/src/resources/ambientlayer.h +++ b/src/resources/ambientlayer.h @@ -39,7 +39,7 @@ class AmbientLayer void update(int timePassed, float dx, float dy); - void draw(Graphics *graphics, int x, int y); + void draw(Graphics *graphics); float mParallax = 0; /**< Scroll factor based on camera position. */ float mSpeedX = 0; /**< Scrolling speed in X direction. */ diff --git a/src/resources/resource.h b/src/resources/resource.h index 7f1f4836..9fe00f3d 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -59,7 +59,7 @@ class Resource { return mIdPath; } protected: - virtual ~Resource() {} + virtual ~Resource() = default; private: std::string mIdPath; /**< Path identifying this resource. */ diff --git a/src/sdlgraphics.cpp b/src/sdlgraphics.cpp index b7f80fd2..3f6e809b 100644 --- a/src/sdlgraphics.cpp +++ b/src/sdlgraphics.cpp @@ -118,23 +118,23 @@ void SDLGraphics::drawRescaledImagePattern(Image *image, if (scaledHeight <= 0 || scaledWidth <= 0) return; + SDL_Rect srcRect; + srcRect.x = image->mBounds.x; + srcRect.y = image->mBounds.y; + for (int py = 0; py < h; py += scaledHeight) // Y position on pattern plane { int dh = (py + scaledHeight >= h) ? h - py : scaledHeight; - int srcY = image->mBounds.y; int dstY = y + py + mClipStack.top().yOffset; for (int px = 0; px < w; px += scaledWidth) // X position on pattern plane { int dw = (px + scaledWidth >= w) ? w - px : scaledWidth; - int srcX = image->mBounds.x; int dstX = x + px + mClipStack.top().xOffset; SDL_Rect dstRect; - SDL_Rect srcRect; dstRect.x = dstX; dstRect.y = dstY; dstRect.w = dw; dstRect.h = dh; - srcRect.x = srcX; srcRect.y = srcY; srcRect.w = dw; srcRect.h = dh; if (SDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect)) -- cgit v1.2.3-70-g09d2