i->second.resource->set_manager(0);
}
+void ResourceManager::set_loading_policy(LoadingPolicy p)
+{
+ policy = p;
+}
+
+void ResourceManager::set_async_loads(bool a)
+{
+ async_loads = a;
+}
+
void ResourceManager::add_resource(Resource &r)
{
insert_unique(resources, &r, ManagedResource(r));
}
+void *ResourceManager::get_data_for_resource(const Resource &r)
+{
+ return &get_item(resources, &r);
+}
+
void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
{
ManagedResource &managed = get_item(resources, &r);
load_resource(r);
}
-void ResourceManager::load_resource(const Resource &r)
+void ResourceManager::load_resource(Resource &r)
{
ManagedResource &managed = get_item(resources, &r);
if(!managed.collection)
if(managed.loader)
return;
- managed.io = managed.collection->open_raw(managed.name);
- managed.loader = managed.resource->load(*managed.io);
- if(!managed.loader)
+ managed.start_loading();
+
+ if(async_loads)
+ queue.push_back(&managed);
+ else
{
- delete managed.io;
- managed.io = 0;
- throw logic_error("no loader created");
+ while(!managed.loader->process()) ;
+ managed.finish_loading();
}
+}
- queue.push_back(&managed);
+void ResourceManager::resource_used(const Resource &r)
+{
+ ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
+ if(!managed->loaded && !managed->loader && policy!=LOAD_MANUALLY)
+ load_resource(*managed->resource);
}
void ResourceManager::remove_resource(Resource &r)
resource(&r),
collection(0),
io(0),
- loader(0)
+ loader(0),
+ loaded(false)
{ }
+void ResourceManager::ManagedResource::start_loading()
+{
+ io = collection->open_raw(name);
+ loader = resource->load(*io);
+ if(!loader)
+ {
+ delete io;
+ io = 0;
+ throw logic_error("no loader created");
+ }
+}
+
+void ResourceManager::ManagedResource::finish_loading()
+{
+ delete loader;
+ loader = 0;
+ loaded = true;
+ delete io;
+ io = 0;
+}
+
ResourceManager::LoadingThread::LoadingThread(ResourceManager &m):
manager(m),
if(finished)
{
- delete resource->loader;
- resource->loader = 0;
- delete resource->io;
- resource->io = 0;
+ resource->finish_loading();
resource = 0;
state = IDLE;
}
std::string name;
IO::Seekable *io;
Resource::AsyncLoader *loader;
+ bool loaded;
ManagedResource(Resource &);
+
+ void start_loading();
+ void finish_loading();
};
class LoadingThread: public Thread
void set_async_loads(bool);
void add_resource(Resource &);
+ void *get_data_for_resource(const Resource &);
void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
- void load_resource(const Resource &);
+ void load_resource(Resource &);
+ void resource_used(const Resource &);
void remove_resource(Resource &);
void tick();
void Texture::bind_to(unsigned i) const
{
- if(!id)
- manager->load_resource(*this);
-
TexUnit &unit = TexUnit::get_unit(i);
const Texture *cur = unit.get_texture();
if(unit.set_texture(this))
{
+ if(manager)
+ manager->resource_used(*this);
+
unit.bind();
if(cur && cur->target!=target)
glDisable(cur->target);