GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/image/subimage.cpp Lines: 32 64 50.0 %
Date: 2018-11-12 Branches: 6 20 30.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  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 "resources/image/subimage.h"
24
25
#include "utils/cast.h"
26
27
#ifdef DEBUG_IMAGES
28
#include "logger.h"
29
#endif  // DEBUG_IMAGES
30
31
#include "debug.h"
32
33
#ifdef USE_SDL2
34
SubImage::SubImage(Image *const parent,
35
                   SDL_Texture *const image,
36
                   const int x, const int y,
37
                   const int width, const int height) :
38
    Image(image, width, height),
39
    mInternalBounds(),
40
    mParent(parent)
41
{
42
    if (mParent)
43
    {
44
        mParent->incRef();
45
        mParent->SDLTerminateAlphaCache();
46
        mHasAlphaChannel = mParent->hasAlphaChannel();
47
        mIsAlphaVisible = mHasAlphaChannel;
48
        mAlphaChannel = mParent->SDLgetAlphaChannel();
49
        mSource = parent->mIdPath;
50
#ifdef DEBUG_IMAGES
51
        logger->log("set name2 %p, %s",
52
            static_cast<void*>(this),
53
            mSource.c_str());
54
#endif  // DEBUG_IMAGES
55
#ifdef DEBUG_BIND_TEXTURE
56
        mIdPath = parent->mIdPath;
57
#endif  // DEBUG_BIND_TEXTURE
58
    }
59
    else
60
    {
61
        mHasAlphaChannel = false;
62
        mIsAlphaVisible = false;
63
        mAlphaChannel = nullptr;
64
    }
65
66
    // Set up the rectangle.
67
    mBounds.x = CAST_S16(x);
68
    mBounds.y = CAST_S16(y);
69
    mBounds.w = CAST_U16(width);
70
    mBounds.h = CAST_U16(height);
71
    if (mParent)
72
    {
73
        mInternalBounds.x = mParent->mBounds.x;
74
        mInternalBounds.y = mParent->mBounds.y;
75
        mInternalBounds.w = mParent->mBounds.w;
76
        mInternalBounds.h = mParent->mBounds.h;
77
    }
78
    else
79
    {
80
        mInternalBounds.x = 0;
81
        mInternalBounds.y = 0;
82
        mInternalBounds.w = 1;
83
        mInternalBounds.h = 1;
84
    }
85
    mUseAlphaCache = false;
86
}
87
#endif  // USE_SDL2
88
89
29006
SubImage::SubImage(Image *const parent,
90
                   SDL_Surface *const image,
91
                   const int x, const int y,
92
29006
                   const int width, const int height) :
93
    Image(image, false, nullptr),
94
    mInternalBounds(),
95
29006
    mParent(parent)
96
{
97
29006
    if (mParent != nullptr)
98
    {
99
29006
        mParent->incRef();
100
29006
        mParent->SDLTerminateAlphaCache();
101
29006
        mHasAlphaChannel = mParent->hasAlphaChannel();
102
29006
        mIsAlphaVisible = mHasAlphaChannel;
103
58012
        mAlphaChannel = mParent->SDLgetAlphaChannel();
104
29006
        mSource = parent->mIdPath;
105
#ifdef DEBUG_IMAGES
106
        logger->log("set name2 %p, %s", static_cast<void*>(this),
107
            mSource.c_str());
108
#endif  // DEBUG_IMAGES
109
#ifdef DEBUG_BIND_TEXTURE
110
        mIdPath = parent->mIdPath;
111
#endif  // DEBUG_BIND_TEXTURE
112
    }
113
    else
114
    {
115
        mHasAlphaChannel = false;
116
        mIsAlphaVisible = false;
117
        mAlphaChannel = nullptr;
118
    }
119
120
    // Set up the rectangle.
121
29006
    mBounds.x = CAST_S16(x);
122
29006
    mBounds.y = CAST_S16(y);
123
29006
    mBounds.w = CAST_U16(width);
124
29006
    mBounds.h = CAST_U16(height);
125
29006
    if (mParent != nullptr)
126
    {
127
29006
        mInternalBounds.x = mParent->mBounds.x;
128
29006
        mInternalBounds.y = mParent->mBounds.y;
129
29006
        mInternalBounds.w = mParent->mBounds.w;
130
29006
        mInternalBounds.h = mParent->mBounds.h;
131
    }
132
    else
133
    {
134
        mInternalBounds.x = 0;
135
        mInternalBounds.y = 0;
136
        mInternalBounds.w = 1;
137
        mInternalBounds.h = 1;
138
    }
139
29006
    mUseAlphaCache = false;
140
29006
}
141
142
#ifdef USE_OPENGL
143
SubImage::SubImage(Image *const parent,
144
                   const GLuint image,
145
                   const int x, const int y,
146
                   const int width, const int height,
147
                   const int texWidth, const int texHeight) :
