| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "Application.h" |
| 10 |
|
| 11 |
#include "Log.h" |
| 12 |
#include "Path.h" |
| 13 |
#include "Random.h" |
| 14 |
#include "AgentPack.h" |
| 15 |
#include "MessagerAgent.h" |
| 16 |
#include "VideoAgent.h" |
| 17 |
#include "InputAgent.h" |
| 18 |
#include "TimerAgent.h" |
| 19 |
#include "GameAgent.h" |
| 20 |
#include "SoundAgent.h" |
| 21 |
#include "SDLSoundAgent.h" |
| 22 |
#include "DummySoundAgent.h" |
| 23 |
#include "ScriptAgent.h" |
| 24 |
#include "OptionAgent.h" |
| 25 |
#include "SubTitleAgent.h" |
| 26 |
#include "ResourceException.h" |
| 27 |
#include "OptionParams.h" |
| 28 |
#include "Font.h" |
| 29 |
|
| 30 |
#include "SimpleMsg.h" |
| 31 |
#include "StringMsg.h" |
| 32 |
|
| 33 |
#include "SDL.h" |
| 34 |
#include <stdio.h> // for fflush, stdout |
| 35 |
|
| 36 |
//----------------------------------------------------------------- |
| 37 |
Application::Application() |
| 38 |
{ |
| 39 |
m_quit = false; |
| 40 |
Random::init(); |
| 41 |
Font::init(); |
| 42 |
|
| 43 |
m_agents = new AgentPack(); |
| 44 |
//NOTE: MessagerAgent is added by AgentPack |
| 45 |
//NOTE: creating order is not significant, names are significant, |
| 46 |
// like rc.d scripts |
| 47 |
m_agents->addAgent(new ScriptAgent()); |
| 48 |
m_agents->addAgent(new OptionAgent()); |
| 49 |
m_agents->addAgent(new VideoAgent()); |
| 50 |
|
| 51 |
m_agents->addAgent(new InputAgent()); |
| 52 |
|
| 53 |
m_agents->addAgent(new SubTitleAgent()); |
| 54 |
m_agents->addAgent(new GameAgent()); |
| 55 |
|
| 56 |
m_agents->addAgent(new TimerAgent()); |
| 57 |
} |
| 58 |
//----------------------------------------------------------------- |
| 59 |
Application::~Application() |
| 60 |
{ |
| 61 |
delete m_agents; |
| 62 |
Font::shutdown(); |
| 63 |
} |
| 64 |
//----------------------------------------------------------------- |
| 65 |
void |
| 66 |
Application::init(int argc, char *argv[]) |
| 67 |
{ |
| 68 |
MessagerAgent::agent()->addListener(this); |
| 69 |
m_agents->init(Name::VIDEO_NAME); |
| 70 |
prepareLogLevel(); |
| 71 |
prepareOptions(argc, argv); |
| 72 |
customizeGame(); |
| 73 |
|
| 74 |
m_agents->init(Name::TIMER_NAME); |
| 75 |
addSoundAgent(); |
| 76 |
|
| 77 |
m_agents->init(); |
| 78 |
} |
| 79 |
//----------------------------------------------------------------- |
| 80 |
void |
| 81 |
Application::run() |
| 82 |
{ |
| 83 |
while (!m_quit) { |
| 84 |
m_agents->update(); |
| 85 |
} |
| 86 |
} |
| 87 |
//----------------------------------------------------------------- |
| 88 |
void |
| 89 |
Application::shutdown() |
| 90 |
{ |
| 91 |
m_agents->shutdown(); |
| 92 |
} |
| 93 |
//----------------------------------------------------------------- |
| 94 |
/** |
| 95 |
* Set loglevel according option. |
| 96 |
* Prepare to change. |
| 97 |
*/ |
| 98 |
void |
| 99 |
Application::prepareLogLevel() |
| 100 |
{ |
| 101 |
OptionAgent *options = OptionAgent::agent(); |
| 102 |
StringMsg *event = new StringMsg(this, "param_changed", "loglevel"); |
| 103 |
options->addWatcher("loglevel", event); |
| 104 |
options->setDefault("loglevel", Log::getLogLevel()); |
| 105 |
} |
| 106 |
//----------------------------------------------------------------- |
| 107 |
void |
| 108 |
Application::prepareOptions(int argc, char *argv[]) |
| 109 |
{ |
| 110 |
OptionParams params; |
| 111 |
params.addParam("loglevel", OptionParams::TYPE_NUMBER, |
| 112 |
"Debug with loglevel 7 (default=6)"); |
| 113 |
params.addParam("systemdir", OptionParams::TYPE_PATH, |
| 114 |
"Path to game data"); |
| 115 |
params.addParam("userdir", OptionParams::TYPE_PATH, |
| 116 |
"Path to game data"); |
| 117 |
params.addParam("lang", OptionParams::TYPE_STRING, |
| 118 |
"2-letter code (e.g., en, cs, fr, de)"); |
| 119 |
params.addParam("speech", OptionParams::TYPE_STRING, |
| 120 |
"Lang for speech"); |
| 121 |
params.addParam("subtitles", OptionParams::TYPE_BOOLEAN, |
| 122 |
"Enable subtitles"); |
| 123 |
params.addParam("fullscreen", OptionParams::TYPE_BOOLEAN, |
| 124 |
"Turn fullscreen on/off"); |
| 125 |
params.addParam("show_steps", OptionParams::TYPE_BOOLEAN, |
| 126 |
"Show a step counter in levels"); |
| 127 |
params.addParam("sound", OptionParams::TYPE_BOOLEAN, |
| 128 |
"Turn sound on/off"); |
| 129 |
params.addParam("volume_sound", OptionParams::TYPE_NUMBER, |
| 130 |
"Sound volume in percentage"); |
| 131 |
params.addParam("volume_music", OptionParams::TYPE_NUMBER, |
| 132 |
"Music volume in percentage"); |
| 133 |
params.addParam("worldmap", OptionParams::TYPE_STRING, |
| 134 |
"Path to the worldmap file"); |
| 135 |
params.addParam("cache_images", OptionParams::TYPE_BOOLEAN, |
| 136 |
"Cache images (default=true)"); |
| 137 |
params.addParam("sound_frequency", OptionParams::TYPE_NUMBER, |
| 138 |
"Sound sample rate (default=44100)"); |
| 139 |
params.addParam("strict_rules", OptionParams::TYPE_BOOLEAN, |
| 140 |
"Disallow pushing of partially supported objects (default=true)"); |
| 141 |
params.addParam("replay_level", OptionParams::TYPE_STRING, |
| 142 |
"Replay the solution for the given level codename"); |
| 143 |
OptionAgent::agent()->parseCmdOpt(argc, argv, params); |
| 144 |
} |
| 145 |
//----------------------------------------------------------------- |
| 146 |
/** |
| 147 |
* Run init script. |
| 148 |
* @throws ResourceException when data are not available |
| 149 |
*/ |
| 150 |
void |
| 151 |
Application::customizeGame() |
| 152 |
{ |
| 153 |
Path initfile = Path::dataReadPath("script/init.lua"); |
| 154 |
if (initfile.exists()) { |
| 155 |
ScriptAgent::agent()->scriptInclude(initfile); |
| 156 |
} |
| 157 |
else { |
| 158 |
throw ResourceException(ExInfo("init file not found") |
| 159 |
.addInfo("path", initfile.getNative()) |
| 160 |
.addInfo("systemdir", |
| 161 |
OptionAgent::agent()->getParam("systemdir")) |
| 162 |
.addInfo("userdir", |
| 163 |
OptionAgent::agent()->getParam("userdir")) |
| 164 |
.addInfo("hint", |
| 165 |
"try command line option \"systemdir=path/to/data\"")); |
| 166 |
} |
| 167 |
} |
| 168 |
//----------------------------------------------------------------- |
| 169 |
/** |
| 170 |
* Choose SDL or Dummy sound agent. |
| 171 |
* Reads 'sound' config option. |
| 172 |
*/ |
| 173 |
void |
| 174 |
Application::addSoundAgent() |
| 175 |
{ |
| 176 |
//TODO: better setting sound on/off |
| 177 |
//TODO: move to the SoundAgent |
| 178 |
SoundAgent *soundAgent = NULL; |
| 179 |
if (OptionAgent::agent()->getAsBool("sound", true)) { |
| 180 |
soundAgent = new SDLSoundAgent(); |
| 181 |
try { |
| 182 |
soundAgent->init(); |
| 183 |
} |
| 184 |
catch (BaseException &e) { |
| 185 |
LOG_WARNING(e.info()); |
| 186 |
delete soundAgent; |
| 187 |
soundAgent = new DummySoundAgent(); |
| 188 |
} |
| 189 |
} |
| 190 |
else { |
| 191 |
soundAgent = new DummySoundAgent(); |
| 192 |
} |
| 193 |
m_agents->addAgent(soundAgent); |
| 194 |
} |
| 195 |
|
| 196 |
//----------------------------------------------------------------- |
| 197 |
/** |
| 198 |
* Handle incoming message. |
| 199 |
* Messages: |
| 200 |
* - quit ... application quit |
| 201 |
* - inc_loglevel ... inc loglevel by 1 (max LEVEL_DEBUG) |
| 202 |
* - dec_loglevel ... dec loglevel by 1 (min LEVEL_ERROR) |
| 203 |
*/ |
| 204 |
void |
| 205 |
Application::receiveSimple(const SimpleMsg *msg) |
| 206 |
{ |
| 207 |
if (msg->equalsName("quit")) { |
| 208 |
m_quit = true; |
| 209 |
} |
| 210 |
else if (msg->equalsName("inc_loglevel")) { |
| 211 |
int level = Log::getLogLevel() + 1; |
| 212 |
if (level <= Log::LEVEL_DEBUG) { |
| 213 |
OptionAgent::agent()->setParam("loglevel", level); |
| 214 |
} |
| 215 |
} |
| 216 |
else if (msg->equalsName("dec_loglevel")) { |
| 217 |
int level = Log::getLogLevel() - 1; |
| 218 |
if (level >= Log::LEVEL_ERROR) { |
| 219 |
OptionAgent::agent()->setParam("loglevel", level); |
| 220 |
} |
| 221 |
} |
| 222 |
else if (msg->equalsName("flush_stdout")) { |
| 223 |
fflush(stdout); |
| 224 |
} |
| 225 |
else { |
| 226 |
LOG_WARNING(ExInfo("unknown msg") |
| 227 |
.addInfo("msg", msg->toString())); |
| 228 |
} |
| 229 |
} |
| 230 |
//----------------------------------------------------------------- |
| 231 |
/** |
| 232 |
* Handle incoming message. |
| 233 |
* Messages: |
| 234 |
* - param_changed(loglevel) ... set loglevel |
| 235 |
*/ |
| 236 |
void |
| 237 |
Application::receiveString(const StringMsg *msg) |
| 238 |
{ |
| 239 |
if (msg->equalsName("param_changed")) { |
| 240 |
std::string param = msg->getValue(); |
| 241 |
if ("loglevel" == param) { |
| 242 |
Log::setLogLevel(OptionAgent::agent()->getAsInt("loglevel")); |
| 243 |
} |
| 244 |
} |
| 245 |
else { |
| 246 |
LOG_WARNING(ExInfo("unknown msg") |
| 247 |
.addInfo("msg", msg->toString())); |
| 248 |
} |
| 249 |
} |
| 250 |
|