2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "supertux/main.hpp"
22 #include <SDL_image.h>
23 #include <boost/format.hpp>
24 #include <boost/optional.hpp>
28 #include <tinygettext/log.hpp>
30 #include <findlocale.h>
33 #include "addon/addon_manager.hpp"
34 #include "audio/sound_manager.hpp"
35 #include "control/input_manager.hpp"
36 #include "math/random_generator.hpp"
37 #include "physfs/ifile_stream.hpp"
38 #include "physfs/physfs_file_system.hpp"
39 #include "physfs/physfs_sdl.hpp"
40 #include "scripting/squirrel_util.hpp"
41 #include "scripting/scripting.hpp"
42 #include "sprite/sprite_manager.hpp"
43 #include "supertux/command_line_arguments.hpp"
44 #include "supertux/game_manager.hpp"
45 #include "supertux/gameconfig.hpp"
46 #include "supertux/globals.hpp"
47 #include "supertux/player_status.hpp"
48 #include "supertux/resources.hpp"
49 #include "supertux/screen_fade.hpp"
50 #include "supertux/screen_manager.hpp"
51 #include "supertux/title_screen.hpp"
52 #include "util/file_system.hpp"
53 #include "util/gettext.hpp"
54 #include "video/drawing_context.hpp"
55 #include "video/lightmap.hpp"
56 #include "video/renderer.hpp"
57 #include "worldmap/worldmap.hpp"
64 g_config.reset(new Config);
68 catch(const std::exception& e)
70 log_info << "Couldn't load config file: " << e.what() << ", using default settings" << std::endl;
73 // init random number stuff
74 g_config->random_seed = gameRandom.srand(g_config->random_seed);
75 graphicsRandom.srand(0);
76 //const char *how = config->random_seed? ", user fixed.": ", from time().";
77 //log_info << "Using random seed " << config->random_seed << how << std::endl;
91 Main::init_tinygettext()
93 g_dictionary_manager.reset(new tinygettext::DictionaryManager);
94 tinygettext::Log::set_log_info_callback(0);
95 g_dictionary_manager->set_filesystem(std::unique_ptr<tinygettext::FileSystem>(new PhysFSFileSystem));
97 g_dictionary_manager->add_directory("locale");
98 g_dictionary_manager->set_charset("UTF-8");
100 // Config setting "locale" overrides language detection
101 if (g_config->locale != "")
103 g_dictionary_manager->set_language(tinygettext::Language::from_name(g_config->locale));
108 FL_FindLocale(&locale);
109 tinygettext::Language language = tinygettext::Language::from_spec( locale->lang?locale->lang:"", locale->country?locale->country:"", locale->variant?locale->variant:"");
110 FL_FreeLocale(&locale);
111 g_dictionary_manager->set_language(language);
115 class PhysfsSubsystem
118 boost::optional<std::string> m_forced_datadir;
119 boost::optional<std::string> m_forced_userdir;
122 PhysfsSubsystem(const char* argv0,
123 boost::optional<std::string> forced_datadir,
124 boost::optional<std::string> forced_userdir) :
125 m_forced_datadir(forced_datadir),
126 m_forced_userdir(forced_userdir)
128 if (!PHYSFS_init(argv0))
130 std::stringstream msg;
131 msg << "Couldn't initialize physfs: " << PHYSFS_getLastError();
132 throw std::runtime_error(msg.str());
136 // allow symbolic links
137 PHYSFS_permitSymbolicLinks(1);
147 if (m_forced_datadir)
149 datadir = *m_forced_datadir;
151 else if (const char* env_datadir = getenv("SUPERTUX2_DATA_DIR"))
153 datadir = env_datadir;
157 // check if we run from source dir
158 char* basepath_c = SDL_GetBasePath();
159 std::string basepath = basepath_c;
160 SDL_free(basepath_c);
162 datadir = FileSystem::join(basepath, "data");
163 std::string testfname = FileSystem::join(datadir, "credits.txt");
164 if (!FileSystem::exists(testfname))
166 // if the game is not run from the source directory, try to find
167 // the global install location
168 datadir = datadir.substr(0, datadir.rfind(INSTALL_SUBDIR_BIN));
169 datadir = FileSystem::join(datadir, INSTALL_SUBDIR_SHARE);
173 if (!PHYSFS_addToSearchPath(datadir.c_str(), 1))
175 log_warning << "Couldn't add '" << datadir << "' to physfs searchpath: " << PHYSFS_getLastError() << std::endl;
182 if (m_forced_userdir)
184 userdir = *m_forced_userdir;
186 else if (const char* env_userdir = getenv("SUPERTUX2_USER_DIR"))
188 userdir = env_userdir;
192 std::string physfs_userdir = PHYSFS_getUserDir();
194 userdir = FileSystem::join(physfs_userdir, PACKAGE_NAME);
196 userdir = FileSystem::join(physfs_userdir, "." PACKAGE_NAME);
200 if (!FileSystem::is_directory(userdir))
202 FileSystem::mkdir(userdir);
203 log_info << "Created SuperTux userdir: " << userdir << std::endl;
206 if (!PHYSFS_setWriteDir(userdir.c_str()))
208 std::ostringstream msg;
209 msg << "Failed to use userdir directory '"
210 << userdir << "': " << PHYSFS_getLastError();
211 throw std::runtime_error(msg.str());
214 PHYSFS_addToSearchPath(userdir.c_str(), 0);
217 void print_search_path()
219 const char* writedir = PHYSFS_getWriteDir();
220 log_info << "PhysfsWritedDir: " << (writedir ? writedir : "(null)") << std::endl;
221 log_info << "PhysfsSearchPath:" << std::endl;
222 char** searchpath = PHYSFS_getSearchPath();
223 for(char** i = searchpath; *i != NULL; ++i)
225 log_info << " " << *i << std::endl;
227 PHYSFS_freeList(searchpath);
241 if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0)
243 std::stringstream msg;
244 msg << "Couldn't initialize SDL: " << SDL_GetError();
245 throw std::runtime_error(msg.str());
260 SDL_SetWindowTitle(VideoSystem::current()->get_renderer().get_window(), PACKAGE_NAME " " PACKAGE_VERSION);
262 const char* icon_fname = "images/engine/icons/supertux-256x256.png";
263 SDL_Surface* icon = IMG_Load_RW(get_physfs_SDLRWops(icon_fname), true);
266 log_warning << "Couldn't load icon '" << icon_fname << "': " << SDL_GetError() << std::endl;
270 SDL_SetWindowIcon(VideoSystem::current()->get_renderer().get_window(), icon);
271 SDL_FreeSurface(icon);
275 log_info << (g_config->use_fullscreen?"fullscreen ":"window ")
276 << " Window: " << g_config->window_size
277 << " Fullscreen: " << g_config->fullscreen_size << "@" << g_config->fullscreen_refresh_rate
278 << " Area: " << g_config->aspect_size << std::endl;
281 static Uint32 last_timelog_ticks = 0;
282 static const char* last_timelog_component = 0;
284 static inline void timelog(const char* component)
286 Uint32 current_ticks = SDL_GetTicks();
288 if(last_timelog_component != 0) {
289 log_info << "Component '" << last_timelog_component << "' finished after " << (current_ticks - last_timelog_ticks) / 1000.0 << " seconds" << std::endl;
292 last_timelog_ticks = current_ticks;
293 last_timelog_component = component;
299 SDLSubsystem sdl_subsystem;
300 ConsoleBuffer console_buffer;
302 timelog("controller");
303 InputManager input_manager(g_config->keyboard_config, g_config->joystick_config);
305 timelog("commandline");
308 std::unique_ptr<VideoSystem> video_system = VideoSystem::create(g_config->video);
309 DrawingContext context(*video_system);
313 SoundManager sound_manager;
314 sound_manager.enable_sound(g_config->sound_enabled);
315 sound_manager.enable_music(g_config->music_enabled);
317 Console console(console_buffer);
319 timelog("scripting");
320 scripting::Scripting scripting(g_config->enable_script_debugger);
322 timelog("resources");
323 TileManager tile_manager;
324 SpriteManager sprite_manager;
328 AddonManager addon_manager("addons", g_config->addons);
332 const std::unique_ptr<Savegame> default_savegame(new Savegame(std::string()));
334 GameManager game_manager;
335 ScreenManager screen_manager;
337 if(g_config->start_level != "") {
338 // we have a normal path specified at commandline, not a physfs path.
339 // So we simply mount that path here...
340 std::string dir = FileSystem::dirname(g_config->start_level);
341 std::string fileProtocol = "file://";
342 std::string::size_type position = dir.find(fileProtocol);
343 if(position != std::string::npos) {
344 dir = dir.replace(position, fileProtocol.length(), "");
346 log_debug << "Adding dir: " << dir << std::endl;
347 PHYSFS_addToSearchPath(dir.c_str(), true);
349 if(g_config->start_level.size() > 4 &&
350 g_config->start_level.compare(g_config->start_level.size() - 5, 5, ".stwm") == 0)
352 screen_manager.push_screen(std::unique_ptr<Screen>(
353 new worldmap::WorldMap(
354 FileSystem::basename(g_config->start_level), *default_savegame)));
356 std::unique_ptr<GameSession> session (
357 new GameSession(FileSystem::basename(g_config->start_level), *default_savegame));
359 g_config->random_seed = session->get_demo_random_seed(g_config->start_demo);
360 g_config->random_seed = gameRandom.srand(g_config->random_seed);
361 graphicsRandom.srand(0);
363 if(g_config->start_demo != "")
364 session->play_demo(g_config->start_demo);
366 if(g_config->record_demo != "")
367 session->record_demo(g_config->record_demo);
368 screen_manager.push_screen(std::move(session));
371 screen_manager.push_screen(std::unique_ptr<Screen>(new TitleScreen(*default_savegame)));
374 screen_manager.run(context);
378 Main::run(int argc, char** argv)
384 CommandLineArguments args;
388 args.parse_args(argc, argv);
389 g_log_level = args.get_log_level();
391 catch(const std::exception& err)
393 std::cout << "Error: " << err.what() << std::endl;
397 PhysfsSubsystem physfs_subsystem(argv[0], args.datadir, args.userdir);
398 physfs_subsystem.print_search_path();
401 ConfigSubsystem config_subsystem;
402 args.merge_into(*g_config);
404 timelog("tinygettext");
407 switch (args.get_action())
409 case CommandLineArguments::PRINT_VERSION:
410 args.print_version();
413 case CommandLineArguments::PRINT_HELP:
414 args.print_help(argv[0]);
417 case CommandLineArguments::PRINT_DATADIR:
418 args.print_datadir();
426 catch(const std::exception& e)
428 log_fatal << "Unexpected exception: " << e.what() << std::endl;
433 log_fatal << "Unexpected exception" << std::endl;
437 g_dictionary_manager.reset();