QTfrontend/main.cpp
branchsdl2transition
changeset 11362 ed5a6478e710
parent 9711 7d0329f37181
parent 11046 47a8c19ecb60
child 11488 637fb0558a73
equal deleted inserted replaced
11361:31570b766315 11362:ed5a6478e710
     1 /*
     1 /*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    12  * GNU General Public License for more details.
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17  */
    17  */
    18 
    18 
    19 #include "HWApplication.h"
    19 #include "HWApplication.h"
    20 
    20 
    21 #include <QTranslator>
    21 #include <QTranslator>
   123         delete engine;
   123         delete engine;
   124         engine = NULL;
   124         engine = NULL;
   125     }
   125     }
   126 }
   126 }
   127 
   127 
       
   128 QString getUsage()
       
   129 {
       
   130     return QString(
       
   131 "%1: hedgewars [%2...] [%3]\n"
       
   132 "\n"
       
   133 "%4:\n"
       
   134 "  --help              %5\n"
       
   135 "  --config-dir=PATH   %6\n"
       
   136 "  --data-dir=PATH     %7\n"
       
   137 "\n"
       
   138 "%8"
       
   139 "\n"
       
   140 ).arg(HWApplication::tr("Usage", "command-line"))
       
   141 .arg(HWApplication::tr("OPTION", "command-line"))
       
   142 .arg(HWApplication::tr("CONNECTSTRING", "command-line"))
       
   143 .arg(HWApplication::tr("Options", "command-line"))
       
   144 .arg(HWApplication::tr("Display this help", "command-line"))
       
   145 .arg(HWApplication::tr("Custom path for configuration data and user data", "command-line"))
       
   146 .arg(HWApplication::tr("Custom path to the game data folder", "command-line"))
       
   147 .arg(HWApplication::tr("Hedgewars can use a %1 (e.g. \"%2\") to connect on start.", "command-line").arg(HWApplication::tr("CONNECTSTRING", "command-line")).arg(QString("hwplay://") + NETGAME_DEFAULT_SERVER));
       
   148 }
       
   149 
   128 int main(int argc, char *argv[])
   150 int main(int argc, char *argv[])
   129 {
   151 {
   130     // Since we're calling this first, closeResources() will be the last thing called after main() returns.
   152     // Since we're calling this first, closeResources() will be the last thing called after main() returns.
   131     atexit(closeResources);
   153     atexit(closeResources);
   132 
   154 
   135 #endif
   157 #endif
   136 
   158 
   137     SDLInteraction::instance();
   159     SDLInteraction::instance();
   138 
   160 
   139     HWApplication app(argc, argv);
   161     HWApplication app(argc, argv);
   140 
   162     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
       
   163 
       
   164     // file engine and splash. to be initialized later
       
   165     engine = NULL;
   141     QLabel *splash = NULL;
   166     QLabel *splash = NULL;
       
   167 
       
   168     // parse arguments
       
   169 
       
   170     QStringList arguments = app.arguments();
       
   171     QMap<QString, QString> parsedArgs;
       
   172     {
       
   173         QList<QString>::iterator i = arguments.begin();
       
   174         while(i != arguments.end())
       
   175         {
       
   176             QString arg = *i;
       
   177 
       
   178 
       
   179             QRegExp opt("--(\\S+)=(.+)");
       
   180             if(opt.exactMatch(arg))
       
   181             {
       
   182                 parsedArgs[opt.cap(1)] = opt.cap(2);
       
   183                 i = arguments.erase(i);
       
   184             }
       
   185             else
       
   186             {
       
   187                 if(arg.startsWith("--")) {
       
   188                     if(arg == "--help")
       
   189                     {
       
   190                         printf("%s", getUsage().toUtf8().constData());
       
   191                         return 0;
       
   192                     }
       
   193                     // argument is something wrong
       
   194                     fprintf(stderr, "%s\n\n%s",
       
   195                         HWApplication::tr("Malformed option argument: %1", "command-line").arg(arg).toUtf8().constData(),
       
   196                         getUsage().toUtf8().constData());
       
   197                     return 1;
       
   198                 }
       
   199 
       
   200                 // if not starting with --, then always skip
       
   201                 // (because we can't determine if executable path/call or not - on windows)
       
   202                 ++i;
       
   203             }
       
   204         }
       
   205     }
       
   206 
       
   207     if(parsedArgs.contains("data-dir"))
       
   208     {
       
   209         QFileInfo f(parsedArgs["data-dir"]);
       
   210         parsedArgs.remove("data-dir");
       
   211         if(!f.exists())
       
   212         {
       
   213             qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath();
       
   214         }
       
   215         *cDataDir = f.absoluteFilePath();
       
   216         custom_data = true;
       
   217     }
       
   218 
       
   219     if(parsedArgs.contains("config-dir"))
       
   220     {
       
   221         QFileInfo f(parsedArgs["config-dir"]);
       
   222         parsedArgs.remove("config-dir");
       
   223         cfgdir->setPath(f.absoluteFilePath());
       
   224         custom_config = true;
       
   225     }
       
   226     else
       
   227     {
       
   228         cfgdir->setPath(QDir::homePath());
       
   229         custom_config = false;
       
   230     }
       
   231 
       
   232     if (!parsedArgs.isEmpty()) {
       
   233         foreach (const QString & key, parsedArgs.keys())
       
   234         {
       
   235             fprintf(stderr, "%s\n", HWApplication::tr("Unknown option argument: %1", "command-line").arg(QString("--") + key).toUtf8().constData());
       
   236         }
       
   237         fprintf(stderr, "\n%s", getUsage().toUtf8().constData());
       
   238         return 1;
       
   239     }
       
   240 
       
   241     // end of parameter parsing
       
   242 
   142 #if defined Q_OS_WIN
   243 #if defined Q_OS_WIN
   143     QPixmap pixmap(":res/splash.png");
   244     QPixmap pixmap(":res/splash.png");
   144     splash = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
   245     splash = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
   145     splash->setAttribute(Qt::WA_TranslucentBackground);
   246     splash->setAttribute(Qt::WA_TranslucentBackground);
   146     const QRect deskSize = HWApplication::desktop()->screenGeometry(-1);
   247     const QRect deskSize = HWApplication::desktop()->screenGeometry(-1);
   148                                   (deskSize.height() - pixmap.height())/2 );
   249                                   (deskSize.height() - pixmap.height())/2 );
   149     splash->move(splashCenter);
   250     splash->move(splashCenter);
   150     splash->setPixmap(pixmap);
   251     splash->setPixmap(pixmap);
   151     splash->show();
   252     splash->show();
   152 #endif
   253 #endif
   153 
       
   154     engine = new FileEngineHandler(argv[0]);
       
   155 
       
   156     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
       
   157 
       
   158     QStringList arguments = app.arguments();
       
   159     QMap<QString, QString> parsedArgs;
       
   160     {
       
   161         QList<QString>::iterator i = arguments.begin();
       
   162         while(i != arguments.end())
       
   163         {
       
   164             QString arg = *i;
       
   165 
       
   166             QRegExp opt("--(\\S+)=(.+)");
       
   167             if(opt.exactMatch(arg))
       
   168             {
       
   169                 parsedArgs[opt.cap(1)] = opt.cap(2);
       
   170                 i = arguments.erase(i);
       
   171             }
       
   172             else
       
   173             {
       
   174                 ++i;
       
   175             }
       
   176         }
       
   177     }
       
   178 
       
   179     if(parsedArgs.contains("data-dir"))
       
   180     {
       
   181         QFileInfo f(parsedArgs["data-dir"]);
       
   182         if(!f.exists())
       
   183         {
       
   184             qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath();
       
   185         }
       
   186         *cDataDir = f.absoluteFilePath();
       
   187         custom_data = true;
       
   188     }
       
   189 
       
   190     if(parsedArgs.contains("config-dir"))
       
   191     {
       
   192         QFileInfo f(parsedArgs["config-dir"]);
       
   193         cfgdir->setPath(f.absoluteFilePath());
       
   194         custom_config = true;
       
   195     }
       
   196     else
       
   197     {
       
   198         cfgdir->setPath(QDir::homePath());
       
   199         custom_config = false;
       
   200     }
       
   201 
       
   202     app.setStyle(new QPlastiqueStyle());
   254     app.setStyle(new QPlastiqueStyle());
   203 
   255 
   204     QDateTime now = QDateTime::currentDateTime();
   256     QDateTime now = QDateTime::currentDateTime();
   205     srand(now.toTime_t());
   257     srand(now.toTime_t());
   206     rand();
   258     rand();
   257         MessageDialog::ShowFatalMessage(HWApplication::tr("Failed to open data directory:\n%1\n\nPlease check your installation!").arg(datadir->absolutePath()+"/Data"));
   309         MessageDialog::ShowFatalMessage(HWApplication::tr("Failed to open data directory:\n%1\n\nPlease check your installation!").arg(datadir->absolutePath()+"/Data"));
   258         return 1;
   310         return 1;
   259     }
   311     }
   260 
   312 
   261     // setup PhysFS
   313     // setup PhysFS
       
   314     engine = new FileEngineHandler(argv[0]);
   262     engine->mount(datadir->absolutePath());
   315     engine->mount(datadir->absolutePath());
   263     engine->mount(cfgdir->absolutePath() + "/Data");
   316     engine->mount(cfgdir->absolutePath() + "/Data");
   264     engine->mount(cfgdir->absolutePath());
   317     engine->mount(cfgdir->absolutePath());
   265     engine->setWriteDir(cfgdir->absolutePath());
   318     engine->setWriteDir(cfgdir->absolutePath());
   266     engine->mountPacks();
   319     engine->mountPacks();
   282 
   335 
   283         // load locale file into translator
   336         // load locale file into translator
   284         if (!Translator.load(QString("physfs://Locale/hedgewars_%1").arg(cc)))
   337         if (!Translator.load(QString("physfs://Locale/hedgewars_%1").arg(cc)))
   285             qWarning("Failed to install translation (%s)", qPrintable(cc));
   338             qWarning("Failed to install translation (%s)", qPrintable(cc));
   286         app.installTranslator(&Translator);
   339         app.installTranslator(&Translator);
       
   340         app.setLayoutDirection(QLocale(cc).textDirection());
   287     }
   341     }
   288 
   342 
   289 #ifdef _WIN32
   343 #ifdef _WIN32
   290     // Win32 registry setup (used for xfire detection etc. - don't set it if we're running in "portable" mode with a custom config dir)
   344     // Win32 registry setup (used for xfire detection etc. - don't set it if we're running in "portable" mode with a custom config dir)
   291     if(!custom_config)
   345     if(!custom_config)