]> git.mxchange.org Git - simgear.git/commitdiff
Merge branch 'next' of git://gitorious.org/fg/simgear into next
authorFrederic Bouvier <fredfgfs01@free.fr>
Thu, 9 Sep 2010 18:06:27 +0000 (20:06 +0200)
committerFrederic Bouvier <fredfgfs01@free.fr>
Thu, 9 Sep 2010 18:06:27 +0000 (20:06 +0200)
15 files changed:
projects/VC100/SimGear.vcxproj
projects/VC90/SimGear.vcproj
simgear/misc/Makefile.am
simgear/misc/ResourceManager.cxx [new file with mode: 0644]
simgear/misc/ResourceManager.hxx [new file with mode: 0644]
simgear/misc/sg_path.cxx
simgear/misc/sg_path.hxx
simgear/scene/model/modellib.cxx
simgear/scene/model/modellib.hxx
simgear/sound/openal_test2.cxx
simgear/sound/openal_test3.cxx
simgear/sound/sample_openal.cxx
simgear/sound/sample_openal.hxx
simgear/sound/xmlsound.cxx
simgear/sound/xmlsound.hxx

index 9e8d52268a1c70605a1100bdbd5cd56c88782f1f..8729073beef7f2c7d21e38e9ba6aec6fb7f572b3 100644 (file)
     <ClCompile Include="..\..\simgear\misc\tabbed_values.cxx" />
     <ClCompile Include="..\..\simgear\misc\texcoord.cxx" />
     <ClCompile Include="..\..\simgear\misc\zfstream.cxx" />
+    <ClCompile Include="..\..\simgear\misc\ResourceManager.cxx" />
+    <ClCompile Include="..\..\simgear\misc\ResourceManager.hxx" />
     <ClCompile Include="..\..\simgear\route\route.cxx" />
     <ClCompile Include="..\..\simgear\route\waypoint.cxx" />
     <ClCompile Include="..\..\simgear\screen\extensions.cxx" />
index 8de61f5b9a185de150acf6575324106863663267..8a245af3e006df464d3d9f58768829e20baab9c6 100644 (file)
                                RelativePath="..\..\simgear\misc\zfstream.hxx"
                                >
                        </File>
+                       <File
+                               RelativePath="..\..\simgear\misc\ResourceManager.cxx"
+                               >
+                       </File>
+                       <File
+                               RelativePath="..\..\simgear\misc\ResourceManager.hxx"
+                               >
+                       </File>
                </Filter>
                <Filter
                        Name="Lib_sgroute"
index 0e0fc94993a0833de1f8dcea4dd5ed0ec4674ae8..4a4b1fb607c7b429e422386d9ea94f2add7e6a1a 100644 (file)
@@ -13,7 +13,8 @@ include_HEADERS = \
        interpolator.hxx \
        stdint.hxx \
        PathOptions.hxx \
-       sg_dir.hxx
+       sg_dir.hxx \
+       ResourceManager.hxx 
 
 libsgmisc_a_SOURCES = \
        sg_path.cxx \
@@ -24,7 +25,8 @@ libsgmisc_a_SOURCES = \
        zfstream.cxx \
        interpolator.cxx \
        PathOptions.cxx \
-       sg_dir.cxx
+       sg_dir.cxx \
+       ResourceManager.cxx 
 
 #noinst_PROGRAMS = tabbed_value_test swap_test
 
