From: Daniel Karbach Date: Thu, 8 Oct 2015 15:39:25 +0000 (+0200) Subject: merge common parts of pre- and unload states X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=9da6ac5e93d79e79658a95d5f6efe42146873583;p=blank.git merge common parts of pre- and unload states --- diff --git a/src/app/ProgressState.cpp b/src/app/ProgressState.cpp new file mode 100644 index 0000000..30a60dc --- /dev/null +++ b/src/app/ProgressState.cpp @@ -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 index 0000000..b3295c2 --- /dev/null +++ b/src/app/ProgressState.hpp @@ -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 diff --git a/src/standalone/PreloadState.cpp b/src/standalone/PreloadState.cpp index 20d4c0f..554a458 100644 --- a/src/standalone/PreloadState.cpp +++ b/src/standalone/PreloadState.cpp @@ -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); -} - } } diff --git a/src/standalone/PreloadState.hpp b/src/standalone/PreloadState.hpp index 8dcd706..1e750d9 100644 --- a/src/standalone/PreloadState.hpp +++ b/src/standalone/PreloadState.hpp @@ -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 @@ -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; diff --git a/src/standalone/UnloadState.cpp b/src/standalone/UnloadState.cpp index db1d2a5..0b48904 100644 --- a/src/standalone/UnloadState.cpp +++ b/src/standalone/UnloadState.cpp @@ -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); -} - } } diff --git a/src/standalone/UnloadState.hpp b/src/standalone/UnloadState.hpp index 5728ba7..959fa94 100644 --- a/src/standalone/UnloadState.hpp +++ b/src/standalone/UnloadState.hpp @@ -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 #include @@ -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::iterator cur; std::list::iterator end; std::size_t done;