Move keyframe loading out of SceneManager to new KeyframeManager

openmw-38
scrawl 9 years ago
parent 295563ba65
commit 64424e7262

@ -19,6 +19,7 @@
#include <components/resource/resourcesystem.hpp> #include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp> #include <components/resource/scenemanager.hpp>
#include <components/resource/keyframemanager.hpp>
#include <components/resource/texturemanager.hpp> #include <components/resource/texturemanager.hpp>
#include <components/nifosg/nifloader.hpp> // KeyframeHolder #include <components/nifosg/nifloader.hpp> // KeyframeHolder
@ -402,7 +403,7 @@ namespace MWRender
boost::shared_ptr<AnimSource> animsrc; boost::shared_ptr<AnimSource> animsrc;
animsrc.reset(new AnimSource); animsrc.reset(new AnimSource);
animsrc->mKeyframes = mResourceSystem->getSceneManager()->getKeyframes(kfname); animsrc->mKeyframes = mResourceSystem->getKeyframeManager()->get(kfname);
if (!animsrc->mKeyframes || animsrc->mKeyframes->mTextKeys.empty() || animsrc->mKeyframes->mKeyframeControllers.empty()) if (!animsrc->mKeyframes || animsrc->mKeyframes->mTextKeys.empty() || animsrc->mKeyframes->mKeyframeControllers.empty())
return; return;

@ -41,7 +41,7 @@ add_component_dir (vfs
) )
add_component_dir (resource add_component_dir (resource
scenemanager texturemanager resourcesystem bulletshapemanager bulletshape niffilemanager objectcache scenemanager keyframemanager texturemanager resourcesystem bulletshapemanager bulletshape niffilemanager objectcache
) )
add_component_dir (sceneutil add_component_dir (sceneutil

@ -37,11 +37,20 @@ namespace NifOsg
}; };
class KeyframeHolder : public osg::Referenced class KeyframeHolder : public osg::Object
{ {
public: public:
KeyframeHolder() {}
KeyframeHolder(const KeyframeHolder& copy, const osg::CopyOp& copyop)
: mTextKeys(copy.mTextKeys)
, mKeyframeControllers(copy.mKeyframeControllers)
{
}
TextKeyMap mTextKeys; TextKeyMap mTextKeys;
META_Object(OpenMW, KeyframeHolder)
typedef std::map<std::string, osg::ref_ptr<const KeyframeController> > KeyframeControllerMap; typedef std::map<std::string, osg::ref_ptr<const KeyframeController> > KeyframeControllerMap;
KeyframeControllerMap mKeyframeControllers; KeyframeControllerMap mKeyframeControllers;
}; };

@ -0,0 +1,41 @@
#include "keyframemanager.hpp"
#include <components/vfs/manager.hpp>
#include <components/nifosg/nifloader.hpp>
#include "objectcache.hpp"
namespace Resource
{
KeyframeManager::KeyframeManager(const VFS::Manager* vfs)
: mCache(new osgDB::ObjectCache)
, mVFS(vfs)
{
}
KeyframeManager::~KeyframeManager()
{
}
osg::ref_ptr<const NifOsg::KeyframeHolder> KeyframeManager::get(const std::string &name)
{
std::string normalized = name;
mVFS->normalizeFilename(normalized);
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(normalized);
if (obj)
return osg::ref_ptr<const NifOsg::KeyframeHolder>(static_cast<NifOsg::KeyframeHolder*>(obj.get()));
else
{
osg::ref_ptr<NifOsg::KeyframeHolder> loaded (new NifOsg::KeyframeHolder);
NifOsg::Loader::loadKf(Nif::NIFFilePtr(new Nif::NIFFile(mVFS->getNormalized(normalized), normalized)), *loaded.get());
mCache->addEntryToObjectCache(name, loaded);
return loaded;
}
}
}

@ -0,0 +1,47 @@
#ifndef OPENMW_COMPONENTS_KEYFRAMEMANAGER_H
#define OPENMW_COMPONENTS_KEYFRAMEMANAGER_H
#include <osg/ref_ptr>
#include <string>
namespace VFS
{
class Manager;
}
namespace osgDB
{
class ObjectCache;
}
namespace NifOsg
{
class KeyframeHolder;
}
namespace Resource
{
/// @brief Managing of keyframe resources
class KeyframeManager
{
public:
KeyframeManager(const VFS::Manager* vfs);
~KeyframeManager();
void clearCache();
/// Retrieve a read-only keyframe resource by name (case-insensitive).
/// @note This method is safe to call from any thread.
/// @note Throws an exception if the resource is not found.
osg::ref_ptr<const NifOsg::KeyframeHolder> get(const std::string& name);
private:
osg::ref_ptr<osgDB::ObjectCache> mCache;
const VFS::Manager* mVFS;
};
}
#endif