diff --git a/simgear/misc/ResourceManager.cxx b/simgear/misc/ResourceManager.cxx
new file mode 100644 (file)
index 0000000..178cc96
--- /dev/null
@@ -0,0 +1,104 @@
+// ResourceManager.cxx -- manage finding resources by names/paths
+// Copyright (C) 2010 James Turner
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+// $Id$
+
+#include <simgear_config.h>
+
+#include <simgear/misc/ResourceManager.hxx>
+
+namespace simgear
+{
+    
+static ResourceManager* static_manager = NULL;
+
+ResourceManager::ResourceManager()
+{
+    
+}
+
+ResourceManager* ResourceManager::instance()
+{
+    if (!static_manager) {
+        static_manager = new ResourceManager();
+    }
+    
+    return static_manager;
+}    
+
+/**
+ * trivial provider using a fixed base path
+ */
+class BasePathProvider : public ResourceProvider
+{
+public:
+    BasePathProvider(const SGPath& aBase, int aPriority) :
+        ResourceProvider(aPriority),
+        _base(aBase)
+    {
+        
+    }
+    
+    virtual SGPath resolve(const std::string& aResource, SGPath&) const
+    {
+        SGPath p(_base, aResource);
+        return p.exists() ? p : SGPath();
+    }
+private:
+    SGPath _base;  
+};
+
+void ResourceManager::addBasePath(const SGPath& aPath, Priority aPriority)
+{
+    addProvider(new BasePathProvider(aPath, aPriority));
+}
+
+void ResourceManager::addProvider(ResourceProvider* aProvider)
+{
+    ProviderVec::iterator it = _providers.begin();
+    for (; it != _providers.end(); ++it) {
+      if (aProvider->priority() > (*it)->priority()) {
+        _providers.insert(it, aProvider);
+        return;
+      }
+    }
+    
+    // fell out of the iteration, goes to the end of the vec
+    _providers.push_back(aProvider);
+}
+
+SGPath ResourceManager::findPath(const std::string& aResource, SGPath aContext)
+{
+    if (!aContext.isNull()) {
+        SGPath r(aContext, aResource);
+        if (r.exists()) {
+            return r;
+        }
+    }
+    
+    ProviderVec::iterator it = _providers.begin();
+    for (; it != _providers.end(); ++it) {
+      SGPath path = (*it)->resolve(aResource, aContext);
+      if (!path.isNull()) {
+        return path;
+      }
+    }
+    
+    return SGPath();
+}
+
+} // of namespace simgear
diff --git a/simgear/misc/ResourceManager.hxx b/simgear/misc/ResourceManager.hxx
new file mode 100644 (file)
index 0000000..bfaa531
--- /dev/null
@@ -0,0 +1,93 @@
+// ResourceManager.hxx -- manage finding resources by names/paths
+// Copyright (C) 2010 James Turner
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+// $Id$
+
+
+#ifndef SG_RESOURCE_MANAGER_HXX
+#define SG_RESOURCE_MANAGER_HXX
+
+#include <vector>
+
+#include <simgear/misc/sg_path.hxx>
+
+namespace simgear
+{
+
+class ResourceProvider;
+
+/**
+ * singleton management of resources
+ */
+class ResourceManager
+{
+public:
+    typedef enum {
+      PRIORITY_DEFAULT = 0,
+      PRIORITY_FALLBACK = -100,
+      PRIORITY_NORMAL = 100,
+      PRIORITY_HIGH = 1000
+    } Priority;
+
+    static ResourceManager* instance();    
+    
+    /**
+     * add a simple fixed resource location, to resolve against
+     */
+    void addBasePath(const SGPath& aPath, Priority aPriority = PRIORITY_DEFAULT);
+    
+    /**
+     *
+     */
+    void addProvider(ResourceProvider* aProvider);
+    
+    /**
+     * given a resource name (or path), find the appropriate real resource
+     * path.
+     * @param aContext an optional current location to resolve relative names
+     *   against (e.g a current directory)
+     */
+    SGPath findPath(const std::string& aResource, SGPath aContext = SGPath());
+    
+private:
+    ResourceManager();
+    
+    typedef std::vector<ResourceProvider*> ProviderVec;
+    ProviderVec _providers;
+};      
+    
+class ResourceProvider
+{
+public:
+    virtual SGPath resolve(const std::string& aResource, SGPath& aContext) const = 0;
+    
+    virtual int priority() const
+    {
+      return _priority;
+    }
+    
+protected:
+    ResourceProvider(int aPriority) :
+      _priority(aPriority)
+    {}
+    
+    int _priority;
+};
+    
+} // of simgear namespace
+
+#endif // of header guard
index 0de0d925c514da478da55052bb62b5186d35eb26..73475b0c6d73654f6732e477bede035db156a30d 100644 (file)
@@ -358,3 +358,8 @@ bool SGPath::isAbsolute() const
   
   return (path[0] == sgDirPathSep);
 }
+
+bool SGPath::isNull() const
+{
+  return path.empty() || (path == "");
+}
index 6f8968b7d5ffb2db6c66423f7f6225b112e1f367..69dd59442441577b9e3eec73b843ae1febd7fd8c 100644 (file)
@@ -166,6 +166,11 @@ public:
      * I.e starts with a directory seperator, or a single character + colon
      */
     bool isAbsolute() const;
+    
+    /**
+     * check for default constructed path
+     */
+    bool isNull() const;
 private:
 
     void fix();
index cdcb724fe786fc34ff8fa55f2781cf205b118c37..ae90f1a46b6d6d4033c636fcacbbdec04c45adf7 100644 (file)
@@ -30,6 +30,7 @@
 #include <simgear/props/props_io.hxx>
 #include <simgear/scene/model/model.hxx>
 #include <simgear/scene/model/ModelRegistry.hxx>
