Line data Source code
1 : /* Copyright (C) 2022 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/Model.h"
28 : #include "graphics/SkeletonAnim.h"
29 : #include "graphics/SkeletonAnimDef.h"
30 : #include "ps/CLogger.h"
31 : #include "ps/CStr.h"
32 : #include "ps/FileIo.h"
33 :
34 : ///////////////////////////////////////////////////////////////////////////////
35 : // CSkeletonAnimManager constructor
36 0 : CSkeletonAnimManager::CSkeletonAnimManager(CColladaManager& colladaManager)
37 0 : : m_ColladaManager(colladaManager)
38 : {
39 0 : }
40 :
41 : ///////////////////////////////////////////////////////////////////////////////
42 : // CSkeletonAnimManager destructor
43 0 : CSkeletonAnimManager::~CSkeletonAnimManager()
44 : {
45 0 : }
46 :
47 : ///////////////////////////////////////////////////////////////////////////////
48 : // GetAnimation: return a given animation by filename; return null if filename
49 : // doesn't refer to valid animation file
50 0 : CSkeletonAnimDef* CSkeletonAnimManager::GetAnimation(const VfsPath& pathname)
51 : {
52 0 : VfsPath name = pathname.ChangeExtension(L"");
53 :
54 : // Find if it's already been loaded
55 0 : std::unordered_map<VfsPath, std::unique_ptr<CSkeletonAnimDef>>::iterator iter = m_Animations.find(name);
56 0 : if (iter != m_Animations.end())
57 0 : return iter->second.get();
58 :
59 0 : std::unique_ptr<CSkeletonAnimDef> def;
60 :
61 : // Find the file to load
62 0 : VfsPath psaFilename = m_ColladaManager.GetLoadablePath(name, CColladaManager::PSA);
63 :
64 0 : if (psaFilename.empty())
65 0 : LOGERROR("Could not load animation '%s'", pathname.string8());
66 : else
67 : try
68 : {
69 0 : def = CSkeletonAnimDef::Load(psaFilename);
70 : }
71 0 : catch (PSERROR_File&)
72 : {
73 0 : LOGERROR("Could not load animation '%s'", psaFilename.string8());
74 : }
75 :
76 0 : if (def)
77 0 : LOGMESSAGE("CSkeletonAnimManager::GetAnimation(%s): Loaded successfully", pathname.string8());
78 : else
79 0 : LOGERROR("CSkeletonAnimManager::GetAnimation(%s): Failed loading, marked file as bad", pathname.string8());
80 :
81 : // Add to map, NULL if failed to load - we won't try loading it again
82 0 : return m_Animations.insert_or_assign(name, std::move(def)).first->second.get();
83 : }
84 :
85 : /**
86 : * BuildAnimation: load raw animation frame animation from given file, and build a
87 : * animation specific to this model
88 : */
89 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)
90 : {
91 0 : CSkeletonAnimDef* def = GetAnimation(pathname);
92 0 : if (!def)
93 0 : return nullptr;
94 :
95 0 : std::unique_ptr<CSkeletonAnim> anim = std::make_unique<CSkeletonAnim>();
96 0 : anim->m_Name = name;
97 0 : anim->m_ID = ID;
98 0 : anim->m_Frequency = frequency;
99 0 : anim->m_AnimDef = def;
100 0 : anim->m_Speed = speed;
101 :
102 0 : if (actionpos == -1.f)
103 0 : anim->m_ActionPos = -1.f;
104 : else
105 0 : anim->m_ActionPos = actionpos * anim->m_AnimDef->GetDuration();
106 :
107 0 : if (actionpos2 == -1.f)
108 0 : anim->m_ActionPos2 = -1.f;
109 : else
110 0 : anim->m_ActionPos2 = actionpos2 * anim->m_AnimDef->GetDuration();
111 :
112 0 : if (soundpos == -1.f)
113 0 : anim->m_SoundPos = -1.f;
114 : else
115 0 : anim->m_SoundPos = soundpos * anim->m_AnimDef->GetDuration();
116 :
117 0 : anim->m_ObjectBounds.SetEmpty();
118 :
119 0 : return anim;
120 3 : }
|