JNR
laGame.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: laGame.cpp
32 //
33 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
34 //
35 #include "stdafx.h"
36 #include "Core.h"
37 
38 laGame::laGame()
39 {
40  _bInitialized = _bFinalized = _bSwitchRequest = M_FALSE;
41  _nActiveStage = _nSwitchStage = 0;
42 }
43 
44 laGame::~laGame(void)
45 {
46  std::vector<laStage*>::iterator iter;
47 
48  for (iter = _vStages.begin(); iter != _vStages.end(); iter++)
49  {
50  delete *iter;
51  }
52 }
53 
54 void laGame::addStage(laStage* pStage)
55 {
56  ASSERT(!_bFinalized, "The game object is terminated.");
57 
58  _vStages.push_back(pStage);
59  pStage->_pGame = this;
60  pStage->bInitialized = M_FALSE;
61 }
62 
63 // Request a different stage
64 // NOTE: This method doesn't switch the state immediately; Instead, it sets the _bSwitchRequest flag
65 // and at the beginning of the next onFrame(), the _switch_actions() does the job;
66 //
67 // WHY? Because calling onDesactivate() / onActivate() can mess up the current onFrame(),
68 // e.g. if they delete objects or change pointers.
69 //
70 void laGame::setStage(unsigned n)
71 {
72  MLOG("Request for stage change (%d -> %d)", _nActiveStage, n);
73  ASSERT(n < _vStages.size(), "Unimplemented stage requested in laGame::setStage(%d)", n);
74  if(n==_nActiveStage) return;
75 
76  if( _bInitialized )
77  {
78  _bSwitchRequest = M_TRUE;
79  _nSwitchStage = n;
80  }
81  else _nActiveStage = n;
82 }
83 
84 void laGame::_switch_actions()
85 {
86  _bSwitchRequest = M_FALSE; // The request is handled
87  unsigned nTarget = _nSwitchStage; // NOTE: It's important to copy, in case onActivate()
88  // decides to make another state change
89 
90 
91  if( _vStages[_nActiveStage]->bInitialized ) _vStages[_nActiveStage]->onDeactivate();
92 
93  if( !(_vStages[nTarget]->bInitialized) )
94  {
95  _vStages[nTarget]->onInit();
96  _vStages[nTarget]->bInitialized = M_TRUE;
97  }
98 
99  _vStages[nTarget]->onActivate();
100 
101  // Set to the new stage
102  //
103  MLOG("Active stage changed %d -> %d", _nActiveStage, nTarget);
104  _nActiveStage = nTarget;
105 }
106 
107 void laGame::setStage(char* strName)
108 {
109  /*std::vector<laStage*>::iterator iter;
110  unsigned n=0;
111 
112  for (iter = _vStages.begin(); iter != _vStages.end(); iter++)
113  {
114  if( !strcmp((*iter)->name(), strName) )
115  {
116  _nActiveStage = n;
117  return;
118  }
119  n++;
120  }*/
121 
122  throw laError("laGame::setStage('%s'): Not implemented", strName);
123  //throw laError("Unimplemented Stage requested in laGame::setStage('%s')", strName);
124 }
125 
126 laStage* laGame::getStage(unsigned n)
127 {
128  ASSERT(n<_vStages.size(), "Unimplemented stage requested in laGame::getStage(%d)", n);
129 
130  return _vStages[n];
131 }
132 
133 laStage* laGame::getStage(char* strName)
134 {
135  /*std::vector<laStage*>::iterator iter;
136 
137  for (iter = _vStages.begin(); iter != _vStages.end(); iter++)
138  {
139  if( !strcmp((*iter)->name(), strName) )
140  {
141  return (*iter);
142  }
143  }*/
144 
145  throw laError("laGame::getStage('%s'): Not implemented", strName);
146  //throw laError("Unimplemented Stage requested in laGame::getStage('%s')", strName);
147 }
148 
149 void laGame::onInit()
150 {
151  ASSERT(!_bInitialized, "Already initialized");
152  _bInitialized = M_TRUE;
153 
154  // Initialize the starting stage
155  //
156  if( !(_vStages[_nActiveStage]->bInitialized) )
157  {
158  _vStages[_nActiveStage]->onInit();
159  _vStages[_nActiveStage]->bInitialized = M_TRUE;
160  _vStages[_nActiveStage]->onActivate();
161  }
162 }
163 
164 void laGame::onFini()
165 {
166  ASSERT(!_bFinalized, "Already discarded");
167  _bFinalized = M_TRUE;
168 
169  std::vector<laStage*>::iterator iter;
170 
171  //Destroy all created stages
172  //
173  for (iter = _vStages.begin(); iter != _vStages.end(); iter++)
174  if(_vStages[_nActiveStage]->bInitialized) {
175  (*iter)->onFini();
176  (*iter)->bInitialized = M_FALSE;
177  }
178 }
179 
180 void laGame::onFrame(laRenderer *pr, laInputManager *pi, laTimer *pt)
181 {
182  ASSERT(_nActiveStage < _vStages.size(), "Unimplemented stage requested (%d)", _nActiveStage);
183 
184  while(_bSwitchRequest) _switch_actions(); // Swith a new stage if there was a request
185  _vStages[_nActiveStage]->onFrame(pr, pi, pt);
186 }
Base Class for Stages.
Definition: laStage.h:48
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
Error handling class.
Definition: laError.h:46