1 #include "Application.hpp"
2 #include "Environment.hpp"
3 #include "PreloadState.hpp"
5 #include "WorldState.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];
116 cerr << "unknown option " << arg << endl;
122 for (int j = 1; arg[j] != '\0'; ++j) {
125 config.doublebuf = false;
129 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
130 cerr << "missing argument to -m" << endl;
133 config.multisampling = strtoul(argv[i], nullptr, 10);
138 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
139 cerr << "missing argument to -n" << endl;
142 n = strtoul(argv[i], nullptr, 10);
147 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
148 cerr << "missing argument to -s" << endl;
151 config.world.gen.solid_seed = strtoul(argv[i], nullptr, 10);
152 config.world.gen.type_seed = config.world.gen.solid_seed;
157 if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
158 cerr << "missing argument to -t" << endl;
161 t = strtoul(argv[i], nullptr, 10);
169 cerr << "unknown option " << arg[j] << endl;
176 cerr << "unable to interpret argument "
177 << i << " (" << arg << ")" << endl;
187 if (config.asset_path.empty()) {
188 config.asset_path = default_asset_path();
190 config.asset_path[config.asset_path.size() - 1] != '/' &&
191 config.asset_path[config.asset_path.size() - 1] != '\\'
193 config.asset_path += '/';
195 if (config.save_path.empty()) {
196 config.save_path = default_save_path();
198 config.save_path[config.save_path.size() - 1] != '/' &&
199 config.save_path[config.save_path.size() - 1] != '\\'
201 config.save_path += '/';
206 mode = FIXED_FRAME_LIMIT;
217 int Runtime::Execute() {
222 Init init(config.doublebuf, config.multisampling);
224 Environment env(init.window, config.asset_path);
225 env.viewport.VSync(config.vsync);
227 Application app(env);
229 WorldState world_state(env, config.interface, config.world);
230 app.PushState(&world_state);
232 PreloadState preloader(env, world_state.GetWorld().Loader());
233 app.PushState(&preloader);
246 case FIXED_FRAME_LIMIT: