1 #include "Application.hpp"
2 #include "Environment.hpp"
4 #include "WorldState.hpp"
7 #include "../io/WorldSave.hpp"
19 string default_asset_path() {
20 char *base = SDL_GetBasePath();
27 string default_save_path() {
29 char *base = SDL_GetBasePath();
35 char *pref = SDL_GetPrefPath("localhorst", "blank");
46 Environment::Environment(Window &win, const string &asset_path)
57 Runtime::Runtime() noexcept
67 void Runtime::ReadArgs(int argc, const char *const *argv) {
68 if (argc <= 0) return;
74 for (int i = 1; i < argc; ++i) {
75 const char *arg = argv[i];
76 if (!arg || arg[0] == '\0') {
77 cerr << "warning: found empty argument at position " << i << endl;
80 if (options && arg[0] == '-') {
82 cerr << "warning: incomplete option list at position " << i << endl;
83 } else if (arg[1] == '-') {
88 const char *param = arg + 2;
90 if (strcmp(param, "no-vsync") == 0) {
92 } else if (strcmp(param, "no-keyboard") == 0) {
93 config.interface.keyboard_disabled = true;
94 } else if (strcmp(param, "no-mouse") == 0) {
95 config.interface.mouse_disabled = true;
96 } else if (strcmp(param, "no-hud") == 0) {
97 config.interface.visual_disabled = true;
98 } else if (strcmp(param, "no-audio") == 0) {
99 config.interface.audio_disabled = true;
100 } else if (strcmp(param, "asset-path") == 0) {
102 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
103 cerr << "missing argument to --asset-path" << endl;
106 config.asset_path = argv[i];
108 } else if (strcmp(param, "save-path") == 0) {
110 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
111 cerr << "missing argument to --save-path" << endl;
114 config.save_path = argv[i];
116 } else if (strcmp(param, "world-name") == 0) {
118 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
119 cerr << "missing argument to --world-name" << endl;
122 config.world_name = argv[i];
125 cerr << "unknown option " << arg << endl;
131 for (int j = 1; arg[j] != '\0'; ++j) {
134 config.doublebuf = false;
138 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
139 cerr << "missing argument to -m" << endl;
142 config.multisampling = strtoul(argv[i], nullptr, 10);
147 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
148 cerr << "missing argument to -n" << endl;
151 n = strtoul(argv[i], nullptr, 10);
156 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
157 cerr << "missing argument to -s" << endl;
160 config.world.gen.seed = strtoul(argv[i], nullptr, 10);
165 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
166 cerr << "missing argument to -t" << endl;
169 t = strtoul(argv[i], nullptr, 10);
177 cerr << "unknown option " << arg[j] << endl;
184 cerr << "unable to interpret argument "
185 << i << " (" << arg << ")" << endl;
195 if (config.asset_path.empty()) {
196 config.asset_path = default_asset_path();
198 config.asset_path[config.asset_path.size() - 1] != '/' &&
199 config.asset_path[config.asset_path.size() - 1] != '\\'
201 config.asset_path += '/';
203 if (config.save_path.empty()) {
204 config.save_path = default_save_path();
206 config.save_path[config.save_path.size() - 1] != '/' &&
207 config.save_path[config.save_path.size() - 1] != '\\'
209 config.save_path += '/';
214 mode = FIXED_FRAME_LIMIT;
225 int Runtime::Execute() {
230 Init init(config.doublebuf, config.multisampling);
232 Environment env(init.window, config.asset_path);
233 env.viewport.VSync(config.vsync);
235 WorldSave save(config.save_path + config.world_name + '/');
237 save.Read(config.world);
239 save.Write(config.world);
242 Application app(env);
244 WorldState world_state(env, config.interface, config.world, save);
245 app.PushState(&world_state);
258 case FIXED_FRAME_LIMIT: