ManaPlus
imageloader.cpp
Go to the documentation of this file.
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/imagehelper.h"
25 
26 #include "fs/virtfs/rwops.h"
27 
28 #include "resources/image/image.h"
29 
31 
33 
34 #include "resources/dye/dye.h"
35 
36 #include "utils/checkutils.h"
37 
38 #include "debug.h"
39 
40 namespace
41 {
43  {
45 
46  std::string path;
47  static Resource *load(const void *const v)
48  {
49  BLOCK_START("DyedImageLoader::load")
50  if (v == nullptr)
51  {
52  BLOCK_END("DyedImageLoader::load")
53  return nullptr;
54  }
55 
56  const DyedImageLoader *const rl
57  = static_cast<const DyedImageLoader *>(v);
58 
59  std::string path1 = rl->path;
60  const size_t p = path1.find('|');
61  Dye *d = nullptr;
62  if (p != std::string::npos)
63  {
64  d = new Dye(path1.substr(p + 1));
65  path1 = path1.substr(0, p);
66  }
67  SDL_RWops *const rw = VirtFs::rwopsOpenRead(path1);
68  if (rw == nullptr)
69  {
70  delete d;
71  reportAlways("Image loading error: %s", path1.c_str())
72  BLOCK_END("DyedImageLoader::load")
73  return nullptr;
74  }
75  Resource *const res = d != nullptr ? imageHelper->load(rw, *d)
76  : imageHelper->load(rw);
77  delete d;
78  if (res == nullptr)
79  reportAlways("Image loading error: %s", path1.c_str())
80  BLOCK_END("DyedImageLoader::load")
81  return res;
82  }
83  };
84 } // namespace
85 
86 Image *Loader::getImage(const std::string &idPath)
87 {
88  DyedImageLoader rl = { idPath };
89  return static_cast<Image*>(ResourceManager::get(idPath,
90  DyedImageLoader::load, &rl));
91 }
#define reportAlways(...)
Definition: checkutils.h:253
Definition: dye.h:41
Image * load(SDL_RWops *const rw)
Definition: imagehelper.cpp:50
ImageHelper * imageHelper
Definition: imagehelper.cpp:44
#define final
Definition: localconsts.h:46
#define A_DEFAULT_COPY(func)
Definition: localconsts.h:41
void load()
Definition: avatardb.cpp:46
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:86
Resource * get(const std::string &idPath, generator fun, const void *const data)
SDL_RWops * rwopsOpenRead(const std::string &fname)
Definition: rwops.cpp:107
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79