From 7730da00569fac8fa6691e98ac2b89cd1d5307c7 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 5 Nov 2023 22:30:52 +0300 Subject: [PATCH] Project structure update --- src/{ => assets}/Assets.cpp | 6 ++--- src/{ => assets}/Assets.h | 6 ++--- src/{ => assets}/AssetsLoader.cpp | 23 +++++++++++++++---- src/{ => assets}/AssetsLoader.h | 7 +++--- .../png_loading.cpp => coders/png.cpp} | 4 ++-- src/coders/png.h | 12 ++++++++++ src/{declarations.cpp => definitions.cpp} | 17 +------------- src/{declarations.h => definitions.h} | 3 --- src/files/WorldFiles.cpp | 2 +- src/graphics/Texture.h | 2 -- src/hud_render.cpp | 2 +- src/loaders/png_loading.h | 10 -------- src/{ => maths}/voxmaths.h | 4 ++-- src/voxel_engine.cpp | 8 +++---- src/voxels/Chunks.cpp | 2 +- src/voxels/ChunksController.cpp | 4 ++-- src/voxels/ChunksStorage.cpp | 4 ++-- src/voxels/WorldGenerator.cpp | 2 +- src/world_render.cpp | 2 +- 19 files changed, 58 insertions(+), 62 deletions(-) rename src/{ => assets}/Assets.cpp (90%) rename src/{ => assets}/Assets.h (87%) rename src/{ => assets}/AssetsLoader.cpp (73%) rename src/{ => assets}/AssetsLoader.h (79%) rename src/{loaders/png_loading.cpp => coders/png.cpp} (99%) create mode 100644 src/coders/png.h rename src/{declarations.cpp => definitions.cpp} (82%) rename src/{declarations.h => definitions.h} (88%) delete mode 100644 src/loaders/png_loading.h rename src/{ => maths}/voxmaths.h (92%) diff --git a/src/Assets.cpp b/src/assets/Assets.cpp similarity index 90% rename from src/Assets.cpp rename to src/assets/Assets.cpp index c0666a24..459ac868 100644 --- a/src/Assets.cpp +++ b/src/assets/Assets.cpp @@ -1,8 +1,8 @@ #include "Assets.h" -#include "graphics/Texture.h" -#include "graphics/Shader.h" -#include "graphics/Font.h" +#include "../graphics/Texture.h" +#include "../graphics/Shader.h" +#include "../graphics/Font.h" Assets::~Assets() { for (auto& iter : shaders){ diff --git a/src/Assets.h b/src/assets/Assets.h similarity index 87% rename from src/Assets.h rename to src/assets/Assets.h index 61fd9d5b..e8122ff0 100644 --- a/src/Assets.h +++ b/src/assets/Assets.h @@ -1,5 +1,5 @@ -#ifndef SRC_ASSETS_H_ -#define SRC_ASSETS_H_ +#ifndef ASSETS_ASSETS_H_ +#define ASSETS_ASSETS_H_ #include #include @@ -24,4 +24,4 @@ public: void store(Font* font, std::string name); }; -#endif /* SRC_ASSETS_H_ */ +#endif /* ASSETS_ASSETS_H_ */ diff --git a/src/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp similarity index 73% rename from src/AssetsLoader.cpp rename to src/assets/AssetsLoader.cpp index eaeba7b9..86b85ae4 100644 --- a/src/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -33,9 +33,10 @@ bool AssetsLoader::loadNext() { return status; } -#include "graphics/Shader.h" -#include "graphics/Texture.h" -#include "graphics/Font.h" +#include "../coders/png.h" +#include "../graphics/Shader.h" +#include "../graphics/Texture.h" +#include "../graphics/Font.h" bool _load_shader(Assets* assets, const std::string& filename, const std::string& name) { Shader* shader = load_shader(filename + ".glslv", filename + ".glslf"); @@ -48,7 +49,7 @@ bool _load_shader(Assets* assets, const std::string& filename, const std::string } bool _load_texture(Assets* assets, const std::string& filename, const std::string& name) { - Texture* texture = load_texture(filename); + Texture* texture = png::load_texture(filename); if (texture == nullptr) { std::cerr << "failed to load texture '" << name << "'" << std::endl; return false; @@ -60,7 +61,7 @@ bool _load_texture(Assets* assets, const std::string& filename, const std::strin bool _load_font(Assets* assets, const std::string& filename, const std::string& name) { std::vector pages; for (size_t i = 0; i <= 4; i++) { - Texture* texture = load_texture(filename + "_" + std::to_string(i) + ".png"); + Texture* texture = png::load_texture(filename + "_" + std::to_string(i) + ".png"); if (texture == nullptr) { std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl; return false; @@ -77,3 +78,15 @@ void AssetsLoader::createDefaults(AssetsLoader& loader) { loader.addLoader(ASSET_TEXTURE, _load_texture); loader.addLoader(ASSET_FONT, _load_font); } + +void AssetsLoader::addDefaults(AssetsLoader& loader) { + loader.add(ASSET_SHADER, "res/main", "main"); + loader.add(ASSET_SHADER, "res/crosshair", "crosshair"); + loader.add(ASSET_SHADER, "res/lines", "lines"); + loader.add(ASSET_SHADER, "res/ui", "ui"); + + loader.add(ASSET_TEXTURE, "res/block.png", "block"); + loader.add(ASSET_TEXTURE, "res/slot.png", "slot"); + + loader.add(ASSET_FONT, "res/font", "normal"); +} diff --git a/src/AssetsLoader.h b/src/assets/AssetsLoader.h similarity index 79% rename from src/AssetsLoader.h rename to src/assets/AssetsLoader.h index ff4ff5ed..e1e7ac91 100644 --- a/src/AssetsLoader.h +++ b/src/assets/AssetsLoader.h @@ -1,5 +1,5 @@ -#ifndef SRC_ASSETS_LOADER_H -#define SRC_ASSETS_LOADER_H +#ifndef ASSETS_ASSETS_LOADER_H +#define ASSETS_ASSETS_LOADER_H #include #include @@ -33,6 +33,7 @@ public: bool loadNext(); static void createDefaults(AssetsLoader& loader); + static void addDefaults(AssetsLoader& loader); }; -#endif // SRC_ASSETS_LOADER_H \ No newline at end of file +#endif // ASSETS_ASSETS_LOADER_H diff --git a/src/loaders/png_loading.cpp b/src/coders/png.cpp similarity index 99% rename from src/loaders/png_loading.cpp rename to src/coders/png.cpp index f05fa8a0..3506ac01 100644 --- a/src/loaders/png_loading.cpp +++ b/src/coders/png.cpp @@ -1,4 +1,4 @@ -#include "png_loading.h" +#include "png.h" #include #include @@ -263,7 +263,7 @@ int _png_load(const char* file, int* pwidth, int* pheight){ #endif -Texture* load_texture(std::string filename){ +Texture* png::load_texture(std::string filename){ int width, height; GLuint texture = _png_load(filename.c_str(), &width, &height); if (texture == 0){ diff --git a/src/coders/png.h b/src/coders/png.h new file mode 100644 index 00000000..0bb46628 --- /dev/null +++ b/src/coders/png.h @@ -0,0 +1,12 @@ +#ifndef CODERS_PNG_H_ +#define CODERS_PNG_H_ + +#include + +class Texture; + +namespace png { + extern Texture* load_texture(std::string filename); +} + +#endif /* CODERS_PNG_H_ */ diff --git a/src/declarations.cpp b/src/definitions.cpp similarity index 82% rename from src/declarations.cpp rename to src/definitions.cpp index a6984798..7475cdc9 100644 --- a/src/declarations.cpp +++ b/src/definitions.cpp @@ -1,23 +1,8 @@ -#include "declarations.h" +#include "definitions.h" -#include "AssetsLoader.h" #include "window/Window.h" - #include "voxels/Block.h" - -void initialize_assets(AssetsLoader* loader) { - loader->add(ASSET_SHADER, "res/main", "main"); - loader->add(ASSET_SHADER, "res/crosshair", "crosshair"); - loader->add(ASSET_SHADER, "res/lines", "lines"); - loader->add(ASSET_SHADER, "res/ui", "ui"); - - loader->add(ASSET_TEXTURE, "res/block.png", "block"); - loader->add(ASSET_TEXTURE, "res/slot.png", "slot"); - - loader->add(ASSET_FONT, "res/font", "normal"); -} - // All in-game definitions (blocks, items, etc..) void setup_definitions() { for (size_t i = 0; i < 256; i++) { diff --git a/src/declarations.h b/src/definitions.h similarity index 88% rename from src/declarations.h rename to src/definitions.h index b2585cb3..236ac204 100644 --- a/src/declarations.h +++ b/src/definitions.h @@ -21,9 +21,6 @@ #define BLOCK_METAL 15 #define BLOCK_RUST 16 -class AssetsLoader; - -void initialize_assets(AssetsLoader* loader); void setup_definitions(); #endif // DECLARATIONS_H diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 94798429..ea351360 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -8,7 +8,7 @@ #include "../voxels/voxel.h" #include "../voxels/Chunk.h" #include "../typedefs.h" -#include "../voxmaths.h" +#include "../maths/voxmaths.h" #include #include diff --git a/src/graphics/Texture.h b/src/graphics/Texture.h index 0a185aae..0046c90f 100644 --- a/src/graphics/Texture.h +++ b/src/graphics/Texture.h @@ -16,6 +16,4 @@ public: void reload(unsigned char* data); }; -extern Texture* load_texture(std::string filename); - #endif /* GRAPHICS_TEXTURE_H_ */ diff --git a/src/hud_render.cpp b/src/hud_render.cpp index 48b22083..0ccf383d 100644 --- a/src/hud_render.cpp +++ b/src/hud_render.cpp @@ -5,7 +5,7 @@ #include #include "typedefs.h" -#include "Assets.h" +#include "assets/Assets.h" #include "graphics/Shader.h" #include "graphics/Batch2D.h" #include "graphics/Font.h" diff --git a/src/loaders/png_loading.h b/src/loaders/png_loading.h deleted file mode 100644 index e4dc3847..00000000 --- a/src/loaders/png_loading.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef LOADERS_PNG_LOADING_H_ -#define LOADERS_PNG_LOADING_H_ - -#include - -class Texture; - -extern Texture* load_texture(std::string filename); - -#endif /* LOADERS_PNG_LOADING_H_ */ diff --git a/src/voxmaths.h b/src/maths/voxmaths.h similarity index 92% rename from src/voxmaths.h rename to src/maths/voxmaths.h index 30476a15..59fe85c7 100644 --- a/src/voxmaths.h +++ b/src/maths/voxmaths.h @@ -1,7 +1,7 @@ #ifndef SRC_VOXNATHS_H_ #define SRC_VOXNATHS_H_ -#include "typedefs.h" +#include "../typedefs.h" inline int floordiv(int a, int b) { if (a < 0 && a % b) { @@ -40,4 +40,4 @@ inline light_t light_pack(ubyte r, ubyte g, ubyte b, ubyte s) { return r | (g << 4) | (b << 8) | (s << 12); } -#endif // SRC_VOXNATHS_H_ \ No newline at end of file +#endif // SRC_VOXNATHS_H_ diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index c63b511d..b1f670ec 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -29,9 +29,9 @@ #include "objects/Player.h" #include "world/Level.h" #include "world/World.h" -#include "declarations.h" -#include "Assets.h" -#include "AssetsLoader.h" +#include "definitions.h" +#include "assets/Assets.h" +#include "assets/AssetsLoader.h" #include "world_render.h" #include "hud_render.h" @@ -76,7 +76,7 @@ Engine::Engine(const EngineSettings& settings) { std::cout << "-- loading assets" << std::endl; AssetsLoader loader(assets); AssetsLoader::createDefaults(loader); - initialize_assets(&loader); + AssetsLoader::addDefaults(loader); while (loader.hasNext()) { if (!loader.loadNext()) { delete assets; diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 165a90ab..ec068e0e 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -8,7 +8,7 @@ #include "../world/LevelEvents.h" #include "../graphics/Mesh.h" -#include "../voxmaths.h" +#include "../maths/voxmaths.h" #include #include diff --git a/src/voxels/ChunksController.cpp b/src/voxels/ChunksController.cpp index ae13c0f0..942375f5 100644 --- a/src/voxels/ChunksController.cpp +++ b/src/voxels/ChunksController.cpp @@ -56,7 +56,7 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){ lighting->buildSkyLight(chunk->x, chunk->z); lighting->onChunkLoaded(chunk->x, chunk->z); chunk->setLighted(true); - return false; + return true; } continue; } @@ -101,4 +101,4 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){ } lighting->prebuildSkyLight(chunk->x, chunk->z); return true; -} \ No newline at end of file +} diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index dba41672..77d508b0 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -4,7 +4,7 @@ #include "VoxelsVolume.h" #include "Chunk.h" -#include "../voxmaths.h" +#include "../maths/voxmaths.h" #include "../lighting/Lightmap.h" @@ -97,4 +97,4 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume) const { } } } -} \ No newline at end of file +} diff --git a/src/voxels/WorldGenerator.cpp b/src/voxels/WorldGenerator.cpp index 208a57d0..962869d6 100644 --- a/src/voxels/WorldGenerator.cpp +++ b/src/voxels/WorldGenerator.cpp @@ -10,7 +10,7 @@ #include "../maths/FastNoiseLite.h" #include -#include "../declarations.h" +#include "../definitions.h" class PseudoRandom { unsigned short seed; diff --git a/src/world_render.cpp b/src/world_render.cpp index 19b8ba1d..67d22708 100644 --- a/src/world_render.cpp +++ b/src/world_render.cpp @@ -19,7 +19,7 @@ #include "world/Level.h" #include "world/LevelEvents.h" #include "objects/Player.h" -#include "Assets.h" +#include "assets/Assets.h" #include "player_control.h" using std::shared_ptr;