]> git.tdb.fi Git - libs/gl.git/commitdiff
Implement manual loading policy and async flag
authorMikko Rasa <tdb@tdb.fi>
Sun, 7 Sep 2014 17:16:36 +0000 (20:16 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 7 Sep 2014 17:16:36 +0000 (20:16 +0300)
source/resource.cpp
source/resource.h
source/resourcemanager.cpp
source/resourcemanager.h
source/texture.cpp

index c2cf94d5391909e3f199a46f658d2a182c810d80..e63341c8517296e1083d25a5daa9ab28c705f587 100644 (file)
@@ -20,7 +20,12 @@ void Resource::set_manager(ResourceManager *m)
                manager->remove_resource(*this);
        manager = m;
        if(manager)
+       {
                manager->add_resource(*this);
+               manager_data = manager->get_data_for_resource(*this);
+       }
+       else
+               manager_data = 0;
 }
 
 } // namespace GL
index c41b88498dae84c5a0774398d410259b71055b5d..625c9f93059dd122cdf2ee526d85568a839f0dce 100644 (file)
@@ -24,12 +24,14 @@ public:
 
 protected:
        ResourceManager *manager;
+       void *manager_data;
 
        Resource();
 public:
        virtual ~Resource();
 
        void set_manager(ResourceManager *);
+       void *get_manager_data() const { return manager_data; }
        virtual AsyncLoader *load(IO::Seekable &) = 0;
        virtual void unload() = 0;
 };
index b778175e12d7d59e8898b1f7c53b4eace1cc5cef..cc69b895aa2ecd33196df0b087e0d3521ee2999c 100644 (file)
@@ -20,11 +20,26 @@ ResourceManager::~ResourceManager()
                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);
@@ -35,7 +50,7 @@ void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c
                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)
@@ -44,16 +59,22 @@ void ResourceManager::load_resource(const Resource &r)
        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)
@@ -83,9 +104,31 @@ ResourceManager::ManagedResource::ManagedResource(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),
@@ -152,10 +195,7 @@ void ResourceManager::LoadingThread::sync()
 
        if(finished)
        {
-               delete resource->loader;
-               resource->loader = 0;
-               delete resource->io;
-               resource->io = 0;
+               resource->finish_loading();
                resource = 0;
                state = IDLE;
        }
index 49a4c0bde3a7d33b1a02da359c486257a2e9b735..1e3cb54f3b439c72fe26811ccd546d6993743327 100644 (file)
@@ -28,8 +28,12 @@ private:
                std::string name;
                IO::Seekable *io;
                Resource::AsyncLoader *loader;
+               bool loaded;
 
                ManagedResource(Resource &);
+
+               void start_loading();
+               void finish_loading();
        };
 
        class LoadingThread: public Thread
@@ -82,8 +86,10 @@ public:
        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();
index a23e57c72615ed632e67c863e9ed37539842dcd2..aec1bc92da90b9b84f8882991f9b6bf4a24d84c2 100644 (file)
@@ -178,13 +178,13 @@ void Texture::set_compare_func(Predicate f)
 
 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);