diff --git a/src/frontend/BlocksPreview.cpp b/src/frontend/BlocksPreview.cpp index 9b81ccbf..26a39d0c 100644 --- a/src/frontend/BlocksPreview.cpp +++ b/src/frontend/BlocksPreview.cpp @@ -2,6 +2,7 @@ #include +#include "../assets/Assets.h" #include "../graphics/Viewport.h" #include "../graphics/Shader.h" #include "../graphics/Texture.h" @@ -11,10 +12,10 @@ #include "../voxels/Block.h" #include "ContentGfxCache.h" -BlocksPreview::BlocksPreview(Shader* shader, - Atlas* atlas, - const ContentGfxCache* cache) - : shader(shader), atlas(atlas), cache(cache) { +BlocksPreview::BlocksPreview(Assets* assets, const ContentGfxCache* cache) + : shader(assets->getShader("ui3d")), + atlas(assets->getAtlas("blocks")), + cache(cache) { batch = std::make_unique(1024); } @@ -37,9 +38,8 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin uint width = viewport->getWidth(); uint height = viewport->getHeight(); - y = height - y - 1; + y = height - y - 1 - 35 /* magic garbage */; x += 2; - y -= 35; glm::vec3 offset (x/float(width) * 2, y/float(height) * 2, 0.0f); shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); diff --git a/src/frontend/BlocksPreview.h b/src/frontend/BlocksPreview.h index c3415d4c..0a1a03f3 100644 --- a/src/frontend/BlocksPreview.h +++ b/src/frontend/BlocksPreview.h @@ -5,6 +5,7 @@ #include #include +class Assets; class Viewport; class Shader; class Atlas; @@ -19,11 +20,11 @@ class BlocksPreview { const ContentGfxCache* const cache; const Viewport* viewport; public: - BlocksPreview(Shader* shader, Atlas* atlas, const ContentGfxCache* cache); + BlocksPreview(Assets* assets, const ContentGfxCache* cache); ~BlocksPreview(); void begin(const Viewport* viewport); void draw(const Block* block, int x, int y, int size, glm::vec4 tint); }; -#endif // FRONTEND_BLOCKS_PREVIEW_H_ \ No newline at end of file +#endif // FRONTEND_BLOCKS_PREVIEW_H_ diff --git a/src/frontend/ContentGfxCache.h b/src/frontend/ContentGfxCache.h index 804d0c15..1f2b3773 100644 --- a/src/frontend/ContentGfxCache.h +++ b/src/frontend/ContentGfxCache.h @@ -19,4 +19,4 @@ public: } }; -#endif // FRONTEND_BLOCKS_GFX_CACHE_H_ \ No newline at end of file +#endif // FRONTEND_BLOCKS_GFX_CACHE_H_ diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index 1ee28db7..eb99fbeb 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -3,6 +3,7 @@ #include #include "BlocksPreview.h" +#include "LevelFrontend.h" #include "../window/Events.h" #include "../assets/Assets.h" #include "../graphics/Shader.h" @@ -15,18 +16,16 @@ InventoryView::InventoryView( int columns, - const Assets* assets, const ContentIndices* indices, - const ContentGfxCache* cache, - std::vector blocks) - : assets(assets), - indices(indices), - cache(cache), + LevelFrontend* frontend, + std::vector blocks) + : indices(indices), blocks(blocks), + frontend(frontend), columns(columns) { - blocksPreview = new BlocksPreview(assets->getShader("ui3d"), - assets->getAtlas("blocks"), - cache); +} + +InventoryView::~InventoryView() { } void InventoryView::setPosition(int x, int y) { @@ -48,6 +47,7 @@ void InventoryView::setSlotConsumer(slotconsumer consumer) { } void InventoryView::actAndDraw(const GfxContext* ctx) { + Assets* assets = frontend->getAssets(); Shader* uiShader = assets->getShader("ui"); auto viewport = ctx->getViewport(); @@ -73,6 +73,8 @@ void InventoryView::actAndDraw(const GfxContext* ctx) { } scroll = std::min(scroll, int(inv_h-viewport.getHeight())); scroll = std::max(scroll, 0); + + auto blocksPreview = frontend->getBlocksPreview(); blocksPreview->begin(&ctx->getViewport()); { Window::clearDepth(); diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index fe85be22..5cbd8b75 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -10,18 +10,15 @@ class Assets; class GfxContext; class ContentIndices; -class BlocksPreview; -class ContentGfxCache; +class LevelFrontend; typedef std::function slotconsumer; class InventoryView { - const Assets* assets; const ContentIndices* indices; - const ContentGfxCache* const cache; std::vector blocks; - BlocksPreview* blocksPreview; slotconsumer consumer = nullptr; + LevelFrontend* frontend; int scroll = 0; int columns; @@ -32,13 +29,15 @@ class InventoryView { public: InventoryView( int columns, - const Assets* assets, const ContentIndices* indices, - const ContentGfxCache* cache, + LevelFrontend* frontend, std::vector blocks); + virtual ~InventoryView(); + + virtual void actAndDraw(const GfxContext* ctx); + void setPosition(int x, int y); - void actAndDraw(const GfxContext* ctx); int getWidth() const; int getHeight() const; void setSlotConsumer(slotconsumer consumer); diff --git a/src/frontend/LevelFrontend.cpp b/src/frontend/LevelFrontend.cpp new file mode 100644 index 00000000..4b1c6542 --- /dev/null +++ b/src/frontend/LevelFrontend.cpp @@ -0,0 +1,33 @@ +#include "LevelFrontend.h" + +#include "../world/Level.h" +#include "../assets/Assets.h" +#include "BlocksPreview.h" +#include "ContentGfxCache.h" + +LevelFrontend::LevelFrontend(Level* level, Assets* assets) +: level(level), + assets(assets), + contentCache(std::make_unique(level->content, assets)), + blocksPreview(std::make_unique(assets, contentCache.get())) { + +} + +LevelFrontend::~LevelFrontend() { +} + +Level* LevelFrontend::getLevel() const { + return level; +} + +Assets* LevelFrontend::getAssets() const { + return assets; +} + +BlocksPreview* LevelFrontend::getBlocksPreview() const { + return blocksPreview.get(); +} + +ContentGfxCache* LevelFrontend::getContentGfxCache() const { + return contentCache.get(); +} diff --git a/src/frontend/LevelFrontend.h b/src/frontend/LevelFrontend.h new file mode 100644 index 00000000..095f5535 --- /dev/null +++ b/src/frontend/LevelFrontend.h @@ -0,0 +1,27 @@ +#ifndef FRONTEND_LEVEL_FRONTEND_H_ +#define FRONTEND_LEVEL_FRONTEND_H_ + +#include + +class Level; +class Assets; +class BlocksPreview; +class ContentGfxCache; + +class LevelFrontend { + Level* level; + Assets* assets; + std::unique_ptr contentCache; + std::unique_ptr blocksPreview; +public: + LevelFrontend(Level* level, Assets* assets); + ~LevelFrontend(); + + Level* getLevel() const; + Assets* getAssets() const; + BlocksPreview* getBlocksPreview() const; + ContentGfxCache* getContentGfxCache() const; +}; + + +#endif // FRONTEND_LEVEL_FRONTEND_H_ diff --git a/src/frontend/WorldRenderer.cpp b/src/frontend/WorldRenderer.cpp index 6c89902b..fc4344f5 100644 --- a/src/frontend/WorldRenderer.cpp +++ b/src/frontend/WorldRenderer.cpp @@ -27,7 +27,7 @@ #include "../maths/voxmaths.h" #include "../settings.h" #include "../engine.h" -#include "ContentGfxCache.h" +#include "LevelFrontend.h" #include "graphics/Skybox.h" using glm::vec3; @@ -38,12 +38,14 @@ using std::shared_ptr; WorldRenderer::WorldRenderer(Engine* engine, Level* level, - const ContentGfxCache* cache) + LevelFrontend* frontend) : engine(engine), level(level), frustumCulling(new Frustum()), lineBatch(new LineBatch()), - renderer( new ChunksRenderer(level, cache, engine->getSettings())) { + renderer(new ChunksRenderer(level, + frontend->getContentGfxCache(), + engine->getSettings())) { auto& settings = engine->getSettings(); level->events->listen(EVT_CHUNK_HIDDEN, diff --git a/src/frontend/WorldRenderer.h b/src/frontend/WorldRenderer.h index fda717b4..cb9fa295 100644 --- a/src/frontend/WorldRenderer.h +++ b/src/frontend/WorldRenderer.h @@ -21,7 +21,7 @@ class Texture; class Frustum; class Engine; class Chunks; -class ContentGfxCache; +class LevelFrontend; class Skybox; class WorldRenderer { @@ -34,7 +34,7 @@ class WorldRenderer { bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling); void drawChunks(Chunks* chunks, Camera* camera, Shader* shader); public: - WorldRenderer(Engine* engine, Level* level, const ContentGfxCache* cache); + WorldRenderer(Engine* engine, Level* level, LevelFrontend* frontend); ~WorldRenderer(); void draw(const GfxContext& context, Camera* camera); diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index be5004fc..231bda3b 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -39,47 +39,44 @@ #include "WorldRenderer.h" #include "BlocksPreview.h" #include "InventoryView.h" +#include "LevelFrontend.h" #include "../engine.h" #include "../core_defs.h" -using std::wstring; -using std::shared_ptr; using glm::vec2; using glm::vec3; using glm::vec4; using namespace gui; -inline Label* create_label(gui::wstringsupplier supplier) { - Label* label = new Label(L"-"); +inline std::shared_ptr