editor_main.cpp

Go to the documentation of this file.
00001 /* $Id: editor_main.cpp 26095 2008-04-25 20:39:59Z shadowmaster $ */
00002 /*
00003   Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
00004   Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License version 2
00008   or at your option any later version.
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY.
00011 
00012   See the COPYING file for more details.
00013 */
00014 
00015 //! @file editor/editor_main.cpp
00016 //!
00017 
00018 #include "editor.hpp"
00019 #include "../config.hpp"
00020 #include "../cursor.hpp"
00021 #include "../filesystem.hpp"
00022 #include "../font.hpp"
00023 #include "../game_config.hpp"
00024 #include "../gettext.hpp"
00025 #include "../image.hpp"
00026 #include "../language.hpp"
00027 #include "../log.hpp"
00028 #include "../map.hpp"
00029 #include "../minimap.hpp"
00030 #include "../preferences.hpp"
00031 #include "../random.hpp"
00032 #include "../util.hpp"
00033 #include "../video.hpp"
00034 #include "../wesconfig.h"
00035 #include "../wml_exception.hpp"
00036 #include "serialization/parser.hpp"
00037 #include "serialization/preprocessor.hpp"
00038 #include "serialization/string_utils.hpp"
00039 
00040 #include <cctype>
00041 #include <clocale>
00042 #include <cstdlib>
00043 #include <iostream>
00044 #include <map>
00045 #include <string>
00046 
00047 namespace {
00048     static std::string wm_title_string;
00049 }
00050 
00051 int main(int argc, char** argv)
00052 {
00053     const std::string rev = game_config::revision.empty() ? "" :
00054         " (" + game_config::revision + ")";
00055 
00056     std::cerr << "Battle for Wesnoth Map Editor v" << VERSION << rev << '\n';
00057     time_t t = time(NULL);
00058     std::cerr << "Started on " << ctime(&t) << "\n";
00059 
00060     game_config::editor = true;
00061     rng generator;
00062     const set_random_generator generator_setter(&generator);
00063 
00064     int arg;
00065     for(arg = 1; arg != argc; ++arg) {
00066         const std::string val(argv[arg]);
00067         if(val.empty()) {
00068             continue;
00069         }
00070 
00071         if(val == "--help" || val == "-h") {
00072             std::cout << "usage: " << argv[0]
00073             << " [options] [map]\n"
00074                     << "  --bpp                     Set the bits per pixel.\n"
00075                     << "  --datadir                 Select the data directory to use.\n"
00076                     << "  -f, --fullscreen          Runs the editor in full screen mode.\n"
00077                     << "  -h, --help                Prints this message and exits.\n"
00078                     << "  --log-<level>=<domain1>,<domain2>,...\n"
00079                     << "                            Sets the severity level of the log domains.\n"
00080                     << "                            'all' can be used to match any log domain.\n"
00081                     << "                            Available levels: error, warning, info, debug.\n"
00082                     << "                            By default the 'error' level is used.\n"
00083                     << "  --logdomains              List defined log domains and exit.\n"
00084                     << "  --path                    Prints the name of the game data directory and exits.\n"
00085             << "  -r, --resolution XxY      Sets the screen resolution. Example: -r 800x600.\n"
00086                     << "  -v, --version             Prints the game's version number and exits.\n"
00087                     << "  -w, --windowed            Runs the editor in windowed mode.\n"
00088             ;
00089             return 0;
00090         } else if(val == "--version" || val == "-v") {
00091             std::cout << "Battle for Wesnoth "
00092                   << game_config::version
00093 #if defined(SVNREV) && defined(DO_DISPLAY_REVISION)
00094                   << " (" << game_config::revision << ")"
00095 #endif /* defined(SVNREV) and defined(DO_DISPLAY_REVISION) */
00096                       << "\n";
00097             return 0;
00098         } else if(val == "--path") {
00099             std::cout <<  game_config::path
00100                       << "\n";
00101             return 0;
00102         } else if(val == "--logdomains") {
00103           std::cout << lg::list_logdomains() << "\n";
00104             return 0;
00105         }
00106     }
00107 
00108     CVideo video;
00109 
00110     const font::manager font_manager;
00111     const preferences::base_manager prefs_manager;
00112     const image::manager image_manager;
00113     resize_monitor resize_monitor_;
00114     binary_paths_manager paths_manager;
00115     std::string filename = "";
00116     std::string mapdata;
00117     bool from_scenario;
00118 
00119     int choosen_bpp = 0;
00120 
00121     for(arg = 1; arg != argc; ++arg) {
00122         const std::string val(argv[arg]);
00123         if(val.empty()) {
00124             continue;
00125         }
00126         if(val == "--bpp" && arg + 1 != argc) {
00127             ++arg;
00128             choosen_bpp = lexical_cast_default<int>(argv[arg]);
00129         }
00130 
00131         else if(val == "--resolution" || val == "-r") {
00132             if(arg+1 != argc) {
00133                 ++arg;
00134                 const std::string val(argv[arg]);
00135                 const std::vector<std::string> res = utils::split(val, 'x');
00136                 if(res.size() == 2) {
00137                     const int xres = lexical_cast_default<int>(res.front());
00138                     const int yres = lexical_cast_default<int>(res.back());
00139                     if(xres > 0 && yres > 0) {
00140                         const std::pair<int,int> resolution(xres,yres);
00141                         preferences::set_resolution(resolution);
00142                     }
00143                 }
00144             }
00145         } else if(val == "--windowed" || val == "-w") {
00146             preferences::set_fullscreen(false);
00147         } else if(val == "--fullscreen" || val == "-f") {
00148             preferences::set_fullscreen(true);
00149         } else if(val == "--datadir") {
00150             if (arg+1 != argc) {
00151                 const std::string val(argv[++arg]);
00152 
00153                 const std::string cwd = get_cwd();
00154 
00155                 if(val[0] == '/') {
00156                     game_config::path = val;
00157                 } else if(cwd != "") {
00158                     game_config::path = cwd + '/' + val;
00159                 } else {
00160                     std::cerr << "Could not get working directory\n";
00161                     return 0;
00162                 }
00163 
00164                 if(!is_directory(game_config::path)) {
00165                     std::cerr << "Could not find directory '" << game_config::path << "'\n";
00166                     return 0;
00167                 }
00168             }
00169         } else if (val.substr(0, 6) == "--log-") {
00170             size_t p = val.find('=');
00171             if (p == std::string::npos) {
00172                 std::cerr << "unknown option: " << val << '\n';
00173                 return 0;
00174             }
00175             std::string s = val.substr(6, p - 6);
00176             int severity;
00177             if (s == "error") severity = 0;
00178             else if (s == "warning") severity = 1;
00179             else if (s == "info") severity = 2;
00180             else {
00181                 std::cerr << "unknown debug level: " << s << '\n';
00182                 return 0;
00183             }
00184             while (p != std::string::npos) {
00185                 size_t q = val.find(',', p + 1);
00186                 s = val.substr(p + 1, q == std::string::npos ? q : q - (p + 1));
00187                 if (!lg::set_log_domain_severity(s, severity)) {
00188                     std::cerr << "unknown debug domain: " << s << '\n';
00189                     return 0;
00190                 }
00191                 p = q;
00192             }
00193         } else if(val[0] == '-') {
00194             std::cerr << "unknown option: " << val << "\n";
00195             return 0;
00196         } else {
00197             filename = val;
00198             try {
00199                 mapdata = read_file(filename);
00200             }
00201             catch (io_exception) {
00202                 std::cerr << "Could not read the map file, sorry." << std::endl;
00203                 return 1;
00204             }
00205         }
00206     }
00207 
00208     setlocale (LC_ALL, "");
00209     const std::string& intl_dir = get_intl_dir();
00210     bindtextdomain (PACKAGE "-editor", intl_dir.c_str());
00211     bind_textdomain_codeset (PACKAGE "-editor", "UTF-8");
00212     bindtextdomain (PACKAGE "-lib", intl_dir.c_str());
00213     bind_textdomain_codeset (PACKAGE "-lib", "UTF-8");
00214     textdomain (PACKAGE "-editor");
00215 
00216     // Blatant cut and paste from game.cpp
00217     image::set_wm_icon();
00218     int video_flags = preferences::fullscreen() ? FULL_SCREEN : 0;
00219     std::pair<int,int> resolution = preferences::resolution();
00220 
00221     std::cerr << "checking mode possible...\n";
00222     const int default_bpp = 24;
00223     int bpp = default_bpp;
00224 
00225     if(choosen_bpp == 0) {
00226         bpp = video.modePossible(resolution.first,resolution.second,default_bpp,video_flags);
00227 
00228         std::cerr << bpp << "\n";
00229 
00230         if(bpp == 0)  {
00231             //Video mode not supported, maybe from bad prefs.
00232             std::cerr << "The video mode, " << resolution.first
00233                   << "x" << resolution.second << "x" << default_bpp <<
00234                   "is not supported\nAttempting 1024x768x" << default_bpp << "...\n";
00235 
00236             //Attempt 1024x768.
00237             resolution.first = 1024;
00238             resolution.second = 768;
00239 
00240             bpp = video.modePossible(resolution.first,resolution.second,default_bpp,video_flags);
00241 
00242             if(bpp == 0) {
00243                  //Attempt 1024x768.
00244                 resolution.first = 1024;
00245                 resolution.second = 768;
00246                 std::cerr << "1024x768x" << default_bpp << " is not possible.\nAttempting 800x600x" << default_bpp << "...\n";
00247 
00248                 resolution.first = 800;
00249                 resolution.second = 600;
00250 
00251                 bpp = video.modePossible(resolution.first,resolution.second,default_bpp,video_flags);
00252             }
00253 
00254             if(bpp == 0) {
00255                 //couldn't do 1024x768 or 800x600
00256 
00257                 std::cerr << "The required video mode, " << resolution.first
00258                       << "x" << resolution.second << "x" << default_bpp <<
00259                       "is not supported\n";
00260 
00261                 return 0;
00262             }
00263         }
00264     } else {
00265         bpp = choosen_bpp;
00266     }
00267 
00268     std::cerr << "setting mode to " << resolution.first << "x" << resolution.second << "\n";
00269     const int res = video.setMode(resolution.first,resolution.second,bpp,video_flags);
00270     video.setBpp(bpp);
00271     if(res == 0) {
00272         std::cerr << "required video mode, " << resolution.first << "x"
00273                   << resolution.second << "x" << bpp << " is not supported\n";
00274         return 0;
00275     }
00276     preproc_map defines_map;
00277     // define editor to do conditionnal loading in the main cfg
00278     defines_map["EDITOR"] = preproc_define();
00279 
00280     defines_map["MEDIUM"] = preproc_define();
00281     defines_map["NORMAL"] = preproc_define();
00282 
00283 #if defined(__APPLE__)
00284     defines_map["APPLE"] = preproc_define();
00285 #endif
00286 
00287     //Set the locale first, then read the configuration, or else WML
00288     //strings are not correctly translated. Does this work on on the win32
00289     //platform?
00290     load_language_list();
00291     const bool lang_res = ::set_language(get_locale());
00292     if(!lang_res) {
00293         std::cerr << "No translation for locale '" << get_locale().language
00294                   << "', default to system locale\n";
00295 
00296         const bool lang_res = ::set_language(get_languages()[0]);
00297         if(!lang_res) {
00298             std::cerr << "Language data not found\n";
00299         }
00300     }
00301 
00302     // Set the caption of the window
00303     wm_title_string = _("Battle for Wesnoth Map Editor");
00304     wm_title_string += " - " + game_config::revision;
00305     SDL_WM_SetCaption(wm_title_string.c_str(), NULL);
00306 
00307     //Read the configuration af
00308     config cfg;
00309     try {
00310         scoped_istream stream = preprocess_file("data/", &defines_map);
00311         read(cfg, *stream);
00312     }
00313     catch (config::error e) {
00314         std::cerr << "Error when reading game config: '" << e.message << "'" << std::endl;
00315     }
00316     font::load_font_config();
00317     paths_manager.set_paths(cfg);
00318 	::init_textdomains(cfg);
00319 
00320     if(mapdata.empty()) {
00321         mapdata = map_editor::new_map(22, 22, t_translation::GRASS_LAND);
00322     }
00323 
00324     srand(time(NULL));
00325     bool done = false;
00326     config* theme_cfg = cfg.find_child("theme", "name", "editor");
00327     config dummy_theme;
00328     if (!theme_cfg) {
00329         std::cerr << "Editor theme could not be loaded." << std::endl;
00330         theme_cfg = &dummy_theme;
00331     }
00332 
00333     std::cerr << "entering while...\n";
00334     events::event_context ec;
00335     map_editor::check_data(mapdata, filename, from_scenario, cfg);
00336     while (!done) {
00337         try {
00338             std::cerr << "creating map...\n";
00339             //! @todo Allow the editor to also create mask maps
00340             editormap map(cfg, mapdata);
00341 
00342             const config dummy_cfg;
00343             editor_display gui(video, map, *theme_cfg, cfg, dummy_cfg);
00344 
00345             map_editor::map_editor editor(gui, map, *theme_cfg, cfg);
00346             editor.set_file_to_save_as(filename, from_scenario);
00347             editor.main_loop();
00348             done = true;
00349         }
00350         catch (map_editor::map_editor::new_map_exception &e) {
00351             mapdata = e.new_map;
00352             filename = e.new_filename;
00353             from_scenario = e.from_scenario;
00354         }
00355         catch (gamemap::incorrect_format_exception) {
00356             std::cerr << "The map is not in a correct format, sorry." << std::endl;
00357             return 1;
00358         } catch(twml_exception& e) {
00359             std::cerr << "WML exception:\nUser message: " 
00360                 << e.user_message << "\nDev message: " << e.dev_message << '\n';
00361             return 1;
00362         } catch (CVideo::quit&) {
00363             return 0;
00364         }
00365     }
00366 
00367     return 0;
00368 }

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs