From d481edb3619e251285c238c05f47a121ecd96df7 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 12 Nov 2022 13:43:34 +0100 Subject: cmake: add targets for loader, serialize, draw --- loader/CMakeLists.txt | 16 ++++ loader/impl.cpp | 203 +++++++++++++++++++++++++++++++++++++++++++++++++ loader/loader-impl.cpp | 203 ------------------------------------------------- loader/loader.cpp | 5 ++ loader/loader.hpp | 36 +++++++++ loader/precomp.hpp | 7 ++ 6 files changed, 267 insertions(+), 203 deletions(-) create mode 100644 loader/CMakeLists.txt create mode 100644 loader/impl.cpp delete mode 100644 loader/loader-impl.cpp create mode 100644 loader/loader.cpp create mode 100644 loader/loader.hpp create mode 100644 loader/precomp.hpp (limited to 'loader') diff --git a/loader/CMakeLists.txt b/loader/CMakeLists.txt new file mode 100644 index 00000000..9c0b6ee7 --- /dev/null +++ b/loader/CMakeLists.txt @@ -0,0 +1,16 @@ +set(self floormat-loader) +file(GLOB sources "*.cpp" CONFIGURE_ARGS) +add_library(${self} STATIC "${sources}") +target_link_libraries( + ${self} PUBLIC + floormat-serialize + floormat + Magnum::Magnum + Magnum::Trade +) +if(WIN32) + target_link_libraries(${self} PUBLIC ntdll) +endif() +if(FLOORMAT_PRECOMPILED-HEADERS) + target_precompile_headers(${self} PRIVATE precomp.hpp) +endif() diff --git a/loader/impl.cpp b/loader/impl.cpp new file mode 100644 index 00000000..99be0be6 --- /dev/null +++ b/loader/impl.cpp @@ -0,0 +1,203 @@ +#include "impl.hpp" +#include "loader/loader.hpp" +#include "src/tile-atlas.hpp" +#include "compat/assert.hpp" +#include "compat/alloca.hpp" +#include "src/anim-atlas.hpp" +#include "src/emplacer.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __GNUG__ +#pragma GCC diagnostic ignored "-Walloca" +#endif + +namespace floormat { + +struct loader_impl final : loader_ +{ + Optional shader_res; + PluginManager::Manager importer_plugins; + Containers::Pointer image_importer = + importer_plugins.loadAndInstantiate("StbImageImporter"); + + Containers::Pointer tga_importer = + importer_plugins.loadAndInstantiate("TgaImporter"); + + std::unordered_map> tile_atlas_map; + std::unordered_map> anim_atlas_map; + std::vector anim_atlases; + + StringView shader(StringView filename) override; + template Trade::ImageData2D texture(const char(&prefix)[N], StringView filename); + std::shared_ptr tile_atlas(StringView filename, Vector2ub size) override; + ArrayView anim_atlas_list() override; + std::shared_ptr anim_atlas(StringView name) override; + + void get_anim_atlas_list(); + + static void set_application_working_directory(); + + explicit loader_impl(); + ~loader_impl() override; +}; + +StringView loader_impl::shader(StringView filename) +{ + if (!shader_res) + shader_res = Optional(InPlaceInit, "floormat/shaders"); + auto ret = shader_res->getString(filename); + if (ret.isEmpty()) + fm_abort("can't find shader resource '%s'", filename.cbegin()); + return ret; +} + +std::shared_ptr loader_impl::tile_atlas(StringView name, Vector2ub size) +{ + const emplacer e{[&] { return std::make_shared(name, texture(FM_IMAGE_PATH, name), size); }}; + auto atlas = tile_atlas_map.try_emplace(name, e).first->second; + return atlas; +} + +template +fm_noinline +Trade::ImageData2D loader_impl::texture(const char(&prefix)[N], StringView filename_) +{ + if constexpr(N > 1) + fm_assert(prefix[N-2] == '/'); + fm_assert(filename_.size() < 4096); + fm_assert(filename_.find('\\') == filename_.end()); + fm_assert(filename_.find('\0') == filename_.end()); + fm_assert(tga_importer); + constexpr std::size_t max_extension_length = 16; + + char* const filename = (char*)alloca(filename_.size() + N + max_extension_length); + const std::size_t len = fm_begin( + std::size_t off = N-1; + if constexpr(N > 1) + std::memcpy(filename, prefix, off); + std::memcpy(filename + off, filename_.cbegin(), filename_.size()); + return off + filename_.size(); + ); + + for (const auto& extension : std::initializer_list{ ".tga", ".png", ".webp", }) + { + std::memcpy(filename + len, extension.data(), extension.size()); + filename[len + extension.size()] = '\0'; + auto& importer = extension == StringView(".tga") ? tga_importer : image_importer; + if (Path::exists(filename) && importer->openFile(filename)) + { + auto img = importer->image2D(0); + if (!img) + fm_abort("can't allocate image for '%s'", filename); + auto ret = std::move(*img); + return ret; + } + } + const auto path = Path::currentDirectory(); + filename[len] = '\0'; + fm_abort("can't open image '%s' (cwd '%s')", filename, path ? path->data() : "(null)"); +} + +ArrayView loader_impl::anim_atlas_list() +{ + if (anim_atlases.empty()) + get_anim_atlas_list(); + return anim_atlases; +} + +std::shared_ptr loader_impl::anim_atlas(StringView name) +{ + if (auto it = anim_atlas_map.find(name); it != anim_atlas_map.end()) + return it->second; + else + { + const auto path = Path::join(FM_ANIM_PATH, Path::splitExtension(name).first()); + auto anim_info = loader_detail::deserialize_anim(path + ".json"); + auto tex = texture("", path); + + fm_assert(!anim_info.anim_name.isEmpty() && !anim_info.object_name.isEmpty()); + fm_assert(anim_info.pixel_size.product() > 0); + fm_assert(!anim_info.groups.empty()); + fm_assert(anim_info.nframes > 0); + fm_assert(anim_info.nframes == 1 || anim_info.fps > 0); + + auto atlas = std::make_shared(path, tex, std::move(anim_info)); + return anim_atlas_map[atlas->name()] = atlas; + } +} + +void loader_impl::get_anim_atlas_list() +{ + anim_atlases.clear(); + anim_atlases.reserve(64); + using f = Path::ListFlag; + constexpr auto flags = f::SkipDirectories | f::SkipDotAndDotDot | f::SkipSpecial | f::SortAscending; + if (const auto list = Path::list(FM_ANIM_PATH, flags); list) + for (StringView str : *list) + if (str.hasSuffix(".json")) + anim_atlases.emplace_back(str.exceptSuffix(std::size(".json")-1)); +} + +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; + if (const auto loc = Path::executableLocation()) + { + StringView path = *loc; + path = Path::split(path).first(); + path = Path::split(path).first(); +#ifdef _WIN32 + String p = "\\\\?\\" + path; + for (char& c : p) + if (c == '/') + c = '\\'; + path = p; +#endif + loader_detail::chdir(path); + } + else + fm_warn("can't find install prefix!"); +} + +loader_impl::loader_impl() +{ + loader_detail::system_init(); + set_application_working_directory(); +} + +loader_impl::~loader_impl() = default; + +loader_& loader_::default_loader() noexcept +{ + static loader_impl loader_singleton{}; + return loader_singleton; +} + +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +loader_& loader = loader_::default_loader(); + +} // namespace floormat diff --git a/loader/loader-impl.cpp b/loader/loader-impl.cpp deleted file mode 100644 index 955ccf03..00000000 --- a/loader/loader-impl.cpp +++ /dev/null @@ -1,203 +0,0 @@ -#include "impl.hpp" -#include "src/loader.hpp" -#include "src/tile-atlas.hpp" -#include "compat/assert.hpp" -#include "compat/alloca.hpp" -#include "src/anim-atlas.hpp" -#include "src/emplacer.hpp" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef __GNUG__ -#pragma GCC diagnostic ignored "-Walloca" -#endif - -namespace floormat { - -struct loader_impl final : loader_ -{ - Optional shader_res; - PluginManager::Manager importer_plugins; - Containers::Pointer image_importer = - importer_plugins.loadAndInstantiate("StbImageImporter"); - - Containers::Pointer tga_importer = - importer_plugins.loadAndInstantiate("TgaImporter"); - - std::unordered_map> tile_atlas_map; - std::unordered_map> anim_atlas_map; - std::vector anim_atlases; - - StringView shader(StringView filename) override; - template Trade::ImageData2D texture(const char(&prefix)[N], StringView filename); - std::shared_ptr tile_atlas(StringView filename, Vector2ub size) override; - ArrayView anim_atlas_list() override; - std::shared_ptr anim_atlas(StringView name) override; - - void get_anim_atlas_list(); - - static void set_application_working_directory(); - - explicit loader_impl(); - ~loader_impl() override; -}; - -StringView loader_impl::shader(StringView filename) -{ - if (!shader_res) - shader_res = Optional(InPlaceInit, "floormat/shaders"); - auto ret = shader_res->getString(filename); - if (ret.isEmpty()) - fm_abort("can't find shader resource '%s'", filename.cbegin()); - return ret; -} - -std::shared_ptr loader_impl::tile_atlas(StringView name, Vector2ub size) -{ - const emplacer e{[&] { return std::make_shared(name, texture(FM_IMAGE_PATH, name), size); }}; - auto atlas = tile_atlas_map.try_emplace(name, e).first->second; - return atlas; -} - -template -fm_noinline -Trade::ImageData2D loader_impl::texture(const char(&prefix)[N], StringView filename_) -{ - if constexpr(N > 1) - fm_assert(prefix[N-2] == '/'); - fm_assert(filename_.size() < 4096); - fm_assert(filename_.find('\\') == filename_.end()); - fm_assert(filename_.find('\0') == filename_.end()); - fm_assert(tga_importer); - constexpr std::size_t max_extension_length = 16; - - char* const filename = (char*)alloca(filename_.size() + N + max_extension_length); - const std::size_t len = fm_begin( - std::size_t off = N-1; - if constexpr(N > 1) - std::memcpy(filename, prefix, off); - std::memcpy(filename + off, filename_.cbegin(), filename_.size()); - return off + filename_.size(); - ); - - for (const auto& extension : std::initializer_list{ ".tga", ".png", ".webp", }) - { - std::memcpy(filename + len, extension.data(), extension.size()); - filename[len + extension.size()] = '\0'; - auto& importer = extension == StringView(".tga") ? tga_importer : image_importer; - if (Path::exists(filename) && importer->openFile(filename)) - { - auto img = importer->image2D(0); - if (!img) - fm_abort("can't allocate image for '%s'", filename); - auto ret = std::move(*img); - return ret; - } - } - const auto path = Path::currentDirectory(); - filename[len] = '\0'; - fm_abort("can't open image '%s' (cwd '%s')", filename, path ? path->data() : "(null)"); -} - -ArrayView loader_impl::anim_atlas_list() -{ - if (anim_atlases.empty()) - get_anim_atlas_list(); - return anim_atlases; -} - -std::shared_ptr loader_impl::anim_atlas(StringView name) -{ - if (auto it = anim_atlas_map.find(name); it != anim_atlas_map.end()) - return it->second; - else - { - const auto path = Path::join(FM_ANIM_PATH, Path::splitExtension(name).first()); - auto anim_info = loader_detail::deserialize_anim(path + ".json"); - auto tex = texture("", path); - - fm_assert(!anim_info.anim_name.isEmpty() && !anim_info.object_name.isEmpty()); - fm_assert(anim_info.pixel_size.product() > 0); - fm_assert(!anim_info.groups.empty()); - fm_assert(anim_info.nframes > 0); - fm_assert(anim_info.nframes == 1 || anim_info.fps > 0); - - auto atlas = std::make_shared(path, tex, std::move(anim_info)); - return anim_atlas_map[atlas->name()] = atlas; - } -} - -void loader_impl::get_anim_atlas_list() -{ - anim_atlases.clear(); - anim_atlases.reserve(64); - using f = Path::ListFlag; - constexpr auto flags = f::SkipDirectories | f::SkipDotAndDotDot | f::SkipSpecial | f::SortAscending; - if (const auto list = Path::list(FM_ANIM_PATH, flags); list) - for (StringView str : *list) - if (str.hasSuffix(".json")) - anim_atlases.emplace_back(str.exceptSuffix(std::size(".json")-1)); -} - -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; - if (const auto loc = Path::executableLocation()) - { - StringView path = *loc; - path = Path::split(path).first(); - path = Path::split(path).first(); -#ifdef _WIN32 - String p = "\\\\?\\" + path; - for (char& c : p) - if (c == '/') - c = '\\'; - path = p; -#endif - loader_detail::chdir(path); - } - else - fm_warn("can't find install prefix!"); -} - -loader_impl::loader_impl() -{ - loader_detail::system_init(); - set_application_working_directory(); -} - -loader_impl::~loader_impl() = default; - -loader_& loader_::default_loader() noexcept -{ - static loader_impl loader_singleton{}; - return loader_singleton; -} - -// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -loader_& loader = loader_::default_loader(); - -} // namespace floormat diff --git a/loader/loader.cpp b/loader/loader.cpp new file mode 100644 index 00000000..83c3eed2 --- /dev/null +++ b/loader/loader.cpp @@ -0,0 +1,5 @@ +#include "loader.hpp" +namespace floormat { +loader_::loader_() = default; +loader_::~loader_() = default; +} // namespace floormat diff --git a/loader/loader.hpp b/loader/loader.hpp new file mode 100644 index 00000000..32158675 --- /dev/null +++ b/loader/loader.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include +#include + +#define FM_IMAGE_PATH "share/floormat/images/" +#define FM_ANIM_PATH "share/floormat/anim/" + +namespace floormat { + +struct tile_atlas; +struct anim_atlas; + +struct loader_ +{ + virtual StringView shader(StringView filename) = 0; + virtual std::shared_ptr tile_atlas(StringView filename, Vector2ub size) = 0; + virtual ArrayView anim_atlas_list() = 0; + virtual std::shared_ptr anim_atlas(StringView name) = 0; + static void destroy(); + static loader_& default_loader() noexcept; + + loader_(const loader_&) = delete; + loader_& operator=(const loader_&) = delete; + + virtual ~loader_(); + +protected: + loader_(); +}; + +extern loader_& loader; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + +} // namespace floormat diff --git a/loader/precomp.hpp b/loader/precomp.hpp new file mode 100644 index 00000000..4ebd56b2 --- /dev/null +++ b/loader/precomp.hpp @@ -0,0 +1,7 @@ +#pragma once +#include "../src/precomp.hpp" +#include +#include +#include +#include +#include -- cgit v1.2.3