xoreos  0.0.5
module.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/error.h"
26 #include "src/common/ustring.h"
27 #include "src/common/uuid.h"
28 
29 #include "src/graphics/camera.h"
30 
31 #include "src/events/events.h"
32 
34 
36 
38 #include "src/engines/sonic/area.h"
39 
40 namespace Engines {
41 
42 namespace Sonic {
43 
45  _console(&console), _running(false), _exit(false), _newArea(-1) {
47  ObjectMan.registerObject(this);
48 }
49 
51  ObjectMan.unregisterObject(this);
52 
53  try {
54  clear();
55  } catch (...) {
56  }
57 }
58 
59 void Module::clear() {
60  unload();
61 }
62 
63 void Module::run() {
64  _newArea = 3;
65  _running = true;
66 
67  EventMan.enableKeyRepeat();
68 
69  try {
70 
71  EventMan.flushEvents();
72 
73  while (!EventMan.quitRequested() && !_exit) {
74  loadArea();
75  if (_exit)
76  break;
77 
78  handleEvents();
79 
80  if (!EventMan.quitRequested() && !_exit)
81  EventMan.delay(10);
82  }
83 
84  } catch (Common::Exception &e) {
85  _running = false;
86 
87  e.add("Failed running module");
88  throw e;
89  }
90 
91  EventMan.enableKeyRepeat(0);
92 
93  _running = false;
94 }
95 
96 bool Module::isRunning() const {
97  return _running;
98 }
99 
100 void Module::exit() {
101  _exit = true;
102 }
103 
105  if (_area && (_area->getID() == (uint32)_newArea))
106  return;
107 
108  unloadArea();
109 
110  if (_newArea < 0) {
111  _exit = true;
112  return;
113  }
114 
115  _area.reset(new Area(*this, (uint32)_newArea));
116 
117  _area->enter();
118  _area->show();
119 }
120 
122  if (_area) {
123  _area->leave();
124  _area->hide();
125  }
126 
127  _area.reset();
128 }
129 
131  unloadArea();
132 }
133 
135  Events::Event event;
136  while (EventMan.pollEvent(event)) {
137  // Handle console
138  if (_console->isVisible()) {
139  _console->processEvent(event);
140  continue;
141  }
142 
143  // Console
144  if (event.type == Events::kEventKeyDown) {
145  if ((event.key.keysym.sym == SDLK_d) && (event.key.keysym.mod & KMOD_CTRL)) {
146  _console->show();
147  continue;
148  }
149  }
150 
151  if (handleCameraEvents(event))
152  continue;
153 
154  if (_area)
155  _area->addEvent(event);
156  }
157 
158  CameraMan.update();
159 
160  if (_area)
161  _area->processEventQueue();
162 }
163 
165  if (event.type == Events::kEventKeyDown) {
166  float multiplier = 1.0f;
167  if (event.key.keysym.mod & KMOD_SHIFT)
168  multiplier = 5.0f;
169 
170  if (event.key.keysym.sym == SDLK_UP)
171  CameraMan.move(0.0f, 0.0f, multiplier * -5.0f);
172  else if (event.key.keysym.sym == SDLK_DOWN)
173  CameraMan.move(0.0f, 0.0f, multiplier * 5.0f);
174  else if (event.key.keysym.sym == SDLK_LEFT)
175  CameraMan.move(multiplier * -5.0f, 0.0f, 0.0f);
176  else if (event.key.keysym.sym == SDLK_RIGHT)
177  CameraMan.move(multiplier * 5.0f, 0.0f, 0.0f);
178  else if (event.key.keysym.scancode == SDL_SCANCODE_W)
179  CameraMan.move(0.0f, 0.0f, multiplier * -5.0f);
180  else if (event.key.keysym.scancode == SDL_SCANCODE_S)
181  CameraMan.move(0.0f, 0.0f, multiplier * 5.0f);
182  else if (event.key.keysym.scancode == SDL_SCANCODE_A)
183  CameraMan.move(multiplier * -5.0f, 0.0f, 0.0f);
184  else if (event.key.keysym.scancode == SDL_SCANCODE_D)
185  CameraMan.move(multiplier * 5.0f, 0.0f, 0.0f);
186  else
187  return false;
188  } else if (event.type == Events::kEventMouseMove) {
189  if (event.motion.state & SDL_BUTTON(2))
190  CameraMan.move(1.0f * event.motion.xrel, 0.0f, 1.0f * event.motion.yrel);
191  else
192  return false;
193  } else
194  return false;
195 
196  return true;
197 }
198 
200  return _area.get();
201 }
202 
203 void Module::movePC(int32 area) {
204  _newArea = area;
205 }
206 
207 } // End of namespace Sonic
208 
209 } // End of namespace Engines
#define ObjectMan
Definition: objectman.h:56
::Engines::Console * _console
Definition: module.h:69
The context holding a Sonic Chronicles: The Dark Brotherhood area.
Common::ScopedPtr< Area > _area
The current area.
Definition: module.h:76
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
bool isVisible() const
Definition: console.cpp:797
Module(::Engines::Console &console)
Definition: module.cpp:44
Camera management.
Utility functions for generating unique IDs.
Mouse was moved.
Definition: types.h:48
Exception that provides a stack of explanations.
Definition: error.h:36
void run()
Run the module.
Definition: module.cpp:63
int32 _newArea
The new area to enter.
Definition: module.h:74
An object within a Sonic area.
Definition: object.h:40
SDL_Event Event
Definition: types.h:42
Keyboard key was pressed.
Definition: types.h:46
NWScript object manager.
Basic exceptions to throw.
The global events manager.
void clear()
Clear the whole context.
Definition: module.cpp:59
Generic Aurora engines (debug) console.
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
Unicode string handling.
bool _exit
Should we exit the module?
Definition: module.h:72
The context needed to run a Sonic Chronicles: The Dark Brotherhood module.
void movePC(int32 area)
Move the player character to this area.
Definition: module.cpp:203
uint32_t uint32
Definition: types.h:204
bool processEvent(const Events::Event &event)
Definition: console.cpp:817
#define CameraMan
Shortcut for accessing the camera manager.
Definition: camera.h:83
void exit()
Exit the module.
Definition: module.cpp:100
bool handleCameraEvents(const Events::Event &event)
Definition: module.cpp:164
bool isRunning() const
Is the module currently running?
Definition: module.cpp:96
uint32 generateIDNumber()
Definition: uuid.cpp:46
bool _running
Are we currently running a module?
Definition: module.h:71
int32_t int32
Definition: types.h:203
Area * getCurrentArea()
Return the area the PC is currently in.
Definition: module.cpp:199