diff options
-rw-r--r-- | external/CMakeLists.txt | 2 | ||||
-rw-r--r-- | images/floor.json | 8 | ||||
-rw-r--r-- | main/app.hpp | 8 | ||||
-rw-r--r-- | main/loader-impl.cpp | 51 | ||||
-rw-r--r-- | test/json.cpp | 8 |
5 files changed, 50 insertions, 27 deletions
diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index ae40f982..84523241 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -67,12 +67,14 @@ if(FLOORMAT_SUBMODULE-DEPENDENCIES) MAGNUM_WITH_MAGNUMFONTCONVERTER ON MAGNUM_WITH_OBJIMPORTER ON MAGNUM_WITH_OPENGLTESTER ON + MAGNUM_WITH_PNGIMPORTER OFF MAGNUM_WITH_SDL2APPLICATION ON MAGNUM_WITH_SHADERCONVERTER ON MAGNUM_WITH_SHADERS ON MAGNUM_WITH_SHADERTOOLS ON MAGNUM_WITH_TGAIMAGECONVERTER ON MAGNUM_WITH_TGAIMPORTER ON + MAGNUM_WITH_WEBPIMPORTER OFF MAGNUM_WITH_WINDOWLESSWGLAPPLICATION ON MAGNUM_WITH_DDSIMPORTER ON diff --git a/images/floor.json b/images/floor.json index a23e3693..1199a57e 100644 --- a/images/floor.json +++ b/images/floor.json @@ -1,6 +1,6 @@ [ - ["floor-tiles.tga", "44 x 4"], - ["metal1.tga", "2 x 2"], - ["tiles.tga", "8 x 5"], - ["wood2.tga", "1 x 1"] + ["floor-tiles", "44 x 4"], + ["metal1", "2 x 2"], + ["tiles", "8 x 5"], + ["wood2", "1 x 1"] ] 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() diff --git a/test/json.cpp b/test/json.cpp index f0a32c1e..6684f404 100644 --- a/test/json.cpp +++ b/test/json.cpp @@ -13,9 +13,9 @@ namespace floormat { static chunk make_test_chunk() { - auto metal1 = loader.tile_atlas("metal1.tga", {2, 2}), - metal2 = loader.tile_atlas("metal2.tga", {2, 2}), - tiles = loader.tile_atlas("tiles.tga", {8, 5}); + auto metal1 = loader.tile_atlas("metal1", {2, 2}), + metal2 = loader.tile_atlas("metal2", {2, 2}), + tiles = loader.tile_atlas("tiles", {8, 5}); constexpr auto N = TILE_MAX_DIM; chunk c; for (auto& [x, k, pt] : c) { @@ -33,7 +33,7 @@ bool app::test_json() // NOLINT(readability-convert-member-functions-to-static) { const std::filesystem::path output_dir = "../test/."; { - auto atlas = loader.tile_atlas("metal1.tga", {2, 2}); + auto atlas = loader.tile_atlas("metal1", {2, 2}); json_helper::to_json(atlas, output_dir/"atlas.json"); } { |