JNR
laTileset.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laTileset.cpp
32 //
33 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
34 //
35 #include "stdafx.h"
36 #include "Core-Level-JR.h"
37 
38 laTileset::laTileset(void)
39 {
40  strcpy(_strName, "Empty Tileset");
41 
42  _dWaveOffset = 0;
43  _dOffset = 0;
44 }
45 
46 laTileset::~laTileset(void) {
47  discard();
48  MLOG("Tileset object deleted");
49 }
50 
51 void laTileset::discard()
52 {
53  delete [] _arLitterPools;
54 
55  std::vector<laElement>::iterator i;
56  for(i=_vElemements.begin(); i!=_vElemements.end(); i++)
57  i->discard();
58  _vElemements.clear();
59 
60  MLOG("Tileset discarded");
61 }
62 
63 void laTileset::load(char* strFile)
64 {
65  ERRORLEVEL_BEGIN;
66  // Set the tileset pointer, this is used to get globals TS elements
67  // TODO: Remove this; Make the TS a porperty list, so rpgAttack and
68  // other classes can use the same interface to access global TS elems.
69  //
70  rpgAttack::_pTS = this;
71 
72  progressReset("Loading tileset '%s'...", strFile);
73 
74  laFileParser fp;
75  char strFilePath[128]=M_DIR_TILESET;
76  unsigned i;
77  char *strSegBgNames;
78  char /*strBackgroundElement[64],*/ junk[128];
79  laEnvironment_win32* pCE = ((laEnvironment_win32*)::laSystemIntegrator::getEnvironment());
80 
81  // Open the file
82  //
83  strcat(strFilePath, strFile);
84  try{ fp.fileOpen(strFilePath); }
85  catch(laError&){ throw laError("Unable to open tileset file '%s'", strFile); }
86 
87  // Section "General"
88  //
89  progressIncrease(0.05, "Loading tileset section 'general'...");
90 
91  try{ fp.readSectionSeparator(junk); }
92  catch(laError& ){ throw laError("Section separator expected but not found (while parsing '%s')", strFile); }
93  _strupr(junk);
94  ASSERT(!strcmp(junk, "GENERAL"), "Section [GENERAL] expected", junk);
95 
96  fp.readText(_strName);
97 
98  // Section "Sky"
99  //
100  progressIncrease(0.05, "Loading tileset section 'sky'...");
101 
102  try{ fp.readSectionSeparator(junk); }
103  catch(laError& ){ throw laError("Section separator expected but not found (while parsing '%s')", strFile); }
104  _strupr(junk);
105  ASSERT(!strcmp(junk, "SKY"), "Section [SKY] expected", junk);
106 
107  fp.readObj(&_TilesetSky, M_FALSE);
108 
109  //Section "Segment Background"
110  //
111  progressIncrease(0.05, "Loading tileset section 'segment background'...");
112 
113  try{ fp.readSectionSeparator(junk); }
114  catch(laError& ){ throw laError("Section separator expected but not found (while parsing '%s')", strFile); }
115  _strupr(junk);
116  ASSERT(!strcmp(junk, "PER-SEGMENT BACKGROUND"), "Section [PER-SEGMENT BACKGROUND] expected", junk);
117 
118  fp.readUnsigned(&_nSegmentBackgroundCount);
119  strSegBgNames = new char [_nSegmentBackgroundCount*128];
120 
121  for(i=0; i<_nSegmentBackgroundCount; i++)
122  fp.readText(strSegBgNames+i*128);
123 
124  //Section "Litter Pools"
125  //
126  progressIncrease(0.05, "Loading tileset section 'litter-pools'...");
127 
128  try{ fp.readSectionSeparator(junk); }
129  catch(laError&){ throw laError("Section separator expected but not found (while parsing '%s')", strFile); }
130  _strupr(junk);
131  ASSERT(!strcmp(junk, "LITTER POOLS"), "Section [LITTER POOLS] expected", junk);
132 
133  fp.readUnsigned(&_nLitterPoolsCount);
134  _arLitterPools = new laLitterPools [_nLitterPoolsCount];
135 
136  for(i=0; i<_nLitterPoolsCount; i++)
137  fp.readObj(_arLitterPools+i);
138 
139  //Start loading tileset elements
140  //
141  progressIncrease(0.05, "Loading tileset section 'litter-pools'...");
142 
143  try{ fp.readSectionSeparator(junk); }
144  catch(laError&){ throw laError("Section separator expected but not found (while parsing '%s')", strFile); }
145  _strupr(junk);
146  ASSERT(!strcmp(junk, "TS INCLUDES"), "Section [TS INCLUDES] expected", junk);
147 
148  laFileParser* pfp = NULL;
149  _vElemements.clear();
150 
151  do {
152  char strIncludeFile[128] = "";
153  fp.readText(strIncludeFile);
154 
155  laFileParser fp_include;
156 
157  if( strcmp(strIncludeFile, "END") )
158  {
159  progressIncrease( 0, "Loading TS INCLUDE '%s'", strIncludeFile);
160  fp_include.fileOpen(strIncludeFile);
161  pfp = &fp_include;
162  }
163  else pfp = &fp;
164 
165  _readTSIncude(pfp);
166 
167  } while( pfp != &fp );
168 
169 
170  // Locate background elements
171  //
172  progressIncrease( M_ABS(0.9-progress()) , "Locating elements...");
173 
174  _pLightProperties = getElement( getElementIndex("light") );
175  _pFogProperties = getElement( getElementIndex("fog") );
176  _pBackgroundProperties = getElement( getElementIndex("global-background") );
177 
178  //Locate background segment elements
179  //
180  for(i=0; i<_nSegmentBackgroundCount; i++)
181  {
182  _arSegmentBackground[i] = getElementIndex(strSegBgNames+i*128);
183  ASSERT(
184  _arSegmentBackground[i] < getElementCnt(),
185  "Element was requested as segment background but not found",
186  strSegBgNames+i*128);
187  }
188  MLOG("All segment backgrounds located...");
189 
190  //Locate tileset Pools elements
191  //
192  for(i=0; i<_nLitterPoolsCount; i++)
193  _arLitterPools[i].setTS(this);
194 
195  progressIncrease(0.1, "Tileset loaded.");
196  ERRORLEVEL_END;
197 }
198 
199 void laTileset::_readTSIncude(laFileParser* pfp)
200 {
201  while(1)
202  {
203  progressIncrease(0.6/150, "Loading tileset element %d...", _vElemements.size());
204 
205  //Load element
206  try
207  {
208  _vElemements.push_back( laElement() );
209  (_vElemements.end()-1)->load(pfp);
210  }
211  catch(laError& pe)
212  {
213  ASSERT( !strcmp(pe.getText(), "END"),
214  "Corrupted element encountered (%d) \nWhy: %s ",
215  (_vElemements.size()-1), pe.getText());
216 
217  _vElemements.pop_back(); //Erase last (the "End" tag)
218  //MSG("ss3");
219  return;
220  }
221  }
222 }
223 
224 unsigned laTileset::getElementIndex(char* strName)
225 {
226  std::vector<laElement>::iterator i;
227  unsigned n=0;
228 
229  for(i=_vElemements.begin(); i!=_vElemements.end(); i++)
230  {
231  if( !strcmp(strName, i->name() ) ) return n;
232  else n++;
233  }
234 
235  ASSERT(0, "Element not found in the TS", strName);
236 }
237 
238 laElement* laTileset::getElement(unsigned i)
239 {
240  ASSERT(i<=_vElemements.size(), "Invalid element", i);
241  return &(_vElemements[i]);
242 }
243 
244 unsigned laTileset::getLitterPoolsIndex(char*strName)
245 {
246  for(unsigned i=0; i<_nLitterPoolsCount; i++)
247  {
248  if(!strcmp(strName, _arLitterPools[i].name()))return i;
249  }
250 
251  ASSERT(0, "No litter pool with this name", strName);
252  //return _nLitterPoolsCount;
253 }
254 
255 // Animate TS
256 //
257 void laTileset::animate(laTimer &t)
258 {
259  ERRORLEVEL_BEGIN;
260  std::vector<laElement>::iterator i;
261 
262  //Elements
263  for(i=_vElemements.begin(); i!=_vElemements.end(); i++)
264  i->animate(t);
265 
266  //Wave
267  _dWaveOffset += WAVE_SPEED*t.delta();
268  _dOffset += t.delta();
269 
270  //Sky
271  _TilesetSky.animate(t);
272 
273  ERRORLEVEL_END;
274 }
Base Class for Tileset Elements.
Definition: laElement.h:48
"Litter pools" to be used for populating laLitterBox
Definition: laLitterBox.h:40
#define M_ABS(a)
Return abs(a)
Error handling class.
Definition: laError.h:46
File Parser.
Definition: laFileParser.h:41