4 // Copyright (C) 2005 Matthias Braun <matze@braunis.de>
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.
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.
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
30 #include <sys/types.h>
37 #include <SDL_mixer.h>
38 #include <SDL_image.h>
39 #include <SDL_opengl.h>
41 #include "gameconfig.h"
42 #include "resources.h"
44 #include "audio/sound_manager.h"
45 #include "video/surface.h"
46 #include "control/joystickkeyboardcontroller.h"
49 #include "game_session.h"
50 #include "file_system.h"
52 SDL_Surface* screen = 0;
53 JoystickKeyboardController* main_controller = 0;
54 TinyGetText::DictionaryManager dictionary_manager;
56 static void init_config()
58 config = new Config();
61 } catch(std::exception& e) {
63 std::cerr << "Couldn't load config file: " << e.what() << "\n";
68 static void find_directories()
70 const char* home = getenv("HOME");
73 std::cerr << "Couldn't find home directory.\n";
79 user_dir += "/.supertux";
82 std::string savedir = user_dir + "/save";
83 mkdir(user_dir.c_str(), 0755);
84 mkdir(savedir.c_str(), 0755);
86 // try current directory as datadir
88 if(FileSystem::faccessible("./data/credits.txt")) {
93 // Detect datadir with some linux magic
96 char exe_file[PATH_MAX];
97 if(readlink("/proc/self/exe", exe_file, PATH_MAX) < 0) {
99 std::cerr << "Couldn't read /proc/self/exe \n";
102 std::string exedir = std::string(dirname(exe_file)) + "/";
103 std::string testdir = exedir + "./data/";
104 if(access(testdir.c_str(), F_OK) == 0) {
108 testdir = exedir + "../share/supertux/";
109 if(datadir.empty() && access(testdir.c_str(), F_OK) == 0) {
117 // use default location
118 if(datadir.empty()) {
119 datadir = DATA_PREFIX;
124 throw std::runtime_error("Couldn't find datadir");
127 static void init_tinygettext()
129 dictionary_manager.add_directory(datadir + "/locale");
130 dictionary_manager.set_charset("iso8859-1");
133 static void print_usage(const char* argv0)
135 fprintf(stderr, _("Usage: %s [OPTIONS] LEVELFILE\n\n"), argv0);
138 " -f, --fullscreen Run in fullscreen mode.\n"
139 " -w, --window Run in window mode.\n"
140 " -g, --geometry WIDTHxHEIGHT Run SuperTux in give resolution\n"
141 " --help Show this help message\n"
142 " --version Display SuperTux version and quit\n"
146 static void parse_commandline(int argc, char** argv)
148 for(int i = 1; i < argc; ++i) {
149 std::string arg = argv[i];
151 if(arg == "--fullscreen" || arg == "-f") {
152 config->use_fullscreen = true;
153 } else if(arg == "--window" || arg == "-w") {
154 config->use_fullscreen = false;
155 } else if(arg == "--geometry" || arg == "-g") {
157 print_usage(argv[0]);
158 throw std::runtime_error("Need to specify a parameter for geometry switch");
160 if(sscanf(argv[++i], "%dx%d", &config->screenwidth, &config->screenheight)
162 print_usage(argv[0]);
163 throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
165 } else if(arg == "--show-fps") {
166 config->show_fps = true;
167 } else if(arg == "--play-demo") {
169 print_usage(argv[0]);
170 throw std::runtime_error("Need to specify a demo filename");
172 config->start_demo = argv[++i];
173 } else if(arg == "--record-demo") {
175 print_usage(argv[0]);
176 throw std::runtime_error("Need to specify a demo filename");
178 config->record_demo = argv[++i];
179 } else if(arg == "--help") {
180 print_usage(argv[0]);
181 throw std::runtime_error("");
182 } else if(arg == "--version") {
183 std::cerr << PACKAGE_NAME << " " << PACKAGE_VERSION << "\n";
184 throw std::runtime_error("");
185 } else if(arg[0] != '-') {
186 config->start_level = arg;
188 std::cerr << "Unknown option '" << arg << "'.\n";
189 std::cerr << "Use --help to see a list of options.\n";
193 // TODO joystick switchyes...
196 static void init_sdl()
198 if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
199 std::stringstream msg;
200 msg << "Couldn't initialize SDL: " << SDL_GetError();
201 throw std::runtime_error(msg.str());
204 SDL_EnableUNICODE(1);
206 // wait 100ms and clear SDL event queue because sometimes we have random
207 // joystick events in the queue on startup...
210 while(SDL_PollEvent(&dummy))
214 static void check_gl_error()
216 GLenum glerror = glGetError();
217 std::string errormsg;
219 if(glerror != GL_NO_ERROR) {
221 case GL_INVALID_ENUM:
222 errormsg = "Invalid enumeration value";
224 case GL_INVALID_VALUE:
225 errormsg = "Numeric argzment out of range";
227 case GL_INVALID_OPERATION:
228 errormsg = "Invalid operation";
230 case GL_STACK_OVERFLOW:
231 errormsg = "stack overflow";
233 case GL_STACK_UNDERFLOW:
234 errormsg = "stack underflow";
236 case GL_OUT_OF_MEMORY:
237 errormsg = "out of memory";
239 case GL_TABLE_TOO_LARGE:
240 errormsg = "table too large";
243 errormsg = "unknown error number";
246 std::stringstream msg;
247 msg << "OpenGL Error: " << errormsg;
248 throw std::runtime_error(msg.str());
254 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
255 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
256 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
257 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
259 int flags = SDL_OPENGL;
260 if(config->use_fullscreen)
261 flags |= SDL_FULLSCREEN;
262 int width = config->screenwidth;
263 int height = config->screenheight;
266 screen = SDL_SetVideoMode(width, height, bpp, flags);
268 std::stringstream msg;
269 msg << "Couldn't set video mode (" << width << "x" << height
270 << "-" << bpp << "bpp): " << SDL_GetError();
271 throw std::runtime_error(msg.str());
274 SDL_WM_SetCaption(PACKAGE_NAME " " PACKAGE_VERSION, 0);
277 SDL_Surface* icon = IMG_Load(
278 get_resource_filename("images/supertux.xpm").c_str());
280 SDL_WM_SetIcon(icon, 0);
281 SDL_FreeSurface(icon);
285 std::cerr << "Warning: Couldn't find icon 'images/supertux.xpm'.\n";
289 // setup opengl state and transform
290 glDisable(GL_DEPTH_TEST);
291 glDisable(GL_CULL_FACE);
293 glViewport(0, 0, screen->w, screen->h);
294 glMatrixMode(GL_PROJECTION);
296 // logical resolution here not real monitor resolution
297 glOrtho(0, 800, 600, 0, -1.0, 1.0);
298 glMatrixMode(GL_MODELVIEW);
300 glTranslatef(0, 0, 0);
304 Surface::reload_all();
307 static void init_audio()
309 sound_manager = new SoundManager();
311 int format = MIX_DEFAULT_FORMAT;
312 if(Mix_OpenAudio(config->audio_frequency, format, config->audio_channels,
313 config->audio_chunksize) < 0) {
314 std::cerr << "Couldn't initialize audio ("
315 << config->audio_frequency << "HZ, " << config->audio_channels
316 << " Channels, Format " << format << ", ChunkSize "
317 << config->audio_chunksize << "): " << SDL_GetError() << "\n";
320 sound_manager->set_audio_device_available(true);
321 sound_manager->enable_sound(config->sound_enabled);
322 sound_manager->enable_music(config->music_enabled);
324 if(Mix_AllocateChannels(config->audio_voices) < 0) {
325 std::cerr << "Couldn't allocate '" << config->audio_voices << "' audio voices: "
326 << SDL_GetError() << "\n";
331 static void quit_audio()
334 if(sound_manager->audio_device_available())
337 delete sound_manager;
342 void wait_for_event(float min_delay, float max_delay)
344 assert(min_delay <= max_delay);
346 Uint32 min = (Uint32) (min_delay * 1000);
347 Uint32 max = (Uint32) (max_delay * 1000);
353 while (SDL_PollEvent(&event))
357 bool running = false;
358 Uint32 ticks = SDL_GetTicks();
360 while(SDL_PollEvent(&event)) {
363 throw std::runtime_error("received window close");
365 case SDL_JOYBUTTONDOWN:
366 case SDL_MOUSEBUTTONDOWN:
370 if(SDL_GetTicks() - ticks >= (max - min))
376 int main(int argc, char** argv)
378 #ifndef DEBUG // we want backtraces in debug mode so don't catch exceptions
383 main_controller = new JoystickKeyboardController();
387 parse_commandline(argc, argv);
393 if(config->start_level != "") {
394 GameSession session(config->start_level, ST_GL_LOAD_LEVEL_FILE);
395 if(config->start_demo != "")
396 session.play_demo(config->start_demo);
397 if(config->record_demo != "")
398 session.record_demo(config->record_demo);
406 } catch(std::exception& e) {
407 std::cerr << "Unexpected exception: " << e.what() << std::endl;
410 std::cerr << "Unexpected exception." << std::endl;
418 Surface::debug_check();
425 delete main_controller;