1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#include "loader.hpp"
#include "tile-atlas.hpp"
#include "compat/assert.hpp"
#include "compat/alloca.hpp"
#include <filesystem>
#include <unordered_map>
#include <utility>
#include <optional>
#include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/Pair.h>
#include <Corrade/Containers/StringStlView.h>
#include <Corrade/PluginManager/PluginManager.h>
#include <Corrade/Utility/Resource.h>
#include <Corrade/Utility/Path.h>
#include <Magnum/ImageView.h>
#include <Magnum/Trade/AbstractImporter.h>
#include <Magnum/Trade/ImageData.h>
#include <Magnum/Trade/AbstractImageConverter.h>
namespace floormat {
struct loader_impl final : loader_
{
std::optional<Utility::Resource> shader_res;
PluginManager::Manager<Trade::AbstractImporter> importer_plugins;
Containers::Pointer<Trade::AbstractImporter> tga_importer =
importer_plugins.loadAndInstantiate("AnyImageImporter");
PluginManager::Manager<Trade::AbstractImageConverter> image_converter_plugins;
Containers::Pointer<Trade::AbstractImageConverter> tga_converter =
image_converter_plugins.loadAndInstantiate("AnyImageConverter");
std::unordered_map<std::string, std::shared_ptr<struct tile_atlas>> atlas_map;
std::string shader(Containers::StringView filename) override;
Trade::ImageData2D tile_texture(Containers::StringView filename) override;
std::shared_ptr<struct tile_atlas> tile_atlas(Containers::StringView filename, Vector2ui size) override;
static void set_application_working_directory();
explicit loader_impl();
~loader_impl() override;
};
std::string loader_impl::shader(Containers::StringView filename)
{
if (!shader_res)
shader_res = std::make_optional<Utility::Resource>("floormat/shaders");
auto ret = shader_res->getString(filename);
if (ret.isEmpty())
ABORT("can't find shader resource '%s'", filename.cbegin());
return ret;
}
std::shared_ptr<tile_atlas> loader_impl::tile_atlas(Containers::StringView name, Vector2ui size)
{
auto it = atlas_map.find(name);
if (it != atlas_map.end())
return it->second;
auto image = tile_texture(name);
auto atlas = std::make_shared<struct tile_atlas>(name, image, size);
atlas_map[name] = atlas;
return atlas;
}
Trade::ImageData2D loader_impl::tile_texture(Containers::StringView filename_)
{
static_assert(IMAGE_PATH[sizeof(IMAGE_PATH)-2] == '/');
char* const filename = (char*)alloca(filename_.size() + sizeof(IMAGE_PATH));
std::memcpy(filename, IMAGE_PATH, sizeof(IMAGE_PATH)-1);
std::strcpy(filename + sizeof(IMAGE_PATH)-1, filename_.cbegin());
if (!tga_importer || !tga_importer->openFile(filename)) {
const auto path = Utility::Path::currentDirectory();
MESSAGE("note: current working directory: '%s'", path->data());
ABORT("can't open tile image '%s'", filename);
}
auto img = tga_importer->image2D(0);
if (!img)
ABORT("can't allocate tile image for '%s'", filename);
auto ret = std::move(*img);
return ret;
}
void loader_::destroy()
{
loader.~loader_();
new (&loader) loader_impl();
}
void loader_impl::set_application_working_directory()
{
static bool once = false;
if (once)
return;
once = true;
const auto location = Utility::Path::executableLocation();
if (!location)
return;
std::filesystem::path path((std::string)*location);
path.replace_filename("..");
std::error_code error;
std::filesystem::current_path(path, error);
if (error.value()) {
WARN("failed to change working directory to '%s' (%s)",
path.string().data(), error.message().data());
}
}
loader_impl::loader_impl()
{
set_application_working_directory();
}
loader_impl::~loader_impl() = default;
static loader_& make_default_loader()
{
static loader_impl loader_singleton{};
return loader_singleton;
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
loader_& loader = make_default_loader();
} // namespace floormat
|