summaryrefslogtreecommitdiffhomepage
path: root/main
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-23 08:53:30 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-23 08:53:30 +0200
commit81a9284fbda7bfefa417a408346ddb2abd26a1e2 (patch)
treeb97fbba0f264d0ffb840671e82ab04a0ac62e620 /main
parent8cad01303ff97c929e8b9d25de5a7a5b264e8bb9 (diff)
autodetect tile atlas extensions
Diffstat (limited to 'main')
-rw-r--r--main/app.hpp8
-rw-r--r--main/loader-impl.cpp51
2 files changed, 40 insertions, 19 deletions
diff --git a/main/app.hpp b/main/app.hpp
index 2876dbc8..1ac23dd2 100644
--- a/main/app.hpp
+++ b/main/app.hpp
@@ -99,10 +99,10 @@ private:
GL::MultisampleTexture2D _msaa_color_texture{};
tile_shader _shader;
- tile_atlas_ floor1 = loader.tile_atlas("floor-tiles.tga", {44, 4});
- tile_atlas_ floor2 = loader.tile_atlas("metal1.tga", {2, 2});
- tile_atlas_ wall1 = loader.tile_atlas("wood2.tga", {2, 2});
- tile_atlas_ wall2 = loader.tile_atlas("wood1.tga", {2, 2});
+ tile_atlas_ floor1 = loader.tile_atlas("floor-tiles", {44, 4});
+ tile_atlas_ floor2 = loader.tile_atlas("metal1", {2, 2});
+ tile_atlas_ wall1 = loader.tile_atlas("wood2", {1, 1});
+ tile_atlas_ wall2 = loader.tile_atlas("wood1", {1, 1});
floor_mesh _floor_mesh;
wall_mesh _wall_mesh;
diff --git a/main/loader-impl.cpp b/main/loader-impl.cpp
index 4a489132..5cbe1759 100644
--- a/main/loader-impl.cpp
+++ b/main/loader-impl.cpp
@@ -17,6 +17,10 @@
#include <Magnum/Trade/ImageData.h>
#include <Magnum/Trade/AbstractImageConverter.h>
+#ifdef __GNUG__
+#pragma GCC diagnostic ignored "-Walloca"
+#endif
+
namespace floormat {
struct loader_impl final : loader_
@@ -54,7 +58,10 @@ std::string loader_impl::shader(Containers::StringView filename)
std::shared_ptr<tile_atlas> loader_impl::tile_atlas(Containers::StringView name, Vector2ub size)
{
- auto it = atlas_map.find(name);
+ auto it = std::find_if(atlas_map.begin(), atlas_map.end(), [&](const auto& x) {
+ const auto& [k, v] = x;
+ return Containers::StringView{k} == name;
+ });
if (it != atlas_map.end())
return it->second;
auto image = tile_texture(name);
@@ -67,21 +74,35 @@ Trade::ImageData2D loader_impl::tile_texture(Containers::StringView filename_)
{
static_assert(IMAGE_PATH[sizeof(IMAGE_PATH)-2] == '/');
fm_assert(filename_.size() < 4096);
-
- char* const filename = (char*)alloca(filename_.size() + std::size(IMAGE_PATH));
- std::memcpy(filename, IMAGE_PATH, std::size(IMAGE_PATH)-1);
- std::memcpy(filename + std::size(IMAGE_PATH)-1, filename_.cbegin(), filename_.size());
- filename[std::size(IMAGE_PATH)-1 + filename_.size()] = '\0';
- if (!tga_importer || !tga_importer->openFile(filename)) {
- const auto path = Utility::Path::currentDirectory();
- fm_log("note: current working directory: '%s'", path->data());
- fm_abort("can't open tile image '%s'", filename);
+ fm_assert(filename_.find('\\') == filename_.end());
+ fm_assert(tga_importer);
+ constexpr std::size_t max_extension_length = 16;
+
+ char* const filename = (char*)alloca(filename_.size() + std::size(IMAGE_PATH) + max_extension_length);
+ const std::size_t len = fm_begin(
+ std::size_t off = std::size(IMAGE_PATH)-1;
+ std::memcpy(filename, IMAGE_PATH, off);
+ std::memcpy(filename + off, filename_.cbegin(), filename_.size());
+ return off + filename_.size();
+ );
+
+ for (const auto& extension : std::initializer_list<Containers::StringView>{ ".tga", ".png", ".webp", })
+ {
+ std::memcpy(filename + len, extension.data(), extension.size());
+ filename[len + extension.size()] = '\0';
+ if (tga_importer->openFile(filename))
+ {
+ auto img = tga_importer->image2D(0);
+ if (!img)
+ fm_abort("can't allocate tile image for '%s'", filename);
+ auto ret = std::move(*img);
+ return ret;
+ }
+ Debug{} << "failed to open" << filename << extension;
}
- auto img = tga_importer->image2D(0);
- if (!img)
- fm_abort("can't allocate tile image for '%s'", filename);
- auto ret = std::move(*img);
- return ret;
+ const auto path = Utility::Path::currentDirectory();
+ fm_log("fatal: can't open tile image '%s' (cwd '%s')", filename, path ? path->data() : "(null)");
+ std::abort();
}
void loader_::destroy()