summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/anim.cpp33
-rw-r--r--src/anim.hpp19
2 files changed, 51 insertions, 1 deletions
diff --git a/src/anim.cpp b/src/anim.cpp
new file mode 100644
index 00000000..f4850deb
--- /dev/null
+++ b/src/anim.cpp
@@ -0,0 +1,33 @@
+#include "anim.hpp"
+#include "compat/exception.hpp"
+#include <cmath>
+
+namespace floormat {
+
+Vector2ui anim_scale::scale_to(Vector2ui image_size) const
+{
+ fm_soft_assert(image_size.product() > 0);
+ Vector2ui ret;
+ switch (type)
+ {
+ default:
+ fm_throw("invalid anim_scale_type '{}'"_cf, (unsigned)type);
+ case anim_scale_type::invalid:
+ fm_throw("anim_scale is invalid"_cf);
+ case anim_scale_type::fixed:
+ fm_soft_assert(f.width_or_height > 0);
+ if (f.is_width)
+ ret = { f.width_or_height, (unsigned)std::round((float)f.width_or_height * (float)image_size.y()/(float)image_size.x()) };
+ else
+ ret = { (unsigned)std::round((float)f.width_or_height * (float)image_size.x()/(float)image_size.y()), f.width_or_height };
+ break;
+ case anim_scale_type::ratio:
+ fm_soft_assert(r.f > 0 && r.f <= 1);
+ ret = { (unsigned)std::round(image_size.x() * r.f), (unsigned)std::round(image_size.y() * r.f) };
+ break;
+ }
+ fm_soft_assert(ret.product() > 0);
+ return ret;
+}
+
+} // namespace floormat
diff --git a/src/anim.hpp b/src/anim.hpp
index 674895ed..a8fb63a3 100644
--- a/src/anim.hpp
+++ b/src/anim.hpp
@@ -28,6 +28,22 @@ struct anim_group final
Vector3b offset;
};
+enum class anim_scale_type : std::uint8_t { invalid, ratio, fixed, };
+
+struct anim_scale final
+{
+ struct ratio { float f; };
+ struct fixed { bool is_width; unsigned width_or_height; };
+ struct empty {};
+ union {
+ struct ratio r;
+ struct fixed f;
+ struct empty e = {};
+ };
+ anim_scale_type type = anim_scale_type::invalid;
+ Vector2ui scale_to(Vector2ui image_size) const;
+};
+
struct anim_def final
{
static constexpr int default_fps = 24;
@@ -35,7 +51,8 @@ struct anim_def final
String object_name, anim_name;
std::vector<anim_group> groups;
Vector2ui pixel_size;
- std::size_t nframes = 0, width = 0, height = 0, fps = default_fps, actionframe = 0;
+ anim_scale scale;
+ std::size_t nframes = 0, fps = default_fps, actionframe = 0;
};
} // namespace floormat