xoreos  0.0.5
game.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 <cassert>
26 
27 #include <algorithm>
28 
29 #include "src/common/filepath.h"
30 #include "src/common/filelist.h"
31 #include "src/common/configman.h"
32 
33 #include "src/events/events.h"
34 
35 #include "src/sound/sound.h"
36 
38 
45 
47 
49 
50 namespace Engines {
51 
52 namespace KotOR2 {
53 
54 Game::Game(KotOR2Engine &engine, ::Engines::Console &console, Aurora::Platform platform) :
55  _engine(&engine), _platform(platform), _console(&console) {
56 
57  _functions.reset(new Functions(*this));
58 }
59 
61 }
62 
64  assert(_module);
65 
66  return *_module;
67 }
68 
69 void Game::run() {
70  _module.reset(new Module(*_console));
71 
72  while (!EventMan.quitRequested()) {
73  mainMenu();
74  runModule();
75  }
76 
77  _module.reset();
78 }
79 
81  if (EventMan.quitRequested() || !_module->isLoaded()) {
82  _module->clear();
83  return;
84  }
85 
86  _module->enter();
87  EventMan.enableKeyRepeat(true);
88 
89  while (!EventMan.quitRequested() && _module->isRunning()) {
90  Events::Event event;
91  while (EventMan.pollEvent(event))
92  _module->addEvent(event);
93 
94  _module->processEventQueue();
95  EventMan.delay(10);
96  }
97 
98  EventMan.enableKeyRepeat(false);
99  _module->leave();
100 
101  _module->clear();
102 }
103 
105  stopMenuMusic();
106 
107  if (music.empty())
108  music = "mus_sion";
109 
111 }
112 
114  SoundMan.stopChannel(_menuMusic);
115 }
116 
117 void Game::playMusic(const Common::UString &music) {
118  if (_module && _module->isRunning()) {
119  Area *area = _module->getCurrentArea();
120  if (area)
121  area->playAmbientMusic(music);
122 
123  return;
124  }
125 
126  playMenuMusic(music);
127 }
128 
130  stopMenuMusic();
131 
132  if (_module && _module->isRunning()) {
133  Area *area = _module->getCurrentArea();
134  if (area)
135  area->stopAmbientMusic();
136  }
137 }
138 
140  EventMan.flushEvents();
141 
142  MainMenu menu(*_module, _console);
143 
144  _console->disableCommand("loadmodule", "not available in the main menu");
145  _console->disableCommand("exitmodule", "not available in the main menu");
146 
147  menu.show();
148  menu.run();
149  menu.hide();
150 
151  _console->enableCommand("loadmodule");
152  _console->enableCommand("exitmodule");
153 }
154 
155 void Game::getModules(std::vector<Common::UString> &modules) {
156  modules.clear();
157 
158  Common::UString moduleDir = ConfigMan.getString("KOTOR2_moduleDir");
159  if (moduleDir.empty())
160  return;
161 
162  Common::FileList mods;
163  mods.addDirectory(moduleDir);
164 
165  for (Common::FileList::const_iterator m = mods.begin(); m != mods.end(); ++m) {
166  Common::UString file = m->toLower();
167  if (!file.endsWith("_s.rim"))
168  continue;
169 
170  file = Common::FilePath::getStem(file);
171  file.truncate(file.size() - Common::UString("_s").size());
172 
173  modules.push_back(file);
174  }
175 
176  std::sort(modules.begin(), modules.end(), Common::UString::iless());
177 }
178 
179 } // End of namespace KotOR2
180 
181 } // End of namespace Engines
Common::ScopedPtr< Module > _module
Definition: game.h:69
A creature in a Star Wars: Knights of the Old Republic II - The Sith Lords area.
A class holding an UTF-8 string.
Definition: ustring.h:48
The context handling the gameplay in Star Wars: Knights of the Old Republic II - The Sith Lords...
The global config manager.
void playMusic(const Common::UString &music="")
Overwrite all currently playing music.
Definition: game.cpp:117
Sound::ChannelHandle _menuMusic
Definition: game.h:76
bool endsWith(const UString &with) const
Definition: ustring.cpp:315
void truncate(const iterator &it)
Definition: ustring.cpp:343
std::list< UString >::const_iterator const_iterator
Definition: filelist.h:37
virtual void hide()
Hide the GUI.
Definition: gui.cpp:75
The context holding a Star Wars: Knights of the Old Republic II - The Sith Lords area.
uint32 run(uint32 startCode=kStartCodeNone)
Run the GUI.
Definition: gui.cpp:94
::Engines::Console * _console
Definition: game.h:74
Game(KotOR2Engine &engine, ::Engines::Console &console, Aurora::Platform platform)
Definition: game.cpp:54
The KotOR 2 main menu.
SDL_Event Event
Definition: types.h:42
void disableCommand(const Common::UString &cmd, const Common::UString &reason="")
Definition: console.cpp:972
#define ConfigMan
Shortcut for accessing the config manager.
Definition: configman.h:176
void playMenuMusic(Common::UString music="")
Definition: game.cpp:104
static void getModules(std::vector< Common::UString > &modules)
Return a list of all modules.
Definition: game.cpp:155
Star Wars: Knights of the Old Republic II - The Sith Lords engine functions.
The global events manager.
Module & getModule()
Return the module context.
Definition: game.cpp:63
Star Wars: Knights of the Old Republic II - The Sith Lords (debug) console.
void stopAmbientMusic()
Stop the ambient music.
Definition: area.cpp:168
void stopMenuMusic()
Definition: game.cpp:113
The global sound manager, handling all sound output.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define SoundMan
Shortcut for accessing the sound manager.
Definition: sound.h:293
void stopMusic()
Force all currently playing music stopped.
Definition: game.cpp:129
Common::ScopedPtr< Functions > _functions
Definition: game.h:70
virtual void show()
Show the GUI.
Definition: gui.cpp:69
The context needed to run a Star Wars: Knights of the Old Republic II - The Sith Lords module...
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
Sound::ChannelHandle playSound(const Common::UString &sound, Sound::SoundType soundType, bool loop, float volume, bool pitchVariance)
Play this sound resource.
Definition: util.cpp:81
void enableCommand(const Common::UString &cmd)
Definition: console.cpp:983
static UString getStem(const UString &p)
Return a file name&#39;s stem.
Definition: filepath.cpp:87
UString toLower() const
Return a lowercased copy of the string.
Definition: ustring.cpp:481
size_t size() const
Return the size of the string, in characters.
Definition: ustring.cpp:241
A list of files.
Definition: filelist.h:35
const_iterator begin() const
Return a const_iterator pointing to the beginning of the list.
Definition: filelist.cpp:94
void playAmbientMusic(Common::UString music="")
Play the specified music (or the area&#39;s default) as ambient music.
Definition: area.cpp:176
bool addDirectory(const UString &directory, int recurseDepth=0)
Add a directory to the list.
Definition: filelist.cpp:102
Generic Aurora engines utility functions.
A list of files.
Engine class handling Star Wars: Knights of the Old Republic II - The Sith Lords. ...
Platform
Definition: types.h:429
Utility class for manipulating file paths.
An area in Star Wars: Knights of the Old Republic II - The Sith Lords, holding all objects and rooms ...
Definition: area.h:64
const_iterator end() const
Return a const_iterator pointing past the end of the list.
Definition: filelist.cpp:98