]> git.localhorst.tv Git - blank.git/commitdiff
extracted configuration of various parts
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 19 Mar 2015 13:10:36 +0000 (14:10 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 19 Mar 2015 16:47:58 +0000 (17:47 +0100)
15 files changed:
running
src/app.cpp
src/app.hpp
src/chunk.cpp
src/chunk.hpp
src/generator.cpp
src/generator.hpp
src/init.cpp
src/init.hpp
src/interface.cpp
src/interface.hpp
src/runtime.cpp
src/runtime.hpp
src/world.cpp
src/world.hpp

diff --git a/running b/running
index bfea07c596bda754af0d2acee88c2117b56f1d4a..be2100994745f85e2241d27b2e80e39f767fd770 100644 (file)
--- a/running
+++ b/running
@@ -1,18 +1,45 @@
 Arguments
 =========
 
-blank
-       normal execution
+Runtime
+-------
 
-blank [-n] <n>
+[-n] <n>
        terminate after <n> frames
 
-blank -t <t>
+-t <t>
        terminate after <t> milliseconds
 
-blank <n> -t <t>
+[-n] <n> -t <t>
        terminate after n frames, assume <t> milliseconds pass each frame
 
+Application
+-----------
+
+-d
+       disable double buffering
+
+-m <num>
+       set sample size to <num> (samples per pixel)
+
+--no-vsync
+       disable vsync
+
+Interface
+---------
+
+--no-keyboard
+       disable keyboard input handling
+
+--no-mouse
+       disable mouse input handling
+
+--no-hud
+       disable HUD drawing (includes the selected block outline)
+
+World
+-----
+
 blank -s <seed>
        use <seed> (unsigned integer) as the world seed. default is 0
 
index b7e71ec681387512db2dc897a4f6063bce6f0d9a..a6f81f1210ecb677e0043341792ab8a0f9dba8ae 100644 (file)
@@ -6,20 +6,22 @@
 
 namespace blank {
 
-Application::Application(unsigned int seed)
+Application::Application(const Config &config)
 : init_sdl()
 , init_img()
-, init_gl()
+, init_gl(config.doublebuf, config.multisampling)
 , window()
 , ctx(window.CreateContext())
 , init_glew()
 , program()
 , cam()
-, world(seed)
-, interface(world)
+, world(config.world)
+, interface(config.interface, world)
 , test_controller(MakeTestEntity(world))
 , running(false) {
-       GLContext::EnableVSync();
+       if (config.vsync) {
+               GLContext::EnableVSync();
+       }
 
        glClearColor(0.0, 0.0, 0.0, 1.0);
 }
index c8a6fb56720e6950fb0a7bc10097cdcbdf47e62e..23dcd0cef9cf7b3a21bf143f6204d5c836dc5641 100644 (file)
@@ -14,7 +14,16 @@ namespace blank {
 class Application {
 
 public:
-       explicit Application(unsigned int seed);
+       struct Config {
+               bool vsync = true;
+               bool doublebuf = true;
+               int multisampling = 1;
+
+               Interface::Config interface = Interface::Config();
+               World::Config world = World::Config();
+       };
+
+       explicit Application(const Config &);
 
        Application(const Application &) = delete;
        Application &operator =(const Application &) = delete;
index 66b049f00366e1ea4a91cb2f0f69d7b200c3d48b..70a2563ae12c490379959bd37a35fc955db69996 100644 (file)
@@ -470,15 +470,15 @@ glm::mat4 Chunk::ToTransform(int idx) const {
 }
 
 
-ChunkLoader::ChunkLoader(const BlockTypeRegistry &reg, const Generator &gen)
+ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry &reg, const Generator &gen)
 : base(0, 0, 0)
 , reg(reg)
 , gen(gen)
 , loaded()
 , to_generate()
 , to_free()
-, load_dist(6)
-, unload_dist(8) {
+, load_dist(config.load_dist)
+, unload_dist(config.unload_dist) {
 
 }
 
index d8408ec9062e34bbb4f5344c4cb9df79eef0dd5b..840bac0f3caf312574ebb888998f5aeae29ea4ea 100644 (file)
@@ -158,7 +158,12 @@ class Generator;
 class ChunkLoader {
 
 public:
-       ChunkLoader(const BlockTypeRegistry &, const Generator &);
+       struct Config {
+               int load_dist = 6;
+               int unload_dist = 8;
+       };
+
+       ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &);
 
        void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
        void GenerateSurrounding(const Chunk::Pos &);
index c1a7a0801198de34e3a4b8b3d0bdddd70f334ba4..43d932cceae421473bc391820370c36dccc1fac7 100644 (file)
@@ -5,11 +5,11 @@
 
 namespace blank {
 
-Generator::Generator(unsigned int seed)
-: solidNoise(seed)
-, typeNoise(seed + 1)
-, stretch(64.0f)
-, solid_threshold(0.8f)
+Generator::Generator(const Config &config)
+: solidNoise(config.solid_seed)
+, typeNoise(config.type_seed)
+, stretch(config.stretch)
+, solid_threshold(config.solid_threshold)
 , space(0)
 , light(0)
 , solids() {
index b3e8ffb0ad2f55045986eaca85d95d365fa80722..3d7ceaff801d82bc2d16e011b4e4d844d4a4a801 100644 (file)
@@ -13,7 +13,14 @@ namespace blank {
 class Generator {
 
 public:
-       explicit Generator(unsigned int seed);
+       struct Config {
+               unsigned int solid_seed = 0;
+               unsigned int type_seed = 0;
+               float stretch = 64.0f;
+               float solid_threshold = 0.8f;
+       };
+
+       explicit Generator(const Config &);
 
        void operator ()(Chunk &) const;
 
index 061e848e073691af5fcedca51763e4af97c91a80..73a73094749ec1e6d6712f5dc2a7296816ee6f96 100644 (file)
@@ -46,7 +46,7 @@ InitIMG::~InitIMG() {
 }
 
 
-InitGL::InitGL() {
+InitGL::InitGL(bool double_buffer, int sample_size) {
        if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) {
                sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)");
        }
@@ -57,8 +57,19 @@ InitGL::InitGL() {
                sdl_error("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)");
        }
 
-       if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
-               sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
+       if (double_buffer) {
+               if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0) {
+                       sdl_error("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)");
+               }
+       }
+
+       if (sample_size > 1) {
+               if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) {
+                       sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)");
+               }
+               if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) {
+                       sdl_error("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)");
+               }
        }
 }
 
index 6064fca4b58a5cd6acb5b037a2f5881216b4823d..d08437b0cd46b5408cc111733ce6587596ff1b81 100644 (file)
@@ -36,7 +36,7 @@ public:
 class InitGL {
 
 public:
-       InitGL();
+       explicit InitGL(bool double_buffer = true, int sample_size = 1);
 
        InitGL(const InitGL &) = delete;
        InitGL &operator =(const InitGL &) = delete;
index f4b13b015d6d69fcbf6adbfc6e0a92d6c3d6454e..c0bfc02ce82c24f3bf23f2f976c4213a60ee5942 100644 (file)
@@ -11,7 +11,7 @@
 
 namespace blank {
 
-Interface::Interface(World &world)
+Interface::Interface(const Config &config, World &world)
 : world(world)
 , ctrl(world.Player())
 , hud(world.BlockTypes())
@@ -20,9 +20,7 @@ Interface::Interface(World &world)
 , aim_normal()
 , outline()
 , outline_transform(1.0f)
-, move_velocity(0.005f)
-, pitch_sensitivity(-0.0025f)
-, yaw_sensitivity(-0.001f)
+, config(config)
 , remove(0)
 , selection(1)
 , front(false)
@@ -37,6 +35,8 @@ Interface::Interface(World &world)
 
 
 void Interface::Handle(const SDL_KeyboardEvent &event) {
+       if (config.keyboard_disabled) return;
+
        switch (event.keysym.sym) {
                case SDLK_w:
                        front = event.state == SDL_PRESSED;
@@ -130,11 +130,14 @@ void Interface::Print(const Block &block) {
 
 
 void Interface::Handle(const SDL_MouseMotionEvent &event) {
-       ctrl.RotateYaw(event.xrel * yaw_sensitivity);
-       ctrl.RotatePitch(event.yrel * pitch_sensitivity);
+       if (config.mouse_disabled) return;
+       ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
+       ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
 }
 
 void Interface::Handle(const SDL_MouseButtonEvent &event) {
+       if (config.mouse_disabled) return;
+
        if (event.state != SDL_PRESSED) return;
 
        if (event.button == 1) {
@@ -172,6 +175,8 @@ void Interface::RemoveBlock() {
 
 
 void Interface::Handle(const SDL_MouseWheelEvent &event) {
+       if (config.mouse_disabled) return;
+
        if (event.y < 0) {
                SelectNext();
        } else if (event.y > 0) {
@@ -205,19 +210,19 @@ void Interface::Handle(const SDL_WindowEvent &event) {
 void Interface::Update(int dt) {
        glm::vec3 vel;
        if (right && !left) {
-               vel.x = move_velocity;
+               vel.x = config.move_velocity;
        } else if (left && !right) {
-               vel.x = -move_velocity;
+               vel.x = -config.move_velocity;
        }
        if (up && !down) {
-               vel.y = move_velocity;
+               vel.y = config.move_velocity;
        } else if (down && !up) {
-               vel.y = -move_velocity;
+               vel.y = -config.move_velocity;
        }
        if (back && !front) {
-               vel.z = move_velocity;
+               vel.z = config.move_velocity;
        } else if (front && !back) {
-               vel.z = -move_velocity;
+               vel.z = -config.move_velocity;
        }
        ctrl.Velocity(vel);
        ctrl.Update(dt);
@@ -238,6 +243,8 @@ void Interface::Update(int dt) {
 
 
 void Interface::Render(DirectionalLighting &program) {
+       if (config.visual_disabled) return;
+
        if (aim_chunk) {
                program.SetM(outline_transform);
                outline.Draw();
index d3d58e8470b4fb9c9883e364917831ceaffe8bdd..7bfd7155cc3823e335fd7d0cf69964a2121fbfbc 100644 (file)
@@ -18,7 +18,17 @@ class World;
 class Interface {
 
 public:
-       explicit Interface(World &);
+       struct Config {
+               float move_velocity = 0.005f;
+               float pitch_sensitivity = -0.0025f;
+               float yaw_sensitivity = -0.001f;
+
+               bool keyboard_disabled = false;
+               bool mouse_disabled = false;
+               bool visual_disabled = false;
+       };
+
+       Interface(const Config &, World &);
 
        void Handle(const SDL_KeyboardEvent &);
        void Handle(const SDL_MouseMotionEvent &);
@@ -57,9 +67,7 @@ private:
        OutlineModel outline;
        glm::mat4 outline_transform;
 
-       float move_velocity;
-       float pitch_sensitivity;
-       float yaw_sensitivity;
+       Config config;
 
        Block remove;
        Block selection;
index 9fb644cb1aa834f8e449c556526435561484e197..18a06bf75154b9ec77b56b1545330e80b5ce50a3 100644 (file)
@@ -16,7 +16,7 @@ Runtime::Runtime()
 , mode(NORMAL)
 , n(0)
 , t(0)
-, seed(0) {
+, config() {
 
 }
 
@@ -43,13 +43,35 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                        options = false;
                                } else {
                                        // long option
-                                       cerr << "unknown option " << arg << endl;
-                                       error = true;
+                                       if (strcmp(arg + 2, "no-vsync") == 0) {
+                                               config.vsync = false;
+                                       } else if (strcmp(arg + 2, "no-keyboard") == 0) {
+                                               config.interface.keyboard_disabled = true;
+                                       } else if (strcmp(arg + 2, "no-mouse") == 0) {
+                                               config.interface.mouse_disabled = true;
+                                       } else if (strcmp(arg + 2, "no-hud") == 0) {
+                                               config.interface.visual_disabled = true;
+                                       } else {
+                                               cerr << "unknown option " << arg << endl;
+                                               error = true;
+                                       }
                                }
                        } else {
                                // short options
                                for (int j = 1; arg[j] != '\0'; ++j) {
                                        switch (arg[j]) {
+                                               case 'd':
+                                                       config.doublebuf = false;
+                                                       break;
+                                               case 'm':
+                                                       ++i;
+                                                       if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
+                                                               cerr << "missing argument to -m" << endl;
+                                                               error = true;
+                                                       } else {
+                                                               config.multisampling = strtoul(argv[i], nullptr, 10);
+                                                       }
+                                                       break;
                                                case 'n':
                                                        ++i;
                                                        if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
@@ -65,7 +87,8 @@ void Runtime::ReadArgs(int argc, const char *const *argv) {
                                                                cerr << "missing argument to -s" << endl;
                                                                error = true;
                                                        } else {
-                                                               seed = strtoul(argv[i], nullptr, 10);
+                                                               config.world.gen.solid_seed = strtoul(argv[i], nullptr, 10);
+                                                               config.world.gen.type_seed = config.world.gen.solid_seed;
                                                        }
                                                        break;
                                                case 't':
@@ -121,7 +144,7 @@ int Runtime::Execute() {
                return 1;
        }
 
-       Application app(seed);
+       Application app(config);
        switch (mode) {
                default:
                case NORMAL:
index 01f6cdd2c9b51df11afd0c453bc3d3a1e76939c4..1fbc4db4fc3b8c27117e8c1e5204d5fab82d29f2 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef BLANK_RUNTIME_HPP_
 #define BLANK_RUNTIME_HPP_
 
+#include "app.hpp"
+
 #include <cstddef>
 
 
@@ -28,7 +30,7 @@ private:
        Mode mode;
        std::size_t n;
        std::size_t t;
-       unsigned int seed;
+       Application::Config config;
 
 };
 
index 30e92297d7402d4b040f09eebc9053b872d1052f..756c66fdff87718ff724ee0263006eac7b4b9f4a 100644 (file)
@@ -6,14 +6,17 @@
 
 namespace blank {
 
-World::World(unsigned int seed)
+World::World(const Config &config)
 : 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(seed)
-, chunks(blockType, generate)
-, player() {
+, generate(config.gen)
+, chunks(config.load, blockType, generate)
+, player()
+, entities()
+, light_direction(config.light_direction)
+, fog_density(config.fog_density) {
        BlockType::Faces block_fill = {  true,  true,  true,  true,  true,  true };
        BlockType::Faces slab_fill  = { false,  true, false, false, false, false };
        BlockType::Faces stair_fill = { false,  true, false, false, false,  true };
@@ -107,7 +110,7 @@ World::World(unsigned int seed)
        generate.Solids({ 1, 4, 7, 10 });
 
        player = &AddEntity();
-       player->Position({ 4.0f, 4.0f, 4.0f });
+       player->Position(config.spawn);
 
        chunks.GenerateSurrounding(player->ChunkCoords());
 }
@@ -198,12 +201,8 @@ void World::Update(int dt) {
 
 
 void World::Render(DirectionalLighting &program) {
-       program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
-       // fade out reaches 1/e (0.3679) at 1/fog_density,
-       // gets less than 0.01 at e/(2 * fog_density)
-       // I chose 0.011 because it yields 91 and 124 for those, so
-       // slightly less than 6 and 8 chunks
-       program.SetFogDensity(0.011f);
+       program.SetLightDirection(light_direction);
+       program.SetFogDensity(fog_density);
        program.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
 
        for (Chunk &chunk : chunks.Loaded()) {
index bca1e0c082e190aadfe147abd2efcd7b852b69d5..3dd04fb31add2b0a8738328c824f54884875b21a 100644 (file)
@@ -17,7 +17,22 @@ namespace blank {
 class World {
 
 public:
-       explicit World(unsigned int seed);
+       struct Config {
+               // initial player position
+               glm::vec3 spawn = { 4.0f, 4.0f, 4.0f };
+               // direction facing towards(!) the light
+               glm::vec3 light_direction = { -1.0f, -3.0f, -2.0f };
+               // fade out reaches 1/e (0.3679) at 1/fog_density,
+               // gets less than 0.01 at e/(2 * fog_density)
+               // I chose 0.011 because it yields 91 and 124 for those, so
+               // slightly less than 6 and 8 chunks
+               float fog_density = 0.011f;
+
+               Generator::Config gen = Generator::Config();
+               ChunkLoader::Config load = ChunkLoader::Config();
+       };
+
+       explicit World(const Config &);
 
        bool Intersection(
                const Ray &,
@@ -51,6 +66,9 @@ private:
        Entity *player;
        std::list<Entity> entities;
 
+       glm::vec3 light_direction;
+       float fog_density;
+
 };
 
 }