Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AnimationManager.cpp
Go to the documentation of this file.
1/*
2Olympe Engine V2 2025
3Animation System - Animation Manager Implementation
4*/
5
6
7#include "AnimationManager.h"
8#include "../system/system_utils.h"
9#include <algorithm>
10
11#ifdef _WIN32
12 #include <windows.h>
13#else
14 #include <dirent.h>
15 #include <sys/stat.h>
16#endif
17
18namespace OlympeAnimation
19{
20 // ========================================================================
21 // AnimationManager Implementation
22 // ========================================================================
23
25 {
26 if (m_initialized)
27 return;
28
29 SYSTEM_LOG << "AnimationManager: Initializing...\n";
30 m_initialized = true;
31 }
32
34 {
35 SYSTEM_LOG << "AnimationManager: Loading animation banks from " << directoryPath << "\n";
36
38 for (const auto& filePath : files)
39 {
40 LoadAnimationBank(filePath);
41 }
42
43 SYSTEM_LOG << "AnimationManager: Loaded " << m_banks.size() << " animation banks\n";
44 }
45
47 {
48 SYSTEM_LOG << "AnimationManager: Loading animation graphs from " << directoryPath << "\n";
49
51 for (const auto& filePath : files)
52 {
53 LoadAnimationGraph(filePath);
54 }
55
56 SYSTEM_LOG << "AnimationManager: Loaded " << m_graphs.size() << " animation graphs\n";
57 }
58
59 bool AnimationManager::LoadAnimationBank(const std::string& filePath)
60 {
61 auto bank = std::make_unique<AnimationBank>();
62 if (bank->LoadFromFile(filePath))
63 {
64 std::string bankName = bank->GetBankName();
65 m_banks[bankName] = std::move(bank);
66 return true;
67 }
68 return false;
69 }
70
71 bool AnimationManager::LoadAnimationGraph(const std::string& filePath)
72 {
73 auto graph = std::make_unique<AnimationGraph>();
74 if (graph->LoadFromFile(filePath))
75 {
76 std::string graphName = graph->GetGraphName();
77 m_graphs[graphName] = std::move(graph);
78 return true;
79 }
80 return false;
81 }
82
84 {
85 auto it = m_banks.find(bankName);
86 if (it != m_banks.end())
87 return it->second.get();
88 return nullptr;
89 }
90
91 const AnimationBank* AnimationManager::GetBank(const std::string& bankName) const
92 {
93 auto it = m_banks.find(bankName);
94 if (it != m_banks.end())
95 return it->second.get();
96 return nullptr;
97 }
98
100 {
101 auto it = m_graphs.find(graphName);
102 if (it != m_graphs.end())
103 return it->second.get();
104 return nullptr;
105 }
106
107 const AnimationGraph* AnimationManager::GetGraph(const std::string& graphName) const
108 {
109 auto it = m_graphs.find(graphName);
110 if (it != m_graphs.end())
111 return it->second.get();
112 return nullptr;
113 }
114
115
117 const std::string& bankId,
118 const std::string& animName
119 ) const
120 {
121 // Find the bank
122 auto bankIt = m_banks.find(bankId);
123 if (bankIt == m_banks.end())
124 {
125 // Bank not found
126 return nullptr;
127 }
128
129 const AnimationBank* bank = bankIt->second.get();
130 if (!bank)
131 {
132 return nullptr;
133 }
134
135 // Get the animation from the bank using the public interface
136 const Animation* anim = bank->GetAnimation(animName);
137 if (!anim)
138 {
139 // Animation not found in bank
140 return nullptr;
141 }
142
143 // Note: This function returns Olympe::AnimationSequence* but AnimationBank
144 // contains OlympeAnimation::Animation. This may need further review.
145 // For now, returning nullptr as the types are incompatible.
146 return nullptr;
147 }
148
150 const std::string& bankId,
151 const std::string& animName
152 ) const
153 {
154 auto bankIt = m_banks.find(bankId);
155 if (bankIt == m_banks.end())
156 {
157 return false;
158 }
159
160 const AnimationBank* bank = bankIt->second.get();
161 if (!bank)
162 {
163 return false;
164 }
165
166 // Use the public GetAnimation method to check if animation exists
167 return bank->GetAnimation(animName) != nullptr;
168 }
169
171 {
172 SYSTEM_LOG << "AnimationManager: Shutting down...\n";
173 m_banks.clear();
174 m_graphs.clear();
175 m_initialized = false;
176 }
177
178 std::vector<std::string> AnimationManager::ScanDirectory(const std::string& directoryPath)
179 {
180 std::vector<std::string> files;
181
182#ifdef _WIN32
183 // Windows implementation
184 std::string searchPattern = directoryPath + "\\*.json";
187
189 {
190 SYSTEM_LOG << "AnimationManager: Directory not found or empty: " << directoryPath << "\n";
191 return files;
192 }
193
194 do
195 {
196 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
197 {
198 std::string fileName = findData.cFileName;
199 std::string fullPath = directoryPath + "\\" + fileName;
200 files.push_back(fullPath);
201 }
202 } while (FindNextFileA(hFind, &findData) != 0);
203
205#else
206 // POSIX implementation (Linux/Mac)
207 DIR* dir = opendir(directoryPath.c_str());
208 if (!dir)
209 {
210 SYSTEM_LOG << "AnimationManager: Directory not found: " << directoryPath << "\n";
211 return files;
212 }
213
214 struct dirent* entry;
215 while ((entry = readdir(dir)) != nullptr)
216 {
217 std::string fileName = entry->d_name;
218
219 // Skip . and ..
220 if (fileName == "." || fileName == "..")
221 continue;
222
223 // Check for .json extension
224 size_t dotPos = fileName.find_last_of('.');
225 if (dotPos != std::string::npos && fileName.substr(dotPos) == ".json")
226 {
227 std::string fullPath = directoryPath + "/" + fileName;
228
229 // Verify it's a regular file
230 struct stat statbuf;
231 if (stat(fullPath.c_str(), &statbuf) == 0 && S_ISREG(statbuf.st_mode))
232 {
233 files.push_back(fullPath);
234 }
235 }
236 }
237
238 closedir(dir);
239#endif
240
241 return files;
242 }
243
244} // namespace OlympeAnimation
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Animation * GetAnimation(const std::string &name)
bool LoadAnimationGraph(const std::string &filePath)
std::vector< std::string > ScanDirectory(const std::string &directoryPath)
AnimationGraph * GetGraph(const std::string &graphName)
void LoadAnimationGraphs(const std::string &directoryPath)
const Olympe::AnimationSequence * GetAnimationSequence(const std::string &bankId, const std::string &animName) const
Get animation sequence from a bank by name.
AnimationBank * GetBank(const std::string &bankName)
bool HasAnimation(const std::string &bankId, const std::string &animName) const
Check if animation exists in bank.
void LoadAnimationBanks(const std::string &directoryPath)
std::unordered_map< std::string, std::unique_ptr< AnimationBank > > m_banks
std::unordered_map< std::string, std::unique_ptr< AnimationGraph > > m_graphs
bool LoadAnimationBank(const std::string &filePath)
Defines a complete animation sequence.
#define SYSTEM_LOG