GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/loaders/imageloader.cpp Lines: 20 23 87.0 %
Date: 2017-11-29 Branches: 17 46 37.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/imagehelper.h"
24
25
#include "fs/virtfs/rwops.h"
26
27
#include "resources/image/image.h"
28
29
#include "resources/loaders/imageloader.h"
30
31
#include "resources/resourcemanager/resourcemanager.h"
32
33
#include "resources/dye/dye.h"
34
35
#include "utils/checkutils.h"
36
37
#include "debug.h"
38
39
namespace
40
{
41
48976
    struct DyedImageLoader final
42
    {
43
        A_DEFAULT_COPY(DyedImageLoader)
44
45
        std::string path;
46
2942
        static Resource *load(const void *const v)
47
        {
48
            BLOCK_START("DyedImageLoader::load")
49
2942
            if (v == nullptr)
50
            {
51
                BLOCK_END("DyedImageLoader::load")
52
                return nullptr;
53
            }
54
55
2942
            const DyedImageLoader *const rl
56
                = static_cast<const DyedImageLoader *>(v);
57
58
5884
            std::string path1 = rl->path;
59
2942
            const size_t p = path1.find('|');
60
2942
            Dye *d = nullptr;
61
2942
            if (p != std::string::npos)
62
            {
63

20
                d = new Dye(path1.substr(p + 1));
64
20
                path1 = path1.substr(0, p);
65
            }
66
2942
            SDL_RWops *const rw = VirtFs::rwopsOpenRead(path1);
67
2942
            if (rw == nullptr)
68
            {
69
                delete d;
70
                reportAlways("Image loading error: %s", path1.c_str());
71
                BLOCK_END("DyedImageLoader::load")
72
                return nullptr;
73
            }
74
10
            Resource *const res = d != nullptr ? imageHelper->load(rw, *d)
75

2952
                : imageHelper->load(rw);
76
2942
            delete d;
77
2942
            if (res == nullptr)
78
                reportAlways("Image loading error: %s", path1.c_str());
79
            BLOCK_END("DyedImageLoader::load")
80
2942
            return res;
81
        }
82
    };
83
}  // namespace
84
85
24488
Image *Loader::getImage(const std::string &idPath)
86
{
87
48976
    DyedImageLoader rl = { idPath };
88
    return static_cast<Image*>(ResourceManager::get(idPath,
89
48976
        DyedImageLoader::load, &rl));
90
}