From: Daniel Karbach Date: Mon, 28 Sep 2015 08:11:13 +0000 (+0200) Subject: move standalone stuff to its own dir X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=e1209ec25c4cc91e13889876106f56bd51aa96e2;p=blank.git move standalone stuff to its own dir --- diff --git a/src/app/PreloadState.cpp b/src/app/PreloadState.cpp deleted file mode 100644 index 7d5f372..0000000 --- a/src/app/PreloadState.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "PreloadState.hpp" - -#include "Environment.hpp" -#include "../world/ChunkLoader.hpp" -#include "../world/ChunkRenderer.hpp" - - -namespace blank { - -PreloadState::PreloadState(Environment &env, ChunkLoader &loader, ChunkRenderer &render) -: 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) { - loader.LoadN(per_update); - if (loader.ToLoad() <= 0) { - env.state.Pop(); - render.Update(render.MissingChunks()); - } else { - progress.Update(total - loader.ToLoad(), total); - } -} - -void PreloadState::Render(Viewport &viewport) { - progress.Render(viewport); -} - -} diff --git a/src/app/PreloadState.hpp b/src/app/PreloadState.hpp deleted file mode 100644 index 64936dc..0000000 --- a/src/app/PreloadState.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef BLANK_APP_PRELOADSTATE_HPP_ -#define BLANK_APP_PRELOADSTATE_HPP_ - -#include "State.hpp" - -#include "../ui/Progress.hpp" - -#include - - -namespace blank { - -class ChunkLoader; -class ChunkRenderer; -class Environment; - -class PreloadState -: public State { - -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; - -}; - -} - -#endif diff --git a/src/app/UnloadState.cpp b/src/app/UnloadState.cpp deleted file mode 100644 index 43aae05..0000000 --- a/src/app/UnloadState.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "UnloadState.hpp" - -#include "Environment.hpp" -#include "../io/WorldSave.hpp" -#include "../world/ChunkLoader.hpp" - - -namespace blank { - -UnloadState::UnloadState( - Environment &env, - ChunkStore &chunks, - const WorldSave &save) -: 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%%)"); -} - - -void UnloadState::OnResume() { - cur = chunks.begin(); - end = chunks.end(); - done = 0; - total = chunks.NumLoaded(); -} - - -void UnloadState::Handle(const SDL_Event &) { - // ignore everything -} - -void UnloadState::Update(int dt) { - for (std::size_t i = 0; i < per_update && cur != end; ++i, ++cur, ++done) { - if (cur->ShouldUpdateSave()) { - save.Write(*cur); - } - } - if (cur == end) { - env.state.PopAll(); - } else { - progress.Update(done, total); - } -} - -void UnloadState::Render(Viewport &viewport) { - progress.Render(viewport); -} - -} diff --git a/src/app/UnloadState.hpp b/src/app/UnloadState.hpp deleted file mode 100644 index ba7e809..0000000 --- a/src/app/UnloadState.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef BLANK_APP_UNLOADSTATE_HPP_ -#define BLANK_APP_UNLOADSTATE_HPP_ - -#include "State.hpp" - -#include "../ui/Progress.hpp" - -#include -#include - - -namespace blank { - -class Chunk; -class ChunkStore; -class Environment; -class WorldSave; - -class UnloadState -: public State { - -public: - UnloadState(Environment &, ChunkStore &, const WorldSave &); - - void OnResume(); - - 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; - std::size_t total; - std::size_t per_update; - -}; - -} - -#endif diff --git a/src/app/WorldState.cpp b/src/app/WorldState.cpp deleted file mode 100644 index 590f6ac..0000000 --- a/src/app/WorldState.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "WorldState.hpp" - -#include "Environment.hpp" -#include "init.hpp" -#include "TextureIndex.hpp" - -#include - - -namespace blank { - -WorldState::WorldState( - Environment &env, - const Generator::Config &gc, - const Interface::Config &ic, - const World::Config &wc, - const WorldSave &save -) -: env(env) -, block_types() -, world(block_types, wc) -, interface(ic, env, world, world.AddPlayer(ic.player_name)) -, generator(gc) -, chunk_loader(world.Chunks(), generator, save) -, chunk_renderer(*interface.GetPlayer().chunks) -, skeletons() -, spawner(world, skeletons, gc.seed) -, sky(env.loader.LoadCubeMap("skybox")) -, preload(env, chunk_loader, chunk_renderer) -, unload(env, world.Chunks(), save) { - TextureIndex tex_index; - env.loader.LoadBlockTypes("default", block_types, tex_index); - chunk_renderer.LoadTextures(env.loader, tex_index); - chunk_renderer.FogDensity(wc.fog_density); - skeletons.Load(); - spawner.LimitSkeletons(0, skeletons.Size()); - // TODO: better solution for initializing HUD - interface.SelectNext(); -} - - -void WorldState::OnEnter() { - env.state.Push(&preload); - env.window.GrabMouse(); -} - - -void WorldState::Handle(const SDL_Event &event) { - switch (event.type) { - case SDL_KEYDOWN: - interface.HandlePress(event.key); - break; - case SDL_KEYUP: - interface.HandleRelease(event.key); - break; - case SDL_MOUSEBUTTONDOWN: - interface.HandlePress(event.button); - break; - case SDL_MOUSEBUTTONUP: - interface.HandleRelease(event.button); - break; - case SDL_MOUSEMOTION: - interface.Handle(event.motion); - break; - case SDL_MOUSEWHEEL: - interface.Handle(event.wheel); - break; - case SDL_QUIT: - env.state.Switch(&unload); - break; - default: - break; - } -} - -void WorldState::Update(int dt) { - interface.Update(dt); - spawner.Update(dt); - world.Update(dt); - chunk_loader.Update(dt); - chunk_renderer.Update(dt); - - Entity &player = *interface.GetPlayer().entity; - - glm::mat4 trans = player.Transform(player.ChunkCoords()); - glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); - glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)); - env.audio.Position(player.Position()); - env.audio.Velocity(player.Velocity()); - env.audio.Orientation(dir, up); -} - -void WorldState::Render(Viewport &viewport) { - Entity &player = *interface.GetPlayer().entity; - viewport.WorldPosition(player.Transform(player.ChunkCoords())); - chunk_renderer.Render(viewport); - world.Render(viewport); - sky.Render(viewport); - - interface.Render(viewport); -} - -} diff --git a/src/app/WorldState.hpp b/src/app/WorldState.hpp deleted file mode 100644 index b48d1b9..0000000 --- a/src/app/WorldState.hpp +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef BLANK_APP_WORLDSTATE_HPP_ -#define BLANK_APP_WORLDSTATE_HPP_ - -#include "PreloadState.hpp" -#include "State.hpp" -#include "UnloadState.hpp" -#include "../ai/Spawner.hpp" -#include "../graphics/SkyBox.hpp" -#include "../model/Skeletons.hpp" -#include "../ui/Interface.hpp" -#include "../world/BlockTypeRegistry.hpp" -#include "../world/ChunkLoader.hpp" -#include "../world/ChunkRenderer.hpp" -#include "../world/Generator.hpp" -#include "../world/World.hpp" - - -namespace blank { - -class Environment; - -class WorldState -: public State { - -public: - WorldState( - Environment &, - const Generator::Config &, - const Interface::Config &, - const World::Config &, - const WorldSave & - ); - - void OnEnter() override; - - void Handle(const SDL_Event &) override; - void Update(int dt) override; - void Render(Viewport &) override; - - World &GetWorld() noexcept { return world; } - Interface &GetInterface() noexcept { return interface; } - -private: - Environment &env; - BlockTypeRegistry block_types; - World world; - Interface interface; - Generator generator; - ChunkLoader chunk_loader; - ChunkRenderer chunk_renderer; - Skeletons skeletons; - Spawner spawner; - - SkyBox sky; - - PreloadState preload; - UnloadState unload; - -}; - -} - -#endif diff --git a/src/app/runtime.cpp b/src/app/runtime.cpp index cc9d3bb..486259a 100644 --- a/src/app/runtime.cpp +++ b/src/app/runtime.cpp @@ -1,13 +1,13 @@ #include "Application.hpp" #include "Environment.hpp" #include "Runtime.hpp" -#include "WorldState.hpp" #include "init.hpp" #include "../client/MasterState.hpp" #include "../io/filesystem.hpp" #include "../io/WorldSave.hpp" #include "../server/ServerState.hpp" +#include "../standalone/MasterState.hpp" #include #include @@ -324,7 +324,7 @@ void Runtime::RunStandalone() { } Application app(env); - WorldState world_state(env, config.gen, config.interface, config.world, save); + standalone::MasterState world_state(env, config.gen, config.interface, config.world, save); app.PushState(&world_state); Run(app); } diff --git a/src/standalone/MasterState.cpp b/src/standalone/MasterState.cpp new file mode 100644 index 0000000..30925a9 --- /dev/null +++ b/src/standalone/MasterState.cpp @@ -0,0 +1,105 @@ +#include "MasterState.hpp" + +#include "../app/Environment.hpp" +#include "../app/init.hpp" +#include "../app/TextureIndex.hpp" + +#include + + +namespace blank { +namespace standalone { + +MasterState::MasterState( + Environment &env, + const Generator::Config &gc, + const Interface::Config &ic, + const World::Config &wc, + const WorldSave &save +) +: env(env) +, block_types() +, world(block_types, wc) +, interface(ic, env, world, world.AddPlayer(ic.player_name)) +, generator(gc) +, chunk_loader(world.Chunks(), generator, save) +, chunk_renderer(*interface.GetPlayer().chunks) +, skeletons() +, spawner(world, skeletons, gc.seed) +, sky(env.loader.LoadCubeMap("skybox")) +, preload(env, chunk_loader, chunk_renderer) +, unload(env, world.Chunks(), save) { + TextureIndex tex_index; + env.loader.LoadBlockTypes("default", block_types, tex_index); + chunk_renderer.LoadTextures(env.loader, tex_index); + chunk_renderer.FogDensity(wc.fog_density); + skeletons.Load(); + spawner.LimitSkeletons(0, skeletons.Size()); + // TODO: better solution for initializing HUD + interface.SelectNext(); +} + + +void MasterState::OnEnter() { + env.state.Push(&preload); + env.window.GrabMouse(); +} + + +void MasterState::Handle(const SDL_Event &event) { + switch (event.type) { + case SDL_KEYDOWN: + interface.HandlePress(event.key); + break; + case SDL_KEYUP: + interface.HandleRelease(event.key); + break; + case SDL_MOUSEBUTTONDOWN: + interface.HandlePress(event.button); + break; + case SDL_MOUSEBUTTONUP: + interface.HandleRelease(event.button); + break; + case SDL_MOUSEMOTION: + interface.Handle(event.motion); + break; + case SDL_MOUSEWHEEL: + interface.Handle(event.wheel); + break; + case SDL_QUIT: + env.state.Switch(&unload); + break; + default: + break; + } +} + +void MasterState::Update(int dt) { + interface.Update(dt); + spawner.Update(dt); + world.Update(dt); + chunk_loader.Update(dt); + chunk_renderer.Update(dt); + + Entity &player = *interface.GetPlayer().entity; + + glm::mat4 trans = player.Transform(player.ChunkCoords()); + glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); + glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)); + env.audio.Position(player.Position()); + env.audio.Velocity(player.Velocity()); + env.audio.Orientation(dir, up); +} + +void MasterState::Render(Viewport &viewport) { + Entity &player = *interface.GetPlayer().entity; + viewport.WorldPosition(player.Transform(player.ChunkCoords())); + chunk_renderer.Render(viewport); + world.Render(viewport); + sky.Render(viewport); + + interface.Render(viewport); +} + +} +} diff --git a/src/standalone/MasterState.hpp b/src/standalone/MasterState.hpp new file mode 100644 index 0000000..ca5cab2 --- /dev/null +++ b/src/standalone/MasterState.hpp @@ -0,0 +1,67 @@ +#ifndef BLANK_STANDALONE_MASTERSTATE_HPP_ +#define BLANK_STANDALONE_MASTERSTATE_HPP_ + +#include "../app/State.hpp" + +#include "PreloadState.hpp" +#include "UnloadState.hpp" +#include "../ai/Spawner.hpp" +#include "../graphics/SkyBox.hpp" +#include "../model/Skeletons.hpp" +#include "../ui/Interface.hpp" +#include "../world/BlockTypeRegistry.hpp" +#include "../world/ChunkLoader.hpp" +#include "../world/ChunkRenderer.hpp" +#include "../world/Generator.hpp" +#include "../world/World.hpp" + + +namespace blank { + +class Environment; + +namespace standalone { + +class MasterState +: public State { + +public: + MasterState( + Environment &, + const Generator::Config &, + const Interface::Config &, + const World::Config &, + const WorldSave & + ); + + void OnEnter() override; + + void Handle(const SDL_Event &) override; + void Update(int dt) override; + void Render(Viewport &) override; + + World &GetWorld() noexcept { return world; } + Interface &GetInterface() noexcept { return interface; } + +private: + Environment &env; + BlockTypeRegistry block_types; + World world; + Interface interface; + Generator generator; + ChunkLoader chunk_loader; + ChunkRenderer chunk_renderer; + Skeletons skeletons; + Spawner spawner; + + SkyBox sky; + + PreloadState preload; + UnloadState unload; + +}; + +} +} + +#endif diff --git a/src/standalone/PreloadState.cpp b/src/standalone/PreloadState.cpp new file mode 100644 index 0000000..20d4c0f --- /dev/null +++ b/src/standalone/PreloadState.cpp @@ -0,0 +1,44 @@ +#include "PreloadState.hpp" + +#include "../app/Environment.hpp" +#include "../world/ChunkLoader.hpp" +#include "../world/ChunkRenderer.hpp" + + +namespace blank { +namespace standalone { + +PreloadState::PreloadState(Environment &env, ChunkLoader &loader, ChunkRenderer &render) +: 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) { + loader.LoadN(per_update); + if (loader.ToLoad() <= 0) { + env.state.Pop(); + render.Update(render.MissingChunks()); + } else { + progress.Update(total - loader.ToLoad(), total); + } +} + +void PreloadState::Render(Viewport &viewport) { + progress.Render(viewport); +} + +} +} diff --git a/src/standalone/PreloadState.hpp b/src/standalone/PreloadState.hpp new file mode 100644 index 0000000..8dcd706 --- /dev/null +++ b/src/standalone/PreloadState.hpp @@ -0,0 +1,42 @@ +#ifndef BLANK_STANDALONE_PRELOADSTATE_HPP_ +#define BLANK_STANDALONE_PRELOADSTATE_HPP_ + +#include "../app/State.hpp" + +#include "../ui/Progress.hpp" + +#include + + +namespace blank { + +class ChunkLoader; +class ChunkRenderer; +class Environment; + +namespace standalone { + +class PreloadState +: public State { + +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; + +}; + +} +} + +#endif diff --git a/src/standalone/UnloadState.cpp b/src/standalone/UnloadState.cpp new file mode 100644 index 0000000..db1d2a5 --- /dev/null +++ b/src/standalone/UnloadState.cpp @@ -0,0 +1,59 @@ +#include "UnloadState.hpp" + +#include "../app/Environment.hpp" +#include "../io/WorldSave.hpp" +#include "../world/ChunkLoader.hpp" + + +namespace blank { +namespace standalone { + +UnloadState::UnloadState( + Environment &env, + ChunkStore &chunks, + const WorldSave &save) +: 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%%)"); +} + + +void UnloadState::OnResume() { + cur = chunks.begin(); + end = chunks.end(); + done = 0; + total = chunks.NumLoaded(); +} + + +void UnloadState::Handle(const SDL_Event &) { + // ignore everything +} + +void UnloadState::Update(int dt) { + for (std::size_t i = 0; i < per_update && cur != end; ++i, ++cur, ++done) { + if (cur->ShouldUpdateSave()) { + save.Write(*cur); + } + } + if (cur == end) { + env.state.PopAll(); + } else { + progress.Update(done, total); + } +} + +void UnloadState::Render(Viewport &viewport) { + progress.Render(viewport); +} + +} +} diff --git a/src/standalone/UnloadState.hpp b/src/standalone/UnloadState.hpp new file mode 100644 index 0000000..5728ba7 --- /dev/null +++ b/src/standalone/UnloadState.hpp @@ -0,0 +1,49 @@ +#ifndef BLANK_STANDALONE_UNLOADSTATE_HPP_ +#define BLANK_STANDALONE_UNLOADSTATE_HPP_ + +#include "../app/State.hpp" + +#include "../ui/Progress.hpp" + +#include +#include + + +namespace blank { + +class Chunk; +class ChunkStore; +class Environment; +class WorldSave; + +namespace standalone { + +class UnloadState +: public State { + +public: + UnloadState(Environment &, ChunkStore &, const WorldSave &); + + void OnResume(); + + 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; + std::size_t total; + std::size_t per_update; + +}; + +} +} + +#endif