Skip to content

Commit dd384de

Browse files
committed
add flip to AnimationFrameData
1 parent 1054b80 commit dd384de

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

include/gf2/core/AnimationData.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,27 @@
1919

2020
namespace gf {
2121

22+
enum class AnimationFrameFlip : uint8_t {
23+
Horizontally = 0x01,
24+
Vertically = 0x02,
25+
Diagonally = 0x04,
26+
};
27+
28+
template<>
29+
struct EnableBitmaskOperators<AnimationFrameFlip> : std::true_type {
30+
};
31+
2232
struct GF_CORE_API AnimationFrameData {
2333
uint32_t texture_index = 0;
2434
RectF texture_region = RectF::from_size({ 0.0f, 0.0f });
2535
Time duration;
36+
Flags<AnimationFrameFlip> flip = None;
2637
};
2738

2839
template<typename Archive>
2940
Archive& operator|(Archive& ar, MaybeConst<AnimationFrameData, Archive>& data)
3041
{
31-
return ar | data.texture_index | data.texture_region | data.duration;
42+
return ar | data.texture_index | data.texture_region | data.duration | data.flip;
3243
}
3344

3445
enum class AnimationProperties : uint8_t {
@@ -45,6 +56,7 @@ namespace gf {
4556
Color color = White;
4657

4758
void add_tileset(uint32_t texture_index, Vec2I layout, Time duration, int32_t frame_count, int32_t frame_offset = 0, int32_t frame_direction = 1);
59+
void add_tileset(uint32_t texture_index, Vec2I layout, Time duration, Flags<AnimationFrameFlip> flip, int32_t frame_count, int32_t frame_offset = 0, int32_t frame_direction = 1);
4860
};
4961

5062
template<typename Archive>

library/core/AnimationData.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
namespace gf {
77

88
void AnimationData::add_tileset(uint32_t texture_index, Vec2I layout, Time duration, int32_t frame_count, int32_t frame_offset, int32_t frame_direction)
9+
{
10+
add_tileset(texture_index, layout, duration, None, frame_count, frame_offset, frame_direction);
11+
}
12+
13+
void AnimationData::add_tileset(uint32_t texture_index, Vec2I layout, Time duration, Flags<AnimationFrameFlip> flip, int32_t frame_count, int32_t frame_offset, int32_t frame_direction)
914
{
1015
int32_t x = frame_offset % layout.w;
1116
int32_t y = frame_offset / layout.w;
@@ -17,6 +22,7 @@ namespace gf {
1722
frame.texture_index = texture_index;
1823
frame.texture_region = RectF::from_position_size(gf::vec(x, y) * size, size);
1924
frame.duration = duration;
25+
frame.flip = flip;
2026

2127
frames.push_back(frame);
2228

library/graphics/AnimationRuntime.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ namespace gf::details {
3535
vertices.push_back({ bounds.position_at(Orientation::NorthWest), frame_data.texture_region.position_at(Orientation::NorthWest), color });
3636
vertices.push_back({ bounds.position_at(Orientation::SouthWest), frame_data.texture_region.position_at(Orientation::SouthWest), color });
3737

38+
if (frame_data.flip.test(AnimationFrameFlip::Diagonally)) {
39+
std::swap(vertices[index + 0].tex_coords, vertices[index + 3].tex_coords);
40+
}
41+
42+
if (frame_data.flip.test(AnimationFrameFlip::Horizontally)) {
43+
std::swap(vertices[index + 0].tex_coords, vertices[index + 2].tex_coords);
44+
std::swap(vertices[index + 1].tex_coords, vertices[index + 3].tex_coords);
45+
}
46+
47+
if (frame_data.flip.test(AnimationFrameFlip::Vertically)) {
48+
std::swap(vertices[index + 0].tex_coords, vertices[index + 1].tex_coords);
49+
std::swap(vertices[index + 2].tex_coords, vertices[index + 3].tex_coords);
50+
}
51+
3852
// first triangle
3953
indices.push_back(index);
4054
indices.push_back(index + 1);

0 commit comments

Comments
 (0)