]> git.localhorst.tv Git - blank.git/blob - src/standalone/UnloadState.cpp
merge common parts of pre- and unload states
[blank.git] / src / standalone / UnloadState.cpp
1 #include "UnloadState.hpp"
2
3 #include "../app/Environment.hpp"
4 #include "../io/WorldSave.hpp"
5 #include "../world/ChunkLoader.hpp"
6
7
8 namespace blank {
9 namespace standalone {
10
11 UnloadState::UnloadState(
12         Environment &env,
13         ChunkStore &chunks,
14         const WorldSave &save)
15 : ProgressState(env, "Unloading chunks: %d/%d (%d%%)")
16 , env(env)
17 , chunks(chunks)
18 , save(save)
19 , cur(chunks.begin())
20 , end(chunks.end())
21 , done(0)
22 , total(chunks.NumLoaded())
23 , per_update(64) {
24
25 }
26
27
28 void UnloadState::OnResume() {
29         cur = chunks.begin();
30         end = chunks.end();
31         done = 0;
32         total = chunks.NumLoaded();
33 }
34
35
36 void UnloadState::Handle(const SDL_Event &) {
37         // ignore everything
38 }
39
40 void UnloadState::Update(int dt) {
41         for (std::size_t i = 0; i < per_update && cur != end; ++i, ++cur, ++done) {
42                 if (cur->ShouldUpdateSave()) {
43                         save.Write(*cur);
44                 }
45         }
46         if (cur == end) {
47                 env.state.Pop();
48         } else {
49                 SetProgress(done, total);
50         }
51 }
52
53 }
54 }