1 #include "Application.hpp"
2 #include "Environment.hpp"
4 #include "WorldState.hpp"
7 #include "../world/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)
56 Runtime::Runtime() noexcept
66 void Runtime::ReadArgs(int argc, const char *const *argv) {
67 if (argc <= 0) return;
73 for (int i = 1; i < argc; ++i) {
74 const char *arg = argv[i];
75 if (!arg || arg[0] == '\0') {
76 cerr << "warning: found empty argument at position " << i << endl;
79 if (options && arg[0] == '-') {
81 cerr << "warning: incomplete option list at position " << i << endl;
82 } else if (arg[1] == '-') {
87 const char *param = arg + 2;
89 if (strcmp(param, "no-vsync") == 0) {
91 } else if (strcmp(param, "no-keyboard") == 0) {
92 config.interface.keyboard_disabled = true;
93 } else if (strcmp(param, "no-mouse") == 0) {
94 config.interface.mouse_disabled = true;
95 } else if (strcmp(param, "no-hud") == 0) {
96 config.interface.visual_disabled = true;
97 } else if (strcmp(param, "no-audio") == 0) {
98 config.interface.audio_disabled = true;
99 } else if (strcmp(param, "asset-path") == 0) {
101 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
102 cerr << "missing argument to --asset-path" << endl;
105 config.asset_path = argv[i];
107 } else if (strcmp(param, "save-path") == 0) {
109 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
110 cerr << "missing argument to --save-path" << endl;
113 config.save_path = argv[i];
115 } else if (strcmp(param, "world-name") == 0) {
117 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
118 cerr << "missing argument to --world-name" << endl;
121 config.world_name = argv[i];
124 cerr << "unknown option " << arg << endl;
130 for (int j = 1; arg[j] != '\0'; ++j) {
133 config.doublebuf = false;
137 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
138 cerr << "missing argument to -m" << endl;
141 config.multisampling = strtoul(argv[i], nullptr, 10);
146 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
147 cerr << "missing argument to -n" << endl;
150 n = strtoul(argv[i], nullptr, 10);
155 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
156 cerr << "missing argument to -s" << endl;
159 config.world.gen.seed = strtoul(argv[i], nullptr, 10);
164 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
165 cerr << "missing argument to -t" << endl;
168 t = strtoul(argv[i], nullptr, 10);
176 cerr << "unknown option " << arg[j] << endl;
183 cerr << "unable to interpret argument "
184 << i << " (" << arg << ")" << endl;
194 if (config.asset_path.empty()) {
195 config.asset_path = default_asset_path();
197 config.asset_path[config.asset_path.size() - 1] != '/' &&
198 config.asset_path[config.asset_path.size() - 1] != '\\'
200 config.asset_path += '/';
202 if (config.save_path.empty()) {
203 config.save_path = default_save_path();
205 config.save_path[config.save_path.size() - 1] != '/' &&
206 config.save_path[config.save_path.size() - 1] != '\\'
208 config.save_path += '/';
213 mode = FIXED_FRAME_LIMIT;
224 int Runtime::Execute() {
229 Init init(config.doublebuf, config.multisampling);
231 Environment env(init.window, config.asset_path);
232 env.viewport.VSync(config.vsync);
234 WorldSave save(config.save_path + config.world_name + '/');
236 save.Read(config.world);
238 save.Write(config.world);
241 Application app(env);
243 WorldState world_state(env, config.interface, config.world, save);
244 app.PushState(&world_state);
257 case FIXED_FRAME_LIMIT: