mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 00:11:55 +07:00
flipbook container impl
This commit is contained in:
@@ -8,9 +8,24 @@
|
||||
|
||||
#include "_high.hpp"
|
||||
|
||||
#include "assets/texture_asset.hpp"
|
||||
#include "assets/material_asset.hpp"
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
class flipbook final {
|
||||
public:
|
||||
struct frame {
|
||||
v2f pivot;
|
||||
b2f texrect;
|
||||
texture_asset::ptr texture;
|
||||
};
|
||||
|
||||
struct sequence {
|
||||
f32 fps{0.f};
|
||||
str_hash name;
|
||||
vector<u32> frames;
|
||||
};
|
||||
public:
|
||||
flipbook();
|
||||
~flipbook() noexcept;
|
||||
@@ -26,9 +41,34 @@ namespace e2d
|
||||
|
||||
flipbook& assign(flipbook&& other) noexcept;
|
||||
flipbook& assign(const flipbook& other);
|
||||
|
||||
flipbook& set_frames(vector<frame>&& frames) noexcept;
|
||||
flipbook& set_frames(const vector<frame>& frames);
|
||||
const vector<frame>& frames() const noexcept;
|
||||
const frame* find_frame(u32 index) const noexcept;
|
||||
|
||||
flipbook& set_sequences(vector<sequence>&& sequences) noexcept;
|
||||
flipbook& set_sequences(const vector<sequence>& sequences);
|
||||
const vector<sequence>& sequences() const noexcept;
|
||||
const sequence* find_sequence(str_hash name) const noexcept;
|
||||
|
||||
flipbook& set_material(const material_asset::ptr& material) noexcept;
|
||||
const material_asset::ptr& material() const noexcept;
|
||||
private:
|
||||
vector<frame> frames_;
|
||||
vector<sequence> sequences_;
|
||||
material_asset::ptr material_;
|
||||
};
|
||||
|
||||
void swap(flipbook& l, flipbook& r) noexcept;
|
||||
bool operator==(const flipbook& l, const flipbook& r) noexcept;
|
||||
bool operator!=(const flipbook& l, const flipbook& r) noexcept;
|
||||
|
||||
void swap(flipbook::frame& l, flipbook::frame& r) noexcept;
|
||||
bool operator==(const flipbook::frame& l, const flipbook::frame& r) noexcept;
|
||||
bool operator!=(const flipbook::frame& l, const flipbook::frame& r) noexcept;
|
||||
|
||||
void swap(flipbook::sequence& l, flipbook::sequence& r) noexcept;
|
||||
bool operator==(const flipbook::sequence& l, const flipbook::sequence& r) noexcept;
|
||||
bool operator!=(const flipbook::sequence& l, const flipbook::sequence& r) noexcept;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
#include "_high.hpp"
|
||||
|
||||
#include "assets/atlas_asset.hpp"
|
||||
#include "assets/texture_asset.hpp"
|
||||
#include "assets/material_asset.hpp"
|
||||
|
||||
@@ -33,7 +32,6 @@ namespace e2d
|
||||
|
||||
sprite& set_pivot(const v2f& pivot) noexcept;
|
||||
sprite& set_texrect(const b2f& texrect) noexcept;
|
||||
sprite& set_region(const atlas::region& region) noexcept;
|
||||
sprite& set_texture(const texture_asset::ptr& texture) noexcept;
|
||||
sprite& set_material(const material_asset::ptr& material) noexcept;
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <enduro2d/high/assets/sprite_asset.hpp>
|
||||
|
||||
#include "json_asset.hpp"
|
||||
#include <enduro2d/high/assets/atlas_asset.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -120,7 +121,8 @@ namespace
|
||||
}
|
||||
|
||||
sprite content;
|
||||
content.set_region(*region);
|
||||
content.set_pivot(region->pivot);
|
||||
content.set_texrect(region->texrect);
|
||||
content.set_texture(texture);
|
||||
content.set_material(material);
|
||||
return content;
|
||||
|
||||
@@ -32,7 +32,9 @@ namespace e2d
|
||||
|
||||
void flipbook::swap(flipbook& other) noexcept {
|
||||
using std::swap;
|
||||
E2D_UNUSED(other);
|
||||
swap(frames_, other.frames_);
|
||||
swap(sequences_, other.sequences_);
|
||||
swap(material_, other.material_);
|
||||
}
|
||||
|
||||
flipbook& flipbook::assign(flipbook&& other) noexcept {
|
||||
@@ -46,10 +48,70 @@ namespace e2d
|
||||
flipbook& flipbook::assign(const flipbook& other) {
|
||||
if ( this != &other ) {
|
||||
flipbook s;
|
||||
s.frames_ = other.frames_;
|
||||
s.sequences_ = other.sequences_;
|
||||
s.material_ = other.material_;
|
||||
swap(s);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
flipbook& flipbook::set_frames(vector<frame>&& frames) noexcept {
|
||||
frames_ = std::move(frames);
|
||||
return *this;
|
||||
}
|
||||
|
||||
flipbook& flipbook::set_frames(const vector<frame>& frames) {
|
||||
return set_frames(vector<frame>(frames));
|
||||
}
|
||||
|
||||
const vector<flipbook::frame>& flipbook::frames() const noexcept {
|
||||
return frames_;
|
||||
}
|
||||
|
||||
const flipbook::frame* flipbook::find_frame(u32 index) const noexcept {
|
||||
return index < frames_.size()
|
||||
? &frames_[index]
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
flipbook& flipbook::set_sequences(vector<sequence>&& sequences) noexcept {
|
||||
sequences_ = std::move(sequences);
|
||||
std::sort(
|
||||
sequences_.begin(), sequences_.end(),
|
||||
[](const flipbook::sequence& l, const flipbook::sequence& r) noexcept {
|
||||
return l.name < r.name;
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
flipbook& flipbook::set_sequences(const vector<sequence>& sequences) {
|
||||
return set_sequences(vector<sequence>(sequences));
|
||||
}
|
||||
|
||||
const vector<flipbook::sequence>& flipbook::sequences() const noexcept {
|
||||
return sequences_;
|
||||
}
|
||||
|
||||
const flipbook::sequence* flipbook::find_sequence(str_hash name) const noexcept {
|
||||
const auto iter = std::lower_bound(
|
||||
sequences_.begin(), sequences_.end(), name,
|
||||
[](const flipbook::sequence& l, str_hash r) noexcept {
|
||||
return l.name < r;
|
||||
});
|
||||
return iter != sequences_.end() && iter->name == name
|
||||
? &*iter
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
flipbook& flipbook::set_material(const material_asset::ptr& material) noexcept {
|
||||
material_ = material;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const material_asset::ptr& flipbook::material() const noexcept {
|
||||
return material_;
|
||||
}
|
||||
}
|
||||
|
||||
namespace e2d
|
||||
@@ -59,11 +121,54 @@ namespace e2d
|
||||
}
|
||||
|
||||
bool operator==(const flipbook& l, const flipbook& r) noexcept {
|
||||
E2D_UNUSED(l, r);
|
||||
return true;
|
||||
if ( l.frames().size() != r.frames().size() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( l.sequences().size() != r.sequences().size() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return l.frames() == r.frames()
|
||||
&& l.sequences() == r.sequences()
|
||||
&& l.material() == r.material();
|
||||
}
|
||||
|
||||
bool operator!=(const flipbook& l, const flipbook& r) noexcept {
|
||||
return !(l == r);
|
||||
}
|
||||
|
||||
void swap(flipbook::frame& l, flipbook::frame& r) noexcept {
|
||||
using std::swap;
|
||||
swap(l.pivot, r.pivot);
|
||||
swap(l.texrect, r.texrect);
|
||||
swap(l.texture, r.texture);
|
||||
}
|
||||
|
||||
bool operator==(const flipbook::frame& l, const flipbook::frame& r) noexcept {
|
||||
return l.pivot == r.pivot
|
||||
&& l.texrect == r.texrect
|
||||
&& l.texture == r.texture;
|
||||
}
|
||||
|
||||
bool operator!=(const flipbook::frame& l, const flipbook::frame& r) noexcept {
|
||||
return !(l == r);
|
||||
}
|
||||
|
||||
void swap(flipbook::sequence& l, flipbook::sequence& r) noexcept {
|
||||
using std::swap;
|
||||
swap(l.fps, r.fps);
|
||||
swap(l.name, r.name);
|
||||
swap(l.frames, r.frames);
|
||||
}
|
||||
|
||||
bool operator==(const flipbook::sequence& l, const flipbook::sequence& r) noexcept {
|
||||
return math::approximately(l.fps, r.fps)
|
||||
&& l.name == r.name
|
||||
&& l.frames == r.frames;
|
||||
}
|
||||
|
||||
bool operator!=(const flipbook::sequence& l, const flipbook::sequence& r) noexcept {
|
||||
return !(l == r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,12 +75,6 @@ namespace e2d
|
||||
return *this;
|
||||
}
|
||||
|
||||
sprite& sprite::set_region(const atlas::region& region) noexcept {
|
||||
pivot_ = region.pivot;
|
||||
texrect_ = region.texrect;
|
||||
return *this;
|
||||
}
|
||||
|
||||
sprite& sprite::set_texture(const texture_asset::ptr& texture) noexcept {
|
||||
texture_ = texture;
|
||||
return *this;
|
||||
|
||||
@@ -1,2 +1,19 @@
|
||||
{
|
||||
"frames" : [{
|
||||
"atlas" : "atlas.json",
|
||||
"region" : "sprite"
|
||||
}, {
|
||||
"texture" : "image.png",
|
||||
"pivot" : { "x" : 1, "y" : 2 },
|
||||
"texrect" : { "x" : 5, "y" : 6, "w" : 7, "h" : 8 }
|
||||
}],
|
||||
"sequences" : [{
|
||||
"fps" : 24,
|
||||
"name" : "sequence_0",
|
||||
"frames" : [0,1,0,1]
|
||||
}, {
|
||||
"fps" : 30,
|
||||
"name" : "sequence_1",
|
||||
"frames" : [1,0,0,1]
|
||||
}]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user