Adonthell 0.4

main.cc

Go to the documentation of this file.
00001 /*
00002    $Id: main.cc,v 1.61 2004/10/25 06:55:01 ksterker Exp $
00003  
00004    Copyright (C) 1999/2000/2001/2002/2003/2004 Kai Sterker
00005    Part of the Adonthell Project http://adonthell.linuxgames.com
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 
00016 /**
00017  * @file   main.cc
00018  * @author Kai Sterker <kaisterker@linuxgames.com>
00019  * 
00020  * @brief  Contains the main() function.
00021  * 
00022  * 
00023  */
00024  
00025 #include "audio.h"
00026 #include "event_handler.h"
00027 #include "game.h"
00028 #include "gamedata.h"
00029 #include "gametime.h"
00030 #include "input.h"
00031 #include "nls.h"
00032 #include "python_class.h"
00033 #include "screen.h"
00034 #include "yarg.h"
00035 #include "win_manager.h"
00036 
00037 #ifdef MEMORY_LEAKS
00038 #include <mcheck.h>
00039 #endif
00040 
00041 using namespace std; 
00042 
00043 /*
00044  * SWIG init prototypes. Should we use dynamic linking??? 
00045  */
00046 extern "C"
00047 {
00048     /** 
00049      * SWIG init prototype.
00050      * 
00051      */
00052     void init_adonthell (void);
00053 }
00054 
00055 bool init_python()
00056 {
00057     // Initialise the import path.
00058     // Shared modules path 
00059     string t;
00060     t = game::global_data_dir() + "/modules";
00061     python::insert_path ((char *) t.c_str()); 
00062     
00063     // Game specific path
00064     t = game::game_data_dir () + "/scripts/modules"; 
00065     python::insert_path ((char *) t.c_str ());
00066     t = game::game_data_dir () + "/scripts"; 
00067     python::insert_path ((char *) t.c_str ());
00068     
00069     // Initialise SWIG module. This should go if we ever switch 
00070     // to dynamic linking
00071     init_adonthell ();
00072     
00073     python::module = python::import_module ("adonthell"); 
00074     if (!python::module) return false;     
00075     
00076     data::globals = PyModule_GetDict (python::module);
00077     
00078     return true; 
00079 }
00080 
00081 /**
00082  * Game's main function.
00083  * It simply initialises the game and runs the "init.py" file in the game
00084  * directory given as first argument. Once the execution is finished,
00085  * it cleans everything up, and exits.
00086  *
00087  * @param argc Number of arguments to the program.
00088  * @param argv Array of strings containing the program's arguments.
00089  *
00090  * @return 0 in case of success, error code otherwise.
00091  * 
00092  */
00093 extern "C"
00094 int main(int argc, char * argv[])
00095 {
00096     config myconfig;
00097 
00098 #ifdef MEMORY_LEAKS
00099     // to debug memory leaks with mtrace. It's better to use
00100     // a tool like memprof, mpatrol or valgrind though.
00101     mtrace ();
00102 #endif
00103 
00104     // init game environment
00105 #if !defined (SINGLE_DIR_INST) 
00106     game::init (DATA_DIR);
00107 #else
00108     // change working directory to the application's location.
00109     if (argc && argv[0])
00110     {
00111         char *str = argv[0];
00112         do if (*str == '\\') *str = '/'; 
00113         while (*(str++));
00114         
00115         str = strrchr (argv[0], '/');
00116         *(str + 1) = 0;
00117         chdir (argv[0]);
00118 
00119         // FIXME: this should go once we have our 'game launcher' gui 
00120         myconfig.gamedir = string (argv[0]) + "/games/wastesedge";
00121 
00122         *(str + 1) = '/';
00123     }
00124     
00125     // get absolute path to current working directory
00126     char buf[500];
00127     getcwd (buf, sizeof (buf));
00128     
00129     char *str = buf;
00130     do if (*str == '\\') *str = '/'; 
00131     while (*(str++));
00132     
00133     game::init (buf);
00134 #endif
00135 
00136     // read the $HOME/.adonthell/adonthellrc file
00137     // and check the arguments we recieved.
00138     myconfig.read_adonthellrc ();
00139     myconfig.parse_arguments (argc, argv);
00140     
00141     // init national language support
00142     nls::init (myconfig);
00143     
00144     game::set_game_data_dir(myconfig.gamedir);
00145     
00146     // init game loading/saving system
00147     gamedata::init (game::user_data_dir(), game::game_data_dir(),
00148         myconfig.game_name, myconfig.quick_load); 
00149 
00150     // init video subsystem
00151     screen::set_video_mode (320, 240, 0, myconfig.double_screen, myconfig.screen_mode);
00152 
00153 #ifdef DEBUG
00154     printf("%s\n", screen::info().c_str());
00155 #endif
00156      
00157     // init audio subsystem
00158     if (myconfig.audio_volume > 0)
00159         audio::init (&myconfig);
00160     
00161     // init input subsystem
00162     input::init ();
00163     
00164     // init python interpreter
00165     python::init (); 
00166     init_python();
00167 
00168     // init the game data
00169     data::engine = new adonthell;
00170     data::the_player = NULL;
00171 
00172     // init window manager
00173     win_manager::init (myconfig.font); 
00174 
00175     // event system
00176     event_handler::init ();
00177 
00178     // gametime system
00179     // (180 realtime minutes make approx. 1 gametime day)
00180     gametime::init (180);
00181 
00182     // init random number generator
00183     yarg::init (myconfig.game_name);
00184     yarg::randomize ();
00185     
00186     // It's up to the game what happens here
00187     python::exec_file ("init");
00188 
00189     // close all windows
00190     // delete all themes and fonts
00191     win_manager::cleanup (); 
00192     
00193     // shutdown input subsystem
00194     input::shutdown ();
00195 
00196     // shutdown audio
00197     audio::cleanup ();
00198 
00199     // cleanup the saves
00200     gamedata::cleanup (); 
00201     
00202     // cleanup data
00203     delete data::engine;
00204     if (data::the_player)
00205         delete data::the_player;
00206 
00207     // cleanup event system
00208     event_handler::cleanup ();
00209     
00210     // shutdown python
00211     python::cleanup ();     
00212 
00213     // shutdown video and SDL
00214     SDL_Quit ();
00215 
00216     return 0;
00217 }