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>
38 #include <SDL_mixer.h>
39 #include <SDL_image.h>
40 #include <SDL_opengl.h>
42 #include "gameconfig.h"
43 #include "resources.h"
45 #include "audio/sound_manager.h"
46 #include "video/surface.h"
47 #include "control/joystickkeyboardcontroller.h"
50 #include "game_session.h"
51 #include "file_system.h"
53 SDL_Surface* screen = 0;
54 JoystickKeyboardController* main_controller = 0;
55 TinyGetText::DictionaryManager dictionary_manager;
57 static void init_config()
59 config = new Config();
62 } catch(std::exception& e) {
64 std::cerr << "Couldn't load config file: " << e.what() << "\n";
69 static void find_directories()
71 const char* home = getenv("HOME");
74 std::cerr << "Couldn't find home directory.\n";
80 user_dir += "/.supertux";
83 std::string savedir = user_dir + "/save";
84 mkdir(user_dir.c_str(), 0755);
85 mkdir(savedir.c_str(), 0755);
87 // try current directory as datadir
89 if(FileSystem::faccessible("./data/credits.txt")) {
94 // Detect datadir with some linux magic
97 char exe_file[PATH_MAX];
98 if(readlink("/proc/self/exe", exe_file, PATH_MAX) < 0) {
100 std::cerr << "Couldn't read /proc/self/exe \n";
103 std::string exedir = std::string(dirname(exe_file)) + "/";
104 std::string testdir = exedir + "./data/";
105 if(access(testdir.c_str(), F_OK) == 0) {
109 testdir = exedir + "../share/supertux/";
110 if(datadir.empty() && access(testdir.c_str(), F_OK) == 0) {
118 // use default location
119 if(datadir.empty()) {
120 datadir = DATA_PREFIX;
125 throw std::runtime_error("Couldn't find datadir");
128 static void init_tinygettext()
130 dictionary_manager.add_directory(datadir + "/locale");
131 dictionary_manager.set_charset("iso8859-1");
134 static void print_usage(const char* argv0)
136 fprintf(stderr, _("Usage: %s [OPTIONS] LEVELFILE\n\n"), argv0);
139 " -f, --fullscreen Run in fullscreen mode.\n"
140 " -w, --window Run in window mode.\n"
141 " -g, --geometry WIDTHxHEIGHT Run SuperTux in give resolution\n"
142 " --help Show this help message\n"
143 " --version Display SuperTux version and quit\n"
147 static void parse_commandline(int argc, char** argv)
149 for(int i = 1; i < argc; ++i) {
150 std::string arg = argv[i];
152 if(arg == "--fullscreen" || arg == "-f") {
153 config->use_fullscreen = true;
154 } else if(arg == "--window" || arg == "-w") {
155 config->use_fullscreen = false;
156 } else if(arg == "--geometry" || arg == "-g") {
158 print_usage(argv[0]);
159 throw std::runtime_error("Need to specify a parameter for geometry switch");
161 if(sscanf(argv[++i], "%dx%d", &config->screenwidth, &config->screenheight)
163 print_usage(argv[0]);
164 throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
166 } else if(arg == "--show-fps") {
167 config->show_fps = true;
168 } else if(arg == "--play-demo") {
170 print_usage(argv[0]);
171 throw std::runtime_error("Need to specify a demo filename");
173 config->start_demo = argv[++i];
174 } else if(arg == "--record-demo") {
176 print_usage(argv[0]);
177 throw std::runtime_error("Need to specify a demo filename");
179 config->record_demo = argv[++i];
180 } else if(arg == "--help") {
181 print_usage(argv[0]);
182 throw std::runtime_error("");
183 } else if(arg == "--version") {
184 std::cerr << PACKAGE_NAME << " " << PACKAGE_VERSION << "\n";
185 throw std::runtime_error("");
186 } else if(arg[0] != '-') {
187 config->start_level = arg;
189 std::cerr << "Unknown option '" << arg << "'.\n";
190 std::cerr << "Use --help to see a list of options.\n";
194 // TODO joystick switchyes...
197 static void init_sdl()
199 if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
200 std::stringstream msg;
201 msg << "Couldn't initialize SDL: " << SDL_GetError();
202 throw std::runtime_error(msg.str());
205 SDL_EnableUNICODE(1);
207 // wait 100ms and clear SDL event queue because sometimes we have random
208 // joystick events in the queue on startup...
211 while(SDL_PollEvent(&dummy))
215 static void check_gl_error()
217 GLenum glerror = glGetError();
218 std::string errormsg;
220 if(glerror != GL_NO_ERROR) {
222 case GL_INVALID_ENUM:
223 errormsg = "Invalid enumeration value";
225 case GL_INVALID_VALUE:
226 errormsg = "Numeric argzment out of range";
228 case GL_INVALID_OPERATION:
229 errormsg = "Invalid operation";
231 case GL_STACK_OVERFLOW:
232 errormsg = "stack overflow";
234 case GL_STACK_UNDERFLOW:
235 errormsg = "stack underflow";
237 case GL_OUT_OF_MEMORY:
238 errormsg = "out of memory";
240 case GL_TABLE_TOO_LARGE:
241 errormsg = "table too large";
244 errormsg = "unknown error number";
247 std::stringstream msg;
248 msg << "OpenGL Error: " << errormsg;
249 throw std::runtime_error(msg.str());
255 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
256 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
257 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
258 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
260 int flags = SDL_OPENGL;
261 if(config->use_fullscreen)
262 flags |= SDL_FULLSCREEN;
263 int width = config->screenwidth;
264 int height = config->screenheight;
267 screen = SDL_SetVideoMode(width, height, bpp, flags);
269 std::stringstream msg;
270 msg << "Couldn't set video mode (" << width << "x" << height
271 << "-" << bpp << "bpp): " << SDL_GetError();
272 throw std::runtime_error(msg.str());
275 SDL_WM_SetCaption(PACKAGE_NAME " " PACKAGE_VERSION, 0);
278 SDL_Surface* icon = IMG_Load(
279 get_resource_filename("images/supertux.xpm").c_str());
281 SDL_WM_SetIcon(icon, 0);
282 SDL_FreeSurface(icon);
286 std::cerr << "Warning: Couldn't find icon 'images/supertux.xpm'.\n";
290 // setup opengl state and transform
291 glDisable(GL_DEPTH_TEST);
292 glDisable(GL_CULL_FACE);
294 glViewport(0, 0, screen->w, screen->h);
295 glMatrixMode(GL_PROJECTION);
297 // logical resolution here not real monitor resolution
298 glOrtho(0, 800, 600, 0, -1.0, 1.0);
299 glMatrixMode(GL_MODELVIEW);
301 glTranslatef(0, 0, 0);
305 Surface::reload_all();
308 static void init_audio()
310 sound_manager = new SoundManager();
312 int format = MIX_DEFAULT_FORMAT;
313 if(Mix_OpenAudio(config->audio_frequency, format, config->audio_channels,
314 config->audio_chunksize) < 0) {
315 std::cerr << "Couldn't initialize audio ("
316 << config->audio_frequency << "HZ, " << config->audio_channels
317 << " Channels, Format " << format << ", ChunkSize "
318 << config->audio_chunksize << "): " << SDL_GetError() << "\n";
321 sound_manager->set_audio_device_available(true);
322 sound_manager->enable_sound(config->sound_enabled);
323 sound_manager->enable_music(config->music_enabled);
325 if(Mix_AllocateChannels(config->audio_voices) < 0) {
326 std::cerr << "Couldn't allocate '" << config->audio_voices << "' audio voices: "
327 << SDL_GetError() << "\n";
332 static void quit_audio()
335 if(sound_manager->audio_device_available())
338 delete sound_manager;
343 void wait_for_event(float min_delay, float max_delay)
345 assert(min_delay <= max_delay);
347 Uint32 min = (Uint32) (min_delay * 1000);
348 Uint32 max = (Uint32) (max_delay * 1000);
354 while (SDL_PollEvent(&event))
358 bool running = false;
359 Uint32 ticks = SDL_GetTicks();
361 while(SDL_PollEvent(&event)) {
364 throw std::runtime_error("received window close");
366 case SDL_JOYBUTTONDOWN:
367 case SDL_MOUSEBUTTONDOWN:
371 if(SDL_GetTicks() - ticks >= (max - min))
377 int main(int argc, char** argv)
379 #ifndef DEBUG // we want backtraces in debug mode so don't catch exceptions
384 main_controller = new JoystickKeyboardController();
388 parse_commandline(argc, argv);
394 if(config->start_level != "") {
395 GameSession session(config->start_level, ST_GL_LOAD_LEVEL_FILE);
396 if(config->start_demo != "")
397 session.play_demo(config->start_demo);
398 if(config->record_demo != "")
399 session.record_demo(config->record_demo);
407 } catch(std::exception& e) {
408 std::cerr << "Unexpected exception: " << e.what() << std::endl;
411 std::cerr << "Unexpected exception." << std::endl;
419 Surface::debug_check();
426 delete main_controller;