GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/ambientlayer.cpp Lines: 0 56 0.0 %
Date: 2021-03-17 Branches: 0 38 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009-2010  The Mana Developers
4
 *  Copyright (C) 2011-2019  The ManaPlus Developers
5
 *  Copyright (C) 2019-2021  Andrei Karas
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 "resources/ambientlayer.h"
24
25
#include "const/render/graphics.h"
26
27
#include "render/graphics.h"
28
29
#include "resources/imagehelper.h"
30
31
#include "resources/image/image.h"
32
33
#include "resources/loaders/rescaledloader.h"
34
35
#include "debug.h"
36
37
AmbientLayer::AmbientLayer(const std::string &name,
38
                           Image *const img,
39
                           const float parallaxX,
40
                           const float parallaxY,
41
                           const float posX,
42
                           const float posY,
43
                           const float speedX,
44
                           const float speedY,
45
                           const bool keepRatio,
46
                           const int mask) :
47
    MemoryCounter(),
48
    mName(name),
49
    mImage(img),
50
    mParallaxX(parallaxX),
51
    mParallaxY(parallaxY),
52
    mPosX(posX),
53
    mPosY(posY),
54
    mSpeedX(speedX),
55
    mSpeedY(speedY),
56
    mMask(mask),
57
    mKeepRatio(keepRatio)
58
{
59
    if (mImage == nullptr)
60
        return;
61
62
    if (keepRatio &&
63
        imageHelper->useOpenGL() == RENDER_SOFTWARE)
64
    {
65
        const int width = mainGraphics->mWidth;
66
        const int height = mainGraphics->mHeight;
67
        if (width != defaultScreenWidth && height != defaultScreenHeight)
68
        {
69
            // Rescale the overlay to keep the ratio as if we were on
70
            // the default resolution...
71
            Image *const rescaledOverlay = Loader::getRescaled(
72
                mImage,
73
                CAST_S32(mImage->mBounds.w) /
74
                defaultScreenWidth * width,
75
                CAST_S32(mImage->mBounds.h) /
76
                defaultScreenHeight * height);
77
78
            if (rescaledOverlay != nullptr)
79
                mImage = rescaledOverlay;
80
            else
81
                mImage->incRef();
82
            return;
83
        }
84
    }
85
    mImage->incRef();
86
}
87
88
AmbientLayer::~AmbientLayer()
89
{
90
    if (mImage != nullptr)
91
    {
92
        mImage->decRef();
93
        mImage = nullptr;
94
    }
95
}
96
97
void AmbientLayer::update(const int timePassed, const float dx, const float dy)
98
{
99
    if (mImage == nullptr)
100
        return;
101
102
    const float time = static_cast<float>(timePassed) / 10;
103
    // Self scrolling of the overlay
104
    mPosX -= mSpeedX * time;
105
    mPosY -= mSpeedY * time;
106
107
    // Parallax scrolling
108
    mPosX += dx * mParallaxX;
109
    mPosY += dy * mParallaxY;
110
111
    const SDL_Rect &rect = mImage->mBounds;
112
    const float imgW = static_cast<float>(rect.w);
113
    const float imgH = static_cast<float>(rect.h);
114
115
    // Wrap values
116
    while (mPosX > imgW)
117
        mPosX -= imgW;
118
    while (mPosX < 0)
119
        mPosX += imgW;
120
121
    while (mPosY > imgH)
122
        mPosY -= imgH;
123
    while (mPosY < 0)
124
        mPosY += imgH;
125
}
126
127
void AmbientLayer::draw(Graphics *const graphics, const int x,
128
                        const int y) const
129
{
130
    if (mImage == nullptr)
131
        return;
132
133
    if (imageHelper->useOpenGL() == RENDER_SOFTWARE ||
134
        !mKeepRatio)
135
    {
136
        graphics->drawPattern(mImage, CAST_S32(-mPosX),
137
            CAST_S32(-mPosY), x + CAST_S32(mPosX),
138
            y + CAST_S32(mPosY));
139
    }
140
    else
141
    {
142
        graphics->drawRescaledPattern(mImage, CAST_S32(-mPosX),
143
                CAST_S32(-mPosY), x + CAST_S32(mPosX),
144
                y + CAST_S32(mPosY),
145
                CAST_S32(mImage->mBounds.w)
146
                / defaultScreenWidth * graphics->mWidth,
147
                CAST_S32(mImage->mBounds.h) / defaultScreenHeight
148
                * graphics->mHeight);
149
    }
150
}
151
152
int AmbientLayer::calcMemoryLocal() const
153
{
154
    return static_cast<int>(sizeof(AmbientLayer) +
155
        mName.capacity());
156
}