+#include <simgear/misc/ResourceManager.hxx>
 
 #include "SGPagedLOD.hxx"
 #include "SGReaderWriterXML.hxx"
@@ -47,7 +48,6 @@ ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
 
 \fSGPropertyNode_ptr SGModelLib::static_propRoot;
 SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
-SGModelLib::resolve_func SGModelLib::static_resolver = NULL;
 
 ////////////////////////////////////////////////////////////////////////
 // Implementation of SGModelLib.
@@ -67,33 +67,15 @@ void SGModelLib::setPanelFunc(panel_func pf)
   static_panelFunc = pf;
 }
 
-void SGModelLib::setResolveFunc(resolve_func rf)
-{
-  static_resolver = rf;
-}
-
 std::string SGModelLib::findDataFile(const std::string& file, 
   const osgDB::ReaderWriter::Options* opts,
   SGPath currentPath)
 {
-  // if we have a valid current path, first attempt to resolve relative
-  // to that path
-  if (currentPath.exists()) {
-    SGPath p = currentPath;
-    p.append(file);
-    if (p.exists()) {
-      return p.str();
-    }
-  }
-  
-  // next try the resolve function if one has been defined
-  if (static_resolver) {
-    SGPath p = static_resolver(file);
-    if (p.exists()) {
-      return p.str();
-    }
+  SGPath p = ResourceManager::instance()->findPath(file, currentPath);
+  if (p.exists()) {
+    return p.str();
   }
-  
+      
   // finally hand on to standard OSG behaviour
   return osgDB::findDataFile(file, opts);
 }
index d28f692bc5aa3ec8ffda411c5e1fd7c87a85fafd..63358855b31a07a7f4a1893421713f91fa1aa92e 100644 (file)
@@ -44,16 +44,12 @@ class SGModelLib
 public:
     typedef osg::Node *(*panel_func)(SGPropertyNode *);
 
-    typedef SGPath (*resolve_func)(const std::string& path);
-
     static void init(const std::string &root_dir);
 
     static void setPropRoot(SGPropertyNode* root);
     
     static void setPanelFunc(panel_func pf);
     
-    static void setResolveFunc(resolve_func rf);
-
     // Load a 3D model (any format)
     // data->modelLoaded() will be called after the model is loaded
     static osg::Node* loadModel(const std::string &path,
@@ -80,7 +76,6 @@ protected:
 private:
   static SGPropertyNode_ptr static_propRoot;
   static panel_func static_panelFunc;
-  static resolve_func static_resolver;
 };
 
 
index eb0da57e5007ccdd98a3b9c361da446156b3ee37..13a62d63cef70448c570d624e2ebed303f366c46 100644 (file)
@@ -26,7 +26,9 @@ int main( int argc, char *argv[] ) {
     smgr->set_position( SGVec3d::fromGeod(pos), pos );
     smgr->activate();
 
-    SGSoundSample *sample1 = new SGSoundSample( SRC_DIR, "jet.wav" );
+    SGPath srcDir(SRC_DIR);
+
+    SGSoundSample *sample1 = new SGSoundSample("jet.wav", srcDir);
     sample1->set_volume(1.0);
     sample1->set_pitch(1.0);
     sample1->play_looped();
@@ -35,7 +37,7 @@ int main( int argc, char *argv[] ) {
     printf("playing sample1\n");
     sleep(1);
 
-    SGSoundSample *sample2 = new SGSoundSample( SRC_DIR, "jet.wav" );
+    SGSoundSample *sample2 = new SGSoundSample("jet.wav", srcDir);
     sample2->set_volume(0.5);
     sample2->set_pitch(0.4);
     sample2->play_looped();
@@ -44,7 +46,7 @@ int main( int argc, char *argv[] ) {
     printf("playing sample2\n");
     sleep(1);
 
-    SGSoundSample *sample3 = new SGSoundSample( SRC_DIR, "jet.wav" );
+    SGSoundSample *sample3 = new SGSoundSample("jet.wav", srcDir);
     sample3->set_volume(0.5);
     sample3->set_pitch(0.8);
     sample3->play_looped();
@@ -53,7 +55,7 @@ int main( int argc, char *argv[] ) {
     printf("playing sample3\n");
     sleep(1);
 
-    SGSoundSample *sample4 = new SGSoundSample( SRC_DIR, "jet.wav" );
+    SGSoundSample *sample4 = new SGSoundSample("jet.wav", srcDir);
     sample4->set_volume(0.5);
     sample4->set_pitch(1.2);
     sample4->play_looped();
@@ -62,7 +64,7 @@ int main( int argc, char *argv[] ) {
     printf("playing sample4\n");
     sleep(1);
 
-    SGSoundSample *sample5 = new SGSoundSample( SRC_DIR, "jet.wav" );
+    SGSoundSample *sample5 = new SGSoundSample("jet.wav", srcDir);
     sample5->set_volume(0.5);
     sample5->set_pitch(1.6);
     sample5->play_looped();
@@ -71,7 +73,7 @@ int main( int argc, char *argv[] ) {
     printf("playing sample5\n");
     sleep(1);
 
-    SGSoundSample *sample6 = new SGSoundSample( SRC_DIR, "jet.wav" );
+    SGSoundSample *sample6 = new SGSoundSample("jet.wav", srcDir);
     sample6->set_volume(0.5);
     sample6->set_pitch(2.0);
     sample6->play_looped();
index 96a57a0460a9814eb26725d92be4391ca53240f1..fafb404538bd1bfbe43bb78b799ddaffcb65d54d 100644 (file)
@@ -25,8 +25,10 @@ int main( int argc, char *argv[] ) {
     smgr->set_volume(0.9);
     smgr->activate();
 
+    SGPath srcDir(SRC_DIR);
+
     printf("default position and orientation\n");
-    SGSoundSample *sample1 = new SGSoundSample( SRC_DIR, "jet.wav" );
+    SGSoundSample *sample1 = new SGSoundSample("jet.wav", srcDir);
     sample1->set_volume(1.0);
     sample1->set_pitch(1.0);
     sample1->play_looped();
index 7b3550027c114e9ceb3cdab86cd3f6e511408121..3c9cb864ad81403fc1d06794c3722046bde1d35f 100644 (file)
@@ -33,6 +33,7 @@
 #include <simgear/structure/exception.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/math/SGMath.hxx>
+#include <simgear/misc/ResourceManager.hxx>
 
 #include "soundmgr_openal.hxx"
 #include "sample_openal.hxx"
@@ -78,7 +79,7 @@ SGSoundSample::SGSoundSample() :
 }
 
 // constructor
-SGSoundSample::SGSoundSample( const char *path, const char *file ) :
+SGSoundSample::SGSoundSample(const char *file, const SGPath& currentDir) :
     _absolute_pos(SGVec3d::zeros()),
     _relative_pos(SGVec3d::zeros()),
     _direction(SGVec3d::zeros()),
@@ -110,11 +111,8 @@ SGSoundSample::SGSoundSample( const char *path, const char *file ) :
     _static_changed(true),
     _is_file(true)
 {
-    SGPath samplepath( path );
-    if ( strlen(file) ) {
-        samplepath.append( file );
-    }
-    _refname = samplepath.str();
+    SGPath p = simgear::ResourceManager::instance()->findPath(file, currentDir);
+    _refname = p.str();
 }
 
 // constructor
index 491ba572ca220a2646f1b5302b72446371af5b93..b2afba4f33fd1da89f138e491b038e115d0625dc 100644 (file)
@@ -60,11 +60,10 @@ public:
 
     /**
      * Constructor
-     * @param path Path name to sound
      * @param file File name of sound
        Buffer data is freed by the sample group
      */
-    SGSoundSample( const char *path, const char *file );
+    SGSoundSample(const char *file, const SGPath& currentDir);
 
     /**
      * Constructor.
index 616d9514f59d976da1c4e4a7f32437929a8e8b3c..2ca9cdfa8f2f6379726149264e0e4309033a8ad8 100644 (file)
@@ -84,7 +84,7 @@ SGXmlSound::~SGXmlSound()
 void
 SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
                  SGSampleGroup *sgrp, SGSampleGroup *avionics,
-                 const string &path)
+                 const SGPath& currentDir)
 {
 
    //
@@ -272,7 +272,7 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
    } else {
       _sgrp = sgrp;
    }
-   _sample = new SGSoundSample( path.c_str(), node->getStringValue("path", ""));
+   _sample = new SGSoundSample(node->getStringValue("path", ""), currentDir);
    if (!_sample->file_path().exists()) {
       throw sg_io_exception("XML sound: couldn't find file: " + _sample->file_path().str());
    }
index 141f5eef299bf1cbb7d98ea5735d7b59aee2d8e9..f3635b08a705393d2c8180bc2a71aeeb9681904e 100644 (file)
@@ -104,7 +104,7 @@ public:
    * @param path The path where the audio files remain.
    */
   virtual void init (SGPropertyNode *, SGPropertyNode *, SGSampleGroup *,
-                     SGSampleGroup *, const string &);
+                     SGSampleGroup *, const SGPath& currentDir);
 
   /**
    * Check whether an event has happened and if action has to be taken.