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>
41 #include "gameconfig.h"
42 #include "resources.h"
43 #include "app/globals.h"
44 #include "app/setup.h"
45 #include "app/gettext.h"
46 #include "audio/sound_manager.h"
47 #include "control/joystickkeyboardcontroller.h"
49 #include "game_session.h"
51 SDL_Surface* screen = 0;
52 JoystickKeyboardController* main_controller = 0;
54 static void init_config()
56 config = new Config();
59 } catch(std::exception& e) {
61 std::cerr << "Couldn't load config file: " << e.what() << "\n";
66 static void find_directories()
68 const char* home = getenv("HOME");
71 std::cerr << "Couldn't find home directory.\n";
77 user_dir += "/.supertux";
79 // Remove .supertux config file from old versions
80 if(FileSystem::faccessible(user_dir)) {
81 std::cerr << "Removing old config file " << user_dir << "\n";
82 remove(user_dir.c_str());
86 std::string savedir = user_dir + "/save";
87 mkdir(user_dir.c_str(), 0755);
88 mkdir(savedir.c_str(), 0755);
90 // try current directory as datadir
92 if(FileSystem::faccessible("./data/credits.txt")) {
97 // Detect datadir with some linux magic
100 char exe_file[PATH_MAX];
101 if(readlink("/proc/self/exe", exe_file, PATH_MAX) < 0) {
103 std::cerr << "Couldn't read /proc/self/exe \n";
106 std::string exedir = std::string(dirname(exe_file)) + "/";
107 std::string testdir = exedir + "./data/";
108 if(access(testdir.c_str(), F_OK) == 0) {
112 testdir = exedir + "../share/supertux/";
113 if(datadir.empty() && access(testdir.c_str(), F_OK) == 0) {
121 // use default location
122 if(datadir.empty()) {
123 datadir = DATA_PREFIX;
128 throw std::runtime_error("Couldn't find datadir");
131 static void init_tinygettext()
133 dictionary_manager.add_directory(datadir + "/locale");
134 dictionary_manager.set_charset("iso8859-1");
137 static void print_usage(const char* argv0)
139 fprintf(stderr, _("Usage: %s [OPTIONS] LEVELFILE\n\n"), argv0);
142 " -f, --fullscreen Run in fullscreen mode.\n"
143 " -w, --window Run in window mode.\n"
144 " -g, --geometry WIDTHxHEIGHT Run SuperTux in give resolution\n"
145 " --help Show this help message\n"
146 " --version Display SuperTux version and quit\n"
150 static void parse_commandline(int argc, char** argv)
152 for(int i = 1; i < argc; ++i) {
153 std::string arg = argv[i];
155 if(arg == "--fullscreen" || arg == "-f") {
156 config->use_fullscreen = true;
157 } else if(arg == "--window" || arg == "-w") {
158 config->use_fullscreen = false;
159 } else if(arg == "--geometry" || arg == "-g") {
161 print_usage(argv[0]);
162 throw std::runtime_error("Need to specify a parameter for geometry switch");
164 if(sscanf(argv[++i], "%dx%d", &config->screenwidth, &config->screenheight)
166 print_usage(argv[0]);
167 throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
169 } else if(arg == "--show-fps") {
170 config->show_fps = true;
171 } else if(arg == "--help") {
172 print_usage(argv[0]);
173 throw std::runtime_error("");
174 } else if(arg == "--version") {
175 std::cerr << PACKAGE_NAME << " " << PACKAGE_VERSION << "\n";
176 throw std::runtime_error("");
177 } else if(arg[0] != '-') {
178 config->start_level = arg;
180 std::cerr << "Unknown option '" << arg << "'.\n";
181 std::cerr << "Use --help to see a list of options.\n";
185 // TODO joystick switchyes...
188 static void init_sdl()
190 if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
191 std::stringstream msg;
192 msg << "Couldn't initialize SDL: " << SDL_GetError();
193 throw std::runtime_error(msg.str());
196 SDL_EnableUNICODE(1);
199 static void check_gl_error()
201 GLenum glerror = glGetError();
202 std::string errormsg;
204 if(glerror != GL_NO_ERROR) {
206 case GL_INVALID_ENUM:
207 errormsg = "Invalid enumeration value";
209 case GL_INVALID_VALUE:
210 errormsg = "Numeric argzment out of range";
212 case GL_INVALID_OPERATION:
213 errormsg = "Invalid operation";
215 case GL_STACK_OVERFLOW:
216 errormsg = "stack overflow";
218 case GL_STACK_UNDERFLOW:
219 errormsg = "stack underflow";
221 case GL_OUT_OF_MEMORY:
222 errormsg = "out of memory";
224 case GL_TABLE_TOO_LARGE:
225 errormsg = "table too large";
228 errormsg = "unknown error number";
231 std::stringstream msg;
232 msg << "OpenGL Error: " << errormsg;
233 throw std::runtime_error(msg.str());
239 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
240 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
241 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
242 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
244 int flags = SDL_OPENGL;
245 if(config->use_fullscreen)
246 flags |= SDL_FULLSCREEN;
247 int width = config->screenwidth;
248 int height = config->screenheight;
251 screen = SDL_SetVideoMode(width, height, bpp, flags);
253 std::stringstream msg;
254 msg << "Couldn't set video mode (" << width << "x" << height
255 << "-" << bpp << "bpp): " << SDL_GetError();
256 throw std::runtime_error(msg.str());
259 SDL_WM_SetCaption(PACKAGE_NAME " " PACKAGE_VERSION, 0);
262 SDL_Surface* icon = IMG_Load(
263 get_resource_filename("images/supertux.xpm").c_str());
265 SDL_WM_SetIcon(icon, 0);
266 SDL_FreeSurface(icon);
270 std::cerr << "Warning: Couldn't find icon 'images/supertux.xpm'.\n";
274 // setup opengl state and transform
275 glDisable(GL_DEPTH_TEST);
276 glDisable(GL_CULL_FACE);
278 glViewport(0, 0, screen->w, screen->h);
279 glMatrixMode(GL_PROJECTION);
281 // logical resolution here not real monitor resolution
282 glOrtho(0, 800, 600, 0, -1.0, 1.0);
283 glMatrixMode(GL_MODELVIEW);
285 glTranslatef(0, 0, 0);
289 Surface::reload_all();
292 static void init_audio()
294 sound_manager = new SoundManager();
296 int format = MIX_DEFAULT_FORMAT;
297 if(Mix_OpenAudio(config->audio_frequency, format, config->audio_channels,
298 config->audio_chunksize) < 0) {
299 std::cerr << "Couldn't initialize audio ("
300 << config->audio_frequency << "HZ, " << config->audio_channels
301 << " Channels, Format " << format << ", ChunkSize "
302 << config->audio_chunksize << "): " << SDL_GetError() << "\n";
305 sound_manager->set_audio_device_available(true);
306 sound_manager->enable_sound(config->sound_enabled);
307 sound_manager->enable_music(config->music_enabled);
309 if(Mix_AllocateChannels(config->audio_voices) < 0) {
310 std::cerr << "Couldn't allocate '" << config->audio_voices << "' audio voices: "
311 << SDL_GetError() << "\n";
316 static void quit_audio()
319 if(sound_manager->audio_device_available())
322 delete sound_manager;
327 int main(int argc, char** argv)
329 #ifndef DEBUG // we want backtraces in debug mode so don't catch exceptions
334 main_controller = new JoystickKeyboardController();
338 parse_commandline(argc, argv);
344 if(config->start_level != "") {
345 GameSession session(config->start_level, ST_GL_LOAD_LEVEL_FILE);
353 } catch(std::exception& e) {
354 std::cerr << "Unexpected exception: " << e.what() << std::endl;
357 std::cerr << "Unexpected exception." << std::endl;
365 Surface::debug_check();
372 delete main_controller;