ManaPlus
Public Member Functions | Private Attributes | Friends
AmbientLayer Class Reference

#include <ambientlayer.h>

Inheritance diagram for AmbientLayer:
MemoryCounter

Public Member Functions

 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)
 
 ~AmbientLayer ()
 
void update (const int timePassed, const float dx, const float dy)
 
void draw (Graphics *const graphics, const int x, const int y) const
 
int calcMemoryLocal () const
 
std::string getCounterName () const
 
- Public Member Functions inherited from MemoryCounter
 MemoryCounter ()
 
virtual ~MemoryCounter ()
 
int calcMemory (const int level) const
 
virtual int calcMemoryChilds (const int level) const
 

Private Attributes

const std::string mName
 
ImagemImage
 
float mParallaxX
 
float mParallaxY
 
float mPosX
 
float mPosY
 
float mSpeedX
 
float mSpeedY
 
int mMask
 
bool mKeepRatio
 

Friends

class Map
 

Detailed Description

Definition at line 33 of file ambientlayer.h.

Constructor & Destructor Documentation

◆ 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 
)

Constructor.

Parameters
imgthe image this overlay displays
parallaxscroll factor based on camera position
speedXscrolling speed in x-direction
speedYscrolling speed in y-direction
keepRatiorescale the image to keep the same ratio than in 800x600 resolution mode.

Definition at line 36 of file ambientlayer.cpp.

References CAST_S32, defaultScreenHeight, defaultScreenWidth, Loader::getRescaled(), imageHelper, Resource::incRef(), mainGraphics, Image::mBounds, Graphics::mHeight, mImage, Graphics::mWidth, RENDER_SOFTWARE, and ImageHelper::useOpenGL().

Referenced by calcMemoryLocal().

45  :
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 }
virtual RenderType useOpenGL() const
Definition: imagehelper.h:106
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
int mWidth
Definition: graphics.h:483
const std::string mName
Definition: ambientlayer.h:77
static const int defaultScreenHeight
Definition: graphics.h:29
#define CAST_S32
Definition: cast.h:29
Image * mImage
Definition: ambientlayer.h:78
float mParallaxY
Definition: ambientlayer.h:80
Graphics * mainGraphics
Definition: graphics.cpp:108
static const int defaultScreenWidth
Definition: graphics.h:28
int mHeight
Definition: graphics.h:484
float mParallaxX
Definition: ambientlayer.h:79
virtual void incRef()
Definition: resource.cpp:37
Definition: image.h:61
SDL_Rect mBounds
Definition: image.h:210
Image * getRescaled(const Image *const image, const int width, const int height)

◆ ~AmbientLayer()

AmbientLayer::~AmbientLayer ( )

Definition at line 87 of file ambientlayer.cpp.

References Image::decRef(), and mImage.

88 {
89  if (mImage != nullptr)
90  {
91  mImage->decRef();
92  mImage = nullptr;
93  }
94 }
Image * mImage
Definition: ambientlayer.h:78
void decRef()
Definition: image.cpp:521

Member Function Documentation

◆ calcMemoryLocal()

int AmbientLayer::calcMemoryLocal ( ) const
virtual

Reimplemented from MemoryCounter.

Definition at line 151 of file ambientlayer.cpp.

References AmbientLayer(), and mName.

152 {
153  return static_cast<int>(sizeof(AmbientLayer) +
154  mName.capacity());
155 }
const std::string mName
Definition: ambientlayer.h:77
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)

◆ draw()

void AmbientLayer::draw ( Graphics *const  graphics,
const int  x,
const int  y 
) const

Definition at line 126 of file ambientlayer.cpp.

References CAST_S32, defaultScreenHeight, defaultScreenWidth, Graphics::drawPattern(), Graphics::drawRescaledPattern(), imageHelper, Image::mBounds, Graphics::mHeight, mImage, mKeepRatio, mPosX, mPosY, Graphics::mWidth, RENDER_SOFTWARE, and ImageHelper::useOpenGL().

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 }
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
virtual RenderType useOpenGL() const
Definition: imagehelper.h:106
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
int mWidth
Definition: graphics.h:483
static const int defaultScreenHeight
Definition: graphics.h:29
#define CAST_S32
Definition: cast.h:29
Image * mImage
Definition: ambientlayer.h:78
static const int defaultScreenWidth
Definition: graphics.h:28
int mHeight
Definition: graphics.h:484
virtual void drawPattern(const Image *const image, const int x, const int y, const int w, const int h)=0
SDL_Rect mBounds
Definition: image.h:210

◆ getCounterName()

std::string AmbientLayer::getCounterName ( ) const
inlinevirtual

Reimplemented from MemoryCounter.

Definition at line 73 of file ambientlayer.h.

References mName.

74  { return mName; }
const std::string mName
Definition: ambientlayer.h:77

◆ update()

void AmbientLayer::update ( const int  timePassed,
const float  dx,
const float  dy 
)

Definition at line 96 of file ambientlayer.cpp.

References Image::mBounds, mImage, mParallaxX, mParallaxY, mPosX, mPosY, mSpeedX, mSpeedY, and ServerCommandType::time.

Referenced by Map::updateAmbientLayers().

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 }
Image * mImage
Definition: ambientlayer.h:78
float mParallaxY
Definition: ambientlayer.h:80
float mParallaxX
Definition: ambientlayer.h:79
SDL_Rect mBounds
Definition: image.h:210

Friends And Related Function Documentation

◆ Map

friend class Map
friend

Definition at line 36 of file ambientlayer.h.

Field Documentation

◆ mImage

Image* AmbientLayer::mImage
private

Definition at line 78 of file ambientlayer.h.

Referenced by AmbientLayer(), draw(), update(), and ~AmbientLayer().

◆ mKeepRatio

bool AmbientLayer::mKeepRatio
private

Keep overlay ratio on every resolution

Definition at line 86 of file ambientlayer.h.

Referenced by draw().

◆ mMask

int AmbientLayer::mMask
private

Definition at line 85 of file ambientlayer.h.

Referenced by Map::updateAmbientLayers().

◆ mName

const std::string AmbientLayer::mName
private

Definition at line 77 of file ambientlayer.h.

Referenced by calcMemoryLocal(), and getCounterName().

◆ mParallaxX

float AmbientLayer::mParallaxX
private

Definition at line 79 of file ambientlayer.h.

Referenced by update().

◆ mParallaxY

float AmbientLayer::mParallaxY
private

Definition at line 80 of file ambientlayer.h.

Referenced by update().

◆ mPosX

float AmbientLayer::mPosX
private

Current layer X position.

Definition at line 81 of file ambientlayer.h.

Referenced by draw(), and update().

◆ mPosY

float AmbientLayer::mPosY
private

Current layer Y position.

Definition at line 82 of file ambientlayer.h.

Referenced by draw(), and update().

◆ mSpeedX

float AmbientLayer::mSpeedX
private

Scrolling speed in X direction.

Definition at line 83 of file ambientlayer.h.

Referenced by update().

◆ mSpeedY

float AmbientLayer::mSpeedY
private

Scrolling speed in Y direction.

Definition at line 84 of file ambientlayer.h.

Referenced by update().


The documentation for this class was generated from the following files: