Adonthell  0.4
main.cc
Go to the documentation of this file.
1 /*
2  $Id: main.cc,v 1.61 2004/10/25 06:55:01 ksterker Exp $
3 
4  Copyright (C) 1999/2000/2001/2002/2003/2004 Kai Sterker
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file main.cc
18  * @author Kai Sterker <kaisterker@linuxgames.com>
19  *
20  * @brief Contains the main() function.
21  *
22  *
23  */
24 
25 #include "audio.h"
26 #include "event_handler.h"
27 #include "game.h"
28 #include "gamedata.h"
29 #include "gametime.h"
30 #include "input.h"
31 #include "nls.h"
32 #include "python_class.h"
33 #include "screen.h"
34 #include "yarg.h"
35 #include "win_manager.h"
36 
37 #ifdef MEMORY_LEAKS
38 #include <mcheck.h>
39 #endif
40 
41 using namespace std;
42 
43 /*
44  * SWIG init prototypes. Should we use dynamic linking???
45  */
46 extern "C"
47 {
48  /**
49  * SWIG init prototype.
50  *
51  */
52  void init_adonthell (void);
53 }
54 
55 bool init_python()
56 {
57  // Initialise the import path.
58  // Shared modules path
59  string t;
60  t = game::global_data_dir() + "/modules";
61  python::insert_path ((char *) t.c_str());
62 
63  // Game specific path
64  t = game::game_data_dir () + "/scripts/modules";
65  python::insert_path ((char *) t.c_str ());
66  t = game::game_data_dir () + "/scripts";
67  python::insert_path ((char *) t.c_str ());
68 
69  // Initialise SWIG module. This should go if we ever switch
70  // to dynamic linking
71  init_adonthell ();
72 
73  python::module = python::import_module ("adonthell");
74  if (!python::module) return false;
75 
76  data::globals = PyModule_GetDict (python::module);
77 
78  return true;
79 }
80 
81 /**
82  * Game's main function.
83  * It simply initialises the game and runs the "init.py" file in the game
84  * directory given as first argument. Once the execution is finished,
85  * it cleans everything up, and exits.
86  *
87  * @param argc Number of arguments to the program.
88  * @param argv Array of strings containing the program's arguments.
89  *
90  * @return 0 in case of success, error code otherwise.
91  *
92  */
93 extern "C"
94 int main(int argc, char * argv[])
95 {
96  config myconfig;
97 
98 #ifdef MEMORY_LEAKS
99  // to debug memory leaks with mtrace. It's better to use
100  // a tool like memprof, mpatrol or valgrind though.
101  mtrace ();
102 #endif
103 
104  // init game environment
105 #if !defined (SINGLE_DIR_INST)
106  game::init (DATA_DIR);
107 #else
108  // change working directory to the application's location.
109  if (argc && argv[0])
110  {
111  char *str = argv[0];
112  do if (*str == '\\') *str = '/';
113  while (*(str++));
114 
115  str = strrchr (argv[0], '/');
116  *(str + 1) = 0;
117  chdir (argv[0]);
118 
119  // FIXME: this should go once we have our 'game launcher' gui
120  myconfig.gamedir = string (argv[0]) + "/games/wastesedge";
121 
122  *(str + 1) = '/';
123  }
124 
125  // get absolute path to current working directory
126  char buf[500];
127  getcwd (buf, sizeof (buf));
128 
129  char *str = buf;
130  do if (*str == '\\') *str = '/';
131  while (*(str++));
132 
133  game::init (buf);
134 #endif
135 
136  // read the $HOME/.adonthell/adonthellrc file
137  // and check the arguments we recieved.
138  myconfig.read_adonthellrc ();
139  myconfig.parse_arguments (argc, argv);
140 
141  // init national language support
142  nls::init (myconfig);
143 
145 
146  // init game loading/saving system
148  myconfig.game_name, myconfig.quick_load);
149 
150  // init video subsystem
151  screen::set_video_mode (320, 240, 0, myconfig.double_screen, myconfig.screen_mode);
152 
153 #ifdef DEBUG
154  printf("%s\n", screen::info().c_str());
155 #endif
156 
157  // init audio subsystem
158  if (myconfig.audio_volume > 0)
159  audio::init (&myconfig);
160 
161  // init input subsystem
162  input::init ();
163 
164  // init python interpreter
165  python::init ();
166  init_python();
167 
168  // init the game data
169  data::engine = new adonthell;
170  data::the_player = NULL;
171 
172  // init window manager
173  win_manager::init (myconfig.font);
174 
175  // event system
177 
178  // gametime system
179  // (180 realtime minutes make approx. 1 gametime day)
180  gametime::init (180);
181 
182  // init random number generator
183  yarg::init (myconfig.game_name);
184  yarg::randomize ();
185 
186  // It's up to the game what happens here
187  python::exec_file ("init");
188 
189  // close all windows
190  // delete all themes and fonts
192 
193  // shutdown input subsystem
194  input::shutdown ();
195 
196  // shutdown audio
197  audio::cleanup ();
198 
199  // cleanup the saves
200  gamedata::cleanup ();
201 
202  // cleanup data
203  delete data::engine;
204  if (data::the_player)
205  delete data::the_player;
206 
207  // cleanup event system
209 
210  // shutdown python
211  python::cleanup ();
212 
213  // shutdown video and SDL
214  SDL_Quit ();
215 
216  return 0;
217 }
bool read_adonthellrc()
Reads the configuration file.
Definition: prefs.cc:295
Declares the event_handler class.
static bool exec_file(string filename)
Executes a Python script.
Definition: python_class.cc:83
static void cleanup()
Cleanup the saved game array.
Definition: gamedata.cc:545
Declares the input class.
static void init()
Instanciate the actual event handlers.
string gamedir
Path of the directory that contains the game running at present.
Definition: prefs.h:135
Declares the screen class.
static bool init(string udir, string gdir, string gname, u_int8 qload)
Initialise the saved games array.
Definition: gamedata.cc:477
Definition: str_hash.h:36
Declares the game class.
Declares the gametime class.
static string info()
Returns information about the current screen settings, suitable for being displayed to the user...
Definition: screen.cc:115
National Language Support.
void init_adonthell(void)
SWIG init prototype.
static void init(const string &font)
Empty for now.
Definition: win_manager.cc:76
static void init(config &myconfig)
Initialize national language support.
Definition: nls.cc:31
static void init(string game_dir)
Initialise the game framework.
Definition: game.cc:38
static string global_data_dir()
Returns the absolute path to the global data directory.
Definition: game.h:86
Declares the gamedata and data classes.
static PyObject * import_module(string filename)
Imports a Python module.
static string game_data_dir()
Returns the absolute path to the current game's directory (if any).
Definition: game.h:97
u_int8 quick_load
Whether the quick-load feature is enabled (1) or not (0)
Definition: prefs.h:144
static void init()
Initialise the input system.
Definition: input.cc:46
static void shutdown()
Free resources occupied by the input system.
Definition: input.cc:58
static void cleanup()
Delete the event handlers.
int main(int argc, char *argv[])
Game's main function.
Definition: main.cc:94
Defines the python class. This file is named this way so it doesn't conflicts with Python...
u_int8 audio_volume
The volume: a value betwen 0 and 100.
Definition: prefs.h:161
void parse_arguments(int argc, char *argv[])
See whether any options have been specified on the command line.
Definition: prefs.cc:117
static void cleanup()
Delete all themes and fonts currently loaded.
Definition: win_manager.cc:83
static void insert_path(char *name)
Adds a directory to Python's include path.
Definition: python_class.cc:60
string game_name
Name of the game that is running at present.
Definition: prefs.h:131
static void set_game_data_dir(string game_dir)
Specify an additional data directory containing game data.
Definition: game.cc:49
Declares the win_manager class.
static void init(u_int16 rt_minutes)
Initialize the gametime class.
Definition: gametime.cc:32
static string user_data_dir()
Returns the absolute path to the user data directory (usually ~/.adonthell).
Definition: game.h:75
static void set_video_mode(u_int16 nl, u_int16 nh, u_int8 depth=0, bool dbl=false, bool fscreen=false)
Sets the video mode.
Definition: screen.cc:39
This class contains the engine's configuration read either from the config file or from the command l...
Definition: prefs.h:70
This is the heart of the Adonthell engine.
Definition: adonthell.h:40
static void cleanup()
Cleanup Python.
Definition: python_class.cc:48
u_int8 screen_mode
Whether the engine shall run in window (0) or fullscreen (1) mode.
Definition: prefs.h:139
static void init()
Initialise Python and insert the Adonthell include paths.
Definition: python_class.cc:40