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  *
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 
30 
32 
33 #include "resources/dye/dye.h"
34 
35 #include "utils/checkutils.h"
36 
37 #include "debug.h"
38 
39 namespace
40 {
42  {
44 
45  std::string path;
46  static Resource *load(const void *const v)
47  {
48  BLOCK_START("DyedImageLoader::load")
49  if (v == nullptr)
50  {
51  BLOCK_END("DyedImageLoader::load")
52  return nullptr;
53  }
54 
55  const DyedImageLoader *const rl
56  = static_cast<const DyedImageLoader *>(v);
57 
58  std::string path1 = rl->path;
59  const size_t p = path1.find('|');
60  Dye *d = nullptr;
61  if (p != std::string::npos)
62  {
63  d = new Dye(path1.substr(p + 1));
64  path1 = path1.substr(0, p);
65  }
66  SDL_RWops *const rw = VirtFs::rwopsOpenRead(path1);
67  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  Resource *const res = d != nullptr ? imageHelper->load(rw, *d)
75  : imageHelper->load(rw);
76  delete d;
77  if (res == nullptr)
78  reportAlways("Image loading error: %s", path1.c_str())
79  BLOCK_END("DyedImageLoader::load")
80  return res;
81  }
82  };
83 } // namespace
84 
85 Image *Loader::getImage(const std::string &idPath)
86 {
87  DyedImageLoader rl = { idPath };
88  return static_cast<Image*>(ResourceManager::get(idPath,
89  DyedImageLoader::load, &rl));
90 }
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
imageloader.h
rwops.h
imagehelper.h
Resource
Definition: resource.h:33
imageHelper
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
resourcemanager.h
VirtFs::rwopsOpenRead
SDL_RWops * rwopsOpenRead(const std::string &fname)
Definition: rwops.cpp:105
Image
Definition: image.h:67
Dye
Definition: dye.h:39
A_DEFAULT_COPY
#define A_DEFAULT_COPY(func)
Definition: localconsts.h:40
final
#define final
Definition: localconsts.h:45
checkutils.h
Loader::getImage
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:85
image.h
anonymous_namespace{imageloader.cpp}::DyedImageLoader::path
std::string path
Definition: imageloader.cpp:45
std
Definition: mseprimitives.h:398
dye.h
ImageHelper::load
Image * load(SDL_RWops *const rw)
Definition: imagehelper.cpp:49
BLOCK_START
#define BLOCK_START(name)
Definition: perfomance.h:78
AvatarDB::load
void load()
Definition: avatardb.cpp:45
BLOCK_END
#define BLOCK_END(name)
Definition: perfomance.h:79
ResourceManager::get
Resource * get(const std::string &idPath, generator fun, const void *const data)
Definition: resourcemanager.cpp:427
debug.h
anonymous_namespace{imageloader.cpp}::DyedImageLoader
Definition: imageloader.cpp:41