From bd6bd2c875f4b6baef913e5315aa9f7e7cd7da7a Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 19 Mar 2015 10:44:44 +0100 Subject: [PATCH] get world seed from command line arguments --- running | 5 +- src/app.cpp | 4 +- src/app.hpp | 2 +- src/main.cpp | 85 ++-------------------------- src/runtime.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++++++++ src/runtime.hpp | 37 +++++++++++++ src/world.cpp | 4 +- src/world.hpp | 2 +- 8 files changed, 194 insertions(+), 88 deletions(-) create mode 100644 src/runtime.cpp create mode 100644 src/runtime.hpp diff --git a/running b/running index 62a3cf6..bfea07c 100644 --- a/running +++ b/running @@ -4,7 +4,7 @@ Arguments blank normal execution -blank +blank [-n] terminate after frames blank -t @@ -13,6 +13,9 @@ blank -t blank -t terminate after n frames, assume milliseconds pass each frame +blank -s + use (unsigned integer) as the world seed. default is 0 + Controls ======== diff --git a/src/app.cpp b/src/app.cpp index cfb1682..b7e71ec 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -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) { diff --git a/src/app.hpp b/src/app.hpp index 413b6f1..c8a6fb5 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -14,7 +14,7 @@ namespace blank { class Application { public: - Application(); + explicit Application(unsigned int seed); Application(const Application &) = delete; Application &operator =(const Application &) = delete; diff --git a/src/main.cpp b/src/main.cpp index f9bc154..3cf7bf9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,86 +1,9 @@ -#include "app.hpp" - -#include -#include -#include +#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 index 0000000..9fb644c --- /dev/null +++ b/src/runtime.cpp @@ -0,0 +1,143 @@ +#include "runtime.hpp" + +#include "app.hpp" + +#include +#include +#include + +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 index 0000000..01f6cdd --- /dev/null +++ b/src/runtime.hpp @@ -0,0 +1,37 @@ +#ifndef BLANK_RUNTIME_HPP_ +#define BLANK_RUNTIME_HPP_ + +#include + + +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 diff --git a/src/world.cpp b/src/world.cpp index e4e3826..40dd6bd 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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 }; diff --git a/src/world.hpp b/src/world.hpp index b5c4a29..bca1e0c 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -17,7 +17,7 @@ namespace blank { class World { public: - World(); + explicit World(unsigned int seed); bool Intersection( const Ray &, -- 2.39.2