Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AnimationBank.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 2025
3Animation System - Animation Bank
4
5Purpose:
6- Define animation data structures (spritesheets, animations, events)
7- Load and parse animation bank JSON files
8- Provide access to animation data for playback
9*/
10
11#pragma once
12
13#include "AnimationTypes.h"
14#include <string>
15#include <vector>
16#include <unordered_map>
17#include <memory>
18
19namespace OlympeAnimation
20{
21 // ========================================================================
22 // Spritesheet - Defines a texture atlas containing animation frames
23 // ========================================================================
25 {
26 std::string id; // Unique identifier
27 std::string path; // Path to texture file
28 TextureHandle texture = nullptr; // Loaded texture handle
29
30 int frameWidth = 32; // Width of each frame
31 int frameHeight = 32; // Height of each frame
32 int columns = 1; // Number of columns in the sheet
33 int rows = 1; // Number of rows in the sheet
34 int totalFrames = 1; // Total number of frames
35 int spacing = 0; // Spacing between frames (inner padding)
36 int margin = 0; // Margin around spritesheet edges (outer padding)
37
38 Hotspot hotspot; // Center point for rendering (default: center)
39
40 SpriteSheet() = default;
41 };
42
43 // ========================================================================
44 // Animation Event - Triggered at specific frames during playback
45 // ========================================================================
47 {
48 int frame = 0; // Frame number to trigger on
49 std::string type; // Event type: "sound", "hitbox", "vfx", "gamelogic"
50 std::string dataJson; // JSON string with event-specific data
51
52 AnimationEvent() = default;
53 AnimationEvent(int f, const std::string& t, const std::string& d)
54 : frame(f), type(t), dataJson(d) {}
55 };
56
57 // ========================================================================
58 // Animation - Defines a single animation sequence
59 // ========================================================================
60 struct Animation
61 {
62 std::string name; // Animation name (e.g., "Idle", "Walk")
63 std::string spritesheetId; // Reference to spritesheet ID
64
65 int startFrame = 0; // First frame index
66 int endFrame = 0; // Last frame index
67 float framerate = 12.0f; // Frames per second
68 bool looping = true; // Should animation loop?
69
70 std::vector<AnimationEvent> events; // Events triggered during playback
71
72 // Helper: Get total number of frames
73 int GetFrameCount() const { return endFrame - startFrame + 1; }
74
75 // Helper: Get duration in seconds
76 float GetDuration() const { return GetFrameCount() / framerate; }
77
78 Animation() = default;
79 };
80
81 // ========================================================================
82 // AnimationBank - Container for all animation data
83 // ========================================================================
85 {
86 public:
87 AnimationBank() = default;
88 ~AnimationBank() = default;
89
90 // Load animation bank from JSON file
91 bool LoadFromFile(const std::string& filePath);
92
93 // Get animation by name
94 Animation* GetAnimation(const std::string& name);
95 const Animation* GetAnimation(const std::string& name) const;
96
97 // Get spritesheet by ID
98 SpriteSheet* GetSpriteSheet(const std::string& id);
99 const SpriteSheet* GetSpriteSheet(const std::string& id) const;
100
101 // Get bank name
102 const std::string& GetBankName() const { return m_bankName; }
103
104 // Check if bank is valid
105 bool IsValid() const { return m_isValid; }
106
107 private:
108 std::string m_bankName;
109 std::string m_description;
110 bool m_isValid = false;
111
112 std::unordered_map<std::string, SpriteSheet> m_spritesheets;
113 std::unordered_map<std::string, Animation> m_animations;
114
115 // Helper: Parse JSON
116 bool ParseJSON(const std::string& jsonContent);
117 };
118
119} // namespace OlympeAnimation
Core animation data structures for 2D sprite animation system.
SDL_Texture * TextureHandle
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
const std::string & GetBankName() const
Animation * GetAnimation(const std::string &name)
std::unordered_map< std::string, Animation > m_animations
bool LoadFromFile(const std::string &filePath)
SpriteSheet * GetSpriteSheet(const std::string &id)
bool ParseJSON(const std::string &jsonContent)
std::unordered_map< std::string, SpriteSheet > m_spritesheets
AnimationEvent(int f, const std::string &t, const std::string &d)
std::vector< AnimationEvent > events