载入中...
搜索中...
未找到
ImageLoader.cpp
浏览该文件的文档.
1// Loads ImageData through the unified resource cache (common/Resource.h).
2//
3// Registered as an eve::caps::IAssetReloader provider: ResourceManager asks it
4// to load() on a cache miss and to produce a fresh ImageData on hot reload,
5// which is adopted into the cached instance in place (identity stays stable).
6// reload() is intentionally not implemented here -- the GPU-side refresh is
7// owned by graphics (TextureReloader) and runs after the cache refresh
8// (kCache < kTexture).
9
11#include "common/Capability.h"
12#include "common/Exception.h"
13#include "common/Module.h"
14#include "common/Resource.h"
15#include "filesystem/FileData.h"
17#include "image/Image.h"
18#include "image/ImageData.h"
19
20#include <cctype>
21#include <memory>
22#include <string>
23
24namespace eve::image {
25namespace {
26
27std::string extensionOf(const std::string &path) {
28 const auto pos = path.find_last_of('.');
29 if (pos == std::string::npos) return {};
30 std::string ext = path.substr(pos);
31 for (char &c : ext) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
32 return ext;
33}
34
35bool isImagePath(const std::string &path) {
36 const std::string ext = extensionOf(path);
37 return ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".bmp" || ext == ".tga" ||
38 ext == ".gif" || ext == ".webp" || ext == ".exr" || ext == ".hdr";
39}
40
41class ImageLoader : public eve::caps::IAssetReloader {
42public:
43 const char *reloadKind() const override { return "image"; }
44
45 bool handlesPath(const std::string &normPath) const override {
46 return isImagePath(eve::ResourceManager::pathOfKey(normPath));
47 }
48
49 eve::Resource *load(const std::string &key) override {
50 const std::string path = eve::ResourceManager::pathOfKey(key);
51 if (!isImagePath(path)) return nullptr;
52
53 auto *fs = filesystem::Filesystem::create();
54 if (!fs) return nullptr;
55 std::unique_ptr<filesystem::FileData> fd(fs->read(path));
56 if (!fd || fd->getData() == nullptr || fd->getSize() == 0)
57 throw eve::Exception("Could not read image file: %s", path.c_str());
58
59 auto *mod = Image::create();
60 if (!mod) throw eve::Exception("eve::image module is not loaded");
61 return mod->newImageData(fd.get());
62 }
63};
64
65struct Register {
66 Register() {
67 static ImageLoader loader;
69 }
70} g_register;
71
72} // namespace
73} // namespace eve::image
uint32_t c
static std::string pathOfKey(const std::string &key)
The path part of a cache key (everything before the first '?').
Definition Resource.cpp:39
Resource is a game object that is managed by the ResourceManager. It can be loaded from a file or gen...
Definition Resource.h:35
I * query()
Definition Capability.h:77