From e62bf6d384cb8e45d9d412269d60e28871be40f3 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 19 Jul 2009 11:47:40 +0100 Subject: [PATCH] Read maps from resources --- maps/{ => figure8}/figure8.bin | Bin maps/{ => figure8}/figure8.xml | 2 +- src/Map.cpp | 36 +++++++++++++++++---------------- 3 files changed, 20 insertions(+), 18 deletions(-) rename maps/{ => figure8}/figure8.bin (100%) rename maps/{ => figure8}/figure8.xml (98%) diff --git a/maps/figure8.bin b/maps/figure8/figure8.bin similarity index 100% rename from maps/figure8.bin rename to maps/figure8/figure8.bin diff --git a/maps/figure8.xml b/maps/figure8/figure8.xml similarity index 98% rename from maps/figure8.xml rename to maps/figure8/figure8.xml index 03fb4a2..a47f77c 100644 --- a/maps/figure8.xml +++ b/maps/figure8/figure8.xml @@ -4,7 +4,7 @@ -maps/figure8.bin +figure8.bin diff --git a/src/Map.cpp b/src/Map.cpp index 3f89239..a546a3d 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -25,6 +25,7 @@ #include "XMLBuilder.hpp" #include "IMesh.hpp" #include "IStation.hpp" +#include "IResource.hpp" #include #include @@ -169,7 +170,7 @@ private: void resetMarks() const; void writeHeightMap(const string& aFileName) const; - void readHeightMap(const string& aFileName); + void readHeightMap(IResource::Handle aHandle); void tileVertices(int x, int y, int* indexes) const; void renderPickSector(Point botLeft, Point topRight); void drawStartLocation() const; @@ -1044,16 +1045,14 @@ void Map::writeHeightMap(const string& aFileName) const } // Read the height data back out of a binary file -void Map::readHeightMap(const string& aFileName) +void Map::readHeightMap(IResource::Handle aHandle) { using namespace boost; - log() << "Reading height map from " << aFileName; - - ifstream is(aFileName.c_str(), ios::binary); - if (!is.good()) - throw runtime_error("Failed to open " + aFileName + " for reading"); + log() << "Reading height map from " << aHandle.fileName(); + istream& is = aHandle.stream(); + // Check the dimensions of the binary file match the XML file int32_t wl, dl; is.read(reinterpret_cast(&wl), sizeof(int32_t)); @@ -1063,7 +1062,7 @@ void Map::readHeightMap(const string& aFileName) error() << "Expected width " << myWidth << " got " << wl; error() << "Expected height " << myDepth << " got " << dl; throw runtime_error - ("Binary file " + aFileName + " dimensions are incorrect"); + ("Binary file " + aHandle.fileName() + " dimensions are incorrect"); } for (int i = 0; i < (myWidth + 1) * (myDepth + 1); i++) @@ -1176,8 +1175,9 @@ IMapPtr makeEmptyMap(int aWidth, int aDepth) // Build a map through XML callbacks class MapLoader : public IXMLCallback { public: - MapLoader(shared_ptr aMap) - : myMap(aMap), myXPtr(0), myYPtr(0) {} + MapLoader(shared_ptr aMap, IResourcePtr aRes) + : myMap(aMap), myXPtr(0), myYPtr(0), + myResource(aRes) {} void startElement(const std::string& localName, const AttributeSet& attrs) @@ -1211,7 +1211,7 @@ public: void text(const string& localName, const string& aString) { if (localName == "heightmap") - myMap->readHeightMap(aString); + myMap->readHeightMap(myResource->openFile(aString)); else if (myActiveStation) { if (localName == "name") { debug() << "Saw station " << aString; @@ -1227,8 +1227,6 @@ private: attrs.get("width", width); attrs.get("height", height); - debug() << "width=" << width << ", height=" << height; - myMap->resetMap(width, height); } @@ -1322,18 +1320,22 @@ private: map myStations; IStationPtr myActiveStation; int myXPtr, myYPtr; + + IResourcePtr myResource; }; -IMapPtr loadMap(const string& aFileName) +IMapPtr loadMap(const string& aResId) { shared_ptr map(new Map); - log() << "Loading map from file " << aFileName; + IResourcePtr res = findResource(aResId, "maps"); + + log() << "Loading map from file " << res->xmlFileName(); static IXMLParserPtr xmlParser = makeXMLParser("schemas/map.xsd"); - MapLoader loader(map); - xmlParser->parse(aFileName, loader); + MapLoader loader(map, res); + xmlParser->parse(res->xmlFileName(), loader); return IMapPtr(map); } -- 2.39.2