src/main.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  *  Starting file. (the 'main' function is here.)
00020  *****************************************************************************/
00021 
00022 #include "include/app.h"
00023 #include <algorithm>
00024 #include <exception>
00025 #include <sstream>
00026 #include <stdexcept>
00027 #include <string>
00028 #include <vector>
00029 #include <iostream>
00030 #include <SDL.h>
00031 #include "game/config.h"
00032 #include "game/time.h"
00033 #include "graphic/font.h"
00034 #include "graphic/video.h"
00035 #include "menu/credits_menu.h"
00036 #include "menu/game_menu.h"
00037 #include "menu/main_menu.h"
00038 #include "menu/network_connection_menu.h"
00039 #include "menu/network_menu.h"
00040 #include "menu/options_menu.h"
00041 #include "include/action_handler.h"
00042 #include "include/constant.h"
00043 #include "sound/jukebox.h"
00044 #include "tool/debug.h"
00045 #include "tool/i18n.h"
00046 #include "tool/random.h"
00047 #include "tool/stats.h"
00048 
00049 #include "network/download.h"
00050 
00051 AppWormux * AppWormux::singleton = NULL;
00052 
00053 AppWormux * AppWormux::GetInstance() {
00054   if (singleton == NULL) {
00055     singleton = new AppWormux();
00056   }
00057   return singleton;
00058 }
00059 
00060 AppWormux::AppWormux(){
00061 }
00062 
00063 int AppWormux::main (int argc, char **argv){
00064   bool quit = false;
00065 
00066   try {
00067     Init(argc, argv);
00068     do {
00069       Main_Menu main_menu;
00070       menu_item choix;
00071 
00072       StatStart("Main:Menu");
00073       choix = main_menu.Run();
00074       StatStop("Main:Menu");
00075 
00076       switch (choix)
00077         {
00078         case menuPLAY:
00079           {
00080             GameMenu game_menu;
00081             game_menu.Run();
00082             break;
00083           }
00084         case menuNETWORK:
00085           {
00086             NetworkConnectionMenu network_connection_menu;
00087             network_connection_menu.Run();
00088             break;
00089           }
00090         case menuOPTIONS:
00091           {
00092             OptionMenu options_menu;
00093             options_menu.Run();
00094             break;
00095           }
00096         case menuCREDITS:
00097           {
00098             CreditsMenu credits_menu;
00099             credits_menu.Run();
00100             break;
00101           }
00102         case menuQUIT:
00103           quit = true;
00104         default:
00105           break;
00106         }
00107     } while (!quit);
00108 
00109     End();
00110   }
00111   catch (const std::exception &e){
00112     std::cerr << std::endl
00113               << _("C++ exception caught:") << std::endl
00114               << e.what() << std::endl
00115               << std::endl;
00116     WakeUpDebugger();
00117   }
00118   catch (...){
00119     std::cerr << std::endl
00120               << _("Unexpected exception caught...") << std::endl
00121               << std::endl;
00122     WakeUpDebugger();
00123   }
00124 
00125   return 0;
00126 }
00127 
00128 void AppWormux::Init(int argc, char **argv){
00129   Config * config = Config::GetInstance();
00130 
00131   DisplayWelcomeMessage();
00132   InitDebugModes(argc, argv);
00133 
00134   video.InitWindow();
00135   InitFonts();
00136 
00137   DisplayLoadingPicture();
00138   config->Apply();
00139 
00140   jukebox.Init();
00141 }
00142 
00143 void AppWormux::DisplayLoadingPicture(){
00144   Config * config = Config::GetInstance();
00145 
00146   std::string txt_version = _("Version") + std::string(" ") + Constants::VERSION;
00147   std::string filename = config->GetDataDir()
00148     + PATH_SEPARATOR + "menu"
00149     + PATH_SEPARATOR + "img"
00150     + PATH_SEPARATOR + "loading.png";
00151 
00152   Surface surfaceLoading = Surface( filename.c_str() );
00153   Sprite loading_image = Sprite( surfaceLoading );
00154 
00155   loading_image.cache.EnableLastFrameCache();
00156   loading_image.ScaleSize( video.window.GetSize() );
00157   loading_image.Blit( video.window, 0, 0 );
00158 
00159   Time::GetInstance()->Reset();
00160 
00161   Text text1(_("Wormux launching..."), white_color, Font::GetInstance(Font::FONT_HUGE), true);
00162   Text text2(txt_version, white_color, Font::GetInstance(Font::FONT_HUGE), true);
00163 
00164   Point2i windowCenter = video.window.GetSize() / 2;
00165 
00166   text1.DrawCenter( windowCenter );
00167   text2.DrawCenter( windowCenter + Point2i(0, (*Font::GetInstance(Font::FONT_HUGE)).GetHeight() + 20 ));
00168 
00169   video.window.Flip();
00170 }
00171 
00172 void AppWormux::InitFonts(){
00173   if( TTF_Init() == -1 )
00174     Error( Format( _("Initialisation of TTF library failed: %s"), TTF_GetError() ) );
00175 }
00176 
00177 void AppWormux::End(){
00178   std::cout << std::endl
00179             << "[ " << _("Quit Wormux") << " ]" << std::endl;
00180 
00181   Config::GetInstance()->Save();
00182   jukebox.End();
00183   delete Config::GetInstance();
00184   delete Time::GetInstance();
00185   delete Constants::GetInstance();
00186   TTF_Quit();
00187 
00188 #ifdef ENABLE_STATS
00189   SaveStatToXML("stats.xml");
00190 #endif
00191   std::cout << "o "
00192             << _("If you found a bug or have a feature request send us a email (in english, please):") << " " << Constants::EMAIL
00193             << std::endl;
00194 }
00195 
00196 void AppWormux::DisplayWelcomeMessage(){
00197   std::cout << "=== " << _("Wormux version ") << Constants::VERSION << std::endl;
00198   std::cout << "=== " << _("Authors:") << ' ';
00199   for( std::vector<std::string>::iterator it=Constants::AUTHORS.begin(),
00200        fin=Constants::AUTHORS.end();
00201        it != fin;
00202        ++it)
00203   {
00204     if( it != Constants::AUTHORS.begin() )
00205       std::cout << ", ";
00206     std::cout << *it;
00207   }
00208   std::cout << std::endl
00209             << "=== " << _("Website: ") << Constants::WEB_SITE << std::endl
00210             << std::endl;
00211 
00212   // Affiche l'absence de garantie sur le jeu
00213   std::cout << "Wormux version " << Constants::VERSION
00214             << ", Copyright (C) 2001-2006 Wormux Team"
00215             << std::endl
00216             << "Wormux comes with ABSOLUTELY NO WARRANTY." << std::endl
00217             << "This is free software, and you are welcome to redistribute it" << std::endl
00218             << "under certain conditions." << std::endl
00219             << std::endl
00220             << "Read COPYING file for details." << std::endl
00221             << std::endl;
00222 
00223 #ifdef DEBUG
00224   std::cout << "!!! This program was compiled in DEBUG mode (development version) !!!" << std::endl
00225             << std::endl;
00226 #endif
00227 
00228   std::cout << "[ " << _("Run game") << " ]" << std::endl;
00229 }
00230 
00231 int main (int argc, char **argv)
00232 {
00233   AppWormux::GetInstance()->main(argc,argv);
00234   delete AppWormux::GetInstance();
00235   exit (EXIT_SUCCESS);
00236 }

Generated on Mon Jan 1 13:10:58 2007 for Wormux by  doxygen 1.4.7