~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/imageparticle.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130917103551-az7p3nz9jgxwqjfn
Tags: 1.3.9.15-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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-2013  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 "imageparticle.h"
24
 
 
25
 
#include "graphics.h"
26
 
#include "logger.h"
27
 
 
28
 
#include "resources/image.h"
29
 
 
30
 
#include "debug.h"
31
 
 
32
 
std::map<std::string, int> ImageParticle::imageParticleCountByName;
33
 
 
34
 
ImageParticle::ImageParticle(Map *const map, Image *const image):
35
 
    Particle(map),
36
 
    mImage(image)
37
 
{
38
 
    if (mImage)
39
 
    {
40
 
        mImage->incRef();
41
 
 
42
 
        const std::string &name = mImage->getIdPath();
43
 
        std::map<std::string, int>::iterator 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
 
ImageParticle::~ImageParticle()
53
 
{
54
 
    if (mImage)
55
 
    {
56
 
        const std::string &name = mImage->getIdPath();
57
 
        std::map<std::string, int>::iterator it
58
 
            = ImageParticle::imageParticleCountByName.find(name);
59
 
        if (it != ImageParticle::imageParticleCountByName.end())
60
 
        {
61
 
            int &cnt = (*it).second;
62
 
            if (cnt > 0)
63
 
                cnt --;
64
 
        }
65
 
 
66
 
        mImage->decRef();
67
 
        mImage = nullptr;
68
 
    }
69
 
    setMap(nullptr);
70
 
}
71
 
 
72
 
bool ImageParticle::draw(Graphics *const graphics,
73
 
                         const int offsetX, const int offsetY) const
74
 
{
75
 
    FUNC_BLOCK("ImageParticle::draw", 1)
76
 
    if (mAlive != ALIVE || !mImage)
77
 
        return false;
78
 
 
79
 
    const int screenX = static_cast<int>(mPos.x)
80
 
        + offsetX - mImage->mBounds.w / 2;
81
 
    const int screenY = static_cast<int>(mPos.y) - static_cast<int>(mPos.z)
82
 
        + offsetY - mImage->mBounds.h / 2;
83
 
 
84
 
    // Check if on screen
85
 
    if (screenX + mImage->mBounds.w < 0 ||
86
 
        screenX > graphics->mWidth ||
87
 
        screenY + mImage->mBounds.h < 0 ||
88
 
        screenY > graphics->mHeight)
89
 
    {
90
 
        return false;
91
 
    }
92
 
 
93
 
    float alphafactor = mAlpha;
94
 
 
95
 
    if (mFadeOut && mLifetimeLeft > -1 && mLifetimeLeft < mFadeOut)
96
 
    {
97
 
        alphafactor *= static_cast<float>(mLifetimeLeft)
98
 
            / static_cast<float>(mFadeOut);
99
 
    }
100
 
 
101
 
    if (mFadeIn && mLifetimePast < mFadeIn)
102
 
    {
103
 
        alphafactor *= static_cast<float>(mLifetimePast)
104
 
        / static_cast<float>(mFadeIn);
105
 
    }
106
 
 
107
 
    mImage->setAlpha(alphafactor);
108
 
    return graphics->drawImage(mImage, screenX, screenY);
109
 
}