GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/particle/imageparticle.cpp Lines: 2 34 5.9 %
Date: 2017-11-29 Branches: 2 34 5.9 %

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

6
}