Skip to content

Commit

Permalink
feat: #45 Add animation manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jan 9, 2020
1 parent eb46b17 commit fa73a7b
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 104 deletions.
9 changes: 8 additions & 1 deletion Projects/Demo/Source/View/CViewInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "CViewDemo.h"
#include "ViewManager/CViewManager.h"
#include "TextureManager/CTextureManager.h"

#include "MeshManager/CMeshManager.h"
#include "Animation/CAnimationManager.h"

#include "Context/CContext.h"

Expand Down Expand Up @@ -74,8 +76,13 @@ void CViewInit::initScene()
// grid
zone->createEmptyObject()->addComponent<CGridPlane>();

// test dae model
/*
// load animation
CAnimationManager *animManager = CAnimationManager::getInstance();
CAnimation* anim = animManager->createAnimation("HeroAnimation");
anim->addClip(animManager->loadAnimation("Demo/Model3D/Hero@Idle.dae"));
// test dae model
CMeshManager *meshManager = CMeshManager::getInstance();
CEntityPrefab *prefab = meshManager->loadModel("Demo/Model3D/Hero.dae", "Demo/Model3D/Textures", false);
if (prefab != NULL)
Expand Down
108 changes: 108 additions & 0 deletions Projects/Skylicht/Engine/Source/Animation/CAnimation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
!@
MIT License
Copyright (c) 2019 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"

#include "CAnimation.h"
#include "Utils/CStringImp.h"

namespace Skylicht
{
CAnimation::CAnimation()
{
}

CAnimation::~CAnimation()
{
removeAll();
}

void CAnimation::removeAll()
{
m_clips.clear();
m_clipName.clear();
}

void CAnimation::sortAnimByName()
{
m_clipName.clear();

u32 numAnim = m_clips.size();
for (u32 i = 0; i < numAnim - 1; i++)
{
for (u32 j = i + 1; j < numAnim; j++)
{
if (strcmp(m_clips[i]->AnimName.c_str(), m_clips[j]->AnimName.c_str()) > 0)
{
CAnimationClip *clip = m_clips[i];
m_clips[i] = m_clips[j];
m_clips[j] = clip;
}
}
}

for (u32 i = 0; i < numAnim; i++)
{
m_clipName[m_clips[i]->AnimName] = m_clips[i];
}
}

void CAnimation::removeClip(const std::string& clipName)
{
CAnimationClip *clip = m_clipName[clipName];
if (clip == NULL)
return;

std::vector<CAnimationClip*>::iterator it = m_clips.begin(), end = m_clips.end();
while (it != end)
{
if ((*it) == clip)
{
m_clips.erase(it);
m_clipName[clipName] = NULL;
return;
}
it++;
}
}

void CAnimation::renameClip(const std::string& oldName, const std::string& newName)
{
CAnimationClip *clip = m_clipName[oldName];
if (clip == NULL)
return;

for (CAnimationClip *&i : m_clips)
{
if (i == clip)
{
i->AnimName = newName;

m_clipName[oldName] = NULL;
m_clipName[newName] = i;
return;
}
}
}
}
88 changes: 88 additions & 0 deletions Projects/Skylicht/Engine/Source/Animation/CAnimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
!@
MIT License
Copyright (c) 2019 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "pch.h"
#include "CAnimationClip.h"

namespace Skylicht
{

class CAnimation
{
protected:
std::vector<CAnimationClip*> m_clips;
std::map<std::string, CAnimationClip*> m_clipName;

public:
CAnimation();

virtual ~CAnimation();

CAnimationClip* getAnim(const char *lpAnimName)
{
return m_clipName[lpAnimName];
}

CAnimationClip* getAnim(int animID)
{
return m_clips[animID];
}

int getAnimCount()
{
return (int)m_clips.size();
}

void addClip(CAnimationClip* clip)
{
if (clip == NULL)
return;

m_clips.push_back(clip);
m_clipName[clip->AnimName] = clip;
}

void sortAnimByName();

std::vector<CAnimationClip*>* getAllAnimClip()
{
return &m_clips;
}

std::map<std::string, CAnimationClip*>* getAllAnimNameClip()
{
return &m_clipName;
}

void removeAll();

void removeClip(const std::string& clipName);

void renameClip(const std::string& oldName, const std::string& newName);

};

}
30 changes: 15 additions & 15 deletions Projects/Skylicht/Engine/Source/Animation/CAnimationClip.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ This file is part of the "Skylicht Engine".

namespace Skylicht
{
struct SNodeAnim
struct SEntityAnim
{
std::string SceneNodeName;
std::string Name;
CFrameData Data;
};

class CAnimationClip
{
public:
std::string AnimName;
std::string AnimName;
float Time;
float Duration;
bool Loop;

std::vector<SNodeAnim*> AnimInfo;
std::map<std::string, SNodeAnim*> AnimNameToInfo;
std::vector<SEntityAnim*> AnimInfo;
std::map<std::string, SEntityAnim*> AnimNameToInfo;

CAnimationClip()
{
Expand All @@ -56,47 +56,47 @@ namespace Skylicht

virtual ~CAnimationClip()
{
freeAllNodeAnim();
releaseAllAnim();
}

void freeAllNodeAnim()
void releaseAllAnim()
{
for (SNodeAnim* &i : AnimInfo)
for (SEntityAnim* &i : AnimInfo)
{
delete i;
}
AnimInfo.clear();
AnimNameToInfo.clear();
}

void addNodeAnim(SNodeAnim* anim)
void addAnim(SEntityAnim* anim)
{
for (SNodeAnim *&i : AnimInfo)
for (SEntityAnim *&i : AnimInfo)
{
if (i->SceneNodeName == anim->SceneNodeName)
if (i->Name == anim->Name)
{
delete i;
i = anim;
AnimNameToInfo[i->SceneNodeName] = anim;
AnimNameToInfo[i->Name] = anim;
return;
}
}

AnimInfo.push_back(anim);
AnimNameToInfo[anim->SceneNodeName] = anim;
AnimNameToInfo[anim->Name] = anim;
}

int getNodeAnimCount()
{
return (int)AnimInfo.size();
}

SNodeAnim* getAnimOfSceneNode(int i)
SEntityAnim* getAnimOfSceneNode(int i)
{
return AnimInfo[i];
}

SNodeAnim* getAnimOfSceneNode(const std::string &sceneNodeName)
SEntityAnim* getAnimOfSceneNode(const std::string &sceneNodeName)
{
return AnimNameToInfo[sceneNodeName];
}
Expand Down
Loading

0 comments on commit fa73a7b

Please sign in to comment.