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
31 #include <sys/types.h>
39 #include <SDL_mixer.h>
40 #include <SDL_image.h>
41 #include <SDL_opengl.h>
43 #include "gameconfig.h"
44 #include "resources.h"
46 #include "audio/sound_manager.h"
47 #include "video/surface.h"
48 #include "control/joystickkeyboardcontroller.h"
51 #include "game_session.h"
52 #include "file_system.h"
55 #define mkdir(dir, mode) mkdir(dir)
58 SDL_Surface* screen = 0;
59 JoystickKeyboardController* main_controller = 0;
60 TinyGetText::DictionaryManager dictionary_manager;
62 static void init_config()
64 config = new Config();
67 } catch(std::exception& e) {
69 std::cerr << "Couldn't load config file: " << e.what() << "\n";
74 static void find_directories()
76 const char* home = getenv("HOME");
79 std::cerr << "Couldn't find home directory.\n";
85 user_dir += "/.supertux";
88 std::string savedir = user_dir + "/save";
89 mkdir(user_dir.c_str(), 0755);
90 mkdir(savedir.c_str(), 0755);
92 // try current directory as datadir
94 if(FileSystem::faccessible("./basest/credits.txt")) {
95 datadir = "./basest/";
99 // Detect datadir with some linux magic
101 if(datadir.empty()) {
102 char exe_file[PATH_MAX];
103 if(readlink("/proc/self/exe", exe_file, PATH_MAX) < 0) {
105 std::cerr << "Couldn't read /proc/self/exe \n";
108 std::string exedir = std::string(dirname(exe_file)) + "/";
109 std::string testdir = exedir + "./basest/";
110 if(access(testdir.c_str(), F_OK) == 0) {
114 testdir = exedir + "../share/supertux/";
115 if(datadir.empty() && access(testdir.c_str(), F_OK) == 0) {
123 // use default location
124 if(datadir.empty()) {
125 datadir = DATA_PREFIX;
130 throw std::runtime_error("Couldn't find datadir");
133 static void init_tinygettext()
135 dictionary_manager.add_directory(datadir + "/locale");
136 dictionary_manager.set_charset("iso8859-1");
139 static void print_usage(const char* argv0)
141 fprintf(stderr, _("Usage: %s [OPTIONS] LEVELFILE\n\n"), argv0);
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"
152 static void parse_commandline(int argc, char** argv)
154 for(int i = 1; i < argc; ++i) {
155 std::string arg = argv[i];
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") {
163 print_usage(argv[0]);
164 throw std::runtime_error("Need to specify a parameter for geometry switch");
166 if(sscanf(argv[++i], "%dx%d", &config->screenwidth, &config->screenheight)
168 print_usage(argv[0]);
169 throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
171 } else if(arg == "--show-fps") {
172 config->show_fps = true;
173 } else if(arg == "--play-demo") {
175 print_usage(argv[0]);
176 throw std::runtime_error("Need to specify a demo filename");
178 config->start_demo = argv[++i];
179 } else if(arg == "--record-demo") {
181 print_usage(argv[0]);
182 throw std::runtime_error("Need to specify a demo filename");
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;
194 std::cerr << "Unknown option '" << arg << "'.\n";
195 std::cerr << "Use --help to see a list of options.\n";
199 // TODO joystick switchyes...
202 static void init_sdl()
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());
210 SDL_EnableUNICODE(1);
212 // wait 100ms and clear SDL event queue because sometimes we have random
213 // joystick events in the queue on startup...
216 while(SDL_PollEvent(&dummy))
220 static void check_gl_error()
222 GLenum glerror = glGetError();
223 std::string errormsg;
225 if(glerror != GL_NO_ERROR) {
227 case GL_INVALID_ENUM:
228 errormsg = "Invalid enumeration value";
230 case GL_INVALID_VALUE:
231 errormsg = "Numeric argzment out of range";
233 case GL_INVALID_OPERATION:
234 errormsg = "Invalid operation";
236 case GL_STACK_OVERFLOW:
237 errormsg = "stack overflow";
239 case GL_STACK_UNDERFLOW:
240 errormsg = "stack underflow";
242 case GL_OUT_OF_MEMORY:
243 errormsg = "out of memory";
245 case GL_TABLE_TOO_LARGE:
246 errormsg = "table too large";
249 errormsg = "unknown error number";
252 std::stringstream msg;
253 msg << "OpenGL Error: " << errormsg;
254 throw std::runtime_error(msg.str());
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);
265 int flags = SDL_OPENGL;
266 if(config->use_fullscreen)
267 flags |= SDL_FULLSCREEN;
268 int width = config->screenwidth;
269 int height = config->screenheight;
272 screen = SDL_SetVideoMode(width, height, bpp, flags);
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());
280 SDL_WM_SetCaption(PACKAGE_NAME " " PACKAGE_VERSION, 0);
283 SDL_Surface* icon = IMG_Load(
284 get_resource_filename("images/engine/icons/supertux.xpm").c_str());
286 SDL_WM_SetIcon(icon, 0);
287 SDL_FreeSurface(icon);
291 std::cerr << "Warning: Couldn't find icon 'images/engine/icons/supertux.xpm'.\n";
295 // setup opengl state and transform
296 glDisable(GL_DEPTH_TEST);
297 glDisable(GL_CULL_FACE);
299 glViewport(0, 0, screen->w, screen->h);
300 glMatrixMode(GL_PROJECTION);
302 // logical resolution here not real monitor resolution
303 glOrtho(0, 800, 600, 0, -1.0, 1.0);
304 glMatrixMode(GL_MODELVIEW);
306 glTranslatef(0, 0, 0);
310 Surface::reload_all();
313 static void init_audio()
315 sound_manager = new SoundManager();
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";
326 sound_manager->set_audio_device_available(true);
327 sound_manager->enable_sound(config->sound_enabled);
328 sound_manager->enable_music(config->music_enabled);
330 if(Mix_AllocateChannels(config->audio_voices) < 0) {
331 std::cerr << "Couldn't allocate '" << config->audio_voices << "' audio voices: "
332 << SDL_GetError() << "\n";
337 static void quit_audio()
340 if(sound_manager->audio_device_available())
343 delete sound_manager;
348 void wait_for_event(float min_delay, float max_delay)
350 assert(min_delay <= max_delay);
352 Uint32 min = (Uint32) (min_delay * 1000);
353 Uint32 max = (Uint32) (max_delay * 1000);
359 while (SDL_PollEvent(&event))
363 bool running = false;
364 Uint32 ticks = SDL_GetTicks();
366 while(SDL_PollEvent(&event)) {
369 throw std::runtime_error("received window close");
371 case SDL_JOYBUTTONDOWN:
372 case SDL_MOUSEBUTTONDOWN:
376 if(SDL_GetTicks() - ticks >= (max - min))
382 int main(int argc, char** argv)
384 #ifndef DEBUG // we want backtraces in debug mode so don't catch exceptions
389 main_controller = new JoystickKeyboardController();
393 parse_commandline(argc, argv);
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);
412 } catch(std::exception& e) {
413 std::cerr << "Unexpected exception: " << e.what() << std::endl;
416 std::cerr << "Unexpected exception." << std::endl;
424 Surface::debug_check();
431 delete main_controller;