]> git.localhorst.tv Git - blank.git/commitdiff
merge common parts of pre- and unload states
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 8 Oct 2015 15:39:25 +0000 (17:39 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 8 Oct 2015 15:39:25 +0000 (17:39 +0200)
src/app/ProgressState.cpp [new file with mode: 0644]
src/app/ProgressState.hpp [new file with mode: 0644]
src/standalone/PreloadState.cpp
src/standalone/PreloadState.hpp
src/standalone/UnloadState.cpp
src/standalone/UnloadState.hpp

diff --git a/src/app/ProgressState.cpp b/src/app/ProgressState.cpp
new file mode 100644 (file)
index 0000000..30a60dc
--- /dev/null
@@ -0,0 +1,29 @@
+#include "ProgressState.hpp"
+
+#include "../app/Environment.hpp"
+
+
+namespace blank {
+
+ProgressState::ProgressState(Environment &env, const char *tpl)
+: env(env)
+, progress(env.assets.large_ui_font) {
+       progress.Position(glm::vec3(0.0f), Gravity::CENTER);
+       progress.Template(tpl);
+}
+
+void ProgressState::SetProgress(int value, int total) {
+       progress.Update(value, total);
+}
+
+void ProgressState::Handle(const SDL_Event &e) {
+       if (e.type == SDL_QUIT) {
+               env.state.PopAll();
+       }
+}
+
+void ProgressState::Render(Viewport &viewport) {
+       progress.Render(viewport);
+}
+
+}
diff --git a/src/app/ProgressState.hpp b/src/app/ProgressState.hpp
new file mode 100644 (file)
index 0000000..b3295c2
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef BLANK_APP_PROGRESSSTATE_HPP_
+#define BLANK_APP_PROGRESSSTATE_HPP_
+
+#include "State.hpp"
+
+#include "../ui/Progress.hpp"
+
+
+namespace blank {
+
+class Environment;
+
+class ProgressState
+: public State {
+
+public:
+       ProgressState(Environment &env, const char *tpl);
+
+       void SetProgress(int value, int total);
+
+       void Handle(const SDL_Event &) override;
+       void Render(Viewport &) override;
+
+private:
+       Environment &env;
+       Progress progress;
+
+};
+
+}
+
+#endif
index 20d4c0f52ca7a16af93b2abb40a926aec7dfe72f..554a4588775ef98b0513b4bf8c0647684662fedf 100644 (file)
@@ -9,21 +9,13 @@ namespace blank {
 namespace standalone {
 
 PreloadState::PreloadState(Environment &env, ChunkLoader &loader, ChunkRenderer &render)
-: env(env)
+: ProgressState(env, "Preloading chunks: %d/%d (%d%%)")
+, env(env)
 , loader(loader)
 , render(render)
-, progress(env.assets.large_ui_font)
 , total(loader.ToLoad())
 , per_update(64) {
-       progress.Position(glm::vec3(0.0f), Gravity::CENTER);
-       progress.Template("Preloading chunks: %d/%d (%d%%)");
-}
-
 
-void PreloadState::Handle(const SDL_Event &e) {
-       if (e.type == SDL_QUIT) {
-               env.state.PopAll();
-       }
 }
 
 void PreloadState::Update(int dt) {
@@ -32,13 +24,9 @@ void PreloadState::Update(int dt) {
                env.state.Pop();
                render.Update(render.MissingChunks());
        } else {
-               progress.Update(total - loader.ToLoad(), total);
+               SetProgress(total - loader.ToLoad(), total);
        }
 }
 
-void PreloadState::Render(Viewport &viewport) {
-       progress.Render(viewport);
-}
-
 }
 }
index 8dcd7060934e1c45bbdef70c5faebcdaf6b1378f..1e750d9c815293255a4010474003f8309d31fde9 100644 (file)
@@ -1,9 +1,7 @@
 #ifndef BLANK_STANDALONE_PRELOADSTATE_HPP_
 #define BLANK_STANDALONE_PRELOADSTATE_HPP_
 
-#include "../app/State.hpp"
-
-#include "../ui/Progress.hpp"
+#include "../app/ProgressState.hpp"
 
 #include <cstddef>
 
@@ -17,20 +15,17 @@ class Environment;
 namespace standalone {
 
 class PreloadState
-: public State {
+: public ProgressState {
 
 public:
        PreloadState(Environment &, ChunkLoader &, ChunkRenderer &);
 
-       void Handle(const SDL_Event &) override;
        void Update(int dt) override;
-       void Render(Viewport &) override;
 
 private:
        Environment &env;
        ChunkLoader &loader;
        ChunkRenderer &render;
-       Progress progress;
        std::size_t total;
        std::size_t per_update;
 
index db1d2a5ec57d1a3c86f4b29a23e2f7391f34530c..0b48904935e5e8a660547c8acc1d3f506e998c2c 100644 (file)
@@ -12,17 +12,16 @@ UnloadState::UnloadState(
        Environment &env,
        ChunkStore &chunks,
        const WorldSave &save)
-: env(env)
+: ProgressState(env, "Unloading chunks: %d/%d (%d%%)")
+, env(env)
 , chunks(chunks)
 , save(save)
-, progress(env.assets.large_ui_font)
 , cur(chunks.begin())
 , end(chunks.end())
 , done(0)
 , total(chunks.NumLoaded())
 , per_update(64) {
-       progress.Position(glm::vec3(0.0f), Gravity::CENTER);
-       progress.Template("Unloading chunks: %d/%d (%d%%)");
+
 }
 
 
@@ -45,15 +44,11 @@ void UnloadState::Update(int dt) {
                }
        }
        if (cur == end) {
-               env.state.PopAll();
+               env.state.Pop();
        } else {
-               progress.Update(done, total);
+               SetProgress(done, total);
        }
 }
 
-void UnloadState::Render(Viewport &viewport) {
-       progress.Render(viewport);
-}
-
 }
 }
index 5728ba706965f8fa105b7bc7b7c9e6ffbce468fd..959fa94df5039afc8fb345bb7b978cc2d8fac8e6 100644 (file)
@@ -1,9 +1,7 @@
 #ifndef BLANK_STANDALONE_UNLOADSTATE_HPP_
 #define BLANK_STANDALONE_UNLOADSTATE_HPP_
 
-#include "../app/State.hpp"
-
-#include "../ui/Progress.hpp"
+#include "../app/ProgressState.hpp"
 
 #include <cstddef>
 #include <list>
@@ -19,7 +17,7 @@ class WorldSave;
 namespace standalone {
 
 class UnloadState
-: public State {
+: public ProgressState {
 
 public:
        UnloadState(Environment &, ChunkStore &, const WorldSave &);
@@ -28,13 +26,11 @@ public:
 
        void Handle(const SDL_Event &) override;
        void Update(int dt) override;
-       void Render(Viewport &) override;
 
 private:
        Environment &env;
        ChunkStore &chunks;
        const WorldSave &save;
-       Progress progress;
        std::list<Chunk>::iterator cur;
        std::list<Chunk>::iterator end;
        std::size_t done;