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