]> git.localhorst.tv Git - blank.git/blob - src/app/UnloadState.cpp
enhanced application state control
[blank.git] / src / app / UnloadState.cpp
1 #include "UnloadState.hpp"
2
3 #include "Environment.hpp"
4 #include "../world/ChunkLoader.hpp"
5 #include "../world/WorldSave.hpp"
6
7
8 namespace blank {
9
10 UnloadState::UnloadState(Environment &env, ChunkLoader &loader)
11 : env(env)
12 , loader(loader)
13 , font(env.assets.LoadFont("DejaVuSans", 24))
14 , progress(font)
15 , cur(loader.Loaded().begin())
16 , end(loader.Loaded().end())
17 , done(0)
18 , total(loader.Loaded().size())
19 , per_update(64) {
20         progress.Position(glm::vec3(0.0f), Gravity::CENTER);
21         progress.Template("Unloading chunks: %d/%d (%d%%)");
22 }
23
24
25 void UnloadState::OnResume() {
26         cur = loader.Loaded().begin();
27         end = loader.Loaded().end();
28         done = 0;
29         total = loader.Loaded().size();
30 }
31
32
33 void UnloadState::Handle(const SDL_Event &) {
34         // ignore everything
35 }
36
37 void UnloadState::Update(int dt) {
38         for (std::size_t i = 0; i < per_update && cur != end; ++i, ++cur, ++done) {
39                 if (cur->ShouldUpdateSave()) {
40                         loader.SaveFile().Write(*cur);
41                 }
42         }
43         if (cur == end) {
44                 env.state.PopAll();
45         } else {
46                 progress.Update(done, total);
47         }
48 }
49
50 void UnloadState::Render(Viewport &viewport) {
51         progress.Render(viewport);
52 }
53
54 }