freg  0.3
Free-Roaming Elementary Game
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
main.cpp
Go to the documentation of this file.
1  /* freg, Free-Roaming Elementary Game with open and interactive world
2  * Copyright (C) 2012-2014 Alexander 'mmaulwurff' Kromm
3  * mmaulwurff@gmail.com
4  *
5  * This file is part of FREG.
6  *
7  * FREG is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * FREG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with FREG. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include <QTranslator>
21 #include <QSettings>
22 #include <QDir>
23 #include <QTime>
24 #include <QCommandLineParser>
25 #include <QLockFile>
26 #include "World.h"
27 #include "Player.h"
28 #include "worldmap.h"
29 #include "CraftManager.h"
30 
31 #ifdef CURSED_SCREEN
32  #include "screens/CursedScreen.h"
33  #include <QCoreApplication>
34  typedef QCoreApplication Application;
35 #else
36  #ifdef TEXT_SCREEN
37  #include "screens/TextScreen.h"
38  #include <QCoreApplication>
39  typedef QCoreApplication Application;
40  #else
41  #include <QApplication>
42  typedef QApplication Application;
43  #endif
44 #endif
45 
46 #ifdef Q_OS_WIN32
47 const QString home_path = "";
48 #else
49 //const QString home_path = QDir::homePath() + "/.freg/";
50 const QString home_path = "";
51 #endif
52 
53 int main(int argc, char ** argv) {
54  setlocale(LC_CTYPE, "C-UTF-8");
55  if ( not QDir::home().mkpath(".freg") ) {
56  puts(qPrintable( QObject::tr("Error creating game home directory") ));
57  return EXIT_FAILURE;
58  }
59  if (freopen(qPrintable(home_path + "err.txt"), "wt", stderr) == nullptr) {
60  puts(qPrintable( QObject::tr(
61  "Error opening errors.txt, writing errors to standard out.") ));
62  }
63 
64  Application freg(argc, argv);
65  QCoreApplication::setOrganizationName("freg-team");
66  QCoreApplication::setApplicationName("freg");
67  QCoreApplication::setApplicationVersion(VER);
68 
69  QTranslator translator;
70  translator.load(QString(":/freg_") + locale);
71  freg.installTranslator(&translator);
72 
73  // parse arguments
74  QCommandLineParser parser;
75  parser.setApplicationDescription(QObject::tr("freg - 3d open world game"));
76  parser.addHelpOption();
77  parser.addVersionOption();
78  const QCommandLineOption ascii(QStringList() << "a" << "ascii",
79  QObject::tr("Use ASCII-characters only and don't animate things."));
80  parser.addOption(ascii);
81  const QCommandLineOption world_argument(QStringList() << "w" << "world",
82  QObject::tr("Specify world."),
83  QObject::tr("world_name"));
84  parser.addOption(world_argument);
85  const QCommandLineOption generate(QStringList() << "g" << "generate",
86  QObject::tr("Generate new map."));
87  parser.addOption(generate);
88  const QCommandLineOption map_size(QStringList() << "s" << "size",
89  QObject::tr("Generated map size. Works only with -g."),
90  QObject::tr("map_size"), QString::number(DEFAULT_MAP_SIZE));
91  parser.addOption(map_size);
92  const QCommandLineOption map_outer(QStringList() << "o" << "outer",
93  QObject::tr("Generated map outer shred. Works only with -g."),
94  QObject::tr("map_outer"), QString(QChar(OUT_BORDER_SHRED)));
95  parser.addOption(map_outer);
96  const QCommandLineOption map_seed(QStringList() << "d" << "seed",
97  QObject::tr("Seed to generate map. Works only with -g."),
98  QObject::tr("map_seed"), QString::number(0));
99  parser.addOption(map_seed);
100  parser.process(freg);
101 
102  QSettings::setDefaultFormat(QSettings::IniFormat);
103  QSettings sett(home_path + ".freg/freg.ini", QSettings::IniFormat);
104  const QString worldName = parser.isSet(world_argument) ?
105  parser.value(world_argument) :
106  sett.value("current_world", "mu").toString();
107  sett.setValue("current_world", worldName);
108  if ( not QDir(home_path).mkpath(worldName) ) {
109  puts(qPrintable(QObject::tr("Error generating world.")));
110  return EXIT_FAILURE;
111  }
112 
113  if ( parser.isSet(generate) ) {
115  worldName,
116  parser.value(map_size).toUShort(),
117  parser.value(map_outer).at(0).toLatin1(),
118  parser.value(map_seed).toInt());
119  puts(qPrintable(QObject::tr("Map generated successfully.")));
120  return EXIT_SUCCESS;
121  }
122 
123  CraftManager craftManager;
124  craft_manager = &craftManager;
125 
126  qsrand(QTime::currentTime().msec());
127  bool world_error = false;
128  World world(worldName, &world_error);
129  if ( world_error ) {
130  puts(qPrintable(QObject::tr("Error loading world.")));
131  return EXIT_FAILURE;
132  }
133  QLockFile lock_file(home_path + worldName + "/lock");
134  if ( not lock_file.tryLock() ) {
135  puts(qPrintable(
136  QObject::tr("World \"%1\" is used by another instance of freg.")
137  .arg(worldName)));
138  return EXIT_FAILURE;
139  }
140  Player player;
141  int error = SCREEN_NO_ERROR;
142  #ifdef Q_OS_WIN32
143  const Screen screen(&world, &player, error, true);
144  #else
145  const Screen screen(&world, &player, error, parser.isSet(ascii));
146  #endif
147 
148  if ( error ) return EXIT_FAILURE;
149 
150  QObject::connect(&screen, SIGNAL(ExitReceived()), &freg, SLOT(quit()),
151  Qt::DirectConnection);
152  QObject::connect(&world, SIGNAL(ExitReceived()), &freg, SLOT(quit()),
153  Qt::DirectConnection);
154 
155  world.start();
156  return freg.exec();
157 }
QApplication Application
Definition: main.cpp:42
World * world
Definition: World.cpp:32
int main(int argc, char **argv)
Definition: main.cpp:53
World provides global physics and shred connection.
Definition: World.h:52
const QString home_path
Definition: main.cpp:50
Provides curses (text-based graphics interface) screen for freg.
const QString locale
Definition: header.h:56
const char OUT_BORDER_SHRED
Definition: header.h:83
static void GenerateMap(QString world_name, int size, char outer, int seed)
Definition: worldmap.cpp:127
const CraftManager * craft_manager
This class contains information specific to player and interface for manipulating him...
Definition: Player.h:34
const int DEFAULT_MAP_SIZE
Definition: header.h:81