GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/ambientlayer.cpp Lines: 0 56 0.0 %
Date: 2017-11-29 Branches: 0 34 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009-2010  The Mana Developers
4
 *  Copyright (C) 2011-2017  The ManaPlus Developers
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "resources/ambientlayer.h"
23
24
#include "const/render/graphics.h"
25
26
#include "render/graphics.h"
27
28
#include "resources/imagehelper.h"
29
30
#include "resources/image/image.h"
31
32
#include "resources/loaders/rescaledloader.h"
33
34
#include "debug.h"
35
36
AmbientLayer::AmbientLayer(const std::string &name,
37
                           Image *const img,
38
                           const float parallaxX,
39
                           const float parallaxY,
40
                           const float posX,
41
                           const float posY,
42
                           const float speedX,
43
                           const float speedY,
44
                           const bool keepRatio,
45
                           const int mask) :
46
    MemoryCounter(),
47
    mName(name),
48
    mImage(img),
49
    mParallaxX(parallaxX),
50
    mParallaxY(parallaxY),
51
    mPosX(posX),
52
    mPosY(posY),
53
    mSpeedX(speedX),
54
    mSpeedY(speedY),
55
    mMask(mask),
56
    mKeepRatio(keepRatio)
57
{
58
    if (mImage == nullptr)
59
        return;
60
61
    if (keepRatio &&
62
        imageHelper->useOpenGL() == RENDER_SOFTWARE)
63
    {
64
        const int width = mainGraphics->mWidth;
65
        const int height = mainGraphics->mHeight;
66
        if (width != defaultScreenWidth && height != defaultScreenHeight)
67
        {
68
            // Rescale the overlay to keep the ratio as if we were on
69
            // the default resolution...
70
            Image *const rescaledOverlay = Loader::getRescaled(
71
                mImage,
72
                CAST_S32(mImage->mBounds.w) /
73
                defaultScreenWidth * width,
74
                CAST_S32(mImage->mBounds.h) /
75
                defaultScreenHeight * height);
76
77
            if (rescaledOverlay != nullptr)
78
                mImage = rescaledOverlay;
79
            else
80
                mImage->incRef();
81
            return;
82
        }
83
    }
84
    mImage->incRef();
85
}
86
87
AmbientLayer::~AmbientLayer()
88
{
89
    if (mImage != nullptr)
90
    {
91
        mImage->decRef();
92
        mImage = nullptr;
93
    }
94
}
95
96
void AmbientLayer::update(const int timePassed, const float dx, const float dy)
97
{
98
    if (mImage == nullptr)
99
        return;
100
101
    const float time = static_cast<float>(timePassed) / 10;
102
    // Self scrolling of the overlay
103
    mPosX -= mSpeedX * time;
104
    mPosY -= mSpeedY * time;
105
106
    // Parallax scrolling
107
    mPosX += dx * mParallaxX;
108
    mPosY += dy * mParallaxY;
109
110
    const SDL_Rect &rect = mImage->mBounds;
111
    const float imgW = static_cast<float>(rect.w);
112
    const float imgH = static_cast<float>(rect.h);
113
114
    // Wrap values
115
    while (mPosX > imgW)
116
        mPosX -= imgW;
117
    while (mPosX < 0)
118
        mPosX += imgW;
119
120
    while (mPosY > imgH)
121
        mPosY -= imgH;
122
    while (mPosY < 0)
123
        mPosY += imgH;
124
}
125
126
void AmbientLayer::draw(Graphics *const graphics, const int x,
127
                        const int y) const
128
{
129
    if (mImage == nullptr)
130
        return;
131
132
    if (imageHelper->useOpenGL() == RENDER_SOFTWARE ||
133
        !mKeepRatio)
134
    {
135
        graphics->drawPattern(mImage, CAST_S32(-mPosX),
136
            CAST_S32(-mPosY), x + CAST_S32(mPosX),
137
            y + CAST_S32(mPosY));
138
    }
139
    else
140
    {
141
        graphics->drawRescaledPattern(mImage, CAST_S32(-mPosX),
142
                CAST_S32(-mPosY), x + CAST_S32(mPosX),
143
                y + CAST_S32(mPosY),
144
                CAST_S32(mImage->mBounds.w)
145
                / defaultScreenWidth * graphics->mWidth,
146
                CAST_S32(mImage->mBounds.h) / defaultScreenHeight
147
                * graphics->mHeight);
148
    }
149
}
150
151
int AmbientLayer::calcMemoryLocal() const
152
{
153
    return static_cast<int>(sizeof(AmbientLayer) +
154
        mName.capacity());
155
}