Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /* 19 : * Owner of all skeleton animations 20 : */ 21 : 22 : #include "precompiled.h" 23 : 24 : #include "SkeletonAnimManager.h" 25 : 26 : #include "graphics/ColladaManager.h" 27 : #include "graphics/SkeletonAnim.h" 28 : #include "graphics/SkeletonAnimDef.h" 29 : #include "maths/BoundingBoxAligned.h" 30 : #include "ps/CLogger.h" 31 : #include "ps/CStr.h" 32 : #include "ps/FileIo.h" 33 : 34 : #include <string> 35 : #include <utility> 36 : 37 : /////////////////////////////////////////////////////////////////////////////// 38 : // CSkeletonAnimManager constructor 39 1 : CSkeletonAnimManager::CSkeletonAnimManager(CColladaManager& colladaManager) 40 1 : : m_ColladaManager(colladaManager) 41 : { 42 1 : } 43 : 44 : /////////////////////////////////////////////////////////////////////////////// 45 : // CSkeletonAnimManager destructor 46 1 : CSkeletonAnimManager::~CSkeletonAnimManager() 47 : { 48 1 : } 49 : 50 : /////////////////////////////////////////////////////////////////////////////// 51 : // GetAnimation: return a given animation by filename; return null if filename 52 : // doesn't refer to valid animation file 53 0 : CSkeletonAnimDef* CSkeletonAnimManager::GetAnimation(const VfsPath& pathname) 54 : { 55 0 : VfsPath name = pathname.ChangeExtension(L""); 56 : 57 : // Find if it's already been loaded 58 0 : std::unordered_map<VfsPath, std::unique_ptr<CSkeletonAnimDef>>::iterator iter = m_Animations.find(name); 59 0 : if (iter != m_Animations.end()) 60 0 : return iter->second.get(); 61 : 62 0 : std::unique_ptr<CSkeletonAnimDef> def; 63 : 64 : // Find the file to load 65 0 : VfsPath psaFilename = m_ColladaManager.GetLoadablePath(name, CColladaManager::PSA); 66 : 67 0 : if (psaFilename.empty()) 68 0 : LOGERROR("Could not load animation '{}'", pathname.string8()); 69 : else 70 0 : try 71 : { 72 0 : def = CSkeletonAnimDef::Load(psaFilename); 73 : } 74 0 : catch (PSERROR_File&) 75 : { 76 0 : LOGERROR("Could not load animation '{}'", psaFilename.string8()); 77 0 : } 78 : 79 0 : if (def) 80 : { 81 0 : LOGMESSAGE("CSkeletonAnimManager::GetAnimation({}): Loaded successfully", 82 : pathname.string8()); 83 : } 84 : else 85 : { 86 0 : LOGERROR("CSkeletonAnimManager::GetAnimation({}): Failed loading, marked file as bad", 87 : pathname.string8()); 88 : } 89 : 90 : // Add to map, NULL if failed to load - we won't try loading it again 91 0 : return m_Animations.insert_or_assign(name, std::move(def)).first->second.get(); 92 0 : } 93 : 94 : /** 95 : * BuildAnimation: load raw animation frame animation from given file, and build a 96 : * animation specific to this model 97 : */ 98 0 : std::unique_ptr<CSkeletonAnim> CSkeletonAnimManager::BuildAnimation(const VfsPath& pathname, const CStr8& name, const CStr8& ID, int frequency, float speed, float actionpos, float actionpos2, float soundpos) 99 : { 100 0 : CSkeletonAnimDef* def = GetAnimation(pathname); 101 0 : if (!def) 102 0 : return nullptr; 103 : 104 0 : std::unique_ptr<CSkeletonAnim> anim = std::make_unique<CSkeletonAnim>(); 105 0 : anim->m_Name = name; 106 0 : anim->m_ID = ID; 107 0 : anim->m_Frequency = frequency; 108 0 : anim->m_AnimDef = def; 109 0 : anim->m_Speed = speed; 110 : 111 0 : if (actionpos == -1.f) 112 0 : anim->m_ActionPos = -1.f; 113 : else 114 0 : anim->m_ActionPos = actionpos * anim->m_AnimDef->GetDuration(); 115 : 116 0 : if (actionpos2 == -1.f) 117 0 : anim->m_ActionPos2 = -1.f; 118 : else 119 0 : anim->m_ActionPos2 = actionpos2 * anim->m_AnimDef->GetDuration(); 120 : 121 0 : if (soundpos == -1.f) 122 0 : anim->m_SoundPos = -1.f; 123 : else 124 0 : anim->m_SoundPos = soundpos * anim->m_AnimDef->GetDuration(); 125 : 126 0 : anim->m_ObjectBounds.SetEmpty(); 127 : 128 0 : return anim; 129 0 : }