Browse Source

refactoring resources loading public api

dmuratshin 9 years ago
parent
commit
af293777e2

+ 2 - 4
examples/Demo/src/TestManageRes.h

@@ -15,7 +15,6 @@ public:
 #ifdef EMSCRIPTEN
 #else
         addButton("mt", "Multithreading loading");
-        addButton("mt_slow", "MT loading (slow demo)");
 #endif
 
 
@@ -96,11 +95,10 @@ public:
             spThreadLoading l = new ThreadLoading;
             l->addEventListener(ThreadLoading::COMPLETE, CLOSURE(this, &ManageResTest::_loaded));
 
-            addRef();//protect Test instance from automatic delete if you close it to fast
+            addRef();//protect Test instance from automatic delete if you close it too fast
 
             l->add(&resources);
-            if (id == "mt_slow")
-                l->setUpdateSize(128);
+
             l->start(getStage());
         }
     }

+ 1 - 4
oxygine/src/DebugActor.cpp

@@ -55,10 +55,7 @@ namespace oxygine
 
         file::mount(&zp);
         resSystem = new Resources;
-        resSystem->loadXML("system/res.xml", 0, true, false, "system");
-        //
-
-        //log::messageln("initialized DebugActor");
+        resSystem->loadXML2("system/res.xml", ResourcesLoadOptions().prebuiltFolder("system"));
     }
 
     void DebugActor::show()

+ 0 - 12
oxygine/src/Multithreading.cpp

@@ -145,11 +145,6 @@ namespace oxygine
         }
     }
 
-    void ThreadLoading::setUpdateSize(int s)
-    {
-        _m2n.setUpdateSize(s);
-    }
-
     void ThreadLoading::start(spActor parent)
     {
         {
@@ -161,11 +156,4 @@ namespace oxygine
         pthread_create(&_thread, 0, _staticThreadFunc, this);
         //_thread = s3eThreadCreate(_staticThreadFunc, this);
     }
-
-    /*
-    void ThreadLoading::stop()
-    {
-
-    }
-    */
 }

+ 0 - 4
oxygine/src/Multithreading.h

@@ -39,10 +39,6 @@ namespace oxygine
         void load();
         void unload();
 
-
-        /**Changes size of updated Texture rectangle each frame. Use it only for tests*/
-        void setUpdateSize(int s);
-
     private:
         static void* _staticThreadFunc(void* t);
         virtual void _threadFunc();

+ 10 - 13
oxygine/src/res/ResAtlas.cpp

@@ -43,16 +43,13 @@ namespace oxygine
         if (!ad.texture)
             return;
 
-        MemoryTexture mt;
+        spMemoryTexture mt = new MemoryTexture;
         Rect bounds = ad.atlas.getBounds();
 
         int w = nextPOT(bounds.getRight());
         int h = nextPOT(bounds.getBottom());
 
-        mt.init(ad.mt.lock().getRect(Rect(0, 0, w, h)));
-
-        ImageData image_data = mt.lock();
-
+        mt->init(ad.mt.lock().getRect(Rect(0, 0, w, h)));
 #if 0
         static int n = 0;
         n++;
@@ -61,12 +58,12 @@ namespace oxygine
         saveImage(image_data, name);
 #endif
 
-        ad.texture->init(image_data, false);
-        ad.mt.unlock();
-
-        ad.texture->apply();
-        ad.texture->setLinearFilter(linear);
-        ad.texture->setClamp2Edge(clamp2edge);
+		CreateTextureTask task;
+		task.linearFilter = linear;
+		task.clamp2edge = clamp2edge;
+		task.src = mt;
+		task.dest = ad.texture;
+		LoadResourcesContext::get()->createTexture(task);
     }
 
     void next_atlas(int w, int h, TextureFormat tf, atlas_data& ad, const char* name)
@@ -599,7 +596,7 @@ namespace oxygine
 
                 ra->init(frames, columns, walker.getScaleFactor(), 1.0f / walker.getScaleFactor());
                 ra->setParent(this);
-                context.resources->add(ra, context.options->shortenIDS);
+                context.resources->add(ra, context.options->_shortenIDS);
             }
 
         }
@@ -825,7 +822,7 @@ namespace oxygine
 
                 ra->init(frames, columns, walker.getScaleFactor(), 1.0f / frame_scale);
                 ra->setParent(this);
-                context.resources->add(ra, context.options->shortenIDS);
+                context.resources->add(ra, context.options->_shortenIDS);
             }
         }
     }

+ 17 - 13
oxygine/src/res/Resources.cpp

@@ -82,9 +82,9 @@ namespace oxygine
         return getT<ResFont>(id, ep);
     }
 
-    void Resources::load(LoadResourcesContext* context, ResLoadedCallback cb)
+    void Resources::load(ResLoadedCallback cb)
     {
-        _Resource::load(context);
+        _Resource::load(0);
         //if (cb)
         //  cb(thi)
     }
@@ -166,10 +166,15 @@ namespace oxygine
         }
     };
 