@ -4,8 +4,6 @@
#include "objectcache.hpp" #include "objectcache.hpp"
#include <osg/Timer>
namespace Resource namespace Resource
{ {

@ -3,6 +3,7 @@
#include "scenemanager.hpp" #include "scenemanager.hpp"
#include "texturemanager.hpp" #include "texturemanager.hpp"
#include "niffilemanager.hpp" #include "niffilemanager.hpp"
#include "keyframemanager.hpp"
namespace Resource namespace Resource
{ {
@ -11,6 +12,7 @@ namespace Resource
: mVFS(vfs) : mVFS(vfs)
{ {
mNifFileManager.reset(new NifFileManager(vfs)); mNifFileManager.reset(new NifFileManager(vfs));
mKeyframeManager.reset(new KeyframeManager(vfs));
mTextureManager.reset(new TextureManager(vfs)); mTextureManager.reset(new TextureManager(vfs));
mSceneManager.reset(new SceneManager(vfs, mTextureManager.get(), mNifFileManager.get())); mSceneManager.reset(new SceneManager(vfs, mTextureManager.get(), mNifFileManager.get()));
} }
@ -30,11 +32,16 @@ namespace Resource
return mTextureManager.get(); return mTextureManager.get();
} }
NifFileManager *ResourceSystem::getNifFileManager() NifFileManager* ResourceSystem::getNifFileManager()
{ {
return mNifFileManager.get(); return mNifFileManager.get();
} }
KeyframeManager* ResourceSystem::getKeyframeManager()
{
return mKeyframeManager.get();
}
void ResourceSystem::clearCache() void ResourceSystem::clearCache()
{ {
mNifFileManager->clearCache(); mNifFileManager->clearCache();

@ -14,6 +14,7 @@ namespace Resource
class SceneManager; class SceneManager;
class TextureManager; class TextureManager;
class NifFileManager; class NifFileManager;
class KeyframeManager;
/// @brief Wrapper class that constructs and provides access to the most commonly used resource subsystems. /// @brief Wrapper class that constructs and provides access to the most commonly used resource subsystems.
/// @par Resource subsystems can be used with multiple OpenGL contexts, just like the OSG equivalents, but /// @par Resource subsystems can be used with multiple OpenGL contexts, just like the OSG equivalents, but
@ -27,6 +28,7 @@ namespace Resource
SceneManager* getSceneManager(); SceneManager* getSceneManager();
TextureManager* getTextureManager(); TextureManager* getTextureManager();
NifFileManager* getNifFileManager(); NifFileManager* getNifFileManager();
KeyframeManager* getKeyframeManager();
/// Indicates to each resource manager to clear the cache, i.e. to drop cached objects that are no longer referenced. /// Indicates to each resource manager to clear the cache, i.e. to drop cached objects that are no longer referenced.
void clearCache(); void clearCache();
@ -37,6 +39,7 @@ namespace Resource
std::auto_ptr<SceneManager> mSceneManager; std::auto_ptr<SceneManager> mSceneManager;
std::auto_ptr<TextureManager> mTextureManager; std::auto_ptr<TextureManager> mTextureManager;
std::auto_ptr<NifFileManager> mNifFileManager; std::auto_ptr<NifFileManager> mNifFileManager;
std::auto_ptr<KeyframeManager> mKeyframeManager;
const VFS::Manager* mVFS; const VFS::Manager* mVFS;

@ -233,31 +233,6 @@ namespace Resource
return cloned; return cloned;
} }
osg::ref_ptr<const NifOsg::KeyframeHolder> SceneManager::getKeyframes(const std::string &name)
{
std::string normalized = name;
mVFS->normalizeFilename(normalized);
KeyframeIndex::iterator it = mKeyframeIndex.find(normalized);
if (it == mKeyframeIndex.end())
{
Files::IStreamPtr file = mVFS->get(normalized);
std::string ext = getFileExtension(normalized);
if (ext != "nif" && ext != "kf")
return NULL;
osg::ref_ptr<NifOsg::KeyframeHolder> loaded (new NifOsg::KeyframeHolder);
NifOsg::Loader::loadKf(Nif::NIFFilePtr(new Nif::NIFFile(file, normalized)), *loaded.get());
mKeyframeIndex[normalized] = loaded;
return loaded;
}
else
return it->second;
}
void SceneManager::attachTo(osg::Node *instance, osg::Group *parentNode) const void SceneManager::attachTo(osg::Node *instance, osg::Group *parentNode) const
{ {
parentNode->addChild(instance); parentNode->addChild(instance);

@ -18,11 +18,6 @@ namespace VFS
class Manager; class Manager;
} }
namespace NifOsg
{
class KeyframeHolder;
}
namespace osgUtil namespace osgUtil
{ {
class IncrementalCompileOperation; class IncrementalCompileOperation;
@ -57,9 +52,6 @@ namespace Resource
/// @note Assumes the given instance was not attached to any parents before. /// @note Assumes the given instance was not attached to any parents before.
void attachTo(osg::Node* instance, osg::Group* parentNode) const; void attachTo(osg::Node* instance, osg::Group* parentNode) const;
/// Get a read-only copy of the given keyframe file.
osg::ref_ptr<const NifOsg::KeyframeHolder> getKeyframes(const std::string& name);
/// Manually release created OpenGL objects for the given graphics context. This may be required /// Manually release created OpenGL objects for the given graphics context. This may be required
/// in cases where multiple contexts are used over the lifetime of the application. /// in cases where multiple contexts are used over the lifetime of the application.
void releaseGLObjects(osg::State* state); void releaseGLObjects(osg::State* state);
@ -90,9 +82,6 @@ namespace Resource
typedef std::map<std::string, osg::ref_ptr<const osg::Node> > Index; typedef std::map<std::string, osg::ref_ptr<const osg::Node> > Index;
Index mIndex; Index mIndex;
typedef std::map<std::string, osg::ref_ptr<const NifOsg::KeyframeHolder> > KeyframeIndex;
KeyframeIndex mKeyframeIndex;
SceneManager(const SceneManager&); SceneManager(const SceneManager&);
void operator = (const SceneManager&); void operator = (const SceneManager&);
}; };

Loading…
Cancel
Save