GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/image/subimage.cpp Lines: 32 64 50.0 %
Date: 2017-11-29 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-2017  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", this, mSource.c_str());
52
#endif  // DEBUG_IMAGES
53
#ifdef DEBUG_BIND_TEXTURE
54
        mIdPath = parent->mIdPath;
55
#endif  // DEBUG_BIND_TEXTURE
56
    }
57
    else
58
    {
59
        mHasAlphaChannel = false;
60
        mIsAlphaVisible = false;
61
        mAlphaChannel = nullptr;
62
    }
63
64
    // Set up the rectangle.
65
    mBounds.x = CAST_S16(x);
66
    mBounds.y = CAST_S16(y);
67
    mBounds.w = CAST_U16(width);
68
    mBounds.h = CAST_U16(height);
69
    if (mParent)
70
    {
71
        mInternalBounds.x = mParent->mBounds.x;
72
        mInternalBounds.y = mParent->mBounds.y;
73
        mInternalBounds.w = mParent->mBounds.w;
74
        mInternalBounds.h = mParent->mBounds.h;
75
    }
76
    else
77
    {
78
        mInternalBounds.x = 0;
79
        mInternalBounds.y = 0;
80
        mInternalBounds.w = 1;
81
        mInternalBounds.h = 1;
82
    }
83
    mUseAlphaCache = false;
84
}
85
#endif  // USE_SDL2
86
87
53010
SubImage::SubImage(Image *const parent,
88
                   SDL_Surface *const image,
89
                   const int x, const int y,
90
53010
                   const int width, const int height) :
91
    Image(image, false),
92
    mInternalBounds(),
93
53010
    mParent(parent)
94
{
95
53010
    if (mParent != nullptr)
96
    {
97
53010
        mParent->incRef();
98
53010
        mParent->SDLTerminateAlphaCache();
99
53010
        mHasAlphaChannel = mParent->hasAlphaChannel();
100
53010
        mIsAlphaVisible = mHasAlphaChannel;
101
106020
        mAlphaChannel = mParent->SDLgetAlphaChannel();
102
53010
        mSource = parent->mIdPath;
103
#ifdef DEBUG_IMAGES
104
        logger->log("set name2 %p, %s", static_cast<void*>(this),
105
            mSource.c_str());
106
#endif  // DEBUG_IMAGES
107
#ifdef DEBUG_BIND_TEXTURE
108
        mIdPath = parent->mIdPath;
109
#endif  // DEBUG_BIND_TEXTURE
110
    }
111
    else
112
    {
113
        mHasAlphaChannel = false;
114
        mIsAlphaVisible = false;
115
        mAlphaChannel = nullptr;
116
    }
117
118
    // Set up the rectangle.
119
53010
    mBounds.x = CAST_S16(x);
120
53010
    mBounds.y = CAST_S16(y);
121
53010
    mBounds.w = CAST_U16(width);
122
53010
    mBounds.h = CAST_U16(height);
123
53010
    if (mParent != nullptr)
124
    {
125
53010
        mInternalBounds.x = mParent->mBounds.x;
126
53010
        mInternalBounds.y = mParent->mBounds.y;
127
53010
        mInternalBounds.w = mParent->mBounds.w;
128
53010
        mInternalBounds.h = mParent->mBounds.h;
129
    }
130
    else
131
    {
132
        mInternalBounds.x = 0;
133
        mInternalBounds.y = 0;
134
        mInternalBounds.w = 1;
135
        mInternalBounds.h = 1;
136
    }
137
53010
    mUseAlphaCache = false;
138
53010
}
139
140
#ifdef USE_OPENGL
141
SubImage::SubImage(Image *const parent,
142
                   const GLuint image,
143
                   const int x, const int y,
144
                   const int width, const int height,
145
                   const int texWidth, const int texHeight) :
146
    Image(image, width, height, texWidth, texHeight),
147
    mInternalBounds(),
148
    mParent(parent)
149
{
150
    if (mParent != nullptr)
151
        mParent->incRef();
152
153
    // Set up the rectangle.
154
    mBounds.x = CAST_S16(x);
155
    mBounds.y = CAST_S16(y);
156
    mBounds.w = CAST_U16(width);
157
    mBounds.h = CAST_U16(height);
158
    if (mParent != nullptr)
159
    {
160
        mInternalBounds.x = mParent->mBounds.x;
161
        mInternalBounds.y = mParent->mBounds.y;
162
        mInternalBounds.w = mParent->mBounds.w;
163
        mInternalBounds.h = mParent->mBounds.h;
164
        mSource = parent->mIdPath;
165
#ifdef DEBUG_IMAGES
166
        logger->log("set name2 %p, %s", static_cast<void*>(this),
167
            mSource.c_str());
168
#endif  // DEBUG_IMAGES
169
#ifdef DEBUG_BIND_TEXTURE
170
        mIdPath = parent->mIdPath;
171
#endif  // DEBUG_BIND_TEXTURE
172
    }
173
    else
174
    {
175
        mInternalBounds.x = 0;
176
        mInternalBounds.y = 0;
177
        mInternalBounds.w = 1;
178
        mInternalBounds.h = 1;
179
    }
180
    mIsAlphaVisible = mHasAlphaChannel;
181
}
182
#endif  // USE_OPENGL
183
184
159030
SubImage::~SubImage()
185
{
186
#ifdef DEBUG_IMAGES
187
    logger->log("delete subimage: %p", static_cast<void*>(this));
188
    logger->log("  %s, %s", mIdPath.c_str(), mSource.c_str());
189
#endif  // DEBUG_IMAGES
190
    // Avoid destruction of the image
191
53010
    mSDLSurface = nullptr;
192
    // Avoid possible destruction of its alpha channel
193
53010
    mAlphaChannel = nullptr;
194
#ifdef USE_SDL2
195
    // Avoid destruction of texture
196
    mTexture = nullptr;
197
#endif  // USE_SDL2
198
#ifdef USE_OPENGL
199
53010
    mGLImage = 0;
200
#endif  // USE_OPENGL
201
53010
    if (mParent != nullptr)
202
    {
203
#ifdef DEBUG_IMAGES
204
        logger->log("decref from subminage: %p, parent: %p",
205
            static_cast<void*>(this), static_cast<void*>(mParent));
206
#endif  // DEBUG_IMAGES
207
208
53010
        mParent->decRef();
209
53010
        mParent = nullptr;
210
    }
211
106020
}
212
213
Image *SubImage::getSubImage(const int x, const int y,
214
                             const int w, const int h)
215
{
216
    if (mParent != nullptr)
217
        return mParent->getSubImage(mBounds.x + x, mBounds.y + y, w, h);
218
    return nullptr;
219
}
220
221
#ifdef USE_OPENGL
222
13040
void SubImage::decRef()
223
{
224
13040
    Resource::decRef();
225
13040
}
226
#endif  // USE_OPENGL
227
228
int SubImage::calcMemoryLocal() const
229
{
230
    return static_cast<int>(sizeof(SubImage) +
231
        sizeof(std::map<float, SDL_Surface*>)) +
232
        Resource::calcMemoryLocal();
233
}