]> git.localhorst.tv Git - blank.git/blobdiff - src/app/runtime.cpp
split chunk redering from world model
[blank.git] / src / app / runtime.cpp
index 37cca50a4ef54229861245df31ba861c8ea6b3d0..d77cf47c228fbbdbc573f1e5e26f2473f2ab00ed 100644 (file)
@@ -4,9 +4,12 @@
 #include "WorldState.hpp"
 
 #include "init.hpp"
+#include "../io/filesystem.hpp"
+#include "../io/WorldSave.hpp"
 
 #include <cctype>
 #include <cstdlib>
+#include <fstream>
 #include <iostream>
 #include <SDL.h>
 
@@ -15,7 +18,7 @@ using namespace std;
 
 namespace {
 
-string get_asset_path() {
+string default_asset_path() {
        char *base = SDL_GetBasePath();
        string assets(base);
        assets += "assets/";
@@ -23,17 +26,34 @@ string get_asset_path() {
        return assets;
 }
 
+string default_save_path() {
+#ifndef NDEBUG
+       char *base = SDL_GetBasePath();
+       string save(base);
+       save += "saves/";
+       SDL_free(base);
+       return save;
+#else
+       char *pref = SDL_GetPrefPath("localhorst", "blank");
+       string save(pref);
+       SDL_free(pref);
+       return save;
+#endif
+}
+
 }
 
 namespace blank {
 
-Environment::Environment(Window &win)
+Environment::Environment(Window &win, const string &asset_path)
 : audio()
 , viewport()
 , window(win)
-, assets(get_asset_path())
+, assets(asset_path)
 , counter() {
-
+       viewport.Clear();
+       window.Flip();
+       keymap.LoadDefault();
 }
 
 
@@ -68,17 +88,42 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                        // stopper
                                        options = false;
                                } else {
+                                       const char *param = arg + 2;
                                        // long option
-                                       if (strcmp(arg + 2, "no-vsync") == 0) {
+                                       if (strcmp(param, "no-vsync") == 0) {
                                                config.vsync = false;
-                                       } else if (strcmp(arg + 2, "no-keyboard") == 0) {
+                                       } else if (strcmp(param, "no-keyboard") == 0) {
                                                config.interface.keyboard_disabled = true;
-                                       } else if (strcmp(arg + 2, "no-mouse") == 0) {
+                                       } else if (strcmp(param, "no-mouse") == 0) {
                                                config.interface.mouse_disabled = true;
-                                       } else if (strcmp(arg + 2, "no-hud") == 0) {
+                                       } else if (strcmp(param, "no-hud") == 0) {
                                                config.interface.visual_disabled = true;
-                                       } else if (strcmp(arg + 2, "no-audio") == 0) {
+                                       } else if (strcmp(param, "no-audio") == 0) {
                                                config.interface.audio_disabled = true;
+                                       } else if (strcmp(param, "asset-path") == 0) {
+                                               ++i;
+                                               if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                       cerr << "missing argument to --asset-path" << endl;
+                                                       error = true;
+                                               } else {
+                                                       config.asset_path = argv[i];
+                                               }
+                                       } else if (strcmp(param, "save-path") == 0) {
+                                               ++i;
+                                               if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                       cerr << "missing argument to --save-path" << endl;
+                                                       error = true;
+                                               } else {
+                                                       config.save_path = argv[i];
+                                               }
+                                       } else if (strcmp(param, "world-name") == 0) {
+                                               ++i;
+                                               if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                       cerr << "missing argument to --world-name" << endl;
+                                                       error = true;
+                                               } else {
+                                                       config.world_name = argv[i];
+                                               }
                                        } else {
                                                cerr << "unknown option " << arg << endl;
                                                error = true;
@@ -115,8 +160,7 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                                                cerr << "missing argument to -s" << endl;
                                                                error = true;
                                                        } else {
-                                                               config.world.gen.solid_seed = strtoul(argv[i], nullptr, 10);
-                                                               config.world.gen.type_seed = config.world.gen.solid_seed;
+                                                               config.world.gen.seed = strtoul(argv[i], nullptr, 10);
                                                        }
                                                        break;
                                                case 't':
@@ -151,6 +195,23 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                return;
        }
 
+       if (config.asset_path.empty()) {
+               config.asset_path = default_asset_path();
+       } else if (
+               config.asset_path[config.asset_path.size() - 1] != '/' &&
+               config.asset_path[config.asset_path.size() - 1] != '\\'
+       ) {
+               config.asset_path += '/';
+       }
+       if (config.save_path.empty()) {
+               config.save_path = default_save_path();
+       } else if (
+               config.save_path[config.save_path.size() - 1] != '/' &&
+               config.save_path[config.save_path.size() - 1] != '\\'
+       ) {
+               config.save_path += '/';
+       }
+
        if (n > 0) {
                if (t > 0) {
                        mode = FIXED_FRAME_LIMIT;
@@ -171,13 +232,30 @@ int Runtime::Execute() {
 
        Init init(config.doublebuf, config.multisampling);
 
-       Environment env(init.window);
+       Environment env(init.window, config.asset_path);
        env.viewport.VSync(config.vsync);
 
+       std::string keys_path = config.save_path + "keys.conf";
+       if (!is_file(keys_path)) {
+               std::ofstream file(keys_path);
+               env.keymap.Save(file);
+       } else {
+               std::ifstream file(keys_path);
+               env.keymap.Load(file);
+       }
+
+
+       WorldSave save(config.save_path + config.world_name + '/');
+       if (save.Exists()) {
+               save.Read(config.world);
+       } else {
+               save.Write(config.world);
+       }
+
        Application app(env);
 
-       WorldState state(env, config.interface, config.world);
-       app.PushState(&state);
+       WorldState world_state(env, config.interface, config.world, save);
+       app.PushState(&world_state);
 
        switch (mode) {
                default: