ManaPlus
imageparticle.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2006-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "particle/imageparticle.h"
25 
26 #include "render/graphics.h"
27 
28 #include "resources/image/image.h"
29 
30 #include "debug.h"
31 
33 
35  Particle()
36 {
38  mImage = image;
39  if (mImage != nullptr)
40  {
41  mImage->incRef();
42 
43  const std::string &restrict name = mImage->mIdPath;
48  else
49  (*it).second ++;
50  }
51 }
52 
53 void ImageParticle::draw(Graphics *restrict const graphics,
54  const int offsetX,
55  const int offsetY) const restrict2
56 {
57  FUNC_BLOCK("ImageParticle::draw", 1)
58  if (mAlive != AliveStatus::ALIVE || (mImage == nullptr))
59  return;
60 
61  const int w = mImage->mBounds.w;
62  const int h = mImage->mBounds.h;
63  const int screenX = CAST_S32(mPos.x)
64  + offsetX - w / 2;
65  const int screenY = CAST_S32(mPos.y) - CAST_S32(mPos.z)
66  + offsetY - h / 2;
67 
68  // Check if on screen
69  if (screenX + w < 0 ||
70  screenX > graphics->mWidth ||
71  screenY + h < 0 ||
72  screenY > graphics->mHeight)
73  {
74  return;
75  }
76 
77  float alphafactor = mAlpha;
78 
79  if ((mFadeOut != 0) && mLifetimeLeft > -1 && mLifetimeLeft < mFadeOut)
80  {
81  alphafactor *= static_cast<float>(mLifetimeLeft)
82  / static_cast<float>(mFadeOut);
83  }
84 
85  if ((mFadeIn != 0) && mLifetimePast < mFadeIn)
86  {
87  alphafactor *= static_cast<float>(mLifetimePast)
88  / static_cast<float>(mFadeIn);
89  }
90 
91  mImage->setAlpha(alphafactor);
92  graphics->drawImage(mImage, screenX, screenY);
93 }
#define CAST_S32
Definition: cast.h:30
ImageParticle(Image *const image)
void draw(Graphics *const graphics, const int offsetX, const int offsetY) const
static StringIntMap imageParticleCountByName
Definition: imageparticle.h:59
Image * mImage
Definition: particle.h:277
ParticleTypeT mType
Definition: particle.h:271
#define restrict
Definition: localconsts.h:165
#define restrict2
Definition: localconsts.h:166
#define FUNC_BLOCK(name, id)
Definition: perfomance.h:81
std::map< std::string, int > StringIntMap
Definition: stringmap.h:28
StringIntMap::iterator StringIntMapIter
Definition: stringmap.h:29