a48b3f4f4e7e604c97f9c0d7220e6d50291f4d9b
[supertux.git] / src / main.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #include <config.h>
21 #include <assert.h>
22
23 #include "main.h"
24
25 #include <stdexcept>
26 #include <iostream>
27 #include <sstream>
28 #include <time.h>
29 #include <stdlib.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <dirent.h>
33 #include <unistd.h>
34 #include <assert.h>
35 #ifndef WIN32
36 #include <libgen.h>
37 #endif
38 #include <SDL.h>
39 #include <SDL_mixer.h>
40 #include <SDL_image.h>
41 #include <SDL_opengl.h>
42
43 #include "gameconfig.h"
44 #include "resources.h"
45 #include "gettext.h"
46 #include "audio/sound_manager.h"
47 #include "video/surface.h"
48 #include "control/joystickkeyboardcontroller.h"
49 #include "misc.h"
50 #include "title.h"
51 #include "game_session.h"
52 #include "file_system.h"
53
54 #ifdef WIN32
55 #define mkdir(dir, mode)    mkdir(dir)
56 #endif
57
58 SDL_Surface* screen = 0;
59 JoystickKeyboardController* main_controller = 0;
60 TinyGetText::DictionaryManager dictionary_manager;
61
62 static void init_config()
63 {
64   config = new Config();
65   try {
66     config->load();
67   } catch(std::exception& e) {
68 #ifdef DEBUG
69     std::cerr << "Couldn't load config file: " << e.what() << "\n";
70 #endif
71   }
72 }
73
74 static void find_directories()
75 {
76   const char* home = getenv("HOME");
77   if(home == 0) {
78 #ifdef DEBUG
79     std::cerr << "Couldn't find home directory.\n";
80 #endif
81     home = ".";
82   }
83
84   user_dir = home;
85   user_dir += "/.supertux";
86
87   // create directories
88   std::string savedir = user_dir + "/save";
89   mkdir(user_dir.c_str(), 0755);
90   mkdir(savedir.c_str(), 0755);
91
92   // try current directory as datadir
93   if(datadir.empty()) {
94     if(FileSystem::faccessible("./data/credits.txt")) {
95       datadir = "./data/";
96     }
97   }
98
99   // Detect datadir with some linux magic
100 #ifndef WIN32
101   if(datadir.empty()) {
102     char exe_file[PATH_MAX];
103     if(readlink("/proc/self/exe", exe_file, PATH_MAX) < 0) {
104 #ifdef DEBUG
105       std::cerr << "Couldn't read /proc/self/exe \n";
106 #endif
107     } else {
108       std::string exedir = std::string(dirname(exe_file)) + "/";
109       std::string testdir = exedir + "./data/";
110       if(access(testdir.c_str(), F_OK) == 0) {
111         datadir = testdir;
112       }
113       
114       testdir = exedir + "../share/supertux/";
115       if(datadir.empty() && access(testdir.c_str(), F_OK) == 0) {
116         datadir = testdir;
117       }
118     }  
119   }
120 #endif
121   
122 #ifdef DATA_PREFIX
123   // use default location
124   if(datadir.empty()) {
125     datadir = DATA_PREFIX;
126   }
127 #endif
128
129   if(datadir.empty())
130     throw std::runtime_error("Couldn't find datadir");
131 }
132
133 static void init_tinygettext()
134 {
135   dictionary_manager.add_directory(datadir + "/locale");
136   dictionary_manager.set_charset("ISO8859-1");
137 }
138
139 static void print_usage(const char* argv0)
140 {
141   fprintf(stderr, _("Usage: %s [OPTIONS] LEVELFILE\n\n"), argv0);
142   fprintf(stderr,
143           _("Options:\n"
144             "  -f, --fullscreen             Run in fullscreen mode.\n"
145             "  -w, --window                 Run in window mode.\n"
146             "  -g, --geometry WIDTHxHEIGHT  Run SuperTux in give resolution\n"
147             "  --help                       Show this help message\n"
148             "  --version                    Display SuperTux version and quit\n"
149             "\n"));
150 }
151
152 static void parse_commandline(int argc, char** argv)
153 {
154   for(int i = 1; i < argc; ++i) {
155     std::string arg = argv[i];
156
157     if(arg == "--fullscreen" || arg == "-f") {
158       config->use_fullscreen = true;
159     } else if(arg == "--window" || arg == "-w") {
160       config->use_fullscreen = false;
161     } else if(arg == "--geometry" || arg == "-g") {
162       if(i+1 >= argc) {
163         print_usage(argv[0]);
164         throw std::runtime_error("Need to specify a parameter for geometry switch");
165       }
166       if(sscanf(argv[++i], "%dx%d", &config->screenwidth, &config->screenheight)
167          != 2) {
168         print_usage(argv[0]);
169         throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
170       }
171     } else if(arg == "--show-fps") {
172       config->show_fps = true;
173     } else if(arg == "--play-demo") {
174       if(i+1 >= argc) {
175         print_usage(argv[0]);
176         throw std::runtime_error("Need to specify a demo filename");
177       }
178       config->start_demo = argv[++i];
179     } else if(arg == "--record-demo") {
180       if(i+1 >= argc) {
181         print_usage(argv[0]);
182         throw std::runtime_error("Need to specify a demo filename");
183       }
184       config->record_demo = argv[++i];
185     } else if(arg == "--help") {
186       print_usage(argv[0]);
187       throw std::runtime_error("");
188     } else if(arg == "--version") {
189       std::cerr << PACKAGE_NAME << " " << PACKAGE_VERSION << "\n";
190       throw std::runtime_error("");
191     } else if(arg[0] != '-') {
192       config->start_level = arg;
193     } else {
194       std::cerr << "Unknown option '" << arg << "'.\n";
195       std::cerr << "Use --help to see a list of options.\n";
196     }
197   }
198
199   // TODO joystick switchyes...
200 }
201
202 static void init_sdl()
203 {
204   if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
205     std::stringstream msg;
206     msg << "Couldn't initialize SDL: " << SDL_GetError();
207     throw std::runtime_error(msg.str());
208   }
209
210   SDL_EnableUNICODE(1);
211
212   // wait 100ms and clear SDL event queue because sometimes we have random
213   // joystick events in the queue on startup...
214   SDL_Delay(100);
215   SDL_Event dummy;
216   while(SDL_PollEvent(&dummy))
217       ;
218 }
219
220 static void check_gl_error()
221 {
222   GLenum glerror = glGetError();
223   std::string errormsg;
224   
225   if(glerror != GL_NO_ERROR) {
226     switch(glerror) {
227       case GL_INVALID_ENUM:
228         errormsg = "Invalid enumeration value";
229         break;
230       case GL_INVALID_VALUE:
231         errormsg = "Numeric argzment out of range";
232         break;
233       case GL_INVALID_OPERATION:
234         errormsg = "Invalid operation";
235         break;
236       case GL_STACK_OVERFLOW:
237         errormsg = "stack overflow";
238         break;
239       case GL_STACK_UNDERFLOW:
240         errormsg = "stack underflow";
241         break;
242       case GL_OUT_OF_MEMORY:
243         errormsg = "out of memory";
244         break;
245       case GL_TABLE_TOO_LARGE:
246         errormsg = "table too large";
247         break;
248       default:
249         errormsg = "unknown error number";
250         break;
251     }
252     std::stringstream msg;
253     msg << "OpenGL Error: " << errormsg;
254     throw std::runtime_error(msg.str());
255   }
256 }
257
258 void init_video()
259 {
260   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 
261   SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
262   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
263   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
264   
265   int flags = SDL_OPENGL;
266   if(config->use_fullscreen)
267     flags |= SDL_FULLSCREEN;
268   int width = config->screenwidth;
269   int height = config->screenheight;
270   int bpp = 0;
271
272   screen = SDL_SetVideoMode(width, height, bpp, flags);
273   if(screen == 0) {
274     std::stringstream msg;
275     msg << "Couldn't set video mode (" << width << "x" << height
276         << "-" << bpp << "bpp): " << SDL_GetError();
277     throw std::runtime_error(msg.str());
278   }
279
280   SDL_WM_SetCaption(PACKAGE_NAME " " PACKAGE_VERSION, 0);
281
282   // set icon
283   SDL_Surface* icon = IMG_Load(
284     get_resource_filename("images/engine/icons/supertux.xpm").c_str());
285   if(icon != 0) {
286     SDL_WM_SetIcon(icon, 0);
287     SDL_FreeSurface(icon);
288   }
289 #ifdef DEBUG
290   else {
291     std::cerr << "Warning: Couldn't find icon 'images/engine/icons/supertux.xpm'.\n";
292   }
293 #endif
294
295   // setup opengl state and transform
296   glDisable(GL_DEPTH_TEST);
297   glDisable(GL_CULL_FACE);
298
299   glViewport(0, 0, screen->w, screen->h);
300   glMatrixMode(GL_PROJECTION);
301   glLoadIdentity();
302   // logical resolution here not real monitor resolution
303   glOrtho(0, 800, 600, 0, -1.0, 1.0);
304   glMatrixMode(GL_MODELVIEW);
305   glLoadIdentity();
306   glTranslatef(0, 0, 0);
307
308   check_gl_error();
309
310   Surface::reload_all();
311 }
312
313 static void init_audio()
314 {
315   sound_manager = new SoundManager();
316   
317   int format = MIX_DEFAULT_FORMAT;
318   if(Mix_OpenAudio(config->audio_frequency, format, config->audio_channels,
319                    config->audio_chunksize) < 0) {
320     std::cerr << "Couldn't initialize audio ("
321               << config->audio_frequency << "HZ, " << config->audio_channels
322               << " Channels, Format " << format << ", ChunkSize "
323               << config->audio_chunksize << "): " << SDL_GetError() << "\n";
324     return;
325   }
326   sound_manager->set_audio_device_available(true);
327   sound_manager->enable_sound(config->sound_enabled);
328   sound_manager->enable_music(config->music_enabled);
329   
330   if(Mix_AllocateChannels(config->audio_voices) < 0) {
331     std::cerr << "Couldn't allocate '" << config->audio_voices << "' audio voices: "
332               << SDL_GetError() << "\n";
333     return;
334   }
335 }
336
337 static void quit_audio()
338 {
339   if(sound_manager) {
340     if(sound_manager->audio_device_available())
341       Mix_CloseAudio();
342
343     delete sound_manager;
344     sound_manager = 0;
345   }
346 }
347
348 void wait_for_event(float min_delay, float max_delay)
349 {
350   assert(min_delay <= max_delay);
351   
352   Uint32 min = (Uint32) (min_delay * 1000);
353   Uint32 max = (Uint32) (max_delay * 1000);
354
355   SDL_Delay(min);
356
357   // clear even queue
358   SDL_Event event;
359   while (SDL_PollEvent(&event))
360   {}
361
362   /* Handle events: */
363   bool running = false;
364   Uint32 ticks = SDL_GetTicks();
365   while(running) {
366     while(SDL_PollEvent(&event)) {
367       switch(event.type) {
368         case SDL_QUIT:
369           throw std::runtime_error("received window close");
370         case SDL_KEYDOWN:
371         case SDL_JOYBUTTONDOWN:
372         case SDL_MOUSEBUTTONDOWN:
373           running = false;
374       }
375     }
376     if(SDL_GetTicks() - ticks >= (max - min))
377       running = false;
378     SDL_Delay(10);
379   }
380 }
381
382 int main(int argc, char** argv) 
383 {
384 #ifndef DEBUG // we want backtraces in debug mode so don't catch exceptions
385   try {
386 #endif
387     srand(time(0));
388     init_sdl();
389     main_controller = new JoystickKeyboardController();    
390     find_directories();
391     init_config();
392     init_tinygettext();
393     parse_commandline(argc, argv);
394     init_audio();
395     init_video();
396
397     setup_menu();
398     load_shared();
399     if(config->start_level != "") {
400       GameSession session(config->start_level, ST_GL_LOAD_LEVEL_FILE);
401       if(config->start_demo != "")
402         session.play_demo(config->start_demo);
403       if(config->record_demo != "")
404         session.record_demo(config->record_demo);
405       session.run();
406     } else {
407       // normal game
408       title();
409     }    
410     
411 #ifndef DEBUG
412   } catch(std::exception& e) {
413     std::cerr << "Unexpected exception: " << e.what() << std::endl;
414     return 1;
415   } catch(...) {
416     std::cerr << "Unexpected exception." << std::endl;
417     return 1;
418   }
419 #endif
420
421   free_menu();
422   unload_shared();
423 #ifdef DEBUG
424   Surface::debug_check();
425 #endif
426   quit_audio();
427
428   if(config)
429     config->save();
430   delete config;
431   delete main_controller;
432   SDL_Quit();
433   
434   return 0;
435 }