GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/loaders/subimageloader.cpp Lines: 21 22 95.5 %
Date: 2021-03-17 Branches: 13 28 46.4 %

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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "resources/image/image.h"
25
26
#include "resources/loaders/subimageloader.h"
27
28
#include "resources/resourcemanager/resourcemanager.h"
29
30
#include "utils/checkutils.h"
31
#include "utils/stringutils.h"
32
33
#include "debug.h"
34
35
namespace
36
{
37
    struct SubImageLoader final
38
    {
39
        A_DEFAULT_COPY(SubImageLoader)
40
41
        Image *const parent;
42
        const int x;
43
        const int y;
44
        const int width;
45
        const int height;
46
4347
        static Resource *load(const void *const v)
47
        {
48
4347
            if (v == nullptr)
49
                return nullptr;
50
51
            const SubImageLoader *const
52
4347
                rl = static_cast<const SubImageLoader *>(v);
53
4347
            if (rl->parent == nullptr)
54
                return nullptr;
55
56
4347
            Image *const res = rl->parent->getSubImage(rl->x, rl->y,
57
8694
                rl->width, rl->height);
58
4347
            if (res == nullptr)
59
            {
60
                reportAlways("SubImage loading error: %s",
61
                    rl->parent->mSource.c_str())
62
            }
63
            return res;
64
        }
65
    };
66
}  // namespace
67
68
4921
Image *Loader::getSubImage(Image *const parent,
69
                           const int x,
70
                           const int y,
71
                           const int width,
72
                           const int height)
73
{
74
4921
    if (parent == nullptr)
75
        return nullptr;
76
77
4921
    const SubImageLoader rl = { parent, x, y, width, height};
78
79
14763
    const std::string str = std::string(parent->mIdPath).append(
80
4921
        ",[").append(
81
14763
        toString(x)).append(
82
4921
        ",").append(
83
14763
        toString(y)).append(
84
4921
        ",").append(
85
14763
        toString(width)).append(
86
4921
        "x").append(
87
14763
        toString(height)).append(
88
14763
        "]");
89
    return static_cast<Image*>(ResourceManager::get(str,
90
4921
        SubImageLoader::load, &rl));
91
}