-    void Resources::load(const std::string& xmlFile, const ResourcesLoadOptions& opt)
+	void Resources::load(const std::string& xmlFile, const ResourcesLoadOptions& opt)
+	{
+		loadXML2(xmlFile, opt);
+	}
+
+    void Resources::loadXML2(const std::string& xmlFile, const ResourcesLoadOptions& opt)
     {
         _name = xmlFile;
-        _loadCounter = opt.loadCompletely ? 1 : 0;
+        _loadCounter = opt._loadCompletely ? 1 : 0;
 
 
         FS_LOG("step0");
@@ -185,7 +190,7 @@ namespace oxygine
         char destTail[255];
         path::split(xmlFile.c_str(), destHead, destTail);
 
-        std::string prebuilt_folder = opt.prebuilFolder + "/" + destTail + ".ox/";
+        std::string prebuilt_folder = opt._prebuilFolder + "/" + destTail + ".ox/";
         if (prebuilt_folder[0] == '/')
         {
             prebuilt_folder.erase(prebuilt_folder.begin());
@@ -232,7 +237,7 @@ namespace oxygine
         FS_LOG("loading xml resources");
 
         std::string xmlFolder = destHead;
-        XmlWalker walker(&xmlFolder, "", 1.0f, opt.loadCompletely, true, resources, resources_meta);
+        XmlWalker walker(&xmlFolder, "", 1.0f, opt._loadCompletely, true, resources, resources_meta);
 
         while (true)
         {
@@ -266,12 +271,12 @@ namespace oxygine
             FS_LOG("resource: %s ", name);
             Resource* res = r.cb(context);
             OX_ASSERT(res);
-            res->setUseLoadCounter(opt.useLoadCounter);
+            res->setUseLoadCounter(opt._useLoadCounter);
 
             if (res)
             {
                 if (context.walker.getLoad())
-                    res->load(opt.loadContext);
+                    res->load(0);
                 res->setParent(this);
                 _resources.push_back(res);
             }
@@ -287,11 +292,10 @@ namespace oxygine
         const std::string& prebuilt_folder_)
     {
         ResourcesLoadOptions opt;
-        opt.loadContext = load_context;
-        opt.loadCompletely = load_completely;
-        opt.useLoadCounter = use_load_counter;
-        opt.prebuilFolder = prebuilt_folder_;
-        load(xml_name, opt);
+        opt._loadCompletely = load_completely;
+        opt._useLoadCounter = use_load_counter;
+        opt._prebuilFolder = prebuilt_folder_;
+        loadXML2(xml_name, opt);
     }
 
     void Resources::addShortIDS()

+ 23 - 9
oxygine/src/res/Resources.h

@@ -30,13 +30,22 @@ namespace oxygine
     class ResourcesLoadOptions
     {
     public:
-        ResourcesLoadOptions() : loadCompletely(true), useLoadCounter(false), loadContext(0), shortenIDS(false) {};
+        ResourcesLoadOptions() : _loadCompletely(true), _useLoadCounter(false), _shortenIDS(false) {};
 
-        bool loadCompletely;
-        bool useLoadCounter;
-        bool shortenIDS;
-        std::string prebuilFolder;
-        LoadResourcesContext* loadContext;
+		//load only Resources definitions. Skips internal heavy data (atlasses/textures/buffers). Could be overridden in xml: <your_res_type ... load = "false"/>
+		ResourcesLoadOptions& dontLoadAll(bool v = false) { _loadCompletely = v; return *this; }
+
+		//use load counter internally
+		ResourcesLoadOptions& useLoadCounter(bool v = true) { _useLoadCounter = v; return *this; }
+
+		//use not standard folder with prebuilt resources (atlasses, fonts, etc)
+		ResourcesLoadOptions& prebuiltFolder(const std::string &folder) {_prebuilFolder = folder; return *this; }
+
+
+        bool _loadCompletely;
+        bool _useLoadCounter;
+        bool _shortenIDS;
+        std::string _prebuilFolder;
     };
 
     class Resources: public _Resource
@@ -57,7 +66,7 @@ namespace oxygine
         ~Resources();
 
         /**
-        DEPRECATED, USE load method
+        DEPRECATED, USE loadXML2 method
         Loads resources from xml file.
         @param xml file path
         @param used for multi threading loading
@@ -65,6 +74,7 @@ namespace oxygine
         @param use load counter internally
         @param use not standard folder with prebuilt resources (atlasses, fonts, etc)
         */
+		OXYGINE_DEPRECATED
         void loadXML(const std::string& file, LoadResourcesContext* load_context = 0,
                      bool load_completely = true, bool use_load_counter = false,
                      const std::string& prebuilt_folder = "");
@@ -74,13 +84,17 @@ namespace oxygine
         @param xml file paths
         @param options
         */
-        void load(const std::string& xmlFile, const ResourcesLoadOptions& opt = ResourcesLoadOptions());
+        void loadXML2(const std::string& xmlFile, const ResourcesLoadOptions& opt = ResourcesLoadOptions());
+
+		//use loadXML2, later it will be renamed to loadXML
+		OXYGINE_DEPRECATED
+		void load(const std::string& xmlFile, const ResourcesLoadOptions& opt = ResourcesLoadOptions());
 
         /**Adds Resource*/
         void add(Resource* r, bool accessByShortenID = false);
 
         /**Calls Resource::load for each resource in the list*/
-        void load(LoadResourcesContext* context = 0, ResLoadedCallback cb = ResLoadedCallback());
+        void load(ResLoadedCallback cb = ResLoadedCallback());
 
         /**Unloads data from memory, all resources handles remain valid*/
         void unload();