system.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00017 #include "system.h"
00018 #include "audio.h"
00019 #include "script.h"
00020 
00021 using namespace std;
00022 
00023 using namespace hoa_utils;
00024 using namespace hoa_audio;
00025 using namespace hoa_script;
00026 using namespace hoa_mode_manager;
00027 
00028 template<> hoa_system::GameSystem* Singleton<hoa_system::GameSystem>::_singleton_reference = NULL;
00029 
00030 namespace hoa_system {
00031 
00032 GameSystem* SystemManager = NULL;
00033 bool SYSTEM_DEBUG = false;
00034 
00035 // -----------------------------------------------------------------------------
00036 // SystemTimer Class
00037 // -----------------------------------------------------------------------------
00038 
00039 SystemTimer::SystemTimer() :
00040   _state(SYSTEM_TIMER_INVALID),
00041   _duration(0),
00042   _number_loops(0),
00043   _mode_owner(NULL),
00044   _time_expired(0),
00045   _times_completed(0)
00046 {}
00047 
00048 
00049 
00050 SystemTimer::~SystemTimer() {
00051   // If the timer is still in the invalid state, the SystemManager never received a reference to it.
00052   if (_state == SYSTEM_TIMER_INVALID)
00053     return;
00054 
00055   // Remove the reference to this timer object from the SystemManager
00056   SystemManager->_system_timers.erase(this);
00057 }
00058 
00059 
00060 
00061 void SystemTimer::Initialize(uint32 duration, int32 number_loops, hoa_mode_manager::GameMode* mode_owner) {
00062   // If the state is invalid, this is the first time that this timer has been initialized and we need to pass it
00063   // along to the SystemManager
00064   if (_state == SYSTEM_TIMER_INVALID) {
00065     SystemManager->_system_timers.insert(this);
00066   }
00067 
00068   _duration = duration;
00069   _number_loops = number_loops;
00070   _mode_owner = mode_owner;
00071 
00072   _state = SYSTEM_TIMER_INITIAL;
00073   _time_expired = 0;
00074   _times_completed = 0;
00075 }
00076 
00077 
00078 
00079 void SystemTimer::SetDuration(uint32 duration) {
00080   if (IsInitial()) {
00081     _duration = duration;
00082   }
00083   else {
00084     if (SYSTEM_DEBUG)
00085       cerr << "SYSTEM WARNING: SystemTimer::SetDuration() was invoked when the timer was not in the "
00086         << "initial state. No operation was performed." << endl;
00087     return;
00088   }
00089 }
00090 
00091 
00092 
00093 void SystemTimer::SetNumberLoops(int32 number_loops) {
00094   if (IsInitial()) {
00095     _number_loops = number_loops;
00096   }
00097   else {
00098     if (SYSTEM_DEBUG)
00099       cerr << "SYSTEM WARNING: SystemTimer::SetNumberLoops() was invoked when the timer was not in the "
00100         << "initial state. No operation was performed." << endl;
00101     return;
00102   }
00103 }
00104 
00105 
00106 
00107 void SystemTimer::SetModeOwner(hoa_mode_manager::GameMode* mode_owner) {
00108   if (IsInitial()) {
00109     _mode_owner = mode_owner;
00110   }
00111   else {
00112     if (SYSTEM_DEBUG)
00113       cerr << "SYSTEM WARNING: SystemTimer::SetModeOwner() was invoked when the timer was not in the "
00114         << "initial state. No operation was performed." << endl;
00115     return;
00116   }
00117 }
00118 
00119 
00120 
00121 void SystemTimer::_UpdateTimer() {
00122   if (IsRunning() == false)
00123     return;
00124 
00125   _time_expired += SystemManager->GetUpdateTime();
00126 
00127   if (_time_expired >= _duration) {
00128     _times_completed++;
00129 
00130     // Checks if infinite looping is enabled
00131     if (_number_loops < 0) { 
00132       _time_expired -= _duration;
00133     }
00134     // Checks if the number of loops have expired
00135     else if (_times_completed > static_cast<uint32>(_number_loops)) {
00136       _time_expired = 0;
00137       _state = SYSTEM_TIMER_FINISHED;
00138     }
00139     // Otherwise, there are still additional loops to complete
00140     else {
00141       _time_expired -= _duration;
00142     }
00143   }
00144 }
00145 
00146 // -----------------------------------------------------------------------------
00147 // GameSystem Class
00148 // -----------------------------------------------------------------------------
00149 
00150 GameSystem::GameSystem() {
00151   if (SYSTEM_DEBUG)
00152     cout << "SETTINGS: GameSystem constructor invoked" << endl;
00153 
00154   _not_done = true;
00155   _language = "en"; // Default language is English
00156 }
00157 
00158 
00159 
00160 GameSystem::~GameSystem() {
00161   if (SYSTEM_DEBUG)
00162     cout << "SETTINGS: GameSystem destructor invoked" << endl;
00163 }
00164 
00165 
00166 
00167 bool GameSystem::SingletonInitialize() {
00168   // TODO: Initialize the gettext library
00169 //  setlocale(LC_ALL, "");
00170 //  bindtextdomain(PACKAGE, DATADIR);
00171 //  textdomain(PACKAGE);
00172 
00173   ReadScriptDescriptor settings_data;
00174 
00175   if (settings_data.OpenFile("dat/config/settings.lua") == false) {
00176     cerr << "SYSTEM ERROR: failed to load settings from data file" << endl;
00177     return false;
00178   }
00179 
00180   settings_data.OpenTable("video_settings");
00181 //  SetFullScreen(settings_data.ReadBool("full_screen"));
00182   settings_data.CloseTable();
00183 
00184   settings_data.OpenTable("audio_settings");
00185   AudioManager->SetMusicVolume(settings_data.ReadFloat("music_vol"));
00186   AudioManager->SetSoundVolume(settings_data.ReadFloat("sound_vol"));
00187   settings_data.CloseTable();
00188 
00189   if (settings_data.IsErrorDetected()) {
00190     cerr << "SETTINGS WARNING: some errors occured during read operations from data file:" << endl;
00191     cerr << settings_data.GetErrorMessages() << endl;
00192   }
00193   settings_data.CloseFile();
00194   return true;
00195 }
00196 
00197 
00198 // Set up the timers before the main game loop begins
00199 void GameSystem::InitializeTimers() {
00200   _last_update = SDL_GetTicks();
00201   _update_time = 1; // Set to non-zero, otherwise bad things may happen...
00202   _hours_played = 0;
00203   _minutes_played = 0;
00204   _seconds_played = 0;
00205   _milliseconds_played = 0;
00206   _system_timers.clear();
00207 }
00208 
00209 
00210 
00211 void GameSystem::UpdateTimers() {
00212   uint32 tmp;
00213 
00214   // ----- (1): Update the simple game timer
00215   tmp = _last_update;
00216   _last_update = SDL_GetTicks();
00217   _update_time = _last_update - tmp;
00218 
00219   // ----- (2): Update the game play timer
00220   _milliseconds_played += _update_time;
00221   if (_milliseconds_played >= 1000) {
00222     _seconds_played += _milliseconds_played / 1000;
00223     _milliseconds_played = _milliseconds_played % 1000;
00224     if (_seconds_played >= 60) {
00225       _minutes_played += _seconds_played / 60;
00226       _seconds_played = _seconds_played % 60;
00227       if (_minutes_played >= 60) {
00228         _hours_played += _minutes_played / 60;
00229         _minutes_played = _minutes_played % 60;
00230       }
00231     }
00232   }
00233 
00234   // ----- (3): Update all SystemTimer objects
00235   for (set<SystemTimer*>::iterator i = _system_timers.begin(); i != _system_timers.end(); i++)
00236     (*i)->_UpdateTimer();
00237 }
00238 
00239 
00240 
00241 void GameSystem::ExamineSystemTimers() {
00242   GameMode* active_mode = ModeManager->GetTop();
00243   GameMode* timer_mode = NULL;
00244 
00245   for (set<SystemTimer*>::iterator i = _system_timers.begin(); i != _system_timers.end(); i++) {
00246     timer_mode = (*i)->GetModeOwner();
00247     if (timer_mode == NULL)
00248       continue;
00249     
00250     if (timer_mode == active_mode)
00251       (*i)->Run();
00252     else
00253       (*i)->Pause();
00254   }
00255 }
00256 
00257 
00258 
00259 void GameSystem::SetLanguage(std::string lang) {
00260   // A 2 character string is the only allowable argument
00261   if (lang.size() != 2) {
00262     return;
00263   }
00264 //  for (uint32 i = 0; i < SUPPORTED_LANGUAGES.size(); i++) {
00265 //    if (lang == SUPPORTED_LANGUAGES[i]) {
00266 //      _language = lang;
00267 //      return;
00268 //    }
00269 //  }
00270 //
00271 //  cerr << "SETTINGS ERROR: attempt to set unsupported language \"" << lang << "\" failed" << endl;
00272   _language = lang;
00273 }
00274 
00275 } // namespace hoa_system

Generated on Fri Jul 6 23:11:24 2007 for Hero of Allacrost by  doxygen 1.5.1