From 2cac5c4988af83bb83084b42e03c375257d5a3f2 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 26 Jul 2009 21:07:14 +0100 Subject: [PATCH] Saving and loading buildings --- .gitignore | 3 +++ include/IMap.hpp | 4 ++++ include/ModelViewer.hpp | 2 ++ maps/coal_mine/coal_mine.xml | 16 ++++++++++++++++ schemas/map.xsd | 3 +++ src/Editor.cpp | 14 +++++++++++++- src/FLTKWindow.cpp | 2 +- src/Map.cpp | 30 +++++++++++++++++++++++++++--- src/ModelViewer.cpp | 10 +++++++++- 9 files changed, 78 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 002adf9..017e011 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ cmake_install.cmake *~ gmon.out TAGS +CPackConfig.cmake +CPackSourceConfig.cmake +install_manifest.txt \ No newline at end of file diff --git a/include/IMap.hpp b/include/IMap.hpp index 1de99c9..42fc228 100644 --- a/include/IMap.hpp +++ b/include/IMap.hpp @@ -22,6 +22,7 @@ #include "ITrackSegment.hpp" #include "IStation.hpp" #include "IResource.hpp" +#include "IBuilding.hpp" #include #include @@ -104,6 +105,9 @@ public: // Create a new station covering this area or extend an existing station virtual IStationPtr extendStation(Point aStartPos, Point aFinishPos) = 0; + + // Place a building at this location + virtual void placeBuilding(Point aPoint, IBuildingPtr aBuilding) = 0; }; diff --git a/include/ModelViewer.hpp b/include/ModelViewer.hpp index 14befbd..599edab 100644 --- a/include/ModelViewer.hpp +++ b/include/ModelViewer.hpp @@ -31,12 +31,14 @@ public: ~ModelViewer(); void setModel(IModelPtr aModel); + void rotate(float anAngle); // Fl_Gl_Window interface void draw(); int handle(int anEvent); private: IModelPtr myModel; + float myRotation; }; #endif diff --git a/maps/coal_mine/coal_mine.xml b/maps/coal_mine/coal_mine.xml index 14fd965..b054a37 100644 --- a/maps/coal_mine/coal_mine.xml +++ b/maps/coal_mine/coal_mine.xml @@ -521,6 +521,10 @@ + +white_house + + @@ -607,6 +611,10 @@ + +white_house + + @@ -621,6 +629,10 @@ + +white_house + + @@ -671,6 +683,10 @@ + +white_house + + diff --git a/schemas/map.xsd b/schemas/map.xsd index eeb403d..c318b19 100644 --- a/schemas/map.xsd +++ b/schemas/map.xsd @@ -72,6 +72,9 @@ + + diff --git a/src/Editor.cpp b/src/Editor.cpp index 1abb520..651aa2f 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -108,6 +108,8 @@ namespace { ModelViewer* theModelViewer; Editor* theEditor = NULL; + + static IBuildingPtr REMOVE_ME; void changeTool(Fl_Widget* aWidget, Editor::Tool aTool) { @@ -115,7 +117,8 @@ namespace { theEditor->setTool(aTool); if (aTool == Editor::BUILDING_TOOL) { - theModelViewer->setModel(loadBuilding("white_house")->model()); + REMOVE_ME = loadBuilding("white_house"); + theModelViewer->setModel(REMOVE_ME->model()); } } @@ -123,6 +126,11 @@ namespace { { theEditor->map()->save(); } + + void onBldRotateClick(Fl_Widget* aWidget) + { + theModelViewer->rotate(90.0f); + } } // Add the editor panel to the FLTK window @@ -141,6 +149,7 @@ void addEditorGUI() theBldNextButton = new Fl_Button(60, 240, 60, 25, "Next"); theBldRotateButton = new Fl_Button(120, 240, 60, 25, "Rotate"); + theBldRotateButton->callback(onBldRotateClick); theSaveButton = new Fl_Button(0, 273, panelW, 25, "Save"); theSaveButton->callback(onSaveClick); @@ -548,6 +557,9 @@ void Editor::onMouseRelease(IPickBufferPtr aPickBuffer, int x, int y, case STATION_TOOL: myMap->extendStation(myDragBegin, myDragEnd); break; + case BUILDING_TOOL: + myMap->placeBuilding(myDragBegin, REMOVE_ME); + break; } amDragging = false; diff --git a/src/FLTKWindow.cpp b/src/FLTKWindow.cpp index 5e8a935..5176153 100644 --- a/src/FLTKWindow.cpp +++ b/src/FLTKWindow.cpp @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include // An OpenGL window for the editor diff --git a/src/Map.cpp b/src/Map.cpp index be816c4..232c491 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -106,6 +106,7 @@ public: IStationPtr extendStation(Point aStartPos, Point aFinishPos); + void placeBuilding(Point aPoint, IBuildingPtr aBuilding); // ISectorRenderable interface void renderSector(IGraphicsPtr aContext, int id, @@ -116,8 +117,9 @@ public: private: // Tiles on the map struct Tile { - TrackNodePtr track; // Track at this location, if any - IStationPtr station; // Station on this tile, if any + TrackNodePtr track; // Track at this location, if any + IStationPtr station; // Station on this tile, if any + IBuildingPtr building; // Building on this tile, if any } *myTiles; // Vertices on the terrain @@ -735,6 +737,14 @@ void Map::renderSector(IGraphicsPtr aContext, int id, if (myStartLocation.x == x && myStartLocation.y == y && shouldDrawGridLines) drawStartLocation(); + + // Draw any buildings + if (tile.building) { + glPushMatrix(); + glTranslatef(static_cast(x), 0.0f, static_cast(y)); + tile.building->model()->render(); + glPopMatrix(); + } } } } @@ -947,6 +957,11 @@ void Map::lowerArea(const Point& aStartPos, changeAreaHeight(aStartPos, aFinishPos, -0.1f); } +void Map::placeBuilding(Point aPoint, IBuildingPtr aBuilding) +{ + tileAt(aPoint.x, aPoint.y).building = aBuilding; +} + // Either extend an existing station which borders this area // or build a new station IStationPtr Map::extendStation(Point aStartPos, Point aFinishPos) @@ -1155,6 +1170,13 @@ void Map::save() useful = true; } + if (tile.building) { + tileXml.addChild + (xml::element("building") + .addText("white_house")); + useful = true; + } + if (useful) tileset.addChild(tileXml); } @@ -1215,6 +1237,8 @@ public: { if (localName == "heightmap") myMap->readHeightMap(myResource->openFile(aString)); + else if (localName == "building") + myMap->placeBuilding(makePoint(myXPtr, myYPtr), loadBuilding(aString)); else if (myActiveStation) { if (localName == "name") { debug() << "Saw station " << aString; @@ -1243,7 +1267,7 @@ private: myStations[id] = myActiveStation; } - + void handleStart(const AttributeSet& attrs) { int x, y, dirX, dirY; diff --git a/src/ModelViewer.cpp b/src/ModelViewer.cpp index 4f3a40d..fb9077c 100644 --- a/src/ModelViewer.cpp +++ b/src/ModelViewer.cpp @@ -28,7 +28,7 @@ using namespace boost; ModelViewer::ModelViewer(int x, int y, int w, int h) - : Fl_Gl_Window(x, y, w, h) + : Fl_Gl_Window(x, y, w, h), myRotation(0.0f) { } @@ -38,6 +38,12 @@ ModelViewer::~ModelViewer() } +void ModelViewer::rotate(float anAngle) +{ + myRotation += anAngle; + redraw(); +} + void ModelViewer::draw() { static ILightPtr sun = makeSunLight(); @@ -50,6 +56,7 @@ void ModelViewer::draw() gluPerspective(45.0f, wf/hf, 0.1f, 50.0f); glMatrixMode(GL_MODELVIEW); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glEnable(GL_LIGHTING); @@ -62,6 +69,7 @@ void ModelViewer::draw() glRotatef(45.0f, 0.0f, 1.0f, 0.0f); glTranslatef(1.5f, -2.6f, -1.5f); glColor3f(1.0f, 1.0f, 1.0f); + glRotatef(myRotation, 0.0f, 1.0f, 0.0f); myModel->render(); } -- 2.39.2