Animation system in progress

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40176
This commit is contained in:
kearwood
2012-12-07 01:49:17 +00:00
parent 1f92268e07
commit accc9ce05a
10 changed files with 121 additions and 16 deletions

View File

@@ -30,12 +30,17 @@
// //
#include "KRAnimation.h" #include "KRAnimation.h"
#include "KRAnimationManager.h"
#include "KRContext.h"
#import "tinyxml2.h" #import "tinyxml2.h"
KRAnimation::KRAnimation(KRContext &context, std::string name) : KRResource(context, name) KRAnimation::KRAnimation(KRContext &context, std::string name) : KRResource(context, name)
{ {
m_auto_play = true; m_auto_play = true;
m_loop = true; m_loop = true;
m_playing = true;
m_local_time = 0.0f;
m_duration = 0.0f;
} }
KRAnimation::~KRAnimation() KRAnimation::~KRAnimation()
{ {
@@ -59,6 +64,7 @@ bool KRAnimation::save(const std::string& path) {
doc.InsertEndChild(animation_node); doc.InsertEndChild(animation_node);
animation_node->SetAttribute("loop", m_loop ? "true" : "false"); animation_node->SetAttribute("loop", m_loop ? "true" : "false");
animation_node->SetAttribute("auto_play", m_auto_play ? "true" : "false"); animation_node->SetAttribute("auto_play", m_auto_play ? "true" : "false");
animation_node->SetAttribute("duration", m_duration);
for(std::map<std::string, KRAnimationLayer *>::iterator itr = m_layers.begin(); itr != m_layers.end(); ++itr){ for(std::map<std::string, KRAnimationLayer *>::iterator itr = m_layers.begin(); itr != m_layers.end(); ++itr){
(*itr).second->saveXML(animation_node); (*itr).second->saveXML(animation_node);
@@ -78,6 +84,10 @@ KRAnimation *KRAnimation::Load(KRContext &context, const std::string &name, KRDa
tinyxml2::XMLElement *animation_node = doc.RootElement(); tinyxml2::XMLElement *animation_node = doc.RootElement();
if(animation_node->QueryFloatAttribute("duration", &new_animation->m_duration) != tinyxml2::XML_SUCCESS) {
new_animation->m_duration = 0.0f; // Default value
}
if(animation_node->QueryBoolAttribute("loop", &new_animation->m_loop) != tinyxml2::XML_SUCCESS) { if(animation_node->QueryBoolAttribute("loop", &new_animation->m_loop) != tinyxml2::XML_SUCCESS) {
new_animation->m_loop = true; // Default value new_animation->m_loop = true; // Default value
} }
@@ -94,6 +104,10 @@ KRAnimation *KRAnimation::Load(KRContext &context, const std::string &name, KRDa
} }
} }
if(new_animation->m_auto_play) {
new_animation->m_playing = true;
}
// KRNode *n = KRNode::LoadXML(*new_scene, scene_element->FirstChildElement()); // KRNode *n = KRNode::LoadXML(*new_scene, scene_element->FirstChildElement());
delete data; delete data;
@@ -110,3 +124,50 @@ KRAnimationLayer *KRAnimation::getLayer(const char *szName)
{ {
return m_layers[szName]; return m_layers[szName];
} }
void KRAnimation::update(float deltaTime)
{
if(m_playing) {
m_local_time += deltaTime;
}
if(m_local_time > m_duration) {
m_local_time -= m_duration;
}
}
void KRAnimation::Play()
{
m_local_time = 0.0f;
m_playing = true;
getContext().getAnimationManager()->updateActiveAnimations(this);
}
void KRAnimation::Stop()
{
m_playing = false;
getContext().getAnimationManager()->updateActiveAnimations(this);
}
float KRAnimation::getTime()
{
return m_local_time;
}
void KRAnimation::setTime(float time)
{
m_local_time = time;
}
float KRAnimation::getDuration()
{
return m_duration;
}
void KRAnimation::setDuration(float duration)
{
m_duration = duration;
}
bool KRAnimation::isPlaying()
{
return m_playing;
}

View File

@@ -53,14 +53,21 @@ public:
void addLayer(KRAnimationLayer *layer); void addLayer(KRAnimationLayer *layer);
std::map<std::string, KRAnimationLayer *> &getLayers(); std::map<std::string, KRAnimationLayer *> &getLayers();
KRAnimationLayer *getLayer(const char *szName); KRAnimationLayer *getLayer(const char *szName);
void Start(); void Play();
void Stop(); void Stop();
void Restart(); void update(float deltaTime);
float getTime();
void setTime(float time);
float getDuration();
void setDuration(float duration);
bool isPlaying();
private: private:
std::map<std::string, KRAnimationLayer *> m_layers; std::map<std::string, KRAnimationLayer *> m_layers;
bool m_auto_play; bool m_auto_play;
bool m_loop; bool m_loop;
bool m_playing;
float m_local_time;
float m_duration;
}; };

View File

@@ -43,7 +43,15 @@ KRAnimationManager::~KRAnimationManager() {
} }
} }
void KRAnimationManager::startFrame() void KRAnimationManager::startFrame(float deltaTime)
{
for(std::set<KRAnimation *>::iterator active_animations_itr = m_activeAnimations.begin(); active_animations_itr != m_activeAnimations.end(); active_animations_itr++) {
KRAnimation *animation = *active_animations_itr;
animation->update(deltaTime);
}
}
void KRAnimationManager::endFrame(float deltaTime)
{ {
} }
@@ -66,5 +74,22 @@ std::map<std::string, KRAnimation *> &KRAnimationManager::getAnimations() {
void KRAnimationManager::addAnimation(KRAnimation *new_animation) void KRAnimationManager::addAnimation(KRAnimation *new_animation)
{ {
m_animations[new_animation->getName()] = new_animation; m_animations[new_animation->getName()] = new_animation;
updateActiveAnimations(new_animation);
}
void KRAnimationManager::updateActiveAnimations(KRAnimation *animation)
{
std::set<KRAnimation *>::iterator active_animations_itr = m_activeAnimations.find(animation);
if(animation->isPlaying()) {
// Add playing animations to the active animations list
if(active_animations_itr == m_activeAnimations.end()) {
m_activeAnimations.insert(animation);
}
} else {
// Remove stopped animations from the active animations list
if(active_animations_itr != m_activeAnimations.end()) {
m_activeAnimations.erase(active_animations_itr);
}
}
} }

View File

@@ -39,9 +39,11 @@
#include "KRDataBlock.h" #include "KRDataBlock.h"
#include <map> #include <map>
#include <set>
#import <string> #import <string>
using std::map; using std::map;
using std::set;
class KRAnimationManager : public KRContextObject { class KRAnimationManager : public KRContextObject {
public: public:
@@ -53,10 +55,14 @@ public:
void addAnimation(KRAnimation *new_animation); void addAnimation(KRAnimation *new_animation);
std::map<std::string, KRAnimation *> &getAnimations(); std::map<std::string, KRAnimation *> &getAnimations();
void startFrame(); void startFrame(float deltaTime);
void endFrame(float deltaTime);
void updateActiveAnimations(KRAnimation *animation);
private: private:
map<std::string, KRAnimation *> m_animations; map<std::string, KRAnimation *> m_animations;
set<KRAnimation *>m_activeAnimations;
}; };

View File

@@ -121,6 +121,10 @@ void KRContext::loadResource(const std::string &file_name, KRDataBlock *data) {
m_pModelManager->loadModel(name.c_str(), data); m_pModelManager->loadModel(name.c_str(), data);
} else if(extension.compare("krscene") == 0) { } else if(extension.compare("krscene") == 0) {
m_pSceneManager->loadScene(name.c_str(), data); m_pSceneManager->loadScene(name.c_str(), data);
} else if(extension.compare("kranimation") == 0) {
m_pAnimationManager->loadAnimation(name.c_str(), data);
} else if(extension.compare("kranimatinocurve") == 0) {
m_pAnimationCurveManager->loadAnimationCurve(name.c_str(), data);
} else if(extension.compare("pvr") == 0) { } else if(extension.compare("pvr") == 0) {
m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data);
} else if(extension.compare("tga") == 0) { } else if(extension.compare("tga") == 0) {
@@ -161,15 +165,16 @@ void KRContext::detectExtensions() {
} }
void KRContext::startFrame() void KRContext::startFrame(float deltaTime)
{ {
m_pTextureManager->startFrame(); m_pTextureManager->startFrame(deltaTime);
m_pAnimationManager->startFrame(); m_pAnimationManager->startFrame(deltaTime);
} }
void KRContext::endFrame(float deltaTime) void KRContext::endFrame(float deltaTime)
{ {
m_pTextureManager->endFrame(); m_pTextureManager->endFrame(deltaTime);
m_pAnimationManager->endFrame(deltaTime);
rotateBuffers(true); rotateBuffers(true);
m_current_frame++; m_current_frame++;
m_absolute_time += deltaTime; m_absolute_time += deltaTime;

View File

@@ -60,7 +60,7 @@ public:
static const char * extension_names[KRENGINE_NUM_EXTENSIONS]; static const char * extension_names[KRENGINE_NUM_EXTENSIONS];
static bool extension_available[KRENGINE_NUM_EXTENSIONS]; static bool extension_available[KRENGINE_NUM_EXTENSIONS];
void startFrame(); void startFrame(float deltaTime);
void endFrame(float deltaTime); void endFrame(float deltaTime);
long getCurrentFrame() const; long getCurrentFrame() const;

View File

@@ -169,7 +169,7 @@ float const PI = 3.141592653589793f;
- (void)renderScene: (KRScene *)pScene WithViewMatrix: (KRMat4)viewMatrix AndDeltaTime: (float)deltaTime - (void)renderScene: (KRScene *)pScene WithViewMatrix: (KRMat4)viewMatrix AndDeltaTime: (float)deltaTime
{ {
_context->startFrame(); _context->startFrame(deltaTime);
_camera->renderFrame(*pScene, viewMatrix, deltaTime); _camera->renderFrame(*pScene, viewMatrix, deltaTime);
_context->endFrame(deltaTime); _context->endFrame(deltaTime);
} }

View File

@@ -316,6 +316,7 @@ KRAnimation *LoadAnimation(KRContext &context, FbxAnimStack* pAnimStack)
KRAnimation *new_animation = new KRAnimation(context, pAnimStack->GetName()); KRAnimation *new_animation = new KRAnimation(context, pAnimStack->GetName());
int cLayers = pAnimStack->GetMemberCount<FbxAnimLayer>(); int cLayers = pAnimStack->GetMemberCount<FbxAnimLayer>();
new_animation->setDuration(pAnimStack->LocalStop.Get().GetSecondDouble());
for(int iLayer=0; iLayer < cLayers; iLayer++) { for(int iLayer=0; iLayer < cLayers; iLayer++) {
new_animation->addLayer(LoadAnimationLayer(context, pAnimStack->GetMember<FbxAnimLayer>(iLayer))); new_animation->addLayer(LoadAnimationLayer(context, pAnimStack->GetMember<FbxAnimLayer>(iLayer)));
} }

View File

@@ -142,14 +142,14 @@ long KRTextureManager::getMemUsed() {
return m_textureMemUsed; return m_textureMemUsed;
} }
void KRTextureManager::startFrame() void KRTextureManager::startFrame(float deltaTime)
{ {
m_memoryTransferredThisFrame = 0; m_memoryTransferredThisFrame = 0;
balanceTextureMemory(); balanceTextureMemory();
rotateBuffers(); rotateBuffers();
} }
void KRTextureManager::endFrame() void KRTextureManager::endFrame(float deltaTime)
{ {
for(int iTexture=0; iTexture < KRENGINE_MAX_TEXTURE_UNITS; iTexture++) { for(int iTexture=0; iTexture < KRENGINE_MAX_TEXTURE_UNITS; iTexture++) {
if(m_boundTextures[iTexture]) { if(m_boundTextures[iTexture]) {

View File

@@ -62,8 +62,8 @@ public:
void memoryChanged(long memoryDelta); void memoryChanged(long memoryDelta);
void startFrame(); void startFrame(float deltaTime);
void endFrame(); void endFrame(float deltaTime);
private: private:
long m_memoryTransferredThisFrame; long m_memoryTransferredThisFrame;