]> git.localhorst.tv Git - blank.git/commitdiff
get world seed from command line arguments
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 19 Mar 2015 09:44:44 +0000 (10:44 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 19 Mar 2015 09:45:37 +0000 (10:45 +0100)
running
src/app.cpp
src/app.hpp
src/main.cpp
src/runtime.cpp [new file with mode: 0644]
src/runtime.hpp [new file with mode: 0644]
src/world.cpp
src/world.hpp

diff --git a/running b/running
index 62a3cf67b3220c768008bb257b4423d182dcf78f..bfea07c596bda754af0d2acee88c2117b56f1d4a 100644 (file)
--- a/running
+++ b/running
@@ -4,7 +4,7 @@ Arguments
 blank
        normal execution
 
-blank <n>
+blank [-n] <n>
        terminate after <n> frames
 
 blank -t <t>
@@ -13,6 +13,9 @@ blank -t <t>
 blank <n> -t <t>
        terminate after n frames, assume <t> milliseconds pass each frame
 
+blank -s <seed>
+       use <seed> (unsigned integer) as the world seed. default is 0
+
 
 Controls
 ========
index cfb16822897a01027da39d62c3624da6fe7fd52b..b7e71ec681387512db2dc897a4f6063bce6f0d9a 100644 (file)
@@ -6,7 +6,7 @@
 
 namespace blank {
 
-Application::Application()
+Application::Application(unsigned int seed)
 : init_sdl()
 , init_img()
 , init_gl()
@@ -15,7 +15,7 @@ Application::Application()
 , init_glew()
 , program()
 , cam()
-, world()
+, world(seed)
 , interface(world)
 , test_controller(MakeTestEntity(world))
 , running(false) {
index 413b6f19869430f22b665d690d4d8a67cd0ff50d..c8a6fb56720e6950fb0a7bc10097cdcbdf47e62e 100644 (file)
@@ -14,7 +14,7 @@ namespace blank {
 class Application {
 
 public:
-       Application();
+       explicit Application(unsigned int seed);
 
        Application(const Application &) = delete;
        Application &operator =(const Application &) = delete;
index f9bc154739fa57a0045f8e6bbd991782367cc9d7..3cf7bf94c97d26d358b0ee97f90d07fc97a16f5a 100644 (file)
@@ -1,86 +1,9 @@
-#include "app.hpp"
-
-#include <cctype>
-#include <cstdlib>
-#include <iostream>
+#include "runtime.hpp"
 
 using namespace blank;
 
-
-namespace {
-
-enum Mode {
-       NORMAL,
-       FRAME_LIMIT,
-       TIME_LIMIT,
-       FIXED_FRAME_LIMIT,
-};
-
-}
-
-
 int main(int argc, char *argv[]) {
-
-       Mode mode = NORMAL;
-       size_t n = 0, t = 0;
-
-       bool error = false;
-       for (int i = 1; i < argc; ++i) {
-               if (argv[i] == nullptr || argv[i][0] == '\0') continue;
-               if (argv[i][0] == '-') {
-                       if (argv[i][1] == 't' && argv[i][2] == '\0') {
-                               ++i;
-                               if (i >= argc) {
-                                       std::cerr << "missing argument to -t" << std::endl;
-                                       error = true;
-                               } else {
-                                       t = std::strtoul(argv[i], nullptr, 10);
-                               }
-                       } else {
-                               std::cerr << "unable to interpret argument "
-                                       << i << " (" << argv[i] << ")" << std::endl;
-                               error = true;
-                       }
-               } else if (std::isdigit(*argv[i])) {
-                       n = std::strtoul(argv[i], nullptr, 10);
-               } else {
-                       std::cerr << "unable to interpret argument "
-                               << i << " (" << argv[i] << ")" << std::endl;
-                       error = true;
-               }
-       }
-
-       if (error) {
-               return 1;
-       }
-
-       if (n > 0) {
-               if (t > 0) {
-                       mode = FIXED_FRAME_LIMIT;
-               } else {
-                       mode = FRAME_LIMIT;
-               }
-       } else if (t > 0) {
-               mode = TIME_LIMIT;
-       }
-
-       Application app;
-       switch (mode) {
-               default:
-               case NORMAL:
-                       app.Run();
-                       break;
-               case FRAME_LIMIT:
-                       app.RunN(n);
-                       break;
-               case TIME_LIMIT:
-                       app.RunT(t);
-                       break;
-               case FIXED_FRAME_LIMIT:
-                       app.RunS(n, t);
-                       break;
-       }
-
-       return 0;
-
+       Runtime rt;
+       rt.ReadArgs(argc, argv);
+       return rt.Execute();
 }
diff --git a/src/runtime.cpp b/src/runtime.cpp
new file mode 100644 (file)
index 0000000..9fb644c
--- /dev/null
@@ -0,0 +1,143 @@
+#include "runtime.hpp"
+
+#include "app.hpp"
+
+#include <cctype>
+#include <cstdlib>
+#include <iostream>
+
+using namespace std;
+
+
+namespace blank {
+
+Runtime::Runtime()
+: name("blank")
+, mode(NORMAL)
+, n(0)
+, t(0)
+, seed(0) {
+
+}
+
+
+void Runtime::ReadArgs(int argc, const char *const *argv) {
+       if (argc <= 0) return;
+       name = argv[0];
+
+       bool options = true;
+       bool error = false;
+
+       for (int i = 1; i < argc; ++i) {
+               const char *arg = argv[i];
+               if (!arg || arg[0] == '\0') {
+                       cerr << "warning: found empty argument at position " << i << endl;
+                       continue;
+               }
+               if (options && arg[0] == '-') {
+                       if (arg[1] == '\0') {
+                               cerr << "warning: incomplete option list at position " << i << endl;
+                       } else if (arg[1] == '-') {
+                               if (arg[2] == '\0') {
+                                       // stopper
+                                       options = false;
+                               } else {
+                                       // long option
+                                       cerr << "unknown option " << arg << endl;
+                                       error = true;
+                               }
+                       } else {
+                               // short options
+                               for (int j = 1; arg[j] != '\0'; ++j) {
+                                       switch (arg[j]) {
+                                               case 'n':
+                                                       ++i;
+                                                       if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                               cerr << "missing argument to -n" << endl;
+                                                               error = true;
+                                                       } else {
+                                                               n = strtoul(argv[i], nullptr, 10);
+                                                       }
+                                                       break;
+                                               case 's':
+                                                       ++i;
+                                                       if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                               cerr << "missing argument to -s" << endl;
+                                                               error = true;
+                                                       } else {
+                                                               seed = strtoul(argv[i], nullptr, 10);
+                                                       }
+                                                       break;
+                                               case 't':
+                                                       ++i;
+                                                       if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                               cerr << "missing argument to -t" << endl;
+                                                               error = true;
+                                                       } else {
+                                                               t = strtoul(argv[i], nullptr, 10);
+                                                       }
+                                                       break;
+                                               case '-':
+                                                       // stopper
+                                                       options = false;
+                                                       break;
+                                               default:
+                                                       cerr << "unknown option " << arg[j] << endl;
+                                                       error = true;
+                                                       break;
+                                       }
+                               }
+                       }
+               } else if (isdigit(arg[0])) {
+                       // positional number interpreted as -n
+                       n = strtoul(arg, nullptr, 10);
+               } else {
+                       cerr << "unable to interpret argument "
+                               << i << " (" << arg << ")" << endl;
+                       error = true;
+               }
+       }
+
+       if (error) {
+               mode = ERROR;
+               return;
+       }
+
+       if (n > 0) {
+               if (t > 0) {
+                       mode = FIXED_FRAME_LIMIT;
+               } else {
+                       mode = FRAME_LIMIT;
+               }
+       } else if (t > 0) {
+               mode = TIME_LIMIT;
+       } else {
+               mode = NORMAL;
+       }
+}
+
+int Runtime::Execute() {
+       if (mode == ERROR) {
+               return 1;
+       }
+
+       Application app(seed);
+       switch (mode) {
+               default:
+               case NORMAL:
+                       app.Run();
+                       break;
+               case FRAME_LIMIT:
+                       app.RunN(n);
+                       break;
+               case TIME_LIMIT:
+                       app.RunT(t);
+                       break;
+               case FIXED_FRAME_LIMIT:
+                       app.RunS(n, t);
+                       break;
+       }
+       return 0;
+}
+
+}
diff --git a/src/runtime.hpp b/src/runtime.hpp
new file mode 100644 (file)
index 0000000..01f6cdd
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef BLANK_RUNTIME_HPP_
+#define BLANK_RUNTIME_HPP_
+
+#include <cstddef>
+
+
+namespace blank {
+
+class Runtime {
+
+public:
+       enum Mode {
+               NORMAL,
+               FRAME_LIMIT,
+               TIME_LIMIT,
+               FIXED_FRAME_LIMIT,
+               ERROR,
+       };
+
+       Runtime();
+
+       void ReadArgs(int argc, const char *const *argv);
+
+       int Execute();
+
+private:
+       const char *name;
+       Mode mode;
+       std::size_t n;
+       std::size_t t;
+       unsigned int seed;
+
+};
+
+}
+
+#endif
index e4e382642be47a7b225d1abc33ad90a0d1889501..40dd6bdfc578ffaa22af252f3b242f7978ea8f87 100644 (file)
@@ -6,12 +6,12 @@
 
 namespace blank {
 
-World::World()
+World::World(unsigned int seed)
 : blockType()
 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
-, generate(0)
+, generate(seed)
 , chunks(blockType, generate)
 , player() {
        BlockType::Faces block_fill = {  true,  true,  true,  true,  true,  true };
index b5c4a29cc66688f9c7a10b0a9150c71122c37718..bca1e0c082e190aadfe147abd2efcd7b852b69d5 100644 (file)
@@ -17,7 +17,7 @@ namespace blank {
 class World {
 
 public:
-       World();
+       explicit World(unsigned int seed);
 
        bool Intersection(
                const Ray &,