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 34 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 37 of file ambientlayer.cpp.

46  :
47  MemoryCounter(),
48  mName(name),
49  mImage(img),
50  mParallaxX(parallaxX),
51  mParallaxY(parallaxY),
52  mPosX(posX),
53  mPosY(posY),
54  mSpeedX(speedX),
55  mSpeedY(speedY),
56  mMask(mask),
57  mKeepRatio(keepRatio)
58 {
59  if (mImage == nullptr)
60  return;
61 
62  if (keepRatio &&
64  {
65  const int width = mainGraphics->mWidth;
66  const int height = mainGraphics->mHeight;
67  if (width != defaultScreenWidth && height != defaultScreenHeight)
68  {
69  // Rescale the overlay to keep the ratio as if we were on
70  // the default resolution...
71  Image *const rescaledOverlay = Loader::getRescaled(
72  mImage,
73  CAST_S32(mImage->mBounds.w) /
74  defaultScreenWidth * width,
75  CAST_S32(mImage->mBounds.h) /
76  defaultScreenHeight * height);
77 
78  if (rescaledOverlay != nullptr)
79  mImage = rescaledOverlay;
80  else
81  mImage->incRef();
82  return;
83  }
84  }
85  mImage->incRef();
86 }
#define CAST_S32
Definition: cast.h:30
float mParallaxY
Definition: ambientlayer.h:81
Image * mImage
Definition: ambientlayer.h:79
float mParallaxX
Definition: ambientlayer.h:80
const std::string mName
Definition: ambientlayer.h:78
int mWidth
Definition: graphics.h:484
int mHeight
Definition: graphics.h:485
virtual RenderType useOpenGL() const
Definition: imagehelper.h:107
static const int defaultScreenWidth
Definition: graphics.h:33
static const int defaultScreenHeight
Definition: graphics.h:34
Graphics * mainGraphics
Definition: graphics.cpp:109
ImageHelper * imageHelper
Definition: imagehelper.cpp:44
Image * getRescaled(const Image *const image, const int width, const int height)
@ RENDER_SOFTWARE
Definition: rendertype.h:27

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

Referenced by calcMemoryLocal().

◆ ~AmbientLayer()

AmbientLayer::~AmbientLayer ( )

Definition at line 88 of file ambientlayer.cpp.

89 {
90  if (mImage != nullptr)
91  {
92  mImage->decRef();
93  mImage = nullptr;
94  }
95 }

References mImage.

Member Function Documentation

◆ calcMemoryLocal()

int AmbientLayer::calcMemoryLocal ( ) const
virtual

Reimplemented from MemoryCounter.

Definition at line 152 of file ambientlayer.cpp.

153 {
154  return static_cast<int>(sizeof(AmbientLayer) +
155  mName.capacity());
156 }
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)

References AmbientLayer(), and mName.

◆ draw()

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

Definition at line 127 of file ambientlayer.cpp.

129 {
130  if (mImage == nullptr)
131  return;
132 
134  !mKeepRatio)
135  {
136  graphics->drawPattern(mImage, CAST_S32(-mPosX),
137  CAST_S32(-mPosY), x + CAST_S32(mPosX),
138  y + CAST_S32(mPosY));
139  }
140  else
141  {
143  CAST_S32(-mPosY), x + CAST_S32(mPosX),
144  y + CAST_S32(mPosY),
145  CAST_S32(mImage->mBounds.w)
146  / defaultScreenWidth * graphics->mWidth,
147  CAST_S32(mImage->mBounds.h) / defaultScreenHeight
148  * graphics->mHeight);
149  }
150 }
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 void drawPattern(const Image *const image, const int x, const int y, const int w, const int h)=0

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

◆ getCounterName()

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

Reimplemented from MemoryCounter.

Definition at line 74 of file ambientlayer.h.

75  { return mName; }

References mName.

◆ update()

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

Definition at line 97 of file ambientlayer.cpp.

98 {
99  if (mImage == nullptr)
100  return;
101 
102  const float time = static_cast<float>(timePassed) / 10;
103  // Self scrolling of the overlay
104  mPosX -= mSpeedX * time;
105  mPosY -= mSpeedY * time;
106 
107  // Parallax scrolling
108  mPosX += dx * mParallaxX;
109  mPosY += dy * mParallaxY;
110 
111  const SDL_Rect &rect = mImage->mBounds;
112  const float imgW = static_cast<float>(rect.w);
113  const float imgH = static_cast<float>(rect.h);
114 
115  // Wrap values
116  while (mPosX > imgW)
117  mPosX -= imgW;
118  while (mPosX < 0)
119  mPosX += imgW;
120 
121  while (mPosY > imgH)
122  mPosY -= imgH;
123  while (mPosY < 0)
124  mPosY += imgH;
125 }

References mImage, mParallaxX, mParallaxY, mPosX, mPosY, mSpeedX, and mSpeedY.

Referenced by Map::updateAmbientLayers().

Friends And Related Function Documentation

◆ Map

friend class Map
friend

Definition at line 37 of file ambientlayer.h.

Field Documentation

◆ mImage

Image* AmbientLayer::mImage
private

Definition at line 79 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 87 of file ambientlayer.h.

Referenced by draw().

◆ mMask

int AmbientLayer::mMask
private

Definition at line 86 of file ambientlayer.h.

Referenced by Map::updateAmbientLayers().

◆ mName

const std::string AmbientLayer::mName
private

Definition at line 78 of file ambientlayer.h.

Referenced by calcMemoryLocal(), and getCounterName().

◆ mParallaxX

float AmbientLayer::mParallaxX
private

Definition at line 80 of file ambientlayer.h.

Referenced by update().

◆ mParallaxY

float AmbientLayer::mParallaxY
private

Definition at line 81 of file ambientlayer.h.

Referenced by update().

◆ mPosX

float AmbientLayer::mPosX
private

Current layer X position.

Definition at line 82 of file ambientlayer.h.

Referenced by draw(), and update().

◆ mPosY

float AmbientLayer::mPosY
private

Current layer Y position.

Definition at line 83 of file ambientlayer.h.

Referenced by draw(), and update().

◆ mSpeedX

float AmbientLayer::mSpeedX
private

Scrolling speed in X direction.

Definition at line 84 of file ambientlayer.h.

Referenced by update().

◆ mSpeedY

float AmbientLayer::mSpeedY
private

Scrolling speed in Y direction.

Definition at line 85 of file ambientlayer.h.

Referenced by update().


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