-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModelsGameData.cc
280 lines (215 loc) · 5.58 KB
/
ModelsGameData.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2009 Isis Innovation Limited
#include "ModelsGameData.h"
#include "tinyxml.h"
#include <iostream>
namespace PTAMM {
using namespace std;
/**
* ModelsGameData constructor
*/
ModelsGameData::ModelsGameData( string sName ) :
mbHideAR(false),
mbSnapTo(false),
mbHideControls(false),
mbHidePoints(true),
mDisplayState(BrowserState),
mnModelIdx(-1),
mdVersion(1.0),
msName( sName )
{
}
/**
* ModelsGameData destructor
*/
ModelsGameData::~ModelsGameData()
{
DeleteAllModels();
}
/**
* Return the current model
* @return Pointer to the current model. NULL if none.
*/
Model3DS * ModelsGameData::CurrentModel()
{
if( mnModelIdx < 0 || mnModelIdx >= static_cast<int>(mvModels.size()) ) {
return NULL;
}
return mvModels[mnModelIdx];
}
/**
* Advance to the next model in the list
*/
void ModelsGameData::NextModel()
{
if( mvModels.empty() ) {
mnModelIdx = -1;
return;
}
mnModelIdx++;
if( (mnModelIdx < 0) || (mnModelIdx >= static_cast<int>(mvModels.size()) ) )
{
mnModelIdx = 0;
}
}
/**
* Advance to the previous model in the list
*/
void ModelsGameData::PrevModel()
{
if( mvModels.empty() ) {
mnModelIdx = -1;
return;
}
mnModelIdx--;
if( (mnModelIdx < 0) || ( mnModelIdx >= static_cast<int>(mvModels.size()) ) ) {
mnModelIdx = static_cast<int>(mvModels.size()) - 1;
}
}
/**
* Add a new model
* @param sFileName Path and name of the model file
* @param sModelDir The directory containing the model
* @param sName The name of the model
* @return success
*/
bool ModelsGameData::AddModel( std::string sModelDir, std::string sFileName, std::string sName, TooN::Vector<3> v3Rotation )
{
Model3DS * pModel = new Model3DS();
if( pModel->Load( sModelDir, sFileName, v3Rotation ) )
{
pModel->Name() = sName;
mvModels.push_back(pModel);
mnModelIdx = static_cast<int>(mvModels.size()) - 1;
mDisplayState = ControlState;
std::cout << "Loaded model " << pModel->Name() << std::endl;
return true;
}
else
{
delete pModel;
return false;
}
}
/**
* Delete the current model
*/
void ModelsGameData::DeleteCurrentModel()
{
if( mvModels.empty() || mnModelIdx < 0 || mnModelIdx >= static_cast<int>(mvModels.size()) ) {
return;
}
delete mvModels[mnModelIdx];
mvModels.erase( mvModels.begin()+mnModelIdx );
if( mvModels.empty() ) {
mnModelIdx = -1;
mDisplayState = BrowserState;
}
else {
mnModelIdx = 0;
}
}
/**
* Delete all of the models
*/
void ModelsGameData::DeleteAllModels()
{
mnModelIdx = -1;
for( size_t ii = 0; ii < mvModels.size(); ++ii )
{
delete mvModels[ii];
}
mvModels.clear();
mDisplayState = BrowserState;
}
/**
* Reset the game data.
* Clears everything out and resets to the initial state.
*/
void ModelsGameData::Reset()
{
DeleteAllModels();
mbHideAR = false;
mbSnapTo = false;
mbHideControls = false;
mbHidePoints = true;
mDisplayState = BrowserState;
mnModelIdx = -1;
}
/**
* Save the game state to disk
* @param sDataFileName The file name to save to
*/
void ModelsGameData::Save( std::string sDataFileName )
{
TiXmlDocument xmlDoc; //XML file
//open the map file for writing
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
xmlDoc.LinkEndChild( decl );
TiXmlElement * rootNode = new TiXmlElement( msName );
xmlDoc.LinkEndChild( rootNode );
rootNode->SetDoubleAttribute("version", mdVersion);
TiXmlElement * modelsNode = new TiXmlElement( "Models" );
rootNode->LinkEndChild( modelsNode );
modelsNode->SetAttribute( "size", static_cast<int>(mvModels.size()) );
for( size_t ii = 0; ii < mvModels.size(); ++ii )
{
mvModels[ii]->Save( modelsNode );
}
xmlDoc.SaveFile(sDataFileName);
}
/**
* Load a saved game
* @param sDataFileName the file to load
*/
void ModelsGameData::Load( std::string sDataFileName )
{
TiXmlDocument mXMLDoc; //XML file
//load the XML file
if( !mXMLDoc.LoadFile( sDataFileName ) ) {
cerr << "Failed to load " << sDataFileName << " game file. Aborting." << endl;
return;
}
TiXmlHandle hDoc(&mXMLDoc);
TiXmlElement* pElem;
TiXmlHandle hRoot(0);
pElem = hDoc.FirstChildElement().Element();
// should always have a valid root but handle gracefully if it does not
if (!pElem)
{
cerr << "No root handle in XML file " << sDataFileName << endl;
return;
}
string sID( msName );
double dFileVersion = 0.0;
pElem->QueryDoubleAttribute("version", &dFileVersion);
if( ( sID.compare( pElem->Value() ) != 0 ) &&
( dFileVersion != mdVersion ) )
{
cerr << "Invalid XML file. Need a version " << mdVersion << " " << sID
<< " XML file. Not a version " << dFileVersion << " " << pElem->Value() << " file." << endl;
return;
}
// save this for later
hRoot = TiXmlHandle(pElem);
int nSize = -1;
TiXmlHandle pNode = hRoot.FirstChild( "Models" );
pNode.ToElement()->QueryIntAttribute("size", &nSize);
for(TiXmlElement* pElem = pNode.FirstChild().Element();
pElem != NULL;
pElem = pElem->NextSiblingElement() )
{
//Load a model - this is a delayed load so no GL calls are made.
//In this function as it will be called from the MapSerialization thread
Model3DS * pModel = new Model3DS();
if( pModel->Load( pElem ) ) {
mvModels.push_back(pModel);
}
}
mnModelIdx = static_cast<int>(mvModels.size()) - 1;
mDisplayState = ControlState;
if( static_cast<int>(mvModels.size() ) != nSize ) {
cerr << "Warning: Loaded the wrong number of models. " << mvModels.size()
<< " instead of " << nSize << "." << endl;
}
}
}