Skip to content

Commit 429e131

Browse files
committed
rework animation classes
1 parent b8bcc7e commit 429e131

File tree

11 files changed

+583
-309
lines changed

11 files changed

+583
-309
lines changed

games/HOME/bits/HeroEntity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <gf2/core/Vec2.h>
77

88
#include <gf2/audio/Sound.h>
9-
#include <gf2/graphics/AnimationEntity.h>
9+
#include <gf2/graphics/AnimationGroupEntity.h>
1010
#include <gf2/graphics/SpriteEntity.h>
1111
#include <gf2/graphics/TransformableEntity.h>
1212
#include <gf2/physics/PhysicsBody.h>

include/gf2/graphics/Animation.h

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,46 @@
88

99
#include <gf2/core/AnimationData.h>
1010
#include <gf2/core/Flags.h>
11+
#include <gf2/core/Id.h>
1112
#include <gf2/core/Rect.h>
1213
#include <gf2/core/Time.h>
1314

1415
#include "Buffer.h"
1516
#include "GraphicsApi.h"
1617
#include "RenderObject.h"
17-
18-
#include "gf2/core/Id.h"
18+
#include "AnimationRuntime.h"
1919

2020
namespace gf {
2121
class Texture;
2222
class RenderManager;
2323
class ResourceManager;
2424

25-
struct GF_GRAPHICS_API AnimationFrameRuntime {
26-
std::size_t texture_index = 0;
27-
Time duration;
28-
std::size_t offset = 0;
29-
};
30-
31-
struct GF_GRAPHICS_API AnimationRuntime {
32-
Flags<AnimationProperties> properties = None;
33-
std::vector<AnimationFrameRuntime> frames;
34-
};
35-
36-
class GF_GRAPHICS_API Animation {
25+
class GF_GRAPHICS_API AnimationState {
3726
public:
38-
Animation(std::vector<const Texture*> textures, const AnimationData& data, RenderManager* render_manager);
39-
Animation(const AnimationResource& resource, RenderManager* render_manager, ResourceManager* resource_manager);
27+
AnimationState(const AnimationData& data);
4028

4129
void update(Time time);
4230
void reset();
4331
bool finished() const;
4432

45-
RenderGeometry geometry() const;
33+
std::size_t current_frame() const
34+
{
35+
return m_current_frame;
36+
}
37+
38+
private:
39+
details::AnimationStateRuntime m_animation;
40+
std::size_t m_current_frame = 0;
41+
Time m_current_time;
42+
};
43+
44+
45+
class GF_GRAPHICS_API AnimationGraphics {
46+
public:
47+
AnimationGraphics(std::vector<const Texture*> textures, const AnimationData& data, RenderManager* render_manager);
48+
AnimationGraphics(const AnimationResource& resource, RenderManager* render_manager, ResourceManager* resource_manager);
49+
50+
RenderGeometry geometry(std::size_t frame_index) const;
4651

4752
RectF bounds() const
4853
{
@@ -54,44 +59,44 @@ namespace gf {
5459
Buffer m_vertices;
5560
Buffer m_indices;
5661
RectF m_bounds = RectF::from_size({ 0.0f, 0.0f });
57-
58-
AnimationRuntime m_animation;
59-
std::size_t m_current_frame = 0;
60-
Time m_current_time;
62+
details::AnimationGraphicsRuntime m_animation;
6163
};
6264

63-
class GF_GRAPHICS_API AnimationGroup {
65+
class GF_GRAPHICS_API Animation {
6466
public:
65-
AnimationGroup(std::vector<const Texture*> textures, const AnimationGroupData& data, RenderManager* render_manager);
66-
AnimationGroup(const AnimationGroupResource& resource, RenderManager* render_manager, ResourceManager* resource_manager);
67+
Animation(std::vector<const Texture*> textures, const AnimationData& data, RenderManager* render_manager);
68+
Animation(const AnimationResource& resource, RenderManager* render_manager, ResourceManager* resource_manager);
6769

68-
void select(std::string_view animation_name);
69-
void select(Id animation_id);
70+
void update(Time time)
71+
{
72+
m_state.update(time);
73+
}
7074

71-
void update(Time time);
72-
void reset();
73-
bool finished() const;
75+
void reset()
76+
{
77+
m_state.reset();
78+
}
7479

75-
RenderGeometry geometry() const;
80+
bool finished() const
81+
{
82+
return m_state.finished();
83+
}
84+
85+
RenderGeometry geometry() const
86+
{
87+
return m_graphics.geometry(m_state.current_frame());
88+
}
7689

7790
RectF bounds() const
7891
{
79-
return m_bounds;
92+
return m_graphics.bounds();
8093
}
8194

8295
private:
83-
const AnimationRuntime* current_animation() const;
84-
85-
std::vector<const Texture*> m_textures;
86-
Buffer m_vertices;
87-
Buffer m_indices;
88-
RectF m_bounds = RectF::from_size({ 0.0f, 0.0f });
89-
90-
std::map<Id, AnimationRuntime> m_animations;
91-
Id m_current_animation_id = InvalidId;
92-
std::size_t m_current_frame = 0;
93-
Time m_current_time;
96+
AnimationState m_state;
97+
AnimationGraphics m_graphics;
9498
};
99+
95100
}
96101

97102
#endif // GF_ANIMATION_H

include/gf2/graphics/AnimationEntity.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
#ifndef GF_ANIMATION_ENTITY_H
44
#define GF_ANIMATION_ENTITY_H
55

6-
#include <gf2/core/AnimationData.h>
7-
#include <gf2/core/Transform.h>
8-
96
#include "Animation.h"
107
#include "GraphicsApi.h"
11-
#include "Texture.h"
128
#include "TransformableEntity.h"
139

1410
namespace gf {
@@ -36,38 +32,6 @@ namespace gf {
3632
Animation m_animation;
3733
};
3834

39-
class GF_GRAPHICS_API AnimationGroupEntity : public TransformableEntity {
40-
public:
41-
AnimationGroupEntity(std::vector<const Texture*> textures, const AnimationGroupData& data, RenderManager* render_manager);
42-
AnimationGroupEntity(const AnimationGroupResource& resource, RenderManager* render_manager, ResourceManager* resource_manager);
43-
44-
void update(Time time) override;
45-
void render(RenderRecorder& recorder) override;
46-
47-
void select(std::string_view animation_name)
48-
{
49-
m_animation_group.select(animation_name);
50-
}
51-
52-
void select(Id animation_id)
53-
{
54-
m_animation_group.select(animation_id);
55-
}
56-
57-
void reset()
58-
{
59-
m_animation_group.reset();
60-
}
61-
62-
bool finished() const
63-
{
64-
return m_animation_group.finished();
65-
}
66-
67-
private:
68-
AnimationGroup m_animation_group;
69-
};
70-
7135
}
7236

7337
#endif // GF_ANIMATION_ENTITY_H
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: Zlib
2+
// Copyright (c) 2023 Julien Bernard
3+
#ifndef GF_ANIMATION_GROUP_H
4+
#define GF_ANIMATION_GROUP_H
5+
6+
#include <string_view>
7+
#include <vector>
8+
9+
#include <gf2/core/AnimationData.h>
10+
#include <gf2/core/Id.h>
11+
#include <gf2/core/Time.h>
12+
13+
#include "Buffer.h"
14+
#include "GraphicsApi.h"
15+
#include "RenderObject.h"
16+
#include "AnimationRuntime.h"
17+
18+
namespace gf {
19+
class Texture;
20+
class RenderManager;
21+
class ResourceManager;
22+
23+
class GF_GRAPHICS_API AnimationGroupState {
24+
public:
25+
AnimationGroupState(const AnimationGroupData& data);
26+
27+
void select(std::string_view animation_name);
28+
void select(Id animation_id);
29+
30+
void update(Time time);
31+
void reset();
32+
bool finished() const;
33+
34+
Id current_animation_id() const
35+
{
36+
return m_current_animation_id;
37+
}
38+
39+
std::size_t current_frame() const
40+
{
41+
return m_current_frame;
42+
}
43+
44+
private:
45+
const details::AnimationStateRuntime* current_animation() const;
46+
47+
std::map<Id, details::AnimationStateRuntime> m_animations;
48+
Id m_current_animation_id = InvalidId;
49+
std::size_t m_current_frame = 0;
50+
Time m_current_time;
51+
};
52+
53+
class GF_GRAPHICS_API AnimationGroupGraphics {
54+
public:
55+
AnimationGroupGraphics(std::vector<const Texture*> textures, const AnimationGroupData& data, RenderManager* render_manager);
56+
AnimationGroupGraphics(const AnimationGroupResource& resource, RenderManager* render_manager, ResourceManager* resource_manager);
57+
58+
RenderGeometry geometry(Id animation_id, std::size_t frame_index) const;
59+
60+
RectF bounds() const
61+
{
62+
return m_bounds;
63+
}
64+
65+
private:
66+
std::vector<const Texture*> m_textures;
67+
Buffer m_vertices;
68+
Buffer m_indices;
69+
RectF m_bounds = RectF::from_size({ 0.0f, 0.0f });
70+
71+
std::map<Id, details::AnimationGraphicsRuntime> m_animations;
72+
};
73+
74+
75+
class GF_GRAPHICS_API AnimationGroup {
76+
public:
77+
AnimationGroup(std::vector<const Texture*> textures, const AnimationGroupData& data, RenderManager* render_manager);
78+
AnimationGroup(const AnimationGroupResource& resource, RenderManager* render_manager, ResourceManager* resource_manager);
79+
80+
void select(std::string_view animation_name)
81+
{
82+
m_state.select(animation_name);
83+
}
84+
85+
void select(Id animation_id)
86+
{
87+
m_state.select(animation_id);
88+
}
89+
90+
void update(Time time)
91+
{
92+
m_state.update(time);
93+
}
94+
95+
void reset()
96+
{
97+
m_state.reset();
98+
}
99+
100+
bool finished() const
101+
{
102+
return m_state.finished();
103+
}
104+
105+
RenderGeometry geometry() const
106+
{
107+
return m_graphics.geometry(m_state.current_animation_id(), m_state.current_frame());
108+
}
109+
110+
RectF bounds() const
111+
{
112+
return m_graphics.bounds();
113+
}
114+
115+
private:
116+
AnimationGroupState m_state;
117+
AnimationGroupGraphics m_graphics;
118+
};
119+
120+
}
121+
122+
#endif // GF_ANIMATION_GROUP_H
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: Zlib
2+
// Copyright (c) 2023 Julien Bernard
3+
#ifndef GF_ANIMATION_GROUP_ENTITY_H
4+
#define GF_ANIMATION_GROUP_ENTITY_H
5+
6+
#include "AnimationGroup.h"
7+
#include "GraphicsApi.h"
8+
#include "TransformableEntity.h"
9+
10+
namespace gf {
11+
class RenderManager;
12+
13+
class GF_GRAPHICS_API AnimationGroupEntity : public TransformableEntity {
14+
public:
15+
AnimationGroupEntity(std::vector<const Texture*> textures, const AnimationGroupData& data, RenderManager* render_manager);
16+
AnimationGroupEntity(const AnimationGroupResource& resource, RenderManager* render_manager, ResourceManager* resource_manager);
17+
18+
void update(Time time) override;
19+
void render(RenderRecorder& recorder) override;
20+
21+
void select(std::string_view animation_name)
22+
{
23+
m_animation_group.select(animation_name);
24+
}
25+
26+
void select(Id animation_id)
27+
{
28+
m_animation_group.select(animation_id);
29+
}
30+
31+
void reset()
32+
{
33+
m_animation_group.reset();
34+
}
35+
36+
bool finished() const
37+
{
38+
return m_animation_group.finished();
39+
}
40+
41+
private:
42+
AnimationGroup m_animation_group;
43+
};
44+
45+
}
46+
47+
#endif // GF_ANIMATION_ENTITY_H

0 commit comments

Comments
 (0)