From 3d8438922e24b787587d0c8f1883c5567a4af573 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 2 May 2018 18:43:55 +0300 Subject: [PATCH] Change some class names --- source/animationplayer.cpp | 72 +++++++++++++++++++------------------- source/animationplayer.h | 18 +++++----- source/object.h | 4 +-- source/resourcemanager.cpp | 30 ++++++++-------- source/resourcemanager.h | 16 +++++---- source/resourceobserver.h | 24 +++++++++++++ source/resourcewatcher.h | 17 +++------ 7 files changed, 100 insertions(+), 81 deletions(-) create mode 100644 source/resourceobserver.h diff --git a/source/animationplayer.cpp b/source/animationplayer.cpp index 95b45dfd..a3676ac9 100644 --- a/source/animationplayer.cpp +++ b/source/animationplayer.cpp @@ -8,34 +8,34 @@ using namespace std; namespace Msp { namespace GL { -AnimationPlayer::ObjectSlot &AnimationPlayer::get_slot(AnimatedObject &obj) +AnimationPlayer::Target &AnimationPlayer::get_slot(AnimatedObject &obj) { ObjectMap::iterator i = objects.find(&obj); if(i!=objects.end()) return i->second; - return objects.insert(ObjectMap::value_type(&obj, ObjectSlot(obj))).first->second; + return objects.insert(ObjectMap::value_type(&obj, Target(obj))).first->second; } void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim) { - ObjectSlot &obj_slot = get_slot(obj); - obj_slot.animations.clear(); - obj_slot.base_matrix = Matrix(); - obj_slot.stacked = false; - obj_slot.armature = anim.get_armature(); - obj_slot.animations.push_back(AnimationSlot(anim)); + Target &target = get_slot(obj); + target.animations.clear(); + target.base_matrix = Matrix(); + target.stacked = false; + target.armature = anim.get_armature(); + target.animations.push_back(PlayingAnimation(anim)); } void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim) { - ObjectSlot &obj_slot = get_slot(obj); - if(obj_slot.animations.empty()) - obj_slot.base_matrix = *obj.get_matrix(); + Target &target = get_slot(obj); + if(target.animations.empty()) + target.base_matrix = *obj.get_matrix(); // TODO check for incompatible armature - obj_slot.stacked = true; - obj_slot.armature = anim.get_armature(); - obj_slot.animations.push_back(AnimationSlot(anim)); + target.stacked = true; + target.armature = anim.get_armature(); + target.animations.push_back(PlayingAnimation(anim)); } unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const @@ -55,7 +55,7 @@ void AnimationPlayer::stop(AnimatedObject &obj, const Animation &anim) if(i==objects.end()) return; - for(vector::iterator j=i->second.animations.begin(); j!=i->second.animations.end(); ++j) + for(vector::iterator j=i->second.animations.begin(); j!=i->second.animations.end(); ++j) if(j->animation==&anim) { i->second.animations.erase(j); @@ -83,64 +83,64 @@ void AnimationPlayer::tick(const Time::TimeDelta &dt) } } -bool AnimationPlayer::tick_single(ObjectSlot &slot, const Time::TimeDelta &dt) +bool AnimationPlayer::tick_single(Target &target, const Time::TimeDelta &dt) { - AnimationSlot &anim = slot.animations.front(); + PlayingAnimation &anim = target.animations.front(); anim.iterator += dt; - slot.object.set_matrix(anim.iterator.get_matrix()); + target.object.set_matrix(anim.iterator.get_matrix()); unsigned n_uniforms = anim.animation->get_n_uniforms(); for(unsigned i=0; iget_uniform_name(i), anim.iterator.get_uniform(i)); + set_object_uniform(target.object, anim.animation->get_uniform_name(i), anim.iterator.get_uniform(i)); - if(slot.armature) + if(target.armature) { - unsigned max_index = slot.armature->get_max_link_index(); + unsigned max_index = target.armature->get_max_link_index(); for(unsigned i=0; i<=max_index; ++i) - slot.object.set_pose_matrix(i, anim.iterator.get_pose_matrix(i)); + target.object.set_pose_matrix(i, anim.iterator.get_pose_matrix(i)); } return !anim.iterator.is_end(); } -bool AnimationPlayer::tick_stacked(ObjectSlot &slot, const Time::TimeDelta &dt) +bool AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt) { - Matrix matrix = slot.base_matrix; - for(vector::iterator i=slot.animations.begin(); i!=slot.animations.end(); ++i) + Matrix matrix = target.base_matrix; + for(vector::iterator i=target.animations.begin(); i!=target.animations.end(); ++i) { i->iterator += dt; matrix *= i->iterator.get_matrix(); unsigned n_uniforms = i->animation->get_n_uniforms(); for(unsigned j=0; janimation->get_uniform_name(j), i->iterator.get_uniform(j)); + set_object_uniform(target.object, i->animation->get_uniform_name(j), i->iterator.get_uniform(j)); } - slot.object.set_matrix(matrix); + target.object.set_matrix(matrix); - if(slot.armature) + if(target.armature) { - unsigned max_index = slot.armature->get_max_link_index(); + unsigned max_index = target.armature->get_max_link_index(); for(unsigned i=0; i<=max_index; ++i) { matrix = Matrix(); /* XXX This is in all likelihood incorrect. The stacking should be performed on local matrices. */ - for(vector::iterator j=slot.animations.begin(); j!=slot.animations.end(); ++j) + for(vector::iterator j=target.animations.begin(); j!=target.animations.end(); ++j) if(j->animation->get_armature()) matrix *= j->iterator.get_pose_matrix(i); - slot.object.set_pose_matrix(i, matrix); + target.object.set_pose_matrix(i, matrix); } } - for(vector::iterator i=slot.animations.begin(); i!=slot.animations.end(); ) + for(vector::iterator i=target.animations.begin(); i!=target.animations.end(); ) { if(i->iterator.is_end()) - i = slot.animations.erase(i); + i = target.animations.erase(i); else ++i; } - return !slot.animations.empty(); + return !target.animations.empty(); } void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name, const KeyFrame::AnimatedUniform &uni) @@ -158,14 +158,14 @@ void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name } -AnimationPlayer::ObjectSlot::ObjectSlot(AnimatedObject &o): +AnimationPlayer::Target::Target(AnimatedObject &o): object(o), armature(0), stacked(false) { } -AnimationPlayer::AnimationSlot::AnimationSlot(const Animation &a): +AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a): animation(&a), iterator(*animation) { } diff --git a/source/animationplayer.h b/source/animationplayer.h index 93482c74..63e6e030 100644 --- a/source/animationplayer.h +++ b/source/animationplayer.h @@ -17,31 +17,31 @@ can handle an arbitrary number of animations simultaneously. class AnimationPlayer { private: - struct AnimationSlot + struct PlayingAnimation { const Animation *animation; Animation::Iterator iterator; - AnimationSlot(const Animation &); + PlayingAnimation(const Animation &); }; - struct ObjectSlot + struct Target: AnimationEventObserver { AnimatedObject &object; Matrix base_matrix; const Armature *armature; - std::vector animations; + std::vector animations; bool stacked; - ObjectSlot(AnimatedObject &); + Target(AnimatedObject &); }; - typedef std::map ObjectMap; + typedef std::map ObjectMap; ObjectMap objects; private: - ObjectSlot &get_slot(AnimatedObject &); + Target &get_slot(AnimatedObject &); public: /// Plays an animation on an object. Any previous animations are replaced. @@ -65,8 +65,8 @@ public: void tick(const Time::TimeDelta &); private: - bool tick_single(ObjectSlot &, const Time::TimeDelta &); - bool tick_stacked(ObjectSlot &, const Time::TimeDelta &); + bool tick_single(Target &, const Time::TimeDelta &); + bool tick_stacked(Target &, const Time::TimeDelta &); static void set_object_uniform(AnimatedObject &, const std::string &, const KeyFrame::AnimatedUniform &); }; diff --git a/source/object.h b/source/object.h index 88a5d9e9..c8e37260 100644 --- a/source/object.h +++ b/source/object.h @@ -5,7 +5,7 @@ #include "bindable.h" #include "renderable.h" #include "renderpass.h" -#include "resourcewatcher.h" +#include "resourceobserver.h" namespace Msp { namespace GL { @@ -28,7 +28,7 @@ Objects can have multiple levels of detail. The most detailed level has index 0, with increasing indices having less detail. When rendering an instance, the instance's get_level_of_detail method is called to determine which LoD to use. */ -class Object: public Renderable, private ResourceWatcher +class Object: public Renderable, private ResourceObserver { private: struct LevelOfDetail; diff --git a/source/resourcemanager.cpp b/source/resourcemanager.cpp index 9835adde..4b72685d 100644 --- a/source/resourcemanager.cpp +++ b/source/resourcemanager.cpp @@ -5,7 +5,7 @@ #include #include "resourcemanager.h" #include "resources.h" -#include "resourcewatcher.h" +#include "resourceobserver.h" using namespace std; @@ -163,21 +163,21 @@ void ResourceManager::remove_resource(Resource &r) else if(state>ManagedResource::LOAD_QUEUED && state::const_iterator i=managed.watchers.begin(); i!=managed.watchers.end(); ++i) + for(vector::const_iterator i=managed.observers.begin(); i!=managed.observers.end(); ++i) (*i)->resource_removed(r); MutexLock lock(map_mutex); remove_existing(resources, &r); } -void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w) +void ResourceManager::observe_resource(const Resource &r, ResourceObserver &w) { - get_managed_resource(r).add_watcher(w); + get_managed_resource(r).add_observer(w); } -void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w) +void ResourceManager::unobserve_resource(const Resource &r, ResourceObserver &w) { - get_managed_resource(r).remove_watcher(w); + get_managed_resource(r).remove_observer(w); } void ResourceManager::tick() @@ -336,7 +336,7 @@ void ResourceManager::ManagedResource::finish_loading(bool successful) state = LOADED; data_size = resource->get_data_size(); - for(vector::const_iterator i=watchers.begin(); i!=watchers.end(); ++i) + for(vector::const_iterator i=observers.begin(); i!=observers.end(); ++i) (*i)->resource_loaded(*resource); } else @@ -356,21 +356,21 @@ void ResourceManager::ManagedResource::unload() resource->unload(); state = NOT_LOADED; - for(vector::const_iterator i=watchers.begin(); i!=watchers.end(); ++i) + for(vector::const_iterator i=observers.begin(); i!=observers.end(); ++i) (*i)->resource_unloaded(*resource); } -void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w) +void ResourceManager::ManagedResource::add_observer(ResourceObserver &w) { - if(find(watchers.begin(), watchers.end(), &w)==watchers.end()) - watchers.push_back(&w); + if(find(observers.begin(), observers.end(), &w)==observers.end()) + observers.push_back(&w); } -void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w) +void ResourceManager::ManagedResource::remove_observer(ResourceObserver &w) { - vector::iterator end = remove(watchers.begin(), watchers.end(), &w); - if(end!=watchers.end()) - watchers.erase(end, watchers.end()); + vector::iterator end = remove(observers.begin(), observers.end(), &w); + if(end!=observers.end()) + observers.erase(end, observers.end()); } diff --git a/source/resourcemanager.h b/source/resourcemanager.h index afa2c2d0..9dcd9b99 100644 --- a/source/resourcemanager.h +++ b/source/resourcemanager.h @@ -11,7 +11,7 @@ namespace Msp { namespace GL { -class ResourceWatcher; +class ResourceObserver; class resource_load_error: public std::runtime_error { @@ -62,7 +62,7 @@ private: State state; unsigned last_used; UInt64 data_size; - std::vector watchers; + std::vector observers; ManagedResource(Resource &); @@ -72,8 +72,8 @@ private: void finish_loading(); void unload(); - void add_watcher(ResourceWatcher &); - void remove_watcher(ResourceWatcher &); + void add_observer(ResourceObserver &); + void remove_observer(ResourceObserver &); }; typedef std::list LoadQueue; @@ -152,8 +152,12 @@ public: void resource_used(const Resource &); void remove_resource(Resource &); - void watch_resource(const Resource &, ResourceWatcher &); - void unwatch_resource(const Resource &, ResourceWatcher &); + void observe_resource(const Resource &, ResourceObserver &); + void unobserve_resource(const Resource &, ResourceObserver &); + + // Deprecated names + void watch_resource(const Resource &r, ResourceObserver &o) { observe_resource(r, o); } + void unwatch_resource(const Resource &r, ResourceObserver &o) { unobserve_resource(r, o); } void tick(); private: diff --git a/source/resourceobserver.h b/source/resourceobserver.h new file mode 100644 index 00000000..d1313d32 --- /dev/null +++ b/source/resourceobserver.h @@ -0,0 +1,24 @@ +#ifndef MSP_GL_RESOURCEOBSERVER_H_ +#define MSP_GL_RESOURCEOBSERVER_H_ + +namespace Msp { +namespace GL { + +class Resource; + +class ResourceObserver +{ +protected: + ResourceObserver() { } +public: + virtual ~ResourceObserver() { } + + virtual void resource_loaded(Resource &) { } + virtual void resource_unloaded(Resource &) { } + virtual void resource_removed(Resource &) { } +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/resourcewatcher.h b/source/resourcewatcher.h index 47dc7983..5281d24d 100644 --- a/source/resourcewatcher.h +++ b/source/resourcewatcher.h @@ -1,22 +1,13 @@ #ifndef MSP_GL_RESOURCEWATCHER_H_ #define MSP_GL_RESOURCEWATCHER_H_ +#include "resourceobserver.h" + namespace Msp { namespace GL { -class Resource; - -class ResourceWatcher -{ -protected: - ResourceWatcher() { } -public: - virtual ~ResourceWatcher() { } - - virtual void resource_loaded(Resource &) { } - virtual void resource_unloaded(Resource &) { } - virtual void resource_removed(Resource &) { } -}; +// Deprecated name +typedef ResourceObserver ResourceWatcher; } // namespace GL } // namespace Msp -- 2.45.2