diff options
author | Hop311 <Hop3114@gmail.com> | 2024-03-28 23:12:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-28 23:12:35 +0100 |
commit | 109d31f147512c8d51f35f9773cd3c6bb1b8b994 (patch) | |
tree | 4bf3a3318880609d6fdf2e96944bf84cdd2b8cdb /src | |
parent | 020ada6c8f0f1bf4486fd9e76ec29673044794d6 (diff) | |
parent | 79c3462396fd4e1dcb7d1c9a2ef7bad741afa3e3 (diff) |
Merge pull request #153 from OpenVicProject/gfx-object
GFX objectTypes loading framework + Actor loading
Diffstat (limited to 'src')
-rw-r--r-- | src/openvic-simulation/interface/GFXObject.cpp | 144 | ||||
-rw-r--r-- | src/openvic-simulation/interface/GFXObject.hpp | 81 | ||||
-rw-r--r-- | src/openvic-simulation/interface/GFXSprite.cpp (renamed from src/openvic-simulation/interface/GFX.cpp) | 2 | ||||
-rw-r--r-- | src/openvic-simulation/interface/GFXSprite.hpp (renamed from src/openvic-simulation/interface/GFX.hpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/interface/GUI.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/interface/UI.cpp | 14 | ||||
-rw-r--r-- | src/openvic-simulation/interface/UI.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/FixedPoint.hpp | 12 |
8 files changed, 253 insertions, 4 deletions
diff --git a/src/openvic-simulation/interface/GFXObject.cpp b/src/openvic-simulation/interface/GFXObject.cpp new file mode 100644 index 0000000..d873db1 --- /dev/null +++ b/src/openvic-simulation/interface/GFXObject.cpp @@ -0,0 +1,144 @@ +#include "GFXObject.hpp" + +using namespace OpenVic; +using namespace OpenVic::GFX; +using namespace OpenVic::NodeTools; + +node_callback_t Object::expect_objects(length_callback_t length_callback, callback_t<std::unique_ptr<Object>&&> callback) { + return expect_dictionary_keys_and_length( + length_callback, + + "EMFXActorType", ZERO_OR_MORE, _expect_instance<Object, Actor>(callback), + + /* arrows.gfx */ + "arrowType", ZERO_OR_MORE, expect_dictionary_keys( + "name", ONE_EXACTLY, success_callback, + "size", ONE_EXACTLY, success_callback, + "textureFile", ONE_EXACTLY, success_callback, + "bodytexture", ONE_EXACTLY, success_callback, + "color", ONE_EXACTLY, success_callback, + "colortwo", ONE_EXACTLY, success_callback, + "endAt", ONE_EXACTLY, success_callback, + "height", ONE_EXACTLY, success_callback, + "type", ONE_EXACTLY, success_callback, + "heading", ONE_EXACTLY, success_callback, + "effect", ONE_EXACTLY, success_callback + ), + + /* battlearrow.gfx */ + "battlearrow", ZERO_OR_MORE, expect_dictionary_keys( + "name", ONE_EXACTLY, success_callback, + "textureFile", ONE_EXACTLY, success_callback, + "textureFile1", ONE_EXACTLY, success_callback, + "start", ONE_EXACTLY, success_callback, + "stop", ONE_EXACTLY, success_callback, + "x", ONE_EXACTLY, success_callback, + "y", ONE_EXACTLY, success_callback, + "font", ONE_EXACTLY, success_callback, + "scale", ONE_EXACTLY, success_callback, + "nofade", ZERO_OR_ONE, success_callback, + "textureloop", ZERO_OR_ONE, success_callback + ), + "mapinfo", ZERO_OR_MORE, expect_dictionary_keys( + "name", ONE_EXACTLY, success_callback, + "textureFile", ZERO_OR_ONE, success_callback, + "scale", ZERO_OR_ONE, success_callback + ), + + /* mapitems.gfx */ + "projectionType", ZERO_OR_MORE, success_callback, + "progressbar3dType", ZERO_OR_MORE, expect_dictionary_keys( + "name", ONE_EXACTLY, success_callback, + "color", ONE_EXACTLY, success_callback, + "colortwo", ONE_EXACTLY, success_callback, + "size", ONE_EXACTLY, success_callback, + "effectFile", ONE_EXACTLY, success_callback + ), + "billboardType", ZERO_OR_MORE, expect_dictionary_keys<StringMapCaseInsensitive>( + "name", ONE_EXACTLY, success_callback, + "texturefile", ONE_EXACTLY, success_callback, + "noOfFrames", ZERO_OR_ONE, success_callback, + "scale", ONE_EXACTLY, success_callback, + "font_size", ZERO_OR_ONE, success_callback, + "offset2", ZERO_OR_ONE, success_callback, + "font", ZERO_OR_ONE, success_callback + ), + + "unitstatsBillboardType", ZERO_OR_MORE, expect_dictionary_keys( + "name", ONE_EXACTLY, success_callback, + "textureFile", ONE_EXACTLY, success_callback, + "mask", ONE_EXACTLY, success_callback, + "effectFile", ONE_EXACTLY, success_callback, + "scale", ONE_EXACTLY, success_callback, + "noOfFrames", ONE_EXACTLY, success_callback, + "font_size", ONE_EXACTLY, success_callback, + "font", ONE_EXACTLY, success_callback + ), + + /* core.gfx */ + "animatedmaptext", ZERO_OR_MORE, expect_dictionary_keys( + "name", ONE_EXACTLY, success_callback, + "speed", ONE_EXACTLY, success_callback, + "position", ZERO_OR_ONE, success_callback, + "scale", ZERO_OR_ONE, success_callback, + "textblock", ONE_EXACTLY, expect_dictionary_keys( + "text", ONE_EXACTLY, success_callback, + "color", ONE_EXACTLY, success_callback, + "font", ONE_EXACTLY, success_callback, + "position", ONE_EXACTLY, success_callback, + "size", ONE_EXACTLY, success_callback, + "format", ONE_EXACTLY, success_callback + ) + ), + "flagType", ZERO_OR_MORE, expect_dictionary_keys( + "name", ONE_EXACTLY, success_callback, + "size", ONE_EXACTLY, success_callback + ), + "provinceType", ZERO_OR_ONE, success_callback, + "provinceWaterType", ZERO_OR_ONE, success_callback, + "mapTextType", ZERO_OR_MORE, success_callback, + "meshType", ZERO_OR_MORE, success_callback + ); +} + +Actor::Attachment::Attachment() : node {}, attach_id { 0 } {} + +bool Actor::Attachment::_fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) { + bool ret = Named::_fill_key_map(key_map); + ret &= add_key_map_entries(key_map, + "node", ONE_EXACTLY, expect_string(assign_variable_callback_string(node)), + "attachId", ONE_EXACTLY, expect_uint(assign_variable_callback(attach_id)) + ); + return ret; +} + +Actor::Animation::Animation() : file {}, default_time { 0 } {} + +bool Actor::Animation::_fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) { + bool ret = Named::_fill_key_map(key_map); + ret &= add_key_map_entries(key_map, + "file", ONE_EXACTLY, expect_string(assign_variable_callback_string(file)), + "defaultAnimationTime", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(default_time)) + ); + return ret; +} + +Actor::Actor() : model_file {}, idle_animation_file {}, move_animation_file {}, attack_animation_file {}, scale { 1 } {} + +bool Actor::_fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) { + bool ret = Object::_fill_key_map(key_map); + ret &= add_key_map_entries(key_map, + "actorfile", ONE_EXACTLY, expect_string(assign_variable_callback_string(model_file)), + "idle", ZERO_OR_ONE, expect_string(assign_variable_callback_string(idle_animation_file)), + "move", ZERO_OR_ONE, expect_string(assign_variable_callback_string(move_animation_file)), + "attack", ZERO_OR_ONE, expect_string(assign_variable_callback_string(attack_animation_file)), + "scale", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(scale)), + "attach", ZERO_OR_MORE, Attachment::_expect_value<Attachment>([this](Attachment&& attachment) -> bool { + return attachments.add_item(std::move(attachment)); + }), + "animation", ZERO_OR_MORE, Animation::_expect_value<Animation>([this](Animation&& animation) -> bool { + return animations.add_item(std::move(animation)); + }) + ); + return ret; +} diff --git a/src/openvic-simulation/interface/GFXObject.hpp b/src/openvic-simulation/interface/GFXObject.hpp new file mode 100644 index 0000000..840e4a9 --- /dev/null +++ b/src/openvic-simulation/interface/GFXObject.hpp @@ -0,0 +1,81 @@ +#pragma once + +#include "openvic-simulation/interface/LoadBase.hpp" + +namespace OpenVic::GFX { + + class Object : public Named<> { + protected: + Object() = default; + + public: + Object(Object&&) = default; + virtual ~Object() = default; + + OV_DETAIL_GET_BASE_TYPE(Object) + OV_DETAIL_GET_TYPE + + static NodeTools::node_callback_t expect_objects( + NodeTools::length_callback_t length_callback, NodeTools::callback_t<std::unique_ptr<Object>&&> callback + ); + }; + + class Actor final : public Object { + friend std::unique_ptr<Actor> std::make_unique<Actor>(); + + public: + class Attachment : public Named<> { + friend class LoadBase; + + private: + std::string PROPERTY(node); + int32_t PROPERTY(attach_id); + + protected: + Attachment(); + + bool _fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) override; + + public: + Attachment(Attachment&&) = default; + virtual ~Attachment() = default; + }; + + class Animation : public Named<> { + friend class LoadBase; + + std::string PROPERTY(file); + fixed_point_t PROPERTY(default_time); + + protected: + Animation(); + + bool _fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) override; + + public: + Animation(Animation&&) = default; + virtual ~Animation() = default; + }; + + private: + std::string PROPERTY(model_file); + std::string PROPERTY(idle_animation_file); + std::string PROPERTY(move_animation_file); + std::string PROPERTY(attack_animation_file); + fixed_point_t PROPERTY(scale); + + NamedRegistry<Attachment> IDENTIFIER_REGISTRY(attachment); + NamedRegistry<Animation> IDENTIFIER_REGISTRY(animation); + + protected: + Actor(); + + bool _fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) override; + + public: + Actor(Actor&&) = default; + virtual ~Actor() = default; + + OV_DETAIL_GET_TYPE + }; +} diff --git a/src/openvic-simulation/interface/GFX.cpp b/src/openvic-simulation/interface/GFXSprite.cpp index 06d4cd3..992a1ff 100644 --- a/src/openvic-simulation/interface/GFX.cpp +++ b/src/openvic-simulation/interface/GFXSprite.cpp @@ -1,4 +1,4 @@ -#include "GFX.hpp" +#include "GFXSprite.hpp" using namespace OpenVic; using namespace OpenVic::GFX; diff --git a/src/openvic-simulation/interface/GFX.hpp b/src/openvic-simulation/interface/GFXSprite.hpp index 49691c1..49691c1 100644 --- a/src/openvic-simulation/interface/GFX.hpp +++ b/src/openvic-simulation/interface/GFXSprite.hpp diff --git a/src/openvic-simulation/interface/GUI.hpp b/src/openvic-simulation/interface/GUI.hpp index d839188..74afbd1 100644 --- a/src/openvic-simulation/interface/GUI.hpp +++ b/src/openvic-simulation/interface/GUI.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic-simulation/interface/GFX.hpp" +#include "openvic-simulation/interface/GFXSprite.hpp" namespace OpenVic { class UIManager; diff --git a/src/openvic-simulation/interface/UI.cpp b/src/openvic-simulation/interface/UI.cpp index 2646bb7..479948d 100644 --- a/src/openvic-simulation/interface/UI.cpp +++ b/src/openvic-simulation/interface/UI.cpp @@ -83,9 +83,21 @@ bool UIManager::load_gfx_file(ast::NodeCPtr root) { return sprites.add_item(std::move(sprite), duplicate_warning_callback); } ), + "bitmapfonts", ZERO_OR_ONE, _load_fonts("bitmapfont"), "fonts", ZERO_OR_ONE, _load_fonts("font"), - "objectTypes", ZERO_OR_ONE, success_callback, + + "objectTypes", ZERO_OR_ONE, Object::expect_objects( + NodeTools::reserve_length_callback(objects), + [this](std::unique_ptr<Object>&& object) -> bool { + /* There are various models with the same name but slight differences, e.g. Prussian and German variants + * of PrussianGCCavalry (the latter added in a spritepack). Currently we default to using the first loaded + * model of each name, but we may want to switch to using the last loaded or allow multiple models per name + * (e.g. by grouping them per gfx file). */ + return objects.add_item(std::move(object), duplicate_warning_callback); + } + ), + "lightTypes", ZERO_OR_ONE, success_callback )(root); } diff --git a/src/openvic-simulation/interface/UI.hpp b/src/openvic-simulation/interface/UI.hpp index c8ffa98..ada540a 100644 --- a/src/openvic-simulation/interface/UI.hpp +++ b/src/openvic-simulation/interface/UI.hpp @@ -1,5 +1,6 @@ #pragma once +#include "openvic-simulation/interface/GFXObject.hpp" #include "openvic-simulation/interface/GUI.hpp" namespace OpenVic { @@ -8,6 +9,7 @@ namespace OpenVic { NamedInstanceRegistry<GFX::Sprite> IDENTIFIER_REGISTRY(sprite); NamedInstanceRegistry<GUI::Scene, UIManager const&> IDENTIFIER_REGISTRY(scene); IdentifierRegistry<GFX::Font> IDENTIFIER_REGISTRY(font); + NamedInstanceRegistry<GFX::Object> IDENTIFIER_REGISTRY(object); bool _load_font(ast::NodeCPtr node); NodeTools::NodeCallback auto _load_fonts(std::string_view font_key); diff --git a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp b/src/openvic-simulation/types/fixed_point/FixedPoint.hpp index 9ad7966..84d2b70 100644 --- a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp +++ b/src/openvic-simulation/types/fixed_point/FixedPoint.hpp @@ -341,7 +341,7 @@ namespace OpenVic { } // Deterministic - static constexpr fixed_point_t parse(char const* str, char const* const end, bool* successful = nullptr) { + static constexpr fixed_point_t parse(char const* str, char const* end, bool* successful = nullptr) { if (successful != nullptr) { *successful = false; } @@ -360,6 +360,16 @@ namespace OpenVic { } } + { + const char last_char = *(end - 1); + if (last_char == 'f' || last_char == 'F') { + --end; + if (str == end) { + return _0(); + } + } + } + char const* dot_pointer = str; while (*dot_pointer != '.' && ++dot_pointer != end) {} |