]> git.tdb.fi Git - libs/gl.git/blobdiff - source/resourcemanager.cpp
Give meshes a higher priority when loading
[libs/gl.git] / source / resourcemanager.cpp
index f008fd272c212462d6db90bdfccc020b7298015a..7d5d752b2351a3607e17b953212d981cde1c1792 100644 (file)
@@ -4,6 +4,7 @@
 #include <msp/strings/format.h>
 #include <msp/time/utils.h>
 #include "resourcemanager.h"
+#include "resources.h"
 #include "resourcewatcher.h"
 
 using namespace std;
@@ -11,6 +12,10 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+resource_load_error::resource_load_error(const string &name, const string &err):
+       runtime_error(format("%s: %s", name, err))
+{ }
+
 resource_load_error::resource_load_error(const string &name, const exception &exc):
        runtime_error(format("%s: %s: %s", name, Debug::demangle(typeid(exc).name()), exc.what()))
 { }
@@ -101,7 +106,9 @@ void ResourceManager::load_resource(Resource &r)
        if(async_loads)
        {
                managed.state = ManagedResource::LOAD_QUEUED;
-               queue.push_back(&managed);
+               LoadQueue::iterator i;
+               for(i=queue.begin(); (i!=queue.end() && (*i)->load_priority>=managed.load_priority); ++i) ;
+               queue.insert(i, &managed);
        }
        else
        {
@@ -120,6 +127,8 @@ bool ResourceManager::is_resource_loaded(const Resource &r) const
 void ResourceManager::resource_used(const Resource &r)
 {
        ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
+       if(!managed)
+               return;
        if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
                load_resource(*managed->resource);
 
@@ -264,6 +273,7 @@ ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, con
 
 ResourceManager::ManagedResource::ManagedResource(Resource &r):
        resource(&r),
+       load_priority(r.get_load_priority()),
        io(0),
        loader(0),
        state(NOT_LOADED),
@@ -274,7 +284,11 @@ ResourceManager::ManagedResource::ManagedResource(Resource &r):
 void ResourceManager::ManagedResource::start_loading()
 {
        io = location.collection->open_raw(location.name);
-       loader = resource->load(*io);
+       if(!io)
+               throw resource_load_error(location.name, "open failed");
+
+       const Resources *res = dynamic_cast<Resources *>(location.collection);
+       loader = resource->load(*io, res);
        if(!loader)
        {
                delete io;