/
clientapp.cpp
417 lines (347 loc) · 10.6 KB
/
clientapp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/** @file clientapp.cpp The client application.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de_platform.h"
#include <QMenuBar>
#include <QAction>
#include <QNetworkProxyFactory>
#include <QDesktopServices>
#include <QFontDatabase>
#include <QDebug>
#include <stdlib.h>
#include <de/Log>
#include <de/DisplayMode>
#include <de/Error>
#include <de/ByteArrayFile>
#include <de/c_wrapper.h>
#include <de/garbage.h>
#include "clientapp.h"
#include "dd_main.h"
#include "dd_def.h"
#include "dd_loop.h"
#include "de_audio.h"
#include "con_main.h"
#include "sys_system.h"
#include "audio/s_main.h"
#include "gl/gl_main.h"
#include "gl/gl_texmanager.h"
#include "ui/inputsystem.h"
#include "ui/windowsystem.h"
#include "ui/clientwindow.h"
#include "updater.h"
#if WIN32
# include "dd_winit.h"
#elif UNIX
# include "dd_uinit.h"
#endif
using namespace de;
static ClientApp *clientAppSingleton = 0;
static void handleLegacyCoreTerminate(char const *msg)
{
Con_Error("Application terminated due to exception:\n%s\n", msg);
}
static void continueInitWithEventLoopRunning()
{
// Show the main window. This causes initialization to finish (in busy mode)
// as the canvas is visible and ready for initialization.
WindowSystem::main().show();
ClientApp::updater().setupUI();
}
Value *Binding_App_GamePlugin(Context &, Function::ArgumentValues const &)
{
if(App_CurrentGame().isNull())
{
// The null game has no plugin.
return 0;
}
String name = Plug_FileForPlugin(App_CurrentGame().pluginId()).name().fileNameWithoutExtension();
if(name.startsWith("lib")) name.remove(0, 3);
return new TextValue(name);
}
Value *Binding_App_LoadFont(Context &, Function::ArgumentValues const &args)
{
LOG_AS("ClientApp");
// We must have one argument.
if(args.size() != 1)
{
throw Function::WrongArgumentsError("Binding_App_LoadFont",
"Expected one argument");
}
try
{
// Try to load the specific font.
Block data(App::fileSystem().root().locate<File const>(args.at(0)->asText()));
int id;
id = QFontDatabase::addApplicationFontFromData(data);
if(id < 0)
{
LOG_WARNING("Failed to load font:");
}
else
{
LOG_VERBOSE("Loaded font: %s") << args.at(0)->asText();
//qDebug() << args.at(0)->asText();
//qDebug() << "Families:" << QFontDatabase::applicationFontFamilies(id);
}
}
catch(Error const &er)
{
LOG_WARNING("Failed to load font:\n") << er.asText();
}
return 0;
}
DENG2_PIMPL(ClientApp)
{
QScopedPointer<Updater> updater;
SettingsRegister audioSettings;
QMenuBar *menuBar;
InputSystem *inputSys;
QScopedPointer<WidgetActions> widgetActions;
RenderSystem *renderSys;
WindowSystem *winSys;
ServerLink *svLink;
GLShaderBank shaderBank;
Games games;
World world;
Instance(Public *i)
: Base(i),
menuBar(0),
inputSys(0),
winSys(0),
renderSys(0),
svLink(0)
{
clientAppSingleton = thisPublic;
}
~Instance()
{
Sys_Shutdown();
DD_Shutdown();
delete svLink;
delete winSys;
delete renderSys;
delete inputSys;
delete menuBar;
clientAppSingleton = 0;
deinitScriptBindings();
}
void initScriptBindings()
{
Function::registerNativeEntryPoint("App_GamePlugin", Binding_App_GamePlugin);
Function::registerNativeEntryPoint("App_LoadFont", Binding_App_LoadFont);
Record &appModule = self.scriptSystem().nativeModule("App");
appModule.addFunction("gamePlugin", refless(new Function("App_GamePlugin"))).setReadOnly();
appModule.addFunction("loadFont", refless(new Function("App_LoadFont", Function::Arguments() << "fileName"))).setReadOnly();
}
void deinitScriptBindings()
{
Function::unregisterNativeEntryPoint("App_GamePlugin");
Function::unregisterNativeEntryPoint("App_LoadFont");
}
/**
* Set up an application-wide menu.
*/
void setupAppMenu()
{
#ifdef MACOSX
menuBar = new QMenuBar;
QMenu *gameMenu = menuBar->addMenu("&Game");
QAction *checkForUpdates = gameMenu->addAction("Check For &Updates...", updater.data(),
SLOT(checkNowShowingProgress()));
checkForUpdates->setMenuRole(QAction::ApplicationSpecificRole);
#endif
}
void initSettings()
{
typedef SettingsRegister SReg;
/// @todo These belong in their respective subsystems.
audioSettings
.define(SReg::IntCVar, "sound-volume", 255)
.define(SReg::IntCVar, "music-volume", 255)
.define(SReg::FloatCVar, "sound-reverb-volume", 0.5f)
.define(SReg::IntCVar, "sound-info", 0)
.define(SReg::IntCVar, "sound-rate", 11025)
.define(SReg::IntCVar, "sound-16bit", 0)
.define(SReg::IntCVar, "sound-3d", 0)
.define(SReg::IntCVar, "sound-overlap-stop", 0)
.define(SReg::IntCVar, "music-source", MUSP_EXT);
}
};
ClientApp::ClientApp(int &argc, char **argv)
: GuiApp(argc, argv), d(new Instance(this))
{
novideo = false;
// Override the system locale (affects number/time formatting).
QLocale::setDefault(QLocale("en_US.UTF-8"));
// Use the host system's proxy configuration.
QNetworkProxyFactory::setUseSystemConfiguration(true);
// Metadata.
QCoreApplication::setOrganizationDomain ("dengine.net");
QCoreApplication::setOrganizationName ("Deng Team");
QCoreApplication::setApplicationName ("Doomsday Engine");
QCoreApplication::setApplicationVersion (DOOMSDAY_VERSION_BASE);
setTerminateFunc(handleLegacyCoreTerminate);
// We must presently set the current game manually (the collection is global).
setGame(d->games.nullGame());
d->initScriptBindings();
}
void ClientApp::initialize()
{
Libdeng_Init();
d->svLink = new ServerLink;
// Config needs DisplayMode, so let's initialize it before the libdeng2
// subsystems and Config.
DisplayMode_Init();
initSubsystems(); // loads Config
// Create the user's configurations and settings folder, if it doesn't exist.
fileSystem().makeFolder("/home/configs");
d->initSettings();
// Initialize.
#if WIN32
if(!DD_Win32_Init())
{
throw Error("ClientApp::initialize", "DD_Win32_Init failed");
}
#elif UNIX
if(!DD_Unix_Init())
{
throw Error("ClientApp::initialize", "DD_Unix_Init failed");
}
#endif
// Load all the shader program definitions.
FS::FoundFiles found;
fileSystem().findAll("shaders.dei", found);
DENG2_FOR_EACH(FS::FoundFiles, i, found)
{
LOG_MSG("Loading shader definitions from %s") << (*i)->description();
d->shaderBank.addFromInfo(**i);
}
// Create the window system.
d->winSys = new WindowSystem;
addSystem(*d->winSys);
// Check for updates automatically.
d->updater.reset(new Updater);
d->setupAppMenu();
Plug_LoadAll();
// Create the main window.
char title[256];
DD_ComposeMainWindowTitle(title);
d->winSys->createWindow()->setWindowTitle(title);
// Create the input system.
d->inputSys = new InputSystem;
addSystem(*d->inputSys);
d->widgetActions.reset(new WidgetActions);
// Create the render system.
d->renderSys = new RenderSystem;
addSystem(*d->renderSys);
// Finally, run the bootstrap script.
scriptSystem().importModule("bootstrap");
App_Timer(1, continueInitWithEventLoopRunning);
}
void ClientApp::preFrame()
{
// Frame syncronous I/O operations.
S_StartFrame(); /// @todo Move to AudioSystem::timeChanged().
if(gx.BeginFrame) /// @todo Move to GameSystem::timeChanged().
{
gx.BeginFrame();
}
}
void ClientApp::postFrame()
{
/// @todo Should these be here? Consider multiple windows, each having a postFrame?
/// Or maybe the frames need to be synced? Or only one of them has a postFrame?
if(gx.EndFrame)
{
gx.EndFrame();
}
S_EndFrame();
Garbage_Recycle();
loop().resume();
}
bool ClientApp::haveApp()
{
return clientAppSingleton != 0;
}
ClientApp &ClientApp::app()
{
DENG2_ASSERT(clientAppSingleton != 0);
return *clientAppSingleton;
}
Updater &ClientApp::updater()
{
DENG2_ASSERT(!app().d->updater.isNull());
return *app().d->updater;
}
SettingsRegister &ClientApp::audioSettings()
{
return app().d->audioSettings;
}
ServerLink &ClientApp::serverLink()
{
ClientApp &a = ClientApp::app();
DENG2_ASSERT(a.d->svLink != 0);
return *a.d->svLink;
}
InputSystem &ClientApp::inputSystem()
{
ClientApp &a = ClientApp::app();
DENG2_ASSERT(a.d->inputSys != 0);
return *a.d->inputSys;
}
RenderSystem &ClientApp::renderSystem()
{
ClientApp &a = ClientApp::app();
DENG2_ASSERT(a.d->renderSys != 0);
return *a.d->renderSys;
}
WindowSystem &ClientApp::windowSystem()
{
ClientApp &a = ClientApp::app();
DENG2_ASSERT(a.d->winSys != 0);
return *a.d->winSys;
}
WidgetActions &ClientApp::widgetActions()
{
return *app().d->widgetActions;
}
GLShaderBank &ClientApp::glShaderBank()
{
return app().d->shaderBank;
}
Games &ClientApp::games()
{
return app().d->games;
}
World &ClientApp::world()
{
return app().d->world;
}
void ClientApp::openHomepageInBrowser()
{
openInBrowser(QUrl(DOOMSDAY_HOMEURL));
}
void ClientApp::openInBrowser(QUrl url)
{
// Get out of fullscreen mode.
int windowed[] = {
ClientWindow::Fullscreen, false,
ClientWindow::End
};
ClientWindow::main().changeAttributes(windowed);
QDesktopServices::openUrl(url);
}