begin cleaning up resource management
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Tue, 15 Jun 2010 18:45:46 +0000 (12:45 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Tue, 15 Jun 2010 18:45:46 +0000 (12:45 -0600)
src/Main.cc
src/moof/resource.cc
src/moof/resource.hh

index 8361b39d3dffe7a5e24eda753650e2722aad6092..6cdadaf23ff4e8087845e697ea2120233a906bbc 100644 (file)
@@ -277,112 +277,11 @@ void goodbye()
 }
 
 
-#include <moof/socket.hh>
-
-#include <fstream>
-
-class MyAsset
-{
-public:
-       MyAsset(const std::string& path)
-       {
-               moof::log_info("MyAsset loading:", path);
-
-               char buffer[1024];
-
-               std::ifstream stream(path.c_str());
-               stream.getline(buffer, sizeof(buffer));
-               str = buffer;
-               stream.close();
-
-               cool();
-       }
-
-       void cool()
-       {
-               moof::log_info("MyAsset COOL:", str);
-       }
-
-       void groovy()
-       {
-               moof::log_info("MyAsset GROOVY!!!!", str);
-       }
-
-       std::string str;
-};
-
-typedef moof::resource_handle<MyAsset> MyAsset_handle;
-
-class AnotherAsset
-{
-public:
-       AnotherAsset(const std::string& path, double d = 5.0)
-       {
-               moof::log_info("AnotherAsset loading:", path);
-               dude = d;
-       }
-
-
-       void cool()
-       {
-               moof::log_info("AnotherAsset cool", dude);
-       }
-
-       void groovy()
-       {
-               moof::log_info("AnotherAsset GROOVY!!!!", dude);
-       }
-
-       double dude;
-};
-
-
 int main(int argc, char* argv[])
 {
-       moof::resource::register_type<MyAsset>("mine");
-       
-       //moof::resource::add_type<AnotherAsset>("k");
-       
-       //{
-       //moof::resource_ptr myAsset = moof::resource::load(assetName,
-                       //"prefix", "mine");
-
-       //MyAsset_handle aCopy = myAsset;
-
-       //MyAsset_handle copy2 = moof::resource::load(assetName, "asdfas", "mine");
-
-       ////if (myAsset->check<MyAsset>()) myAsset->get<AnotherAsset>()->cool();
-       //myAsset->get<MyAsset>()->cool();
-       ////myAsset->get<AnotherAsset>()->groovy();
-       
-       //aCopy.get()->cool();
-       //copy2.get()->cool();
-
-       //log_info("rsrc ptr:", moof::resource::load(assetName, "", "mine"));
-       //}
-       //log_info("rsrc ptr:", moof::resource::load(assetName, "", "k"));
-       //moof::resource::load(assetName, "", "mine")->get<MyAsset>()->cool();
-
-       ////if (myAsset) myAsset.get()->cool();
-       ////else moof::log_error("asset not obtained...");
-       
-       MyAsset_handle myAsset = moof::resource::load("/home/chaz/meh.mine");
-       MyAsset* asset = myAsset.get();
-       if (asset) asset->cool();
-       else moof::log_warning("no asset obtained!!");
-
-       //moof::timer reloadTimer(
-                       //boost::bind(&moof::resource::reload_as_needed),
-                       //SCALAR(2.0),
-                       //moof::timer::repeat);
-
-       for (;;)
-       {
-               if (myAsset) myAsset.get()->cool();
-               moof::resource::reload_as_needed();
-               sleep(1);
-       }
-       
+       moof::timer reloadTimer(boost::bind(&moof::resource::reload_as_needed),
+                                                       SCALAR(2.0),
+                                                       moof::timer::repeat);
 
        if (argc > 1)
        {
index 5cd75360d85d5c8c5041d76da80987bc830cccf0..d22ae217a28d81269460fa8fda3201764bccc37d 100644 (file)
@@ -9,8 +9,15 @@
 *
 **************************************************************************/
 
+#include "../config.h"
+
 #include <queue>
 
+#ifdef USE_HOTLOADING
+#include <sys/inotify.h>
+#include <sys/ioctl.h>
+#endif
+
 #include <boost/algorithm/string.hpp>
 #include <boost/weak_ptr.hpp>
 
 #include "hash.hh"
 #include "resource.hh"
 
-#if HAVE_CONFIG_H
-#include "../config.h"
-#endif
-
-#ifdef USE_HOTLOADING
-#include <sys/inotify.h>
-#include <sys/ioctl.h>
-#endif
 
 #ifndef BUF_SIZE
 #define BUF_SIZE 4096
@@ -36,7 +35,7 @@
 namespace moof {
        
 
-static std::vector<std::string> search_paths_;
+static std::string search_paths_;
 
 typedef boost::weak_ptr<resource> resource_weakptr;
 static hash<std::string,resource_weakptr,hash_function> resource_table_;
@@ -185,66 +184,19 @@ void resource::reload()
 
 void resource::add_search_paths(const std::string& paths)
 {
-       std::vector<std::string> pathList;
-       boost::split(pathList, paths, boost::is_any_of(":"));
-
-       add_search_paths(pathList);
-}
-
-void resource::add_search_paths(const std::vector<std::string>& pathList)
-{
-       std::vector<std::string>::const_iterator it;
-       for (it = pathList.begin(); it != pathList.end(); ++it)
-       {
-               std::string path(*it);
-
-               ASSERT(!path.empty() && "empty search path string");
-
-               // add a slash if there isn't one already
-               if (*path.rbegin() != '/') path += '/';
-
-#if defined(_WIN32)
-               //boost::replace_all(path, "/", "\\");
-#endif
-
-               search_paths_.push_back(path);
-               log_info << "added search path " << path << std::endl;
-       }
+       search_paths_ = paths;
 }
 
 
 bool resource::find(const std::string& path)
 {
-       FILE* file = open_file(path);
-       if (file)
-       {
-               fclose(file);
-               return true;
-       }
-
-       return false;
+       return !stlplus::lookup(path, search_paths_, ":").empty();
 }
 
 FILE* resource::open_file(const std::string& path, const std::string& mode)
 {
-#if defined(_WIN32)
-       // windows always has to be a little different
-       //boost::replace_all(path, "/", "\\");
-#endif
-
-       std::vector<std::string>::iterator it;
-       for (it = search_paths_.begin(); it != search_paths_.end(); ++it)
-       {
-               // check path relative to search path
-               std::string complete_path(*it);
-               complete_path += path;
-
-               FILE* file = fopen(complete_path.c_str(), mode.c_str());
-               if (file) return file;
-       }
-
-       // last ditch effort; maybe it's already a path to a valid resource
-       return fopen(path.c_str(), mode.c_str());
+       std::string file = stlplus::lookup(path, search_paths_, ":");
+       return fopen(file.c_str(), mode.c_str());
 }
 
 
index 7616d69c280ecde861e9857c1a1138bd3127a4d8..a269a2d04d46b6b9aea405563a2be7a2e836a005 100644 (file)
@@ -17,6 +17,8 @@
  * Interface for textures, sounds, and other types of resources.
  */
 
+#include "../config.h"
+
 #include <cstdio>
 #include <map>
 #include <stdexcept>
 
 #include <moof/debug.hh>
 
-#if HAVE_CONFIG_H
-#include "../config.h"
-#endif
-
 
 namespace moof {
 
@@ -58,12 +56,6 @@ public:
         */
        static void add_search_paths(const std::string& paths);
 
-       /**
-        * Add directories to search when looking for resource files.
-        * \param pathList The list of directory paths.
-        */
-       static void add_search_paths(const std::vector<std::string>& pathList);
-
 
        /**
         * Get the path to a resource of a given name.
This page took 0.02389 seconds and 4 git commands to generate.