Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TerrainProperties.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 #include "TerrainProperties.h"
20 
21 #include <vector>
22 #include <string>
23 
24 #include "ps/Filesystem.h"
25 #include "ps/CLogger.h"
26 
27 #include "ps/Parser.h"
28 #include "ps/XML/XeroXMB.h"
29 #include "ps/XML/Xeromyces.h"
30 
31 #include "TerrainTextureManager.h"
32 #include "ps/Overlay.h"
33 
34 #include "maths/MathUtil.h"
35 
37  m_pParent(parent),
38  m_BaseColor(0),
39  m_HasBaseColor(false),
40  m_TextureAngle((float)M_PI / 4.f),
41  m_TextureSize(32.f),
42  m_MovementClass("default")
43 {
44  if (m_pParent)
45  m_Groups = m_pParent->m_Groups;
46 }
47 
49 {
50  CXeromyces XeroFile;
51  if (XeroFile.Load(g_VFS, pathname) != PSRETURN_OK)
52  return CTerrainPropertiesPtr();
53 
54  XMBElement root = XeroFile.GetRoot();
55  CStr rootName = XeroFile.GetElementString(root.GetNodeName());
56 
57  // Check that we've got the right kind of xml document
58  if (rootName != "Terrains")
59  {
60  LOGERROR(
61  L"TerrainProperties: Loading %ls: Root node is not terrains (found \"%hs\")",
62  pathname.string().c_str(),
63  rootName.c_str());
64  return CTerrainPropertiesPtr();
65  }
66 
67  #define ELMT(x) int el_##x = XeroFile.GetElementID(#x)
68  ELMT(terrain);
69  #undef ELMT
70 
71  // Ignore all non-terrain nodes, loading the first terrain node and
72  // returning it.
73  // Really, we only expect there to be one child and it to be of the right
74  // type, though.
75  XERO_ITER_EL(root, child)
76  {
77  if (child.GetNodeName() == el_terrain)
78  {
79  CTerrainPropertiesPtr ret (new CTerrainProperties(parent));
80  ret->LoadXml(child, &XeroFile, pathname);
81  return ret;
82  }
83  else
84  {
85  LOGWARNING(
86  L"TerrainProperties: Loading %ls: Unexpected node %hs\n",
87  pathname.string().c_str(),
88  XeroFile.GetElementString(child.GetNodeName()).c_str());
89  // Keep reading - typos shouldn't be showstoppers
90  }
91  }
92 
93  return CTerrainPropertiesPtr();
94 }
95 
96 void CTerrainProperties::LoadXml(XMBElement node, CXeromyces *pFile, const VfsPath& UNUSED(pathname))
97 {
98  #define ELMT(x) int elmt_##x = pFile->GetElementID(#x)
99  #define ATTR(x) int attr_##x = pFile->GetAttributeID(#x)
100  // Terrain Attribs
101  ATTR(mmap);
102  ATTR(groups);
103  ATTR(movementclass);
104  ATTR(angle);
105  ATTR(size);
106  #undef ELMT
107  #undef ATTR
108 
109  XERO_ITER_ATTR(node, attr)
110  {
111  if (attr.Name == attr_groups)
112  {
113  // Parse a comma-separated list of groups, add the new entry to
114  // each of them
115  CParser parser;
116  CParserLine parserLine;
117  parser.InputTaskType("GroupList", "<_$value_,>_$value_");
118 
119  if (!parserLine.ParseString(parser, attr.Value))
120  continue;
121  m_Groups.clear();
122  for (size_t i=0;i<parserLine.GetArgCount();i++)
123  {
124  std::string value;
125  if (!parserLine.GetArgString(i, value))
126  continue;
127  CTerrainGroup *pType = g_TexMan.FindGroup(value);
128  m_Groups.push_back(pType);
129  }
130  }
131  else if (attr.Name == attr_mmap)
132  {
133  CColor col;
134  if (!col.ParseString(attr.Value, 255))
135  continue;
136 
137  // m_BaseColor is BGRA
138  u8 *baseColor = (u8*)&m_BaseColor;
139  baseColor[0] = (u8)(col.b*255);
140  baseColor[1] = (u8)(col.g*255);
141  baseColor[2] = (u8)(col.r*255);
142  baseColor[3] = (u8)(col.a*255);
143  m_HasBaseColor = true;
144  }
145  else if (attr.Name == attr_angle)
146  {
147  m_TextureAngle = DEGTORAD(attr.Value.ToFloat());
148  }
149  else if (attr.Name == attr_size)
150  {
151  m_TextureSize = attr.Value.ToFloat();
152  }
153  else if (attr.Name == attr_movementclass)
154  {
155  m_MovementClass = attr.Value;
156  }
157  }
158 }
159 
161 {
162  return m_HasBaseColor || (m_pParent && m_pParent->HasBaseColor());
163 }
164 
166 {
167  if (m_HasBaseColor || !m_pParent)
168  return m_BaseColor;
169  else if (m_pParent)
170  return m_pParent->GetBaseColor();
171  else
172  // White, full opacity.. but this value shouldn't ever be used
173  return 0xFFFFFFFF;
174 }
#define M_PI
Definition: wposix.h:64
#define u8
Definition: types.h:39
shared_ptr< CTerrainProperties > CTerrainPropertiesPtr
CTerrainPropertiesPtr m_pParent
#define g_TexMan
float g
Definition: Overlay.h:57
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename)
Load from an XML file (with invisible XMB caching).
Definition: Xeromyces.cpp:65
#define LOGERROR
Definition: CLogger.h:35
#define XERO_ITER_ATTR(parent_element, attribute)
Definition: Xeromyces.h:99
const PSRETURN PSRETURN_OK
Definition: Errors.h:103
Definition: Overlay.h:34
#define XERO_ITER_EL(parent_element, child_element)
Definition: Xeromyces.h:91
void LoadXml(XMBElement node, CXeromyces *pFile, const VfsPath &pathname)
bool ParseString(const CStr8 &Value, float DefaultAlpha)
Definition: Overlay.cpp:30
#define LOGWARNING
Definition: CLogger.h:34
static CTerrainPropertiesPtr FromXML(const CTerrainPropertiesPtr &parent, const VfsPath &pathname)
CTerrainProperties(CTerrainPropertiesPtr parent)
float b
Definition: Overlay.h:57
void * mmap(void *start, size_t len, int prot, int flags, int fd, off_t ofs)
Definition: wmman.cpp:202
Definition: path.h:75
const String & string() const
Definition: path.h:123
float a
Definition: Overlay.h:57
std::string GetElementString(const int ID) const
Definition: XeroXMB.cpp:148
bool ParseString(const CParser &parser, const std::string &line)
Definition: Parser.cpp:325
#define ELMT(x)
bool GetArgString(size_t arg, std::string &ret)
XMBElement GetRoot() const
Definition: XeroXMB.cpp:84
#define u32
Definition: types.h:41
#define ATTR(x)
size_t GetArgCount() const
Definition: Parser.h:219
bool InputTaskType(const std::string &strName, const std::string &strSyntax)
Definition: Parser.cpp:816
#define DEGTORAD(a)
Definition: MathUtil.h:21
PIVFS g_VFS
Definition: Filesystem.cpp:30
float r
Definition: Overlay.h:57
int GetNodeName() const
Definition: XeroXMB.cpp:166