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/graphics/graphics.h"
34 
35 #include "src/events/events.h"
36 
37 #include "src/sound/sound.h"
38 
40 
41 #include "src/engines/kotor/game.h"
45 #include "src/engines/kotor/area.h"
47 
49 
51 
52 namespace Engines {
53 
54 namespace KotOR {
55 
56 Game::Game(KotOREngine &engine, ::Engines::Console &console, const Version &gameVersion) :
57  _engine(&engine), _gameVersion(&gameVersion), _console(&console) {
58 
59  _functions.reset(new Functions(*this));
60 }
61 
63 }
64 
66  assert(_module);
67 
68  return *_module;
69 }
70 
71 void Game::run() {
72  _module.reset(new Module(*_console));
73 
74  while (!EventMan.quitRequested()) {
75  mainMenu();
76  runModule();
77  }
78 
79  _module.reset();
80 }
81 
83  if (EventMan.quitRequested() || !_module->isLoaded()) {
84  _module->clear();
85  return;
86  }
87 
88  _module->enter();
89  EventMan.enableKeyRepeat(true);
90 
91  while (!EventMan.quitRequested() && _module->isRunning()) {
92  Events::Event event;
93  while (EventMan.pollEvent(event))
94  _module->addEvent(event);
95 
96  _module->processEventQueue();
97  EventMan.delay(10);
98  }
99 
100  EventMan.enableKeyRepeat(false);
101  _module->leave();
102 
103  _module->clear();
104 }
105 
107  stopMenuMusic();
108 
109  if (music.empty())
110  music = "mus_theme_cult";
111 
113 }
114 
116  SoundMan.stopChannel(_menuMusic);
117 }
118 
119 void Game::playMusic(const Common::UString &music) {
120  if (_module && _module->isRunning()) {
121  Area *area = _module->getCurrentArea();
122  if (area)
123  area->playAmbientMusic(music);
124 
125  return;
126  }
127 
128  playMenuMusic(music);
129 }
130 
132  stopMenuMusic();
133 
134  if (_module && _module->isRunning()) {
135  Area *area = _module->getCurrentArea();
136  if (area)
137  area->stopAmbientMusic();
138  }
139 }
140 
142  EventMan.flushEvents();
143 
145 
146  _console->disableCommand("loadmodule", "not available in the main menu");
147  _console->disableCommand("exitmodule", "not available in the main menu");
148 
149  menu.show();
150  int ret = menu.run();
151  if (ret == 2)
152  GfxMan.unlockFrame();
153  menu.hide();
154 
155  _console->enableCommand("loadmodule");
156  _console->enableCommand("exitmodule");
157 }
158 
159 void Game::getModules(std::vector<Common::UString> &modules) {
160  modules.clear();
161 
162  Common::UString moduleDir = ConfigMan.getString("KOTOR_moduleDir");
163  if (moduleDir.empty())
164  return;
165 
166  Common::FileList mods;
167  mods.addDirectory(moduleDir);
168 
169  for (Common::FileList::const_iterator m = mods.begin(); m != mods.end(); ++m) {
170  Common::UString file = m->toLower();
171  if (!file.endsWith("_s.rim"))
172  continue;
173 
174  file = Common::FilePath::getStem(file);
175  file.truncate(file.size() - Common::UString("_s").size());
176 
177  modules.push_back(file);
178  }
179 
180  std::sort(modules.begin(), modules.end(), Common::UString::iless());
181 }
182 
183 } // End of namespace KotOR
184 
185 } // End of namespace Engines
A creature in a Star Wars: Knights of the Old Republic area.
void stopMenuMusic()
Definition: game.cpp:115
void playAmbientMusic(Common::UString music="")
Play the specified music (or the area&#39;s default) as ambient music.
Definition: area.cpp:180
The global graphics manager.
A class holding an UTF-8 string.
Definition: ustring.h:48
const Version * _gameVersion
Definition: game.h:73
The global config manager.
void playMusic(const Common::UString &music="")
Overwrite all currently playing music.
Definition: game.cpp:119
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
The KotOR main menu.
virtual void hide()
Hide the GUI.
Definition: gui.cpp:75
Common::ScopedPtr< Functions > _functions
Definition: game.h:71
uint32 run(uint32 startCode=kStartCodeNone)
Run the GUI.
Definition: gui.cpp:94
::Engines::Console * _console
Definition: game.h:75
void stopMusic()
Force all currently playing music stopped.
Definition: game.cpp:131
SDL_Event Event
Definition: types.h:42
void runModule()
Definition: game.cpp:82
void disableCommand(const Common::UString &cmd, const Common::UString &reason="")
Definition: console.cpp:972
Star Wars: Knights of the Old Republic (debug) console.
#define ConfigMan
Shortcut for accessing the config manager.
Definition: configman.h:176
The context handling the gameplay in Star Wars: Knights of the Old Republic.
Engine class handling Star Wars: Knights of the Old Republic.
The global events manager.
void stopAmbientMusic()
Stop the ambient music.
Definition: area.cpp:172
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
An area in Star Wars: Knights of the Old Republic, holding all objects and rooms within, as well as general area properties like the current background music and ambient sounds.
Definition: area.h:65
Common::ScopedPtr< Module > _module
Definition: game.h:70
Star Wars: Knights of the Old Republic engine functions.
The context holding a Star Wars: Knights of the Old Republic area.
virtual void show()
Show the GUI.
Definition: gui.cpp:69
#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
static void getModules(std::vector< Common::UString > &modules)
Return a list of all modules.
Definition: game.cpp:159
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
Sound::ChannelHandle _menuMusic
Definition: game.h:77
A list of files.
Definition: filelist.h:35
Module & getModule()
Return the module context.
Definition: game.cpp:65
const_iterator begin() const
Return a const_iterator pointing to the beginning of the list.
Definition: filelist.cpp:94
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.
Game(KotOREngine &engine, ::Engines::Console &console, const Version &gameVersion)
Definition: game.cpp:56
The context needed to run a Star Wars: Knights of the Old Republic module.
void playMenuMusic(Common::UString music="")
Definition: game.cpp:106
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
Utility class for manipulating file paths.
const_iterator end() const
Return a const_iterator pointing past the end of the list.
Definition: filelist.cpp:98