ManaPlus
maingui.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "maingui.h"
25 
26 #include "client.h"
27 #include "commandline.h"
28 #include "settings.h"
29 
30 #include "fs/virtfs/fs.h"
31 
32 #include "utils/delete2.h"
33 #ifdef ANDROID
34 #include "fs/mkdir.h"
35 #include "fs/paths.h"
36 #endif // ANDROID
37 #include "utils/process.h"
38 
39 #ifdef __MINGW32__
40 #include <windows.h>
41 #endif // __MINGW32__
42 
43 #include <iostream>
44 
45 PRAGMA48(GCC diagnostic push)
46 PRAGMA48(GCC diagnostic ignored "-Wshadow")
47 #include <SDL_image.h>
48 #include <SDL_mixer.h>
49 PRAGMA48(GCC diagnostic pop)
50 
51 #ifndef SDL_VERSIONNUM
52 #error missing <SDL_version.h>
53 #endif // SDL_VERSIONNUM
54 
55 #ifdef UNITTESTS
56 #include "logger.h"
57 
58 #include "utils/cpu.h"
59 #include "utils/sdlhelper.h"
61 #ifdef UNITTESTS_CATCH
62 #define CATCH_CONFIG_RUNNER
63 #ifdef UNITTESTS_EMBED
64 #include "unittests/catch.hpp"
65 #else // UNITTESTS_EMBED
66 #include <catch.hpp>
67 #endif // UNITTESTS_EMBED
68 #endif // UNITTESTS_CATCH
69 #ifdef UNITTESTS_DOCTEST
70 #define DOCTEST_CONFIG_IMPLEMENT
71 #ifdef UNITTESTS_EMBED
72 #include "unittests/doctest.h"
73 #else // UNITTESTS_EMBED
74 #include <doctest/doctest.h>
75 #endif // UNITTESTS_EMBED
76 #endif // UNITTESTS_DOCTEST
77 #else // UNITTESTS
78 #include "utils/xml.h"
79 #endif // UNITTESTS
80 
81 #define SDL_IMAGE_COMPILEDVERSION \
82  SDL_VERSIONNUM(SDL_IMAGE_MAJOR_VERSION, \
83  SDL_IMAGE_MINOR_VERSION, SDL_IMAGE_PATCHLEVEL)
84 
85 #define SDL_IMAGE_VERSION_ATLEAST(X, Y, Z) \
86  (SDL_IMAGE_COMPILEDVERSION >= SDL_VERSIONNUM(X, Y, Z))
87 
88 #ifndef SDL_MIXER_COMPILEDVERSION
89 #define SDL_MIXER_COMPILEDVERSION \
90  SDL_VERSIONNUM(SDL_MIXER_MAJOR_VERSION, \
91  SDL_MIXER_MINOR_VERSION, SDL_MIXER_PATCHLEVEL)
92 #endif // SDL_MIXER_COMPILEDVERSION
93 
94 #ifndef SDL_MIXER_VERSION_ATLEAST
95 #define SDL_MIXER_VERSION_ATLEAST(X, Y, Z) \
96  (SDL_MIXER_COMPILEDVERSION >= SDL_VERSIONNUM(X, Y, Z))
97 #endif // SDL_MIXER_VERSION_ATLEAST
98 
99 #include "debug.h"
100 
101 #ifdef __SWITCH__
102 extern "C"
103 {
104 #include <switch/runtime/devices/socket.h>
105 #include <switch/runtime/nxlink.h>
106 }
107 #ifndef NDEBUG
108 #include <unistd.h>
109 static int sock = -1;
110 #endif
111 void nxInit()
112 {
113  socketInitializeDefault();
114 #ifndef NDEBUG
115  sock = nxlinkStdio();
116  if (sock < 0)
117  {
118  socketExit();
119  }
120 #endif
121 }
122 void NxDeinit()
123 {
124 #ifndef NDEBUG
125  if (sock >= 0)
126  {
127  close(sock);
128  sock = -1;
129  }
130 #endif
131  socketExit();
132 }
133 #endif
134 
135 char *selfName = nullptr;
136 
137 #ifndef UNITTESTS
138 #ifdef ANDROID
139 int main(int argc, char *argv[])
140 #else // ANDROID
141 
142 int mainGui(int argc, char *argv[])
143 #endif // ANDROID
144 {
145 #ifdef __SWITCH__
146  nxInit();
147 #endif
148 #if defined(__MINGW32__)
149  // load mingw crash handler. Won't fail if dll is not present.
150  // may load libray from current dir, it may not same as program dir
151  LoadLibrary("exchndl.dll");
152 #endif // defined(__MINGW32__)
153 
154  selfName = argv[0];
155 
156  parseOptions(argc, argv);
157 
158  std::ios::sync_with_stdio(false);
159 
160 #ifdef ANDROID
161  mkdir_r(getSdStoragePath().c_str());
162 #endif // ANDROID
163 
164  VirtFs::init(argv[0]);
165  XML::initXML();
166 #if SDL_IMAGE_VERSION_ATLEAST(1, 2, 11)
167  IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
168 #endif // SDL_IMAGE_VERSION_ATLEAST(1, 2, 11)
169 #if SDL_MIXER_VERSION_ATLEAST(1, 2, 11)
170  Mix_Init(MIX_INIT_OGG);
171 #endif // SDL_MIXER_VERSION_ATLEAST(1, 2, 11)
172 
173 #ifdef WIN32
174  SetCurrentDirectory(VirtFs::getBaseDir());
175 #endif // WIN32
176 
177  setPriority(true);
178  client = new Client;
179  int ret = 0;
181  {
182  client->gameInit();
183  ret = client->gameExec();
184  }
185  else
186  {
187  client->testsInit();
188  ret = Client::testsExec();
189  }
190  delete2(client)
191  VirtFs::deinit();
192 
193 #if SDL_MIXER_VERSION_ATLEAST(1, 2, 11)
194  Mix_Quit();
195 #endif // SDL_MIXER_VERSION_ATLEAST(1, 2, 11)
196 #if SDL_IMAGE_VERSION_ATLEAST(1, 2, 11)
197  IMG_Quit();
198 #endif // SDL_IMAGE_VERSION_ATLEAST(1, 2, 11)
199 
200 #ifdef __SWITCH__
201  SDL_Quit();
202 #endif
203 
204 #ifdef __SWITCH__
205  NxDeinit();
206 #endif
207 
208  return ret;
209 }
210 #else // UNITTESTS
211 
212 int main(int argc, char *argv[])
213 {
214  logger = new Logger;
215  SDL::initLogger();
216  VirtFs::init(argv[0]);
217  Cpu::detect();
219 #ifdef UNITTESTS_CATCH
220  return Catch::Session().run(argc, argv);
221 #elif defined(UNITTESTS_DOCTEST)
222  doctest::Context context;
223  context.applyCommandLine(argc, argv);
224  return context.run();
225 #else // UNITTESTS_CATCH
226  return 1;
227 #endif // UNITTESTS_CATCH
228 }
229 
230 #endif // UNITTESTS
Definition: client.h:50
void gameInit()
Definition: client.cpp:170
static int testsExec()
Definition: client.cpp:833
void testsInit()
Definition: client.cpp:161
int gameExec()
Definition: client.cpp:577
static void initFunctions()
Definition: dyepalette.cpp:253
Definition: logger.h:69
Options options
Definition: settings.h:130
void applyCommandLine(int argc, const char *const *argv)
void parseOptions(const int argc, char *const argv[])
#define delete2(var)
Definition: delete2.h:25
Client * client
Definition: client.cpp:118
#define PRAGMA48(str)
Definition: localconsts.h:199
Logger * logger
Definition: logger.cpp:89
int main(int argc, char *argv[])
Definition: main.cpp:40
char * selfName
Definition: maingui.cpp:135
int mainGui(int argc, char *argv[])
Definition: maingui.cpp:142
int mkdir_r(const char *const pathname)
Create a directory, making leading components first if necessary.
Definition: mkdir.cpp:109
void detect()
Definition: cpu.cpp:46
void initLogger()
Definition: sdlhelper.cpp:187
bool deinit()
Definition: fs.cpp:785
void init(const std::string &name)
Definition: fs.cpp:58
const char * getBaseDir()
Definition: fs.cpp:79
int close(File *const file)
Definition: fs.cpp:808
void initXML()
Definition: libxml.cpp:305
void setPriority(const bool big)
Definition: process.cpp:328
Settings settings
Definition: settings.cpp:32
bool testMode
Definition: options.h:99