GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/particle/textparticle.cpp Lines: 1 27 3.7 %
Date: 2017-11-29 Branches: 0 16 0.0 %

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/textparticle.h"
24
25
#include "gui/theme.h"
26
27
#include "gui/fonts/font.h"
28
29
#include "render/graphics.h"
30
31
#include "debug.h"
32
33
TextParticle::TextParticle(const std::string &restrict text,
34
                           const Color *restrict const color,
35
                           Font *restrict const font,
36
                           const bool outline) :
37
    Particle(),
38
    mText(text),
39
    mTextFont(font),
40
    mColor(color),
41
    mTextWidth(mTextFont != nullptr ? mTextFont->getWidth(mText) / 2 : 1),
42
    mOutline(outline)
43
{
44
    mType = ParticleType::Text;
45
}
46
47
void TextParticle::draw(Graphics *restrict const graphics,
48
                        const int offsetX,
49
                        const int offsetY) const restrict2
50
{
51
    BLOCK_START("TextParticle::draw")
52
    if (!isAlive())
53
    {
54
        BLOCK_END("TextParticle::draw")
55
        return;
56
    }
57
58
    const int screenX = CAST_S32(mPos.x) + offsetX;
59
    const int screenY = CAST_S32(mPos.y) - CAST_S32(mPos.z)
60
        + offsetY;
61
62
    float alpha = mAlpha * 255.0F;
63
64
    if ((mFadeOut != 0) && mLifetimeLeft > -1 && mLifetimeLeft < mFadeOut)
65
    {
66
        alpha *= static_cast<float>(mLifetimeLeft)
67
                / static_cast<float>(mFadeOut);
68
    }
69
70
    if ((mFadeIn != 0) && mLifetimePast < mFadeIn)
71
    {
72
        alpha *= static_cast<float>(mLifetimePast)
73
                / static_cast<float>(mFadeIn);
74
    }
75
76
    Color color = *mColor;
77
    color.a = CAST_U32(alpha);
78
79
    graphics->setColor(color);
80
    if (mOutline)
81
    {
82
        const Color &restrict color2 = theme->getColor(ThemeColorId::OUTLINE,
83
            CAST_S32(alpha));
84
        mTextFont->drawString(graphics,
85
            color, color2,
86
            mText,
87
            screenX - mTextWidth, screenY);
88
    }
89
    else
90
    {
91
        mTextFont->drawString(graphics,
92
            color, color,
93
            mText, screenX - mTextWidth, screenY);
94
    }
95
    BLOCK_END("TextParticle::draw")
96
4
}