/
main.cpp
121 lines (108 loc) · 3.41 KB
/
main.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
#include <cstdlib>
#include <vector>
#include <string>
#include <SDL.h>
#include "version.hpp"
#include "Log.hpp"
#include "Config.hpp"
#include "system/system.hpp"
#include "io/filesystem.hpp"
#include "graphics/video.hpp"
#include "sound/audio.hpp"
#include "input/input.hpp"
#include "script/script.hpp"
//-----------------------------------------------------------------
int main(int argc, char* argv[])
{
Log log;
log.open("log.txt");
// print sphere version
log.info() << "Sphere Engine " << SPHERE_VERSION_STRING << " " << SPHERE_VERSION_AFFIX;
// initialize filesystem
log.info() << "Initialize filesystem";
if (!InitFilesystem(log)) {
log.error() << "Could not initialize filesystem";
return 0;
}
atexit(DeinitFilesystem);
// initialize system
log.info() << "Initialize system";
if (!InitSystem(log)) {
log.error() << "Could not initialize system";
return 0;
}
atexit(DeinitSystem);
// initialize video
log.info() << "Initialize video";
if (!InitVideo(log)) {
log.error() << "Could not initialize video";
return 0;
}
atexit(DeinitVideo);
// initialize audio
log.info() << "Initialize audio";
if (!InitAudio(log)) {
log.error() << "Could not initialize audio";
return 0;
}
atexit(DeinitAudio);
// initialize input
log.info() << "Initialize input";
if (!InitInput(log)) {
log.error() << "Could not initialize input";
return 0;
}
atexit(DeinitInput);
// initialize script
log.info() << "Initialize script";
if (!InitScript(log)) {
log.error() << "Could not initialize script";
return 0;
}
atexit(DeinitScript);
// load configuration
log.info() << "Load configuration";
Config config("engine.cfg");
// process command line options
log.info() << "Process command line options";
for (int i = 0; i < argc; i++) {
std::string arg = argv[i];
if (arg == "-common" && i + 1 < argc) {
config.CommonPath = argv[i + 1];
i++;
} else if (arg == "-data" && i + 1 < argc) {
config.DataPath = argv[i + 1];
i++;
} else if (arg == "-startup" && i + 1 < argc) {
config.StartupScript = argv[i + 1];
i++;
} else if (arg == "-arg" && i + 1 < argc) {
config.GameArgs.push_back(argv[i + 1]);
i++;
}
}
// set paths and enter game directory
SetCommonPath(config.CommonPath);
SetDataPath(config.DataPath);
log.info() << "Enter game directory";
if (!SetCurrentPath(config.DataPath)) {
log.error() << "Could not enter game directory '" << config.DataPath << "'";
return 0;
}
// run game
log.info() << "Run game";
try {
if (DoesFileExist(config.StartupScript + ".bytecode")) {
RunGame(log, config.StartupScript + ".bytecode", config.GameArgs);
} else if (DoesFileExist(config.StartupScript + ".script")) {
RunGame(log, config.StartupScript + ".script", config.GameArgs);
} else {
log.error() << "Startup script '" << config.StartupScript << "' does not exist";
}
} catch (const std::exception& e) {
log.error() << "Exception caught: " << (e.what() ? e.what() : "N/A");
} catch (...) {
log.error() << "Unknown exception caught";
}
return 0;
}