+#include <algorithm>
#include <msp/time/utils.h>
#include "resourcemanager.h"
+#include "resourcewatcher.h"
using namespace std;
remove_existing(resources, &r);
}
+void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
+{
+ get_item(resources, &r).add_watcher(w);
+}
+
+void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
+{
+ get_item(resources, &r).remove_watcher(w);
+}
+
void ResourceManager::tick()
{
LoadingThread::State thread_state = thread.get_state();
delete io;
io = 0;
data_size = resource->get_data_size();
+
+ for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
+ (*i)->resource_loaded(*resource);
}
void ResourceManager::ManagedResource::unload()
{
resource->unload();
loaded = false;
+
+ for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
+ (*i)->resource_unloaded(*resource);
+}
+
+void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
+{
+ if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
+ watchers.push_back(&w);
+}
+
+void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
+{
+ vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
+ if(end!=watchers.end())
+ watchers.erase(end, watchers.end());
}
namespace Msp {
namespace GL {
+class ResourceWatcher;
+
class ResourceManager
{
public:
bool loaded;
unsigned last_used;
UInt64 data_size;
+ std::vector<ResourceWatcher *> watchers;
ManagedResource(Resource &);
void start_loading();
void finish_loading();
void unload();
+
+ void add_watcher(ResourceWatcher &);
+ void remove_watcher(ResourceWatcher &);
};
class LoadingThread: public Thread
void resource_used(const Resource &);
void remove_resource(Resource &);
+ void watch_resource(const Resource &, ResourceWatcher &);
+ void unwatch_resource(const Resource &, ResourceWatcher &);
+
void tick();
UInt64 get_total_data_size() const;
};
--- /dev/null
+#ifndef RESOURCEWATCHER_H_
+#define RESOURCEWATCHER_H_
+
+namespace Msp {
+namespace GL {
+
+class Resource;
+
+class ResourceWatcher
+{
+protected:
+ ResourceWatcher() { }
+public:
+ virtual ~ResourceWatcher() { }
+
+ virtual void resource_loaded(Resource &) { }
+ virtual void resource_unloaded(Resource &) { }
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif