JNR
stageLoad.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 #include "stdafx.h"
31 #include "laStagesJR.h"
32 
33 class laLoadingThread: public laThread, public laProgressiveTask
34 {
35  JR_Objects* _pObjects;
36  M_BOOL _bReady;
37 
38  char _strFileName[128];
39 
40 public:
41  laLoadingThread(){
42  _pObjects = NULL;
43  _bReady = M_FALSE;
44  _strFileName[0] = '\0';
45  }
46 
47  ~laLoadingThread(){
48  }
49 
50  void setFileName(char*strFileName) {
51  strcpy(_strFileName, strFileName);
52  }
53 
54  M_BOOL isReady(){
55  return _bReady;
56  }
57 
58  JR_Objects* getObjects(){
59  ASSERT(_pObjects, "Nil objects");
60  return _pObjects;
61  }
62 
63  // Loading thread runs in parallel to the main game thread
64  //
65  virtual void run()
66  {
67  try
68  {
69  // Create new game objects; These objects are then passed to the JR Game stage;
70  // The game takes care to erase them upon desactivation.
71  //
72  progressReset("Creating game objects...");
73  ASSERT(!_pObjects, "Any old objects must be discarded");
74  _pObjects = new JR_Objects();
75 
76  // Load sounds
77  //
78  progressIncrease(0.1, "Loading sounds and music...");
79 
80  _pObjects->pIntroSound = laSystemIntegrator::getSound()->soundCreate("sound\\loading(Aesma Daeva - Lost Melody).mp3");
81  _pObjects->pIntroSound->play();
82 
83  _pObjects->pMusicLoop = laSystemIntegrator::getSound()->soundCreate("sound\\loop(In The Desert).mp3");
84  _pObjects->pMusicLoop->setLoopMode(M_TRUE, 95);
85 
86  //Load a level
87  //
88  progressIncrease(0.1);
89 
90  progressSubtask( &(_pObjects->level), 0.7);
91  laTexture::toggleLazyLoad(M_TRUE);
92  _pObjects->level.load( _strFileName );
93  laTexture::toggleLazyLoad(M_FALSE);
94  progressSubtask(NULL,0);
95 
96  // Ready
97  //
98  progressIncrease(0.1, "Ready.");
99 
100  _pObjects->pIntroSound->stop();
101  _bReady = M_TRUE;
102  }
103  // Catch errors
104  //
105  catch(laError& error)
106  {
107  ::LeaveCriticalSection(&cs);
108 
109  MSG("uiParametersDialog::run() catched the following error:\n\n %s", error.getText());
110  laSystemIntegrator::getEnvironment()->terminate();
111  }
112  }
113 };
114 
115 stageLoad::stageLoad(void) {
116  _pThread = NULL;
117  _bFirstRun = M_TRUE;
118 }
119 
120 stageLoad::~stageLoad(void) {
121  delete _uiProgress;
122 }
123 
124 void stageLoad::onInit()
125 {
126  ERRORLEVEL_BEGIN;
127 
128  _uiDesktop.create("ui\\desktop-mm.dsk");
129 
130  _uiProgress = new uiContainer("ui\\dialogs\\gen-progress-centered.cui");
131  ((uiLabel*)(_uiProgress->get(1)))->setText("Loading...");
132  _uiDesktop.insert(_uiProgress);
133 
134  ERRORLEVEL_END;
135 }
136 
137 void stageLoad::onActivate()
138 {
139  ERRORLEVEL_BEGIN;
140  laSystemIntegrator::getEnvironment()->setTitle("LOADING...");
141 
142  ASSERT(!_pThread, "Old thread object must be discarded");
143  _pThread = new laLoadingThread();
144 
145  if(_bFirstRun)
146  {
147  // Load the main-menu demo level
148  _pThread->setFileName( M_INTRO_LEVEL );
149  laSystemIntegrator::getSettings()->load_player_name[0] = '\0'; // No player profile
150  }
151  else
152  {
153  // Load a specified level
154  _pThread->setFileName( laSystemIntegrator::getSettings()->load_level );
155  }
156 
157  ::laSystemIntegrator::getEnvironment()->thread(_pThread);
158  ((uiProgress*)(_uiProgress->get(2)))->setTask(_pThread);
159 
160  ERRORLEVEL_END;
161 }
162 
163 void stageLoad::onDeactivate()
164 {
165  ERRORLEVEL_BEGIN;
166 
167  // Destroy the loading thread;
168  // NOTE: The JR_Objects are dynamically allocated and are not be destroyed by the thread
169  //
170  ((uiProgress*)(_uiProgress->get(2)))->setTask(NULL);
171 
172  if( _pThread )
173  {
174  delete _pThread;
175  _pThread = NULL;
176  }
177 
178  ERRORLEVEL_END;
179 }
180 
181 void stageLoad::onFini() {
182 }
183 
184 void stageLoad::onFrame(laRenderer *pr, laInputManager *pi, laTimer *pt)
185 {
186  ERRORLEVEL_BEGIN;
187  ASSERT(_pThread, "Nil thread object");
188 
189  // Draw interface
190  //
191  pr->modeInterface();
192  _uiDesktop.drawBackground();
193  _uiDesktop.reply();
194  _uiDesktop.draw();
195 
196  //Handle input
197  //if(in::key(KEY_ESCAPE)) ::laSystemIntegrator::getEnvironment()->terminate();
198 
199  // Moitor thread status
200  //
201  ::EnterCriticalSection( &(_pThread->cs) );
202  ((uiLabel*)(_uiProgress->get(1)))->setText(_pThread->status());
203  ::LeaveCriticalSection( &(_pThread->cs) );
204 
205  if( _pThread->isReady() )
206  {
207  unsigned nTargetStage;
208 
209  ::laTexture::loadForceAll(); // force-load all requested textures
210 
211  if(_bFirstRun) {
212  // Go to JR main menu if this is the first run for laLoad
213  _bFirstRun = M_FALSE;
214  nTargetStage = M_STAGE_MENU;
215  }
216  else {
217  // Go to JR Game otherwise
218  nTargetStage = M_STAGE_GAME;
219  }
220 
221  stageJR *jr = ((stageJR*)(laSystemIntegrator::getGame()->getStage(nTargetStage)));
222  jr->setGameObjects( _pThread->getObjects() );
223 
224  laSystemIntegrator::getGame()->setStage(nTargetStage);
225  }
226 
227  ERRORLEVEL_END;
228 }
GUI Container Window.
Definition: uiContainer.h:42
virtual void terminate()=0
Terminate application.
GUI Progress Bar.
Definition: uiProgress.h:39
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
virtual void modeInterface()=0
Switch to interface rendering (2D projection mode)
Text Label.
Definition: uiLabel.h:41
virtual void reply()
Handle input message.
Definition: uiDesktop.cpp:124
virtual void draw()
Display the window.
Definition: uiDesktop.cpp:112
Error handling class.
Definition: laError.h:46
Progressive Task.
virtual void run()=0
Virtual execution function can be defined by children.
Level objects to be created during the Loading stage.
Definition: JR_Objects.h:39
Engine multi-threading class.
Definition: laEnvironment.h:40