ManaPlus
ambientlayer.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009-2010 The Mana Developers
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "resources/ambientlayer.h"
23 
24 #include "const/render/graphics.h"
25 
26 #include "render/graphics.h"
27 
28 #include "resources/imagehelper.h"
29 
30 #include "resources/image/image.h"
31 
33 
34 #include "debug.h"
35 
36 AmbientLayer::AmbientLayer(const std::string &name,
37  Image *const img,
38  const float parallaxX,
39  const float parallaxY,
40  const float posX,
41  const float posY,
42  const float speedX,
43  const float speedY,
44  const bool keepRatio,
45  const int mask) :
46  MemoryCounter(),
47  mName(name),
48  mImage(img),
49  mParallaxX(parallaxX),
50  mParallaxY(parallaxY),
51  mPosX(posX),
52  mPosY(posY),
53  mSpeedX(speedX),
54  mSpeedY(speedY),
55  mMask(mask),
56  mKeepRatio(keepRatio)
57 {
58  if (mImage == nullptr)
59  return;
60 
61  if (keepRatio &&
63  {
64  const int width = mainGraphics->mWidth;
65  const int height = mainGraphics->mHeight;
66  if (width != defaultScreenWidth && height != defaultScreenHeight)
67  {
68  // Rescale the overlay to keep the ratio as if we were on
69  // the default resolution...
70  Image *const rescaledOverlay = Loader::getRescaled(
71  mImage,
72  CAST_S32(mImage->mBounds.w) /
73  defaultScreenWidth * width,
74  CAST_S32(mImage->mBounds.h) /
75  defaultScreenHeight * height);
76 
77  if (rescaledOverlay != nullptr)
78  mImage = rescaledOverlay;
79  else
80  mImage->incRef();
81  return;
82  }
83  }
84  mImage->incRef();
85 }
86 
88 {
89  if (mImage != nullptr)
90  {
91  mImage->decRef();
92  mImage = nullptr;
93  }
94 }
95 
96 void AmbientLayer::update(const int timePassed, const float dx, const float dy)
97 {
98  if (mImage == nullptr)
99  return;
100 
101  const float time = static_cast<float>(timePassed) / 10;
102  // Self scrolling of the overlay
103  mPosX -= mSpeedX * time;
104  mPosY -= mSpeedY * time;
105 
106  // Parallax scrolling
107  mPosX += dx * mParallaxX;
108  mPosY += dy * mParallaxY;
109 
110  const SDL_Rect &rect = mImage->mBounds;
111  const float imgW = static_cast<float>(rect.w);
112  const float imgH = static_cast<float>(rect.h);
113 
114  // Wrap values
115  while (mPosX > imgW)
116  mPosX -= imgW;
117  while (mPosX < 0)
118  mPosX += imgW;
119 
120  while (mPosY > imgH)
121  mPosY -= imgH;
122  while (mPosY < 0)
123  mPosY += imgH;
124 }
125 
126 void AmbientLayer::draw(Graphics *const graphics, const int x,
127  const int y) const
128 {
129  if (mImage == nullptr)
130  return;
131 
133  !mKeepRatio)
134  {
135  graphics->drawPattern(mImage, CAST_S32(-mPosX),
136  CAST_S32(-mPosY), x + CAST_S32(mPosX),
137  y + CAST_S32(mPosY));
138  }
139  else
140  {
142  CAST_S32(-mPosY), x + CAST_S32(mPosX),
143  y + CAST_S32(mPosY),
145  / defaultScreenWidth * graphics->mWidth,
147  * graphics->mHeight);
148  }
149 }
150 
152 {
153  return static_cast<int>(sizeof(AmbientLayer) +
154  mName.capacity());
155 }
imagehelper.h
AmbientLayer::mName
const std::string mName
Definition: ambientlayer.h:77
defaultScreenWidth
static const int defaultScreenWidth
Definition: graphics.h:32
AmbientLayer::mSpeedX
float mSpeedX
Definition: ambientlayer.h:83
MemoryCounter
Definition: memorycounter.h:28
imageHelper
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
rescaledloader.h
Image
Definition: image.h:67
defaultScreenHeight
static const int defaultScreenHeight
Definition: graphics.h:33
Graphics
Definition: graphics.h:107
AmbientLayer::~AmbientLayer
~AmbientLayer()
Definition: ambientlayer.cpp:87
AmbientLayer::mParallaxY
float mParallaxY
Definition: ambientlayer.h:80
Resource::incRef
virtual void incRef()
Definition: resource.cpp:37
AmbientLayer::mPosX
float mPosX
Definition: ambientlayer.h:81
graphics.h
mainGraphics
Graphics * mainGraphics
Definition: graphics.cpp:108
image.h
x
x
Definition: graphics_calcImageRect.hpp:72
AmbientLayer::update
void update(const int timePassed, const float dx, const float dy)
Definition: ambientlayer.cpp:96
ambientlayer.h
AmbientLayer::mImage
Image * mImage
Definition: ambientlayer.h:78
RENDER_SOFTWARE
@ RENDER_SOFTWARE
Definition: rendertype.h:26
Image::mBounds
SDL_Rect mBounds
Definition: image.h:216
Graphics::mHeight
int mHeight
Definition: graphics.h:484
Graphics::drawPattern
virtual void drawPattern(const Image *const image, const int x, const int y, const int w, const int h)=0
AmbientLayer::draw
void draw(Graphics *const graphics, const int x, const int y) const
Definition: ambientlayer.cpp:126
AmbientLayer::calcMemoryLocal
int calcMemoryLocal() const
Definition: ambientlayer.cpp:151
AmbientLayer::mParallaxX
float mParallaxX
Definition: ambientlayer.h:79
CAST_S32
#define CAST_S32
Definition: cast.h:29
AmbientLayer::AmbientLayer
AmbientLayer(const std::string &name, Image *const img, const float parallax, const float parallaxY, const float posX, const float posY, const float speedX, const float speedY, const bool keepRatio, const int mask)
Definition: ambientlayer.cpp:36
ImageHelper::useOpenGL
virtual RenderType useOpenGL() const
Definition: imagehelper.h:106
Graphics::drawRescaledPattern
virtual void drawRescaledPattern(const Image *const image, const int x, const int y, const int w, const int h, const int scaledWidth, const int scaledHeight)=0
AmbientLayer::mPosY
float mPosY
Definition: ambientlayer.h:82
Loader::getRescaled
Image * getRescaled(const Image *const image, const int width, const int height)
Definition: rescaledloader.cpp:64
AmbientLayer::mSpeedY
float mSpeedY
Definition: ambientlayer.h:84
y
y
Definition: graphics_calcImageRect.hpp:72
AmbientLayer::mKeepRatio
bool mKeepRatio
Definition: ambientlayer.h:86
debug.h
Image::decRef
void decRef()
Definition: image.cpp:522
Graphics::mWidth
int mWidth
Definition: graphics.h:483