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 }