148
    Image(image, width, height, texWidth, texHeight),
149
    mInternalBounds(),
150
    mParent(parent)
151
{
152
    if (mParent != nullptr)
153
        mParent->incRef();
154
155
    // Set up the rectangle.
156
    mBounds.x = CAST_S16(x);
157
    mBounds.y = CAST_S16(y);
158
    mBounds.w = CAST_U16(width);
159
    mBounds.h = CAST_U16(height);
160
    if (mParent != nullptr)
161
    {
162
        mInternalBounds.x = mParent->mBounds.x;
163
        mInternalBounds.y = mParent->mBounds.y;
164
        mInternalBounds.w = mParent->mBounds.w;
165
        mInternalBounds.h = mParent->mBounds.h;
166
        mSource = parent->mIdPath;
167
#ifdef DEBUG_IMAGES
168
        logger->log("set name2 %p, %s", static_cast<void*>(this),
169
            mSource.c_str());
170
#endif  // DEBUG_IMAGES
171
#ifdef DEBUG_BIND_TEXTURE
172
        mIdPath = parent->mIdPath;
173
#endif  // DEBUG_BIND_TEXTURE
174
    }
175
    else
176
    {
177
        mInternalBounds.x = 0;
178
        mInternalBounds.y = 0;
179
        mInternalBounds.w = 1;
180
        mInternalBounds.h = 1;
181
    }
182
    mIsAlphaVisible = mHasAlphaChannel;
183
}
184
#endif  // USE_OPENGL
185
186
87018
SubImage::~SubImage()
187
{
188
#ifdef DEBUG_IMAGES
189
    logger->log("delete subimage: %p", static_cast<void*>(this));
190
    logger->log("  %s, %s", mIdPath.c_str(), mSource.c_str());
191
#endif  // DEBUG_IMAGES
192
    // Avoid destruction of the image
193
29006
    mSDLSurface = nullptr;
194
    // Avoid possible destruction of its alpha channel
195
29006
    mAlphaChannel = nullptr;
196
#ifdef USE_SDL2
197
    // Avoid destruction of texture
198
    mTexture = nullptr;
199
#endif  // USE_SDL2
200
#ifdef USE_OPENGL
201
29006
    mGLImage = 0;
202
#endif  // USE_OPENGL
203
29006
    if (mParent != nullptr)
204
    {
205
#ifdef DEBUG_IMAGES
206
        logger->log("decref from subminage: %p, parent: %p",
207
            static_cast<void*>(this), static_cast<void*>(mParent));
208
#endif  // DEBUG_IMAGES
209
210
29006
        mParent->decRef();
211
29006
        mParent = nullptr;
212
    }
213
58012
}
214
215
Image *SubImage::getSubImage(const int x, const int y,
216
                             const int w, const int h)
217
{
218
    if (mParent != nullptr)
219
        return mParent->getSubImage(mBounds.x + x, mBounds.y + y, w, h);
220
    return nullptr;
221
}
222
223
#ifdef USE_OPENGL
224
6636
void SubImage::decRef()
225
{
226
6636
    Resource::decRef();
227
6636
}
228
#endif  // USE_OPENGL
229
230
int SubImage::calcMemoryLocal() const
231
{
232
    return static_cast<int>(sizeof(SubImage) +
233
        sizeof(std::map<float, SDL_Surface*>)) +
234
        Image::calcMemoryLocal();
235
}