FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
mapsaver.cpp
1 /***************************************************************************
2 * Copyright (C) 2005-2011 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "ext/tinyxml/fife_tinyxml.h"
31 #include "model/structures/map.h"
32 #include "model/structures/layer.h"
33 #include "model/structures/instance.h"
34 #include "model/metamodel/object.h"
35 #include "model/metamodel/grids/cellgrid.h"
36 #include "util/structures/point.h"
37 #include "util/structures/rect.h"
38 #include "view/visual.h"
39 #include "view/camera.h"
40 
41 #include "mapsaver.h"
42 
43 namespace FIFE {
44  static Logger _log(LM_NATIVE_SAVERS);
45 
47  //m_objectSaver = new ObjectSaver();
48  //m_animationSaver = new AnimationSaver();
49  //m_atlasSaver = new AtlasSaver();
50  }
51 
53  {
54 
55  }
56 
58  m_objectSaver = objectSaver;
59  }
60 
61 
63  m_animationSaver = animationSaver;
64  }
65 
66 
67  void MapSaver::setAtlasSaver(const FIFE::AtlasSaverPtr& atlasSaver) {
68  m_atlasSaver = atlasSaver;
69  }
70 
71  void MapSaver::save(const Map& map, const std::string& filename, const std::vector<std::string>& importFiles) {
72  TiXmlDocument doc;
73 
74  // add xml declaration
75  TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "ascii", "");
76  doc.LinkEndChild(decl);
77 
78  // add map element
79  TiXmlElement* mapElement = new TiXmlElement("map");
80  mapElement->SetAttribute("id", map.getId());
81  mapElement->SetAttribute("format", "1.0");
82  doc.LinkEndChild(mapElement);
83 
84  for (std::vector<std::string>::const_iterator iter = importFiles.begin(); iter != importFiles.end(); ++iter)
85  {
86  TiXmlElement* importElement = new TiXmlElement("import");
87  importElement->SetAttribute("file", *iter);
88 
89  // link to map element
90  mapElement->LinkEndChild(importElement);
91  }
92 
93  typedef std::list<Layer*> LayerList;
94  LayerList layers = map.getLayers();
95  for (LayerList::iterator iter = layers.begin(); iter != layers.end(); ++iter)
96  {
97  TiXmlElement* layerElement = new TiXmlElement("layer");
98  CellGrid* grid = (*iter)->getCellGrid();
99  layerElement->SetAttribute("id", (*iter)->getId());
100  layerElement->SetDoubleAttribute("x_offset", grid->getXShift());
101  layerElement->SetDoubleAttribute("y_offset", grid->getYShift());
102  layerElement->SetDoubleAttribute("z_offset", grid->getZShift());
103  layerElement->SetDoubleAttribute("x_scale", grid->getXScale());
104  layerElement->SetDoubleAttribute("y_scale", grid->getYScale());
105  layerElement->SetDoubleAttribute("rotation", grid->getRotation());
106  layerElement->SetAttribute("grid_type", grid->getType());
107  layerElement->SetAttribute("transparency", (*iter)->getLayerTransparency());
108 
109  std::string pathingStrategy;
110  switch ((*iter)->getPathingStrategy())
111  {
112  case CELL_EDGES_ONLY:
113  {
114  pathingStrategy = "cell_edges_only";
115  }
116  break;
117  case CELL_EDGES_AND_DIAGONALS:
118  {
119  pathingStrategy = "cell_edges_and_diagonals";
120  }
121  break;
122  case FREEFORM:
123  {
124  pathingStrategy = "freeform";
125  }
126  break;
127  default:
128  {
129  pathingStrategy = "cell_edges_only";
130  }
131  break;
132  }
133  layerElement->SetAttribute("pathing", pathingStrategy);
134 
135  // add layer to document
136  mapElement->LinkEndChild(layerElement);
137 
138  // add instances tag to document
139  TiXmlElement* instancesElement = new TiXmlElement("instances");
140  layerElement->LinkEndChild(instancesElement);
141 
142  std::string currentNamespace = "";
143  typedef std::vector<Instance*> InstancesContainer;
144  InstancesContainer instances = (*iter)->getInstances();
145  for (InstancesContainer::iterator iter = instances.begin(); iter != instances.end(); ++iter)
146  {
147  // create instance element
148  TiXmlElement* instanceElement = new TiXmlElement("i");
149 
150  Object* obj = (*iter)->getObject();
151  if (!obj->getNamespace().empty() && currentNamespace != obj->getNamespace())
152  {
153  instanceElement->SetAttribute("ns", obj->getNamespace());
154 
155  // update current namespace
156  currentNamespace = obj->getNamespace();
157  }
158 
159  if (!(*iter)->getId().empty())
160  {
161  instanceElement->SetAttribute("id", (*iter)->getId());
162  }
163 
164  instanceElement->SetAttribute("o", obj->getId());
165 
166  ExactModelCoordinate position = (*iter)->getLocationRef().getExactLayerCoordinates();
167  instanceElement->SetDoubleAttribute("x", position.x);
168  instanceElement->SetDoubleAttribute("y", position.y);
169  instanceElement->SetDoubleAttribute("z", position.z);
170  instanceElement->SetAttribute("r", (*iter)->getRotation());
171 
172  if ((*iter)->isBlocking())
173  {
174  instanceElement->SetAttribute("blocking", (*iter)->isBlocking());
175  }
176 
177  InstanceVisual* instanceVisual = (*iter)->getVisual<InstanceVisual>();
178  instanceElement->SetAttribute("stackpos", instanceVisual->getStackPosition());
179 
180  instancesElement->LinkEndChild(instanceElement);
181  }
182  }
183 
184  typedef std::vector<Camera*> CameraContainer;
185  CameraContainer cameras = map.getCameras();
186  for (CameraContainer::iterator iter = cameras.begin(); iter != cameras.end(); ++iter)
187  {
188  if ((*iter)->getLocationRef().getMap()->getId() == map.getId())
189  {
190  TiXmlElement* cameraElement = new TiXmlElement("camera");
191 
192  cameraElement->SetAttribute("id", (*iter)->getId());
193  cameraElement->SetAttribute("ref_layer_id", (*iter)->getLocation().getLayer()->getId());
194  cameraElement->SetDoubleAttribute("zoom", (*iter)->getZoom());
195  cameraElement->SetDoubleAttribute("tilt", (*iter)->getTilt());
196  cameraElement->SetDoubleAttribute("rotation", (*iter)->getRotation());
197 
198  Rect viewport = (*iter)->getViewPort();
199  std::ostringstream viewportString;
200  viewportString << viewport.x << ","
201  << viewport.y << ","
202  << viewport.w << ","
203  << viewport.h;
204 
205  cameraElement->SetAttribute("viewport", viewportString.str());
206 
207  Point p = (*iter)->getCellImageDimensions();
208  cameraElement->SetAttribute("ref_cell_width", p.x);
209  cameraElement->SetAttribute("ref_cell_height", p.y);
210 
211  std::vector<float> lightingColor = (*iter)->getLightingColor();
212  bool writeLightingColor = false;
213  for (uint32_t i=0; i < lightingColor.size(); ++i)
214  {
215  if (lightingColor[i] < 1.0)
216  {
217  writeLightingColor = true;
218  break;
219  }
220  }
221 
222  if (writeLightingColor)
223  {
224  std::ostringstream lightingColorString;
225  for (uint32_t i=0; i < lightingColor.size(); ++i)
226  {
227  if (i > 0)
228  {
229  lightingColorString << ",";
230  }
231 
232  lightingColorString << lightingColor[i];
233 
234  cameraElement->SetAttribute("light_color", lightingColorString.str());
235  }
236  }
237 
238  mapElement->LinkEndChild(cameraElement);
239  }
240  }
241 
242  // save the map xml file
243  doc.SaveFile(filename);
244  }
245 }
void SetDoubleAttribute(const char *name, double value)
Definition: tinyxml.cpp:705
virtual void setAnimationSaver(const FIFE::AnimationSaverPtr &animationSaver)
Definition: mapsaver.cpp:62
TiXmlNode * LinkEndChild(TiXmlNode *addThis)
Definition: tinyxml.cpp:184
int32_t getStackPosition()
Definition: visual.h:155
bool SaveFile() const
Save a file using the current document value. Returns true if successful.
Definition: tinyxml.cpp:933
virtual void setObjectSaver(const FIFE::ObjectSaverPtr &objectSaver)
Definition: mapsaver.cpp:57
virtual void setAtlasSaver(const FIFE::AtlasSaverPtr &atlasSaver)
Definition: mapsaver.cpp:67
const std::string & getId() const
Definition: map.h:102
void SetAttribute(const char *name, const char *_value)
Definition: tinyxml.cpp:717
const std::list< Layer * > & getLayers() const
Definition: map.h:118
virtual void save(const Map &map, const std::string &filename, const std::vector< std::string > &importFiles)
Definition: mapsaver.cpp:71
Definition: map.h:86
const std::vector< Camera * > & getCameras() const
Definition: map.cpp:256