main.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 
00028 #include <ctime>
00029 #ifdef __MACH__
00030   #include <unistd.h>
00031   #include <string>
00032 #endif
00033 
00034 #include "utils.h"
00035 #include "defs.h"
00036 
00037 #include "main_options.h"
00038 
00039 #include "audio.h"
00040 #include "video.h"
00041 #include "input.h"
00042 #include "script.h"
00043 #include "system.h"
00044 #include "global.h"
00045 #include "mode_manager.h"
00046 
00047 #include "boot.h"
00048 #include "map.h"
00049 
00050 using namespace std;
00051 using namespace hoa_utils;
00052 using namespace hoa_audio;
00053 using namespace hoa_video;
00054 using namespace hoa_mode_manager;
00055 using namespace hoa_input;
00056 using namespace hoa_system;
00057 using namespace hoa_global;
00058 using namespace hoa_script;
00059 using namespace hoa_boot;
00060 using namespace hoa_map;
00061 
00062 
00075 void QuitAllacrost() {
00076   // NOTE: Even if the singleton objects do not exist when this function is called, invoking the
00077   // static Destroy() singleton function will do no harm (it checks that the object exists before deleting it).
00078 
00079   // Delete the mode manager first so that all game modes free their resources
00080   GameModeManager::SingletonDestroy();
00081 
00082   // Delete the global manager second to remove all object references corresponding to other engine subsystems
00083   GameGlobal::SingletonDestroy();
00084 
00085   // Delete all of the reamining independent engine components
00086   GameAudio::SingletonDestroy();
00087   GameInput::SingletonDestroy();
00088   GameScript::SingletonDestroy();
00089   GameSystem::SingletonDestroy();
00090   GameVideo::SingletonDestroy();
00091 } // void QuitAllacrost()
00092 
00093 
00098 bool InitializeEngine() {
00099   // Initialize SDL. The video, audio, and joystick subsystems are initialized elsewhere.
00100   if (SDL_Init(SDL_INIT_TIMER) != 0) {
00101     cerr << "MAIN ERROR: Unable to initialize SDL: " << SDL_GetError() << endl;
00102     return false;
00103   }
00104 
00105   // Create and initialize singleton class managers
00106   AudioManager = GameAudio::SingletonCreate();
00107   InputManager = GameInput::SingletonCreate();
00108   VideoManager = GameVideo::SingletonCreate();
00109   ScriptManager = GameScript::SingletonCreate();
00110   SystemManager = GameSystem::SingletonCreate();
00111   ModeManager = GameModeManager::SingletonCreate();
00112   GlobalManager = GameGlobal::SingletonCreate();
00113 
00114   if (VideoManager->SingletonInitialize() == false) {
00115     cerr << "ERROR: unable to initialize VideoManager" << endl;
00116     return false;
00117   }
00118 
00119   if (VideoManager->LoadMenuSkin("black_sleet", "img/menus/black_sleet_skin.png", "img/menus/black_sleet_texture.png") == false) {
00120     return false;
00121   }
00122 
00123   if (VideoManager->LoadFont("img/fonts/vtc_switchblade_romance.ttf", "default", 18) == false) {
00124     return false;
00125   }
00126 
00127   VideoManager->SetFontShadowXOffset("default", 1);
00128   VideoManager->SetFontShadowYOffset("default", -2);
00129   VideoManager->SetFontShadowStyle("default", VIDEO_TEXT_SHADOW_BLACK);
00130 
00131   VideoManager->SetFontShadowXOffset("default", 1);
00132   VideoManager->SetFontShadowYOffset("default", -2);
00133   VideoManager->SetFontShadowStyle("default", VIDEO_TEXT_SHADOW_BLACK);
00134 
00135   if (!VideoManager->LoadFont("img/fonts/vtc_switchblade_romance.ttf", "map", 24)) {
00136     return false;
00137   }
00138 
00139   VideoManager->SetFontShadowXOffset("map", 0);
00140   VideoManager->SetFontShadowYOffset("map", 0);
00141   VideoManager->SetFontShadowStyle("map", VIDEO_TEXT_SHADOW_BLACK);
00142 
00143   if (!VideoManager->LoadFont("img/fonts/vtc_switchblade_romance.ttf", "battle", 20)) {
00144     return false;
00145   }
00146 
00147   VideoManager->SetFontShadowXOffset("battle", 1);
00148   VideoManager->SetFontShadowYOffset("battle", -2);
00149   VideoManager->SetFontShadowStyle("battle", VIDEO_TEXT_SHADOW_BLACK);
00150 
00151   // Font used to show damage received / given in battle mode
00152   if (!VideoManager->LoadFont("img/fonts/vtc_switchblade_romance.ttf", "battle_dmg", 24)) {
00153     return false;
00154   }
00155 
00156   VideoManager->SetFontShadowXOffset("battle_dmg", 1);
00157   VideoManager->SetFontShadowYOffset("battle_dmg", -2);
00158   VideoManager->SetFontShadowStyle("battle_dmg", VIDEO_TEXT_SHADOW_BLACK);
00159 
00160   if (AudioManager->SingletonInitialize() == false) {
00161     cerr << "ERROR: unable to initialize AudioManager" << endl;
00162     return false;
00163   }
00164   if (ScriptManager->SingletonInitialize() == false) {
00165     cerr << "ERROR: unable to initialize ScriptManager" << endl;
00166     return false;
00167   }
00168   hoa_defs::BindEngineToLua();
00169   if (ModeManager->SingletonInitialize() == false) {
00170     cerr << "ERROR: unable to initialize ModeManager" << endl;
00171     return false;
00172   }
00173   if (SystemManager->SingletonInitialize() == false) {
00174     cerr << "ERROR: unable to initialize SystemManager" << endl;
00175     return false;
00176   }
00177   if (InputManager->SingletonInitialize() == false) {
00178     cerr << "ERROR: unable to initialize InputManager" << endl;
00179     return false;
00180   }
00181   if (GlobalManager->SingletonInitialize() == false) {
00182     cerr << "ERROR: unable to initialize GlobalManager" << endl;
00183     return false;
00184   }
00185 
00186   // Set the window title and icon name
00187   SDL_WM_SetCaption("Hero of Allacrost", "Hero of Allacrost");
00188 
00189   // Set the window icon
00190   #ifdef _WIN32
00191     SDL_WM_SetIcon(SDL_LoadBMP("img/logos/program_icon.bmp"), NULL);
00192   #else
00193     // Later, add an icon here for non-Windows systems (which support more than 32x32 .bmp files)
00194     SDL_WM_SetIcon(SDL_LoadBMP("img/logos/program_icon.bmp"), NULL);
00195   #endif
00196 
00197   // Hide the mouse cursor since we don't use or acknowledge mouse input from the user
00198   SDL_ShowCursor(SDL_DISABLE);
00199 
00200   // Enabled for multilingual keyboard support
00201   SDL_EnableUNICODE(1);
00202 
00203   // Ignore the events that we don't care about so they never appear in the event queue
00204   SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
00205   SDL_EventState(SDL_MOUSEBUTTONDOWN, SDL_IGNORE);
00206   SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);
00207   SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
00208   SDL_EventState(SDL_VIDEOEXPOSE, SDL_IGNORE);
00209   SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
00210 
00211   SystemManager->InitializeTimers();
00212 
00213   return true;
00214 } // bool InitializeEngine()
00215 
00216 
00217 
00218 // Every great game begins with a single function :)
00219 int32 main(int32 argc, char *argv[]) {
00220   // When the program exits, first the QuitAllacrost() function is called, followed by SDL_Quit()
00221   atexit(SDL_Quit);
00222   atexit(QuitAllacrost);
00223 
00224   // Change to the directory where the Allacrost data is stored
00225   #ifdef __MACH__
00226     string path;
00227     path = argv[0];
00228     // Remove the binary name
00229     path.erase(path.find_last_of('/'));
00230     // Remove the MacOS directory
00231     path.erase(path.find_last_of('/'));
00232     // Now the program should be in app/Contents
00233     path.append ("/Resources/");
00234     chdir(path.c_str());
00235   #elif defined(__linux__) || defined(__FreeBSD__)
00236     // Look for data files in DATADIR only if they are not available in the
00237     // current directory.
00238     if (ifstream("dat/config/settings.lua") == NULL)
00239       chdir(DATADIR);
00240   #endif
00241 
00242   // Initialize the random number generator (note: 'unsigned int' is a required usage in this case)
00243   srand(static_cast<unsigned int>(time(NULL)));
00244 
00245   // This variable will be set by the ParseProgramOptions function
00246   int32 return_code;
00247 
00248   // Parse command lines and exit out of the game if needed
00249   if (hoa_main::ParseProgramOptions(return_code, argc, argv) == false) {
00250     return return_code;
00251   }
00252 
00253   if (InitializeEngine() == false) {
00254     cerr << "ERROR: failed to initialize game engine, exiting..." << endl;
00255     return 1;
00256   }
00257 
00258   // This is the main loop for the game. The loop iterates once for every frame drawn to the screen.
00259   while (SystemManager->NotDone()) {
00260     // 1) Render the scene
00261     VideoManager->Clear();
00262     ModeManager->Draw();
00263     VideoManager->Display(SystemManager->GetUpdateTime());
00264 
00265     // 2) Process all new events
00266     InputManager->EventHandler();
00267 
00268     // 3) Update any streaming audio sources
00269     AudioManager->Update();
00270 
00271     // 4) Update timers for correct time-based movement operation
00272     SystemManager->UpdateTimers();
00273 
00274     // 5) Update the game status
00275     ModeManager->Update();
00276   } // while (SystemManager->NotDone())
00277 
00278   return EXIT_SUCCESS;
00279 } // int32 main(int32 argc, char *argv[])

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