/
dd_uinit.c
438 lines (373 loc) · 11.3 KB
/
dd_uinit.c
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**\file dd_uinit.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2004-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006-2007 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Engine Initialization - Unix.
*
* Load libraries and set up APIs.
*/
// HEADER FILES ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <SDL.h>
#ifdef UNIX
# include "library.h"
#endif
#include "de_base.h"
#include "de_graphics.h"
#include "de_console.h"
#include "de_system.h"
#include "de_play.h"
#include "de_refresh.h"
#include "de_network.h"
#include "de_misc.h"
#include "fs_util.h"
#include "dd_uinit.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
typedef Library* PluginHandle;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
uint windowIDX; // Main window.
// PRIVATE DATA DEFINITIONS ------------------------------------------------
application_t app;
// CODE --------------------------------------------------------------------
static PluginHandle* findFirstUnusedPluginHandle(application_t* app)
{
int i;
assert(app);
for(i = 0; i < MAX_PLUGS; ++i)
{
if(!app->hInstPlug[i])
return &app->hInstPlug[i];
}
return 0;
}
static int loadPlugin(application_t* app, const char* pluginPath, void* paramaters)
{
Library* plugin;
PluginHandle* handle;
void (*initializer)(void);
filename_t name;
assert(app && pluginPath && pluginPath[0]);
plugin = Library_New(pluginPath);
if(!plugin)
{
Con_Message(" loadPlugin: Error loading \"%s\" (%s).\n", pluginPath, Library_LastError());
return 0; // Continue iteration.
}
initializer = Library_Symbol(plugin, "DP_Initialize");
if(!initializer)
{
// Clearly not a Doomsday plugin.
#if _DEBUG
Con_Message(" loadPlugin: \"%s\" does not export entrypoint DP_Initialize, ignoring.\n", pluginPath);
#endif
Library_Delete(plugin);
return 0; // Continue iteration.
}
handle = findFirstUnusedPluginHandle(app);
if(!handle)
{
#if _DEBUG
Con_Message(" loadPlugin: Failed acquiring new handle for \"%s\", ignoring.\n", pluginPath);
#endif
Library_Delete(plugin);
return 0; // Continue iteration.
}
// This seems to be a Doomsday plugin.
_splitpath(pluginPath, NULL, NULL, name, NULL);
Con_Message(" %s\n", name);
*handle = plugin;
initializer();
return 0; // Continue iteration.
}
typedef struct {
boolean loadingGames;
application_t* app;
} loadpluginparamaters_t;
static int loadPluginWorker(const char* pluginPath, void* data)
{
loadpluginparamaters_t* params = (loadpluginparamaters_t*) data;
filename_t name;
// What is the actual file name?
#ifndef MACOSX
_splitpath(pluginPath, NULL, NULL, name, NULL);
if((params->loadingGames && !strncmp(name, "libj", 4)) ||
(!params->loadingGames && !strncmp(name, "libdp", 5)))
#endif
#ifdef MACOSX
_splitpath(pluginPath, NULL, NULL, name, NULL);
if((params->loadingGames && !strncmp(name, "j", 1)) ||
(!params->loadingGames && !strncmp(name, "dp", 2)))
#endif
{
loadPlugin(params->app, pluginPath, NULL/*no paramaters*/);
}
return 0; // Continue search.
}
static boolean unloadPlugin(PluginHandle* handle)
{
int result;
assert(handle);
if(!*handle) return true;
Library_Delete(*handle);
*handle = 0;
return result;
}
/**
* Loads all the plugins from the library directory.
*/
static boolean loadAllPlugins(application_t* app)
{
assert(app);
Con_Message("Initializing plugins...\n");
// Try to load all libraries that begin with libj.
{ loadpluginparamaters_t params;
params.app = app;
params.loadingGames = true;
Library_IterateAvailableLibraries(loadPluginWorker, ¶ms);
}
// Try to load all libraries that begin with libdp.
{ loadpluginparamaters_t params;
params.app = app;
params.loadingGames = false;
Library_IterateAvailableLibraries(loadPluginWorker, ¶ms);
}
return true;
}
static boolean unloadAllPlugins(application_t* app)
{
int i;
assert(app);
for(i = 0; i < MAX_PLUGS && app->hInstPlug[i]; ++i)
{
unloadPlugin(&app->hInstPlug[i]);
}
return true;
}
static int initTimingSystem(void)
{
// For timing, we use SDL under *nix, so get it initialized. SDL_Init() returns zero on success.
return !SDL_Init(SDL_INIT_TIMER);
}
static int initPluginSystem(void)
{
#ifdef DENG_LIBRARY_DIR
// The default directory is defined in the Makefile. For instance, "/usr/local/lib".
Library_AddSearchDir(DENG_LIBRARY_DIR);
#endif
return true;
}
static int initDGL(void)
{
return (int) Sys_GLPreInit();
}
static void determineGlobalPaths(application_t* app)
{
assert(app);
#ifndef MACOSX
if(getenv("HOME"))
{
filename_t homePath;
directory_t* temp;
dd_snprintf(homePath, FILENAME_T_MAXLEN, "%s/.deng/", getenv("HOME"));
temp = Dir_New(homePath);
Dir_mkpath(Dir_Path(temp));
app->usingHomeDir = Dir_SetCurrent(Dir_Path(temp));
if(app->usingHomeDir)
{
strncpy(ddRuntimePath, Dir_Path(temp), FILENAME_T_MAXLEN);
}
Dir_Delete(temp);
}
#endif
// The -userdir option sets the working directory.
if(ArgCheckWith("-userdir", 1))
{
filename_t runtimePath;
directory_t* temp;
strncpy(runtimePath, ArgNext(), FILENAME_T_MAXLEN);
Dir_CleanPath(runtimePath, FILENAME_T_MAXLEN);
// Ensure the path is closed with a directory separator.
F_AppendMissingSlashCString(runtimePath, FILENAME_T_MAXLEN);
temp = Dir_New(runtimePath);
app->usingUserDir = Dir_SetCurrent(Dir_Path(temp));
if(app->usingUserDir)
{
strncpy(ddRuntimePath, Dir_Path(temp), FILENAME_T_MAXLEN);
#ifndef MACOSX
app->usingHomeDir = false;
#endif
}
Dir_Delete(temp);
}
#ifndef MACOSX
if(!app->usingHomeDir && !app->usingUserDir)
#else
if(!app->usingUserDir)
#endif
{
// The current working directory is the runtime dir.
directory_t* temp = Dir_NewFromCWD();
strncpy(ddRuntimePath, Dir_Path(temp), FILENAME_T_MAXLEN);
Dir_Delete(temp);
}
/**
* Determine the base path. Unless overridden on the command line this is
* determined according to the the build configuration.
* Usually this is something like "/usr/share/deng/".
*/
if(ArgCheckWith("-basedir", 1))
{
strncpy(ddBasePath, ArgNext(), FILENAME_T_MAXLEN);
}
else
{
#ifdef MACOSX
strncpy(ddBasePath, "./", FILENAME_T_MAXLEN);
#else
strncpy(ddBasePath, DENG_BASE_DIR, FILENAME_T_MAXLEN);
#endif
}
Dir_CleanPath(ddBasePath, FILENAME_T_MAXLEN);
Dir_MakeAbsolutePath(ddBasePath, FILENAME_T_MAXLEN);
// Ensure it ends with a directory separator.
if(ddBasePath[strlen(ddBasePath)-1] != '/')
strncat(ddBasePath, "/", FILENAME_T_MAXLEN);
}
static char* buildCommandLineString(int argc, char** argv)
{
char* cmdLine;
int i, length = 0;
// Assemble a command line string.
for(i = 0; i < argc; ++i)
{
length += strlen(argv[i]) + 1;
}
// Allocate a large enough string.
cmdLine = M_Malloc(length);
length = 0;
for(i = 0; i < argc; ++i)
{
strcpy(cmdLine + length, argv[i]);
if(i < argc - 1)
strcat(cmdLine, " ");
length += strlen(argv[i]) + 1;
}
return cmdLine;
}
static int createMainWindow(void)
{
Point2Raw origin = { 0, 0 };
Size2Raw size = { 640, 480 };
char buf[256];
DD_ComposeMainWindowTitle(buf);
windowIDX = Sys_CreateWindow(&app, 0, &origin, &size, 32, 0,
isDedicated? WT_CONSOLE : WT_NORMAL, buf, 0);
return windowIDX != 0;
}
int main(int argc, char** argv)
{
boolean doShutdown = true;
int exitCode = 0;
memset(&app, 0, sizeof(app));
/*if(!initApplication(&app))
{
DD_ErrorBox(true, "Failed to initialize application.\n");
}
else*/
{
// Prepare the command line arguments.
char* cmdLine = buildCommandLineString(argc, argv);
DD_InitCommandLine(cmdLine);
M_Free(cmdLine);
// First order of business: are we running in dedicated mode?
isDedicated = ArgCheck("-dedicated");
novideo = ArgCheck("-novideo") || isDedicated;
Library_Init();
// Determine our basedir and other global paths.
determineGlobalPaths(&app);
if(!DD_EarlyInit())
{
DD_ErrorBox(true, "Error during early init.");
}
else if(!initTimingSystem())
{
DD_ErrorBox(true, "Error initalizing timing system.");
}
else if(!initPluginSystem())
{
DD_ErrorBox(true, "Error initializing plugin system.");
}
else if(!initDGL())
{
DD_ErrorBox(true, "Error initializing DGL.");
}
else if(!loadAllPlugins(&app))
{
DD_ErrorBox(true, "Error loading plugins.");
}
else if(!createMainWindow())
{
DD_ErrorBox(true, "Error creating main window.");
}
else if(!Sys_GLInitialize())
{
DD_ErrorBox(true, "Error initializing OpenGL.");
}
else
{ // All initialization complete.
doShutdown = false;
{ char buf[256];
DD_ComposeMainWindowTitle(buf);
Sys_SetWindowTitle(windowIDX, buf);
}
// \todo Set foreground window and focus.
}
}
if(!doShutdown)
{ // Fire up the engine. The game loop will also act as the message pump.
exitCode = DD_Main();
}
DD_Shutdown();
// Bye!
return exitCode;
}
/**
* Shuts down the engine.
*/
void DD_Shutdown(void)
{
// Shutdown all subsystems.
DD_ShutdownAll();
SDL_Quit();
unloadAllPlugins(&app);
Library_Shutdown();
}