ResourceManager::ResourceManager():
policy(LOAD_ON_DEMAND),
async_loads(true),
+ frame(0),
+ max_retain_frames(0),
+ next_unload(0),
thread(*this)
{ }
async_loads = a;
}
+void ResourceManager::set_max_retain_frames(unsigned f)
+{
+ max_retain_frames = f;
+}
+
void ResourceManager::add_resource(Resource &r)
{
insert_unique(resources, &r, ManagedResource(r));
ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
if(!managed->loaded && !managed->loader && policy!=LOAD_MANUALLY)
load_resource(*managed->resource);
+
+ managed->last_used = frame;
+ if(max_retain_frames && !next_unload)
+ next_unload = frame+max_retain_frames;
}
void ResourceManager::remove_resource(Resource &r)
queue.pop_front();
thread.set_resource(managed);
}
+
+ ++frame;
+ if(frame==next_unload)
+ {
+ unsigned unload_limit = frame-max_retain_frames;
+ next_unload = 0;
+
+ for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
+ if(i->second.loaded)
+ {
+ if(i->second.last_used<=unload_limit)
+ i->second.unload();
+ else if(!next_unload || i->second.last_used<next_unload)
+ next_unload = i->second.last_used;
+ }
+
+ if(next_unload)
+ next_unload += max_retain_frames;
+ }
}
collection(0),
io(0),
loader(0),
- loaded(false)
+ loaded(false),
+ last_used(0)
{ }
void ResourceManager::ManagedResource::start_loading()
io = 0;
}
+void ResourceManager::ManagedResource::unload()
+{
+ resource->unload();
+ loaded = false;
+}
+
ResourceManager::LoadingThread::LoadingThread(ResourceManager &m):
manager(m),
IO::Seekable *io;
Resource::AsyncLoader *loader;
bool loaded;
+ unsigned last_used;
ManagedResource(Resource &);
void start_loading();
void finish_loading();
+ void unload();
};
class LoadingThread: public Thread
bool async_loads;
ResourceMap resources;
LoadQueue queue;
+ unsigned frame;
+ unsigned min_retain_frames;
+ unsigned max_retain_frames;
+ unsigned next_unload;
LoadingThread thread;
public:
void set_loading_policy(LoadingPolicy);
void set_async_loads(bool);
+ void set_max_retain_frames(unsigned);
void add_resource(Resource &);
void *get_data_for_resource(const Resource &);