From 9c5308ba4108bd842af6d9d2e893ea575a7e6ca8 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 15 Oct 2015 16:59:08 +0200 Subject: [PATCH] lil cleanup of common and unused stuff --- src/app/Assets.hpp | 8 +-- src/app/FPSController.cpp | 53 ------------------ src/app/FPSController.hpp | 54 ------------------- src/app/app.cpp | 24 ++------- src/client/InteractiveState.hpp | 9 +--- src/client/client.cpp | 24 ++++----- src/model/Part.hpp | 4 +- src/model/model.cpp | 4 +- src/server/ServerState.cpp | 22 +++----- src/server/ServerState.hpp | 8 +-- .../ResourceIndex.hpp} | 12 ++--- src/shared/WorldResources.hpp | 33 ++++++++++++ src/shared/shared.cpp | 39 ++++++++++++++ src/standalone/MasterState.cpp | 26 ++++----- src/standalone/MasterState.hpp | 8 +-- src/world/ChunkRenderer.hpp | 4 +- src/world/chunk.cpp | 2 +- 17 files changed, 126 insertions(+), 208 deletions(-) delete mode 100644 src/app/FPSController.cpp delete mode 100644 src/app/FPSController.hpp rename src/{app/TextureIndex.hpp => shared/ResourceIndex.hpp} (51%) create mode 100644 src/shared/WorldResources.hpp create mode 100644 src/shared/shared.cpp diff --git a/src/app/Assets.hpp b/src/app/Assets.hpp index 64bc72c..c069c3e 100644 --- a/src/app/Assets.hpp +++ b/src/app/Assets.hpp @@ -12,10 +12,10 @@ class ArrayTexture; class BlockTypeRegistry; class CubeMap; class ModelRegistry; +class ResourceIndex; class ShapeRegistry; class Sound; class Texture; -class TextureIndex; class AssetLoader { @@ -25,20 +25,20 @@ public: void LoadBlockTypes( const std::string &set_name, BlockTypeRegistry &, - TextureIndex &, + ResourceIndex &, const ShapeRegistry &) const; CubeMap LoadCubeMap(const std::string &name) const; Font LoadFont(const std::string &name, int size) const; void LoadModels( const std::string &set_name, ModelRegistry &, - TextureIndex &, + ResourceIndex &, const ShapeRegistry &) const; void LoadShapes(const std::string &set_name, ShapeRegistry &) const; Sound LoadSound(const std::string &name) const; Texture LoadTexture(const std::string &name) const; void LoadTexture(const std::string &name, ArrayTexture &, int layer) const; - void LoadTextures(const TextureIndex &, ArrayTexture &) const; + void LoadTextures(const ResourceIndex &, ArrayTexture &) const; private: std::string fonts; diff --git a/src/app/FPSController.cpp b/src/app/FPSController.cpp deleted file mode 100644 index 87591aa..0000000 --- a/src/app/FPSController.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "FPSController.hpp" - -#include -#include - - -namespace blank { - -FPSController::FPSController(Entity &entity) noexcept -: entity(entity) -, pitch(0) -, yaw(0) { - entity.Ref(); -} - -FPSController::~FPSController() { - entity.UnRef(); -} - - -void FPSController::Pitch(float p) noexcept { - pitch = p; - if (pitch > PI / 2) { - pitch = PI / 2; - } else if (pitch < -PI / 2) { - pitch = -PI / 2; - } -} - -void FPSController::RotatePitch(float delta) noexcept { - Pitch(pitch + delta); -} - -void FPSController::Yaw(float y) noexcept { - yaw = y; - if (yaw > PI) { - yaw -= PI * 2; - } else if (yaw < -PI) { - yaw += PI * 2; - } -} - -void FPSController::RotateYaw(float delta) noexcept { - Yaw(yaw + delta); -} - - -void FPSController::Update(int dt) noexcept { - entity.Orientation(glm::quat(glm::vec3(pitch, yaw, 0.0f))); - entity.Velocity(glm::rotateY(velocity, yaw)); -} - -} diff --git a/src/app/FPSController.hpp b/src/app/FPSController.hpp deleted file mode 100644 index ee10691..0000000 --- a/src/app/FPSController.hpp +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef BLANK_APP_FPSCONTROLLER_HPP_ -#define BLANK_APP_FPSCONTROLLER_HPP_ - -#include "../model/geometry.hpp" -#include "../world/Entity.hpp" - -#include - - -namespace blank { - -/// Sets entity rotation and velocity according to stored velocity -/// and pitch/yaw components. -/// Rotation is applied in yaw,pitch order (YX). Velocity is relative -/// to yaw only (Y axis). -class FPSController { - -public: - explicit FPSController(Entity &) noexcept; - ~FPSController(); - - Entity &Controlled() noexcept { return entity; } - const Entity &Controlled() const noexcept { return entity; } - - /// get position and face direction of controlled entity - Ray Aim() const noexcept { return entity.Aim(entity.ChunkCoords()); } - - /// velocity, relative to heading (yaw only) - const glm::vec3 &Velocity() const noexcept { return velocity; } - void Velocity(const glm::vec3 &vel) noexcept { velocity = vel; } - - // all angles in radians (full circle = 2π) - float Pitch() const noexcept { return pitch; } - void Pitch(float p) noexcept; - void RotatePitch(float delta) noexcept; - float Yaw() const noexcept { return yaw; } - void Yaw(float y) noexcept; - void RotateYaw(float delta) noexcept; - - void Update(int dt) noexcept; - -private: - Entity &entity; - - glm::vec3 velocity; - - float pitch; - float yaw; - -}; - -} - -#endif diff --git a/src/app/app.cpp b/src/app/app.cpp index 7f5f02f..ab5170f 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -4,7 +4,6 @@ #include "FrameCounter.hpp" #include "State.hpp" #include "StateControl.hpp" -#include "TextureIndex.hpp" #include "init.hpp" #include "../audio/Sound.hpp" @@ -18,6 +17,7 @@ #include "../model/ModelRegistry.hpp" #include "../model/Shape.hpp" #include "../model/ShapeRegistry.hpp" +#include "../shared/ResourceIndex.hpp" #include "../world/BlockType.hpp" #include "../world/BlockTypeRegistry.hpp" #include "../world/Entity.hpp" @@ -318,7 +318,7 @@ CuboidBounds slab_shape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }}); void AssetLoader::LoadBlockTypes( const string &set_name, BlockTypeRegistry ®, - TextureIndex &tex_index, + ResourceIndex &tex_index, const ShapeRegistry &shapes ) const { string full = data + set_name + ".types"; @@ -499,7 +499,7 @@ Font AssetLoader::LoadFont(const string &name, int size) const { void AssetLoader::LoadModels( const string &set_name, ModelRegistry &models, - TextureIndex &tex_index, + ResourceIndex &tex_index, const ShapeRegistry &shapes ) const { string full = data + set_name + ".models"; @@ -584,7 +584,7 @@ void AssetLoader::LoadTexture(const string &name, ArrayTexture &tex, int layer) SDL_FreeSurface(srf); } -void AssetLoader::LoadTextures(const TextureIndex &index, ArrayTexture &tex) const { +void AssetLoader::LoadTextures(const ResourceIndex &index, ArrayTexture &tex) const { // TODO: where the hell should that size come from? tex.Reserve(16, 16, index.Size(), Format()); for (const auto &entry : index.Entries()) { @@ -593,22 +593,6 @@ void AssetLoader::LoadTextures(const TextureIndex &index, ArrayTexture &tex) con } -TextureIndex::TextureIndex() -: id_map() { - -} - -int TextureIndex::GetID(const string &name) { - auto entry = id_map.find(name); - if (entry == id_map.end()) { - auto result = id_map.emplace(name, Size()); - return result.first->second; - } else { - return entry->second; - } -} - - void FrameCounter::EnterFrame() noexcept { last_enter = SDL_GetTicks(); last_tick = last_enter; diff --git a/src/client/InteractiveState.hpp b/src/client/InteractiveState.hpp index 2bc459a..02385e2 100644 --- a/src/client/InteractiveState.hpp +++ b/src/client/InteractiveState.hpp @@ -9,13 +9,11 @@ #include "../app/IntervalTimer.hpp" #include "../graphics/SkyBox.hpp" #include "../io/WorldSave.hpp" -#include "../model/ModelRegistry.hpp" -#include "../model/ShapeRegistry.hpp" #include "../net/Packet.hpp" +#include "../shared/WorldResources.hpp" #include "../ui/HUD.hpp" #include "../ui/InteractiveManipulator.hpp" #include "../ui/Interface.hpp" -#include "../world/BlockTypeRegistry.hpp" #include "../world/ChunkRenderer.hpp" #include "../world/EntityState.hpp" #include "../world/Player.hpp" @@ -40,7 +38,6 @@ public: World &GetWorld() noexcept { return world; } Player &GetPlayer() noexcept { return player; } ChunkReceiver &GetChunkReceiver() noexcept { return chunk_receiver; } - ModelRegistry &GetModels() noexcept { return models; } void OnEnter() override; @@ -69,9 +66,7 @@ private: private: MasterState &master; - ShapeRegistry shapes; - BlockTypeRegistry block_types; - ModelRegistry models; + WorldResources res; WorldSave save; World world; Player &player; diff --git a/src/client/client.cpp b/src/client/client.cpp index cc3106f..236fa1a 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -4,7 +4,6 @@ #include "../app/Environment.hpp" #include "../app/init.hpp" -#include "../app/TextureIndex.hpp" #include "../model/Model.hpp" #include "../io/WorldSave.hpp" #include "../world/ChunkIndex.hpp" @@ -48,11 +47,9 @@ void InitialState::Render(Viewport &viewport) { // TODO: this clutter is a giant mess InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) : master(master) -, shapes() -, block_types() -, models() +, res() , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host)) -, world(block_types, master.GetWorldConf()) +, world(res.block_types, master.GetWorldConf()) , player(*world.AddPlayer(master.GetConfig().player.name)) , hud(master.GetEnv(), master.GetConfig(), player) , manip(master.GetEnv(), player.GetEntity()) @@ -66,12 +63,9 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) if (!save.Exists()) { save.Write(master.GetWorldConf()); } - TextureIndex tex_index; - master.GetEnv().loader.LoadShapes("default", shapes); - master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index, shapes); - master.GetEnv().loader.LoadModels("default", models, tex_index, shapes); - interface.SetInventorySlots(block_types.size() - 1); - chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index); + res.Load(master.GetEnv().loader, "default"); + interface.SetInventorySlots(res.block_types.size() - 1); + chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index); chunk_renderer.FogDensity(master.GetWorldConf().fog_density); loop_timer.Start(); if (save.Exists(player)) { @@ -120,7 +114,7 @@ void InteractiveState::Update(int dt) { } else { hud.FocusNone(); } - hud.Display(block_types[player.GetInventorySlot() + 1]); + hud.Display(res.block_types[player.GetInventorySlot() + 1]); loop_timer.Update(dt); master.Update(dt); chunk_receiver.Update(dt); @@ -164,8 +158,8 @@ void InteractiveState::Handle(const Packet::SpawnEntity &pack) { pack.ReadEntity(entity); uint32_t skel_id; pack.ReadSkeletonID(skel_id); - if (skel_id > 0 && skel_id <= models.size()) { - Model &skel = models.Get(skel_id); + if (skel_id > 0 && skel_id <= res.models.size()) { + Model &skel = res.models.Get(skel_id); skel.Instantiate(entity.GetModel()); } cout << "spawned entity #" << entity_id << " (" << entity.Name() @@ -257,7 +251,7 @@ void InteractiveState::Handle(const Packet::BlockUpdate &pack) { Block block; pack.ReadIndex(index, i); pack.ReadBlock(block, i); - if (index < Chunk::size && block.type < block_types.size()) { + if (index < Chunk::size && block.type < res.block_types.size()) { manip.SetBlock(*chunk, index, block); } } diff --git a/src/model/Part.hpp b/src/model/Part.hpp index f71efc5..3e3b570 100644 --- a/src/model/Part.hpp +++ b/src/model/Part.hpp @@ -17,9 +17,9 @@ class DirectionalLighting; class EntityMesh; class Instance; class Model; +class ResourceIndex; class Shape; class ShapeRegistry; -class TextureIndex; class TokenStreamReader; struct Part { @@ -33,7 +33,7 @@ public: Part(); ~Part(); - void Read(TokenStreamReader &, TextureIndex &, const ShapeRegistry &); + void Read(TokenStreamReader &, ResourceIndex &, const ShapeRegistry &); Part &AddChild(); const std::list &Children() const noexcept { return children; } diff --git a/src/model/model.cpp b/src/model/model.cpp index 63187b8..55050e0 100644 --- a/src/model/model.cpp +++ b/src/model/model.cpp @@ -5,10 +5,10 @@ #include "Shape.hpp" #include "ShapeRegistry.hpp" -#include "../app/TextureIndex.hpp" #include "../io/TokenStreamReader.hpp" #include "../graphics/DirectionalLighting.hpp" #include "../graphics/EntityMesh.hpp" +#include "../shared/ResourceIndex.hpp" #include #include @@ -101,7 +101,7 @@ Part::~Part() { } -void Part::Read(TokenStreamReader &in, TextureIndex &tex_index, const ShapeRegistry &shapes) { +void Part::Read(TokenStreamReader &in, ResourceIndex &tex_index, const ShapeRegistry &shapes) { std::string name; std::string shape_name; std::string tex_name; diff --git a/src/server/ServerState.cpp b/src/server/ServerState.cpp index 3e2869f..0bec391 100644 --- a/src/server/ServerState.cpp +++ b/src/server/ServerState.cpp @@ -1,7 +1,6 @@ #include "ServerState.hpp" #include "../app/Environment.hpp" -#include "../app/TextureIndex.hpp" #include "../io/WorldSave.hpp" #include "../net/io.hpp" @@ -19,25 +18,20 @@ ServerState::ServerState( const Config &config ) : env(env) -, shapes() -, block_types() -, models() -, world(block_types, wc) +, res() +, world(res.block_types, wc) , generator(gc) , chunk_loader(world.Chunks(), generator, ws) -, spawner(world, models, env.rng) +, spawner(world, res.models, env.rng) , server(config.net, world, wc, ws) , loop_timer(16) { - TextureIndex tex_index; - env.loader.LoadShapes("default", shapes); - env.loader.LoadBlockTypes("default", block_types, tex_index, shapes); - env.loader.LoadModels("default", models, tex_index, shapes); - if (models.size() < 2) { + res.Load(env.loader, "default"); + if (res.models.size() < 2) { throw std::runtime_error("need at least two models to run"); } - generator.LoadTypes(block_types); - spawner.LimitModels(1, models.size()); - server.SetPlayerModel(models[0]); + generator.LoadTypes(res.block_types); + spawner.LimitModels(1, res.models.size()); + server.SetPlayerModel(res.models[0]); loop_timer.Start(); diff --git a/src/server/ServerState.hpp b/src/server/ServerState.hpp index b62cf09..fbf28e5 100644 --- a/src/server/ServerState.hpp +++ b/src/server/ServerState.hpp @@ -5,9 +5,7 @@ #include "../ai/Spawner.hpp" #include "../app/IntervalTimer.hpp" #include "../app/State.hpp" -#include "../model/ModelRegistry.hpp" -#include "../model/ShapeRegistry.hpp" -#include "../world/BlockTypeRegistry.hpp" +#include "../shared/WorldResources.hpp" #include "../world/ChunkLoader.hpp" #include "../world/Generator.hpp" #include "../world/World.hpp" @@ -40,9 +38,7 @@ public: private: HeadlessEnvironment &env; - ShapeRegistry shapes; - BlockTypeRegistry block_types; - ModelRegistry models; + WorldResources res; World world; Generator generator; ChunkLoader chunk_loader; diff --git a/src/app/TextureIndex.hpp b/src/shared/ResourceIndex.hpp similarity index 51% rename from src/app/TextureIndex.hpp rename to src/shared/ResourceIndex.hpp index 8c6e22f..e04ec6f 100644 --- a/src/app/TextureIndex.hpp +++ b/src/shared/ResourceIndex.hpp @@ -1,5 +1,5 @@ -#ifndef BLANK_APP_TEXTUREINDEX_HPP_ -#define BLANK_APP_TEXTUREINDEX_HPP_ +#ifndef BLANK_SHARED_RESOURCEINDEX_HPP_ +#define BLANK_SHARED_RESOURCEINDEX_HPP_ #include #include @@ -7,14 +7,14 @@ namespace blank { -class TextureIndex { +class ResourceIndex { - using MapType = std::map; + using MapType = std::map; public: - TextureIndex(); + ResourceIndex(); - int GetID(const std::string &); + std::size_t GetID(const std::string &); std::size_t Size() const noexcept { return id_map.size(); } const MapType &Entries() const noexcept { return id_map; } diff --git a/src/shared/WorldResources.hpp b/src/shared/WorldResources.hpp new file mode 100644 index 0000000..d92101d --- /dev/null +++ b/src/shared/WorldResources.hpp @@ -0,0 +1,33 @@ +#ifndef BLANK_SHARED_WORLDRESOURCES_HPP_ +#define BLANK_SHARED_WORLDRESOURCES_HPP_ + +#include "ResourceIndex.hpp" +#include "../model/ModelRegistry.hpp" +#include "../model/ShapeRegistry.hpp" +#include "../world/BlockTypeRegistry.hpp" + +#include + + +namespace blank { + +class AssetLoader; + +struct WorldResources { + + ShapeRegistry shapes; + BlockTypeRegistry block_types; + ModelRegistry models; + + ResourceIndex tex_index; + + + WorldResources(); + + void Load(const AssetLoader &, const std::string &set); + +}; + +} + +#endif diff --git a/src/shared/shared.cpp b/src/shared/shared.cpp new file mode 100644 index 0000000..9541c95 --- /dev/null +++ b/src/shared/shared.cpp @@ -0,0 +1,39 @@ +#include "ResourceIndex.hpp" +#include "WorldResources.hpp" + +#include "../app/Assets.hpp" + + +namespace blank { + +ResourceIndex::ResourceIndex() +: id_map() { + +} + +std::size_t ResourceIndex::GetID(const std::string &name) { + auto entry = id_map.find(name); + if (entry == id_map.end()) { + auto result = id_map.emplace(name, Size()); + return result.first->second; + } else { + return entry->second; + } +} + + +WorldResources::WorldResources() +: shapes() +, block_types() +, models() +, tex_index() { + +} + +void WorldResources::Load(const AssetLoader &loader, const std::string &set) { + loader.LoadShapes("default", shapes); + loader.LoadBlockTypes("default", block_types, tex_index, shapes); + loader.LoadModels("default", models, tex_index, shapes); +} + +} diff --git a/src/standalone/MasterState.cpp b/src/standalone/MasterState.cpp index 9d95966..7ae6741 100644 --- a/src/standalone/MasterState.cpp +++ b/src/standalone/MasterState.cpp @@ -3,7 +3,6 @@ #include "../app/Config.hpp" #include "../app/Environment.hpp" #include "../app/init.hpp" -#include "../app/TextureIndex.hpp" #include "../io/WorldSave.hpp" #include @@ -21,11 +20,9 @@ MasterState::MasterState( ) : config(config) , env(env) -, shapes() -, block_types() -, models() +, res() , save(save) -, world(block_types, wc) +, world(res.block_types, wc) , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3)) , player(*world.AddPlayer(config.player.name)) , spawn_player(false) @@ -36,21 +33,18 @@ MasterState::MasterState( , generator(gc) , chunk_loader(world.Chunks(), generator, save) , chunk_renderer(player.GetChunks()) -, spawner(world, models, env.rng) +, spawner(world, res.models, env.rng) , sky(env.loader.LoadCubeMap("skybox")) , preload(env, chunk_loader, chunk_renderer) , unload(env, world.Chunks(), save) { - TextureIndex tex_index; - env.loader.LoadShapes("default", shapes); - env.loader.LoadBlockTypes("default", block_types, tex_index, shapes); - env.loader.LoadModels("default", models, tex_index, shapes); - if (models.size() < 2) { + res.Load(env.loader, "default"); + if (res.models.size() < 2) { throw std::runtime_error("need at least two models to run"); } - spawner.LimitModels(0, models.size()); - interface.SetInventorySlots(block_types.size() - 1); - generator.LoadTypes(block_types); - chunk_renderer.LoadTextures(env.loader, tex_index); + spawner.LimitModels(0, res.models.size()); + interface.SetInventorySlots(res.block_types.size() - 1); + generator.LoadTypes(res.block_types); + chunk_renderer.LoadTextures(env.loader, res.tex_index); chunk_renderer.FogDensity(wc.fog_density); if (save.Exists(player)) { save.Read(player); @@ -119,7 +113,7 @@ void MasterState::Update(int dt) { } else { hud.FocusNone(); } - hud.Display(block_types[player.GetInventorySlot() + 1]); + hud.Display(res.block_types[player.GetInventorySlot() + 1]); hud.Update(dt); spawner.Update(dt); world.Update(dt); diff --git a/src/standalone/MasterState.hpp b/src/standalone/MasterState.hpp index f72c38c..6e63020 100644 --- a/src/standalone/MasterState.hpp +++ b/src/standalone/MasterState.hpp @@ -8,13 +8,11 @@ #include "UnloadState.hpp" #include "../ai/Spawner.hpp" #include "../graphics/SkyBox.hpp" -#include "../model/ModelRegistry.hpp" -#include "../model/ShapeRegistry.hpp" +#include "../shared/WorldResources.hpp" #include "../ui/DirectInput.hpp" #include "../ui/HUD.hpp" #include "../ui/InteractiveManipulator.hpp" #include "../ui/Interface.hpp" -#include "../world/BlockTypeRegistry.hpp" #include "../world/ChunkIndex.hpp" #include "../world/ChunkLoader.hpp" #include "../world/ChunkRenderer.hpp" @@ -63,9 +61,7 @@ public: private: Config &config; Environment &env; - ShapeRegistry shapes; - BlockTypeRegistry block_types; - ModelRegistry models; + WorldResources res; const WorldSave &save; World world; ChunkIndex &spawn_index; diff --git a/src/world/ChunkRenderer.hpp b/src/world/ChunkRenderer.hpp index 714bb84..7234941 100644 --- a/src/world/ChunkRenderer.hpp +++ b/src/world/ChunkRenderer.hpp @@ -14,7 +14,7 @@ namespace blank { class AssetLoader; class BlockMesh; class ChunkIndex; -class TextureIndex; +class ResourceIndex; class Viewport; class ChunkRenderer { @@ -23,7 +23,7 @@ public: explicit ChunkRenderer(ChunkIndex &); ~ChunkRenderer(); - void LoadTextures(const AssetLoader &, const TextureIndex &); + void LoadTextures(const AssetLoader &, const ResourceIndex &); void FogDensity(float d) noexcept { fog_density = d; } int MissingChunks() const noexcept; diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index f819ef9..ef9f578 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -675,7 +675,7 @@ int ChunkRenderer::MissingChunks() const noexcept { return index.MissingChunks(); } -void ChunkRenderer::LoadTextures(const AssetLoader &loader, const TextureIndex &tex_index) { +void ChunkRenderer::LoadTextures(const AssetLoader &loader, const ResourceIndex &tex_index) { block_tex.Bind(); loader.LoadTextures(tex_index, block_tex); block_tex.FilterNearest(); -- 2.39.2