From 97db48eeb942ae9f0c490983ce7a1f04379224b8 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 19 Jul 2009 11:05:45 +0100 Subject: [PATCH] Move everything into resources --- pclass.mtl => engines/pclass/pclass.mtl | 0 pclass.obj => engines/pclass/pclass.obj | 0 pclass.wings => engines/pclass/pclass.wings | Bin engines/pclass/pclass.xml | 5 ++ train.mtl => engines/red/train.mtl | 0 train.obj => engines/red/train.obj | 0 train.wings => engines/red/train.wings | Bin include/IModel.hpp | 4 +- include/{Resources.hpp => IResource.hpp} | 17 ++++++- include/ResourceCache.hpp | 2 +- src/Building.cpp | 4 +- src/Engine.cpp | 8 +-- src/Model.cpp | 48 +++++++++--------- src/{Resources.cpp => Resource.cpp} | 15 +++++- src/Waggon.cpp | 5 +- .../coal_truck/coal_truck.mtl | 0 .../coal_truck/coal_truck.obj | 0 .../coal_truck/coal_truck.wings | Bin waggons/coal_truck/coal_truck.xml | 5 ++ .../waggon_base.wings | Bin 20 files changed, 79 insertions(+), 34 deletions(-) rename pclass.mtl => engines/pclass/pclass.mtl (100%) rename pclass.obj => engines/pclass/pclass.obj (100%) rename pclass.wings => engines/pclass/pclass.wings (100%) create mode 100644 engines/pclass/pclass.xml rename train.mtl => engines/red/train.mtl (100%) rename train.obj => engines/red/train.obj (100%) rename train.wings => engines/red/train.wings (100%) rename include/{Resources.hpp => IResource.hpp} (75%) rename src/{Resources.cpp => Resource.cpp} (89%) rename coal_truck.mtl => waggons/coal_truck/coal_truck.mtl (100%) rename coal_truck.obj => waggons/coal_truck/coal_truck.obj (100%) rename coal_truck.wings => waggons/coal_truck/coal_truck.wings (100%) create mode 100644 waggons/coal_truck/coal_truck.xml rename waggon_base.wings => waggons/waggon_base.wings (100%) diff --git a/pclass.mtl b/engines/pclass/pclass.mtl similarity index 100% rename from pclass.mtl rename to engines/pclass/pclass.mtl diff --git a/pclass.obj b/engines/pclass/pclass.obj similarity index 100% rename from pclass.obj rename to engines/pclass/pclass.obj diff --git a/pclass.wings b/engines/pclass/pclass.wings similarity index 100% rename from pclass.wings rename to engines/pclass/pclass.wings diff --git a/engines/pclass/pclass.xml b/engines/pclass/pclass.xml new file mode 100644 index 0000000..9552b46 --- /dev/null +++ b/engines/pclass/pclass.xml @@ -0,0 +1,5 @@ + + + SECR P Class + pclass.obj + diff --git a/train.mtl b/engines/red/train.mtl similarity index 100% rename from train.mtl rename to engines/red/train.mtl diff --git a/train.obj b/engines/red/train.obj similarity index 100% rename from train.obj rename to engines/red/train.obj diff --git a/train.wings b/engines/red/train.wings similarity index 100% rename from train.wings rename to engines/red/train.wings diff --git a/include/IModel.hpp b/include/IModel.hpp index 2a29215..2051490 100644 --- a/include/IModel.hpp +++ b/include/IModel.hpp @@ -20,6 +20,7 @@ #include "Platform.hpp" #include "Maths.hpp" +#include "IResource.hpp" #include @@ -33,6 +34,7 @@ struct IModel { typedef std::tr1::shared_ptr IModelPtr; // Load a model from a WaveFront .obj file -IModelPtr loadModel(const std::string& fileName, float aScale = 1.0); +IModelPtr loadModel(IResourcePtr aRes, const string& aFileName, + float aScale = 1.0f); #endif diff --git a/include/Resources.hpp b/include/IResource.hpp similarity index 75% rename from include/Resources.hpp rename to include/IResource.hpp index 48d7c1d..05aa22f 100644 --- a/include/Resources.hpp +++ b/include/IResource.hpp @@ -22,6 +22,7 @@ #include #include +#include // Interface to game resource // A game resource is a directory containing related media files @@ -31,7 +32,21 @@ struct IResource { virtual ~IResource() {} virtual string name() const = 0; - virtual string xmlFileName() const = 0; + virtual string xmlFileName() const = 0; // REMOVE + + // A handle for reading data out of files in resources + class Handle { + public: + explicit Handle(const string& aFileName); + + ifstream& stream() { return *myStream; } + string fileName() const { return myFileName; } + private: + shared_ptr myStream; + const string myFileName; + }; + + virtual Handle openFile(const string& aName) = 0; }; typedef shared_ptr IResourcePtr; diff --git a/include/ResourceCache.hpp b/include/ResourceCache.hpp index fd11aa9..38c6946 100644 --- a/include/ResourceCache.hpp +++ b/include/ResourceCache.hpp @@ -21,7 +21,7 @@ #include #include -#include "Resources.hpp" +#include "IResource.hpp" // A generic cache for resources template diff --git a/src/Building.cpp b/src/Building.cpp index 0e7468e..4548e7d 100644 --- a/src/Building.cpp +++ b/src/Building.cpp @@ -16,7 +16,7 @@ // #include "IBuilding.hpp" -#include "Resources.hpp" +#include "IResource.hpp" #include "ResourceCache.hpp" #include "ILogger.hpp" #include "IXMLParser.hpp" @@ -52,7 +52,7 @@ void Building::text(const string& localName, const string& aString) if (localName == "name") myName = aString; else if (localName == "model") - myModel = loadModel(aString); + myModel = loadModel(myResource, aString); } namespace { diff --git a/src/Engine.cpp b/src/Engine.cpp index 7729144..9f78df5 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -22,8 +22,6 @@ #include -using namespace std; - // // READ THIS FIRST: physics model used by the steam engine // @@ -57,7 +55,7 @@ using namespace std; // Concrete implementation of a steam engine class Engine : public IRollingStock, public IController, - public std::tr1::enable_shared_from_this { + public enable_shared_from_this { public: Engine(); @@ -111,7 +109,9 @@ Engine::Engine() myStatTractiveEffort(34.7), isBrakeOn(true), myThrottle(0) { - myModel = loadModel("pclass.obj", MODEL_SCALE); + IResourcePtr res = findResource("pclass", "engines"); + + myModel = loadModel(res, "pclass.obj", MODEL_SCALE); } // Draw the engine model diff --git a/src/Model.cpp b/src/Model.cpp index 5e471f4..8770f99 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -20,6 +20,7 @@ #include "ITexture.hpp" #include "ILogger.hpp" #include "IMesh.hpp" +#include "ResourceCache.hpp" #include #include @@ -46,7 +47,7 @@ namespace { // Abstracts a WaveFront material file class MaterialFile { public: - MaterialFile(const string& aFileName); + MaterialFile(IResource::Handle aHandle); ~MaterialFile() {} const Material& get(const string& aName) const; @@ -57,14 +58,12 @@ private: typedef tr1::shared_ptr MaterialFilePtr; -MaterialFile::MaterialFile(const string& aFileName) +MaterialFile::MaterialFile(IResource::Handle aHandle) { - ifstream is(aFileName.c_str()); - if (!is.good()) - throw runtime_error("Failed to load material: " + aFileName); - - log() << "Loading materials from " << aFileName; + log() << "Loading materials from " << aHandle.fileName(); + istream& is = aHandle.stream(); + string activeMaterial; while (!is.eof()) { string word; @@ -146,22 +145,21 @@ void Model::render() const (*it)->render(); } -// Load a WaveFront .obj model from disk or the cache -// Each vertex is scaled by `aScale' -IModelPtr loadModel(const string& fileName, float aScale) +// Load a model from a resource +IModelPtr loadModel(IResourcePtr aRes, const string& aFileName, float aScale) { - ModelCache::iterator it = theCache.find(fileName); + // Make a unique cache name + const string cacheName = aRes->name() + ":" + aFileName; + + // Check the cache for the model + ModelCache::iterator it = theCache.find(cacheName); if (it != theCache.end()) return (*it).second; - - ifstream f(fileName.c_str()); - if (!f.good()) { - ostringstream ss; - ss << "Failed to open model: " << fileName; - throw runtime_error(ss.str()); - } - log() << "Loading model " << fileName; + log() << "Loading model " << cacheName; + + // Not in the cache, load it from the resource + IResource::Handle h = aRes->openFile(aFileName); vector vertices; vector normals; @@ -176,6 +174,8 @@ IModelPtr loadModel(const string& fileName, float aScale) int faceCount = 0; MaterialFilePtr materialFile; + + ifstream& f = h.stream(); while (!f.eof()) { string first; @@ -194,7 +194,8 @@ IModelPtr loadModel(const string& fileName, float aScale) string fileName; f >> fileName; - materialFile = MaterialFilePtr(new MaterialFile(fileName)); + materialFile = + MaterialFilePtr(new MaterialFile(aRes->openFile(fileName))); } else if (first == "v") { // Vertex @@ -320,8 +321,9 @@ IModelPtr loadModel(const string& fileName, float aScale) << faceCount << " faces"; IModelPtr ptr(new Model(dim, meshes)); - - theCache[fileName] = ptr; - + + theCache[cacheName] = ptr; return ptr; } + + diff --git a/src/Resources.cpp b/src/Resource.cpp similarity index 89% rename from src/Resources.cpp rename to src/Resource.cpp index fe1e904..13f4b46 100644 --- a/src/Resources.cpp +++ b/src/Resource.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include "Resources.hpp" +#include "IResource.hpp" #include "ILogger.hpp" #include @@ -40,10 +40,23 @@ public: { return (myPath / (name() + ".xml")).file_string(); } + + Handle openFile(const string& aFileName) + { + return Handle((myPath / aFileName).file_string()); + } private: const path myPath; }; +IResource::Handle::Handle(const string& aFileName) + : myStream(new ifstream(aFileName.c_str())), + myFileName(aFileName) +{ + if (!myStream->good()) + throw runtime_error("Failed to open resource file " + aFileName); +} + namespace { const char* classes[] = { "maps", "buildings", "engines", "waggons", diff --git a/src/Waggon.cpp b/src/Waggon.cpp index 7f6af58..ad9197d 100644 --- a/src/Waggon.cpp +++ b/src/Waggon.cpp @@ -17,6 +17,7 @@ #include "IRollingStock.hpp" #include "IModel.hpp" +#include "IResource.hpp" #include @@ -43,7 +44,9 @@ const float Waggon::MODEL_SCALE(0.4f); Waggon::Waggon() { - myModel = loadModel("coal_truck.obj", MODEL_SCALE); + IResourcePtr res = findResource("coal_truck", "waggons"); + + myModel = loadModel(res, "coal_truck.obj", MODEL_SCALE); } void Waggon::update(int aDelta) diff --git a/coal_truck.mtl b/waggons/coal_truck/coal_truck.mtl similarity index 100% rename from coal_truck.mtl rename to waggons/coal_truck/coal_truck.mtl diff --git a/coal_truck.obj b/waggons/coal_truck/coal_truck.obj similarity index 100% rename from coal_truck.obj rename to waggons/coal_truck/coal_truck.obj diff --git a/coal_truck.wings b/waggons/coal_truck/coal_truck.wings similarity index 100% rename from coal_truck.wings rename to waggons/coal_truck/coal_truck.wings diff --git a/waggons/coal_truck/coal_truck.xml b/waggons/coal_truck/coal_truck.xml new file mode 100644 index 0000000..62fbf34 --- /dev/null +++ b/waggons/coal_truck/coal_truck.xml @@ -0,0 +1,5 @@ + + + Coal truck + coal_truck.obj + diff --git a/waggon_base.wings b/waggons/waggon_base.wings similarity index 100% rename from waggon_base.wings rename to waggons/waggon_base.wings -- 2.39.2