1 #include "Application.hpp"
2 #include "Environment.hpp"
4 #include "WorldState.hpp"
7 #include "../io/filesystem.hpp"
8 #include "../io/WorldSave.hpp"
21 string default_asset_path() {
22 char *base = SDL_GetBasePath();
29 string default_save_path() {
31 char *base = SDL_GetBasePath();
37 char *pref = SDL_GetPrefPath("localhorst", "blank");
48 Environment::Environment(Window &win, const string &asset_path)
60 Runtime::Runtime() noexcept
70 void Runtime::ReadArgs(int argc, const char *const *argv) {
71 if (argc <= 0) return;
77 for (int i = 1; i < argc; ++i) {
78 const char *arg = argv[i];
79 if (!arg || arg[0] == '\0') {
80 cerr << "warning: found empty argument at position " << i << endl;
83 if (options && arg[0] == '-') {
85 cerr << "warning: incomplete option list at position " << i << endl;
86 } else if (arg[1] == '-') {
91 const char *param = arg + 2;
93 if (strcmp(param, "no-vsync") == 0) {
95 } else if (strcmp(param, "no-keyboard") == 0) {
96 config.interface.keyboard_disabled = true;
97 } else if (strcmp(param, "no-mouse") == 0) {
98 config.interface.mouse_disabled = true;
99 } else if (strcmp(param, "no-hud") == 0) {
100 config.interface.visual_disabled = true;
101 } else if (strcmp(param, "no-audio") == 0) {
102 config.interface.audio_disabled = true;
103 } else if (strcmp(param, "asset-path") == 0) {
105 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
106 cerr << "missing argument to --asset-path" << endl;
109 config.asset_path = argv[i];
111 } else if (strcmp(param, "save-path") == 0) {
113 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
114 cerr << "missing argument to --save-path" << endl;
117 config.save_path = argv[i];
119 } else if (strcmp(param, "world-name") == 0) {
121 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
122 cerr << "missing argument to --world-name" << endl;
125 config.world_name = argv[i];
128 cerr << "unknown option " << arg << endl;
134 for (int j = 1; arg[j] != '\0'; ++j) {
137 config.doublebuf = false;
141 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
142 cerr << "missing argument to -m" << endl;
145 config.multisampling = strtoul(argv[i], nullptr, 10);
150 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
151 cerr << "missing argument to -n" << endl;
154 n = strtoul(argv[i], nullptr, 10);
159 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
160 cerr << "missing argument to -s" << endl;
163 config.world.gen.seed = strtoul(argv[i], nullptr, 10);
168 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
169 cerr << "missing argument to -t" << endl;
172 t = strtoul(argv[i], nullptr, 10);
180 cerr << "unknown option " << arg[j] << endl;
187 cerr << "unable to interpret argument "
188 << i << " (" << arg << ")" << endl;
198 if (config.asset_path.empty()) {
199 config.asset_path = default_asset_path();
201 config.asset_path[config.asset_path.size() - 1] != '/' &&
202 config.asset_path[config.asset_path.size() - 1] != '\\'
204 config.asset_path += '/';
206 if (config.save_path.empty()) {
207 config.save_path = default_save_path();
209 config.save_path[config.save_path.size() - 1] != '/' &&
210 config.save_path[config.save_path.size() - 1] != '\\'
212 config.save_path += '/';
217 mode = FIXED_FRAME_LIMIT;
228 int Runtime::Execute() {
233 Init init(config.doublebuf, config.multisampling);
235 Environment env(init.window, config.asset_path);
236 env.viewport.VSync(config.vsync);
238 std::string keys_path = config.save_path + "keys.conf";
239 if (!is_file(keys_path)) {
240 std::ofstream file(keys_path);
241 env.keymap.Save(file);
243 std::ifstream file(keys_path);
244 env.keymap.Load(file);
248 WorldSave save(config.save_path + config.world_name + '/');
250 save.Read(config.world);
252 save.Write(config.world);
255 Application app(env);
257 WorldState world_state(env, config.interface, config.world, save);
258 app.PushState(&world_state);
271 case FIXED_FRAME_LIMIT: