simple-registry.cpp

Go to the documentation of this file.
00001 /*
00002  * simple-registry.cpp
00003  *
00004  * Copyright (C) 2008-2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Simple implementation of a material registry
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include "glut-model.h"         // always include our own header first!
00012 
00013 #include <iostream>
00014 #include <fstream>
00015 
00016 #include "perf/perf.h"
00017 #include "util/file.h"
00018 #include "util/token_stream.h"
00019 
00020 
00021 namespace glut {
00022 
00023 
00024 ////////////////////////////////////////////////////////////////////////////////
00025 //
00026 //      SimpleRegistry -- object that implements the glut::MaterialRegistry
00027 //      interface.
00028 //
00029 ////////////////////////////////////////////////////////////////////////////////
00030 
00031 class SimpleRegistry : public glut::MaterialRegistry {
00032 public:
00033         SimpleRegistry(IN const char * material_dir) {
00034                         ASSERT(material_dir, "null");
00035                         m_materialDir = material_dir;
00036                 }
00037         ~SimpleRegistry(void) throw() { }
00038 
00039         smart_ptr<glut::material_t> getMaterial(IN const char * id) {
00040                         ASSERT(id, "null");
00041 
00042                         smart_ptr<glut::material_t> m = this->find(id);
00043                         if (m)
00044                                 return m;
00045 
00046                         // not in map!  load
00047                         std::string path = m_materialDir;
00048                         path += "/";
00049                         path += id;
00050 
00051                         DPRINTF("Loading '%s' from path: %s", id, path.c_str());
00052 
00053                         m = new glut::material_t;
00054                         ASSERT(m, "null");
00055 
00056                         std::ifstream infile(path.c_str());
00057                         if (!infile.good()) {
00058                                 WAVE_EX(wex);
00059                                 wex << "Failed to open material file: " << path;
00060                         }
00061                         try {
00062                                 expectToken(infile, "materialVersion");
00063                                 expectToken(infile, "0.1");
00064                                 expectToken(infile, "material");
00065                                 glut::parseMaterial(infile, m_materialDir.c_str(), *m);
00066                         } catch (std::exception& e) {
00067                                 WAVE_EX(wex);
00068                                 wex << "Error loading material '" << id << "' ";
00069                                 wex << "from path: " << path << "\n";
00070                                 wex << e.what();
00071                         }
00072 
00073                         m_materials[id] = m;
00074                         return m;
00075                 }
00076 
00077 private:
00078         typedef std::map<std::string, smart_ptr<glut::material_t> > material_map_t;
00079 
00080         smart_ptr<glut::material_t> find(IN const char * id) {
00081                         material_map_t::iterator i = m_materials.find(id);
00082                         if (m_materials.end() == i)
00083                                 return NULL;
00084                         return i->second;
00085                 }
00086 
00087         material_map_t          m_materials;
00088         std::string             m_materialDir;
00089 };
00090 
00091 
00092 
00093 ////////////////////////////////////////////////////////////////////////////////
00094 //
00095 //      public API
00096 //
00097 ////////////////////////////////////////////////////////////////////////////////
00098 
00099 smart_ptr<MaterialRegistry>
00100 getSimpleRegistry
00101 (
00102 IN const char * materialDir
00103 )
00104 {
00105         ASSERT(materialDir, "null");
00106         static smart_ptr<SimpleRegistry> s_registry;
00107         if (!s_registry) {
00108                 s_registry = new SimpleRegistry(materialDir);
00109                 ASSERT(s_registry, "out of memory");
00110         }
00111 
00112         return s_registry;
00113 }
00114 
00115 
00116 
00117 };      // glut namespace
00118