GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/loaders/subimageloader.cpp Lines: 21 22 95.5 %
Date: 2017-11-29 Branches: 14 30 46.7 %

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/image.h"
24
25
#include "resources/loaders/subimageloader.h"
26
27
#include "resources/resourcemanager/resourcemanager.h"
28
29
#include "utils/checkutils.h"
30
#include "utils/stringutils.h"
31
32
#include "debug.h"
33
34
namespace
35
{
36
    struct SubImageLoader final
37
    {
38
        A_DEFAULT_COPY(SubImageLoader)
39
40
        Image *const parent;
41
        const int x;
42
        const int y;
43
        const int width;
44
        const int height;
45
8526
        static Resource *load(const void *const v)
46
        {
47
8526
            if (v == nullptr)
48
                return nullptr;
49
50
            const SubImageLoader *const
51
8526
                rl = static_cast<const SubImageLoader *>(v);
52
8526
            if (rl->parent == nullptr)
53
                return nullptr;
54
55
8526
            Image *const res = rl->parent->getSubImage(rl->x, rl->y,
56
17052
                rl->width, rl->height);
57
8526
            if (res == nullptr)
58
            {
59
                reportAlways("SubImage loading error: %s",
60
                    rl->parent->mSource.c_str());
61
            }
62
            return res;
63
        }
64
    };
65
}  // namespace
66
67
9638
Image *Loader::getSubImage(Image *const parent,
68
                           const int x,
69
                           const int y,
70
                           const int width,
71
                           const int height)
72
{
73
9638
    if (parent == nullptr)
74
        return nullptr;
75
76
9638
    const SubImageLoader rl = { parent, x, y, width, height};
77
78
28914
    const std::string str = std::string(parent->mIdPath).append(
79
9638
        ",[").append(
80
28914
        toString(x)).append(
81
9638
        ",").append(
82
28914
        toString(y)).append(
83
9638
        ",").append(
84
28914
        toString(width)).append(
85
9638
        "x").append(
86
28914
        toString(height)).append(
87
19276
        "]");
88
    return static_cast<Image*>(ResourceManager::get(str,
89
9638
        SubImageLoader::load, &rl));
90
}