diff --git a/res/texts/fi_FI.txt b/res/texts/fi_FI.txt new file mode 100644 index 00000000..dbc85649 --- /dev/null +++ b/res/texts/fi_FI.txt @@ -0,0 +1,53 @@ +# Общее +Yes=Joo +No=Ei +Ok = Kyllä +Cancel= Hylää +Back = Takaisin +Continue=Jatka + +error.pack-not-found=Pakettia ei löytynyt! + +# Меню +menu.New World = Uusi Maailma +menu.Quit=Poistu +menu.Continue=Jatka +menu.Save and Quit to Menu=Tallenna ja Takaisin valikoon +menu.missing-content=Puuttuu jotkut lisäosat! +menu.Controls=Ohjaus +menu.Back to Main Menu=Takaisin Valikoon +menu.Settings=Asetukset +menu.Content=Lisäosat +menu.Seed=Siemen +menu.Name=Nimi +menu.Create World=Luo Maailma + +world.convert-request=Indeksit vaihdettu! Lataa maailma uudeleen? + +# Настройки +settings.Load Distance=Lataus Alue +settings.Load Speed=Lataus Nopeus +settings.Fog Curve=Sumun valaistus +settings.Backlight=Valaistus +settings.V-Sync=Pystytahdistus + +settings.FOV=Näkökenttä +settings.Mouse Sensitivity=Hiiren nopeus +settings.Language=Kieli + +# Управление +movement.forward=Eteenpäin +movement.back=Taaksepäin +movement.left=Vaseemalle +movement.right=Oikealle +movement.jump=Hyppy +movement.sprint=Kiihtyvyys +movement.crouch=Istu alas +movement.cheat=Huijata +hud.inventory=Varasto +player.pick=Ottaa lohko +player.attack=Lyödä / Rikkoa +player.build=Aseta lohko +player.flight=Lento +camera.zoom=Lähentäminen +camera.mode=Vaihda Kameratila \ No newline at end of file diff --git a/res/texts/langs.json b/res/texts/langs.json index 1f1c884c..2dc4aab4 100644 --- a/res/texts/langs.json +++ b/res/texts/langs.json @@ -14,6 +14,9 @@ }, "uk_UA": { "name": "Українська" + }, + "fi_FI": { + "name": "Suomi" } }, "fallback": "en_US" diff --git a/src/content/Content.cpp b/src/content/Content.cpp index 4c924087..773a0bda 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -4,22 +4,43 @@ #include #include "../voxels/Block.h" +#include "../content/ItemDef.h" using glm::vec3; -using std::vector; using std::string; using std::unordered_map; void ContentBuilder::add(Block* def) { - if (blockDefs.find(def->name) != blockDefs.end()) { - throw std::runtime_error("block name duplicate: "+def->name); - } + checkIdentifier(def->name); blockDefs[def->name] = def; blockIds.push_back(def->name); } +void ContentBuilder::add(ItemDef* def) { + checkIdentifier(def->name); + itemDefs[def->name] = def; + itemIds.push_back(def->name); +} + +void ContentBuilder::checkIdentifier(std::string id) { + contenttype result; + if ((checkContentType(id) != contenttype::none)) { + throw contentindexreuse_error("identifier "+id+" is already used", result); + } +} + +contenttype ContentBuilder::checkContentType(std::string id) { + if (blockDefs.find(id) != blockDefs.end()) { + return contenttype::block; + } + if (itemDefs.find(id) != itemDefs.end()) { + return contenttype::item; + } + return contenttype::none; +} + Content* ContentBuilder::build() { - vector blockDefsIndices; + std::vector blockDefsIndices; DrawGroups* groups = new DrawGroups; for (const string& name : blockIds) { Block* def = blockDefs[name]; @@ -42,15 +63,26 @@ Content* ContentBuilder::build() { if (groups->find(def->drawGroup) == groups->end()) { groups->insert(def->drawGroup); } - - } - ContentIndices* indices = new ContentIndices(blockDefsIndices); + + std::vector itemDefsIndices; + for (const string& name : itemIds) { + ItemDef* def = itemDefs[name]; + + // Generating runtime info + def->rt.id = itemDefsIndices.size(); + itemDefsIndices.push_back(def); + } + + auto indices = new ContentIndices(blockDefsIndices, itemDefsIndices); return new Content(indices, groups, blockDefs); } -ContentIndices::ContentIndices(vector blockDefs) - : blockDefs(blockDefs) { +ContentIndices::ContentIndices( + std::vector blockDefs, + std::vector itemDefs) + : blockDefs(blockDefs), + itemDefs(itemDefs) { } Content::Content(ContentIndices* indices, DrawGroups* drawGroups, @@ -80,3 +112,19 @@ Block* Content::requireBlock(string id) const { } return found->second; } + +ItemDef* Content::findItem(string id) const { + auto found = itemDefs.find(id); + if (found == itemDefs.end()) { + return nullptr; + } + return found->second; +} + +ItemDef* Content::requireItem(string id) const { + auto found = itemDefs.find(id); + if (found == itemDefs.end()) { + throw std::runtime_error("missing item "+id); + } + return found->second; +} diff --git a/src/content/Content.h b/src/content/Content.h index f54f7fe5..3c661582 100644 --- a/src/content/Content.h +++ b/src/content/Content.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include "../typedefs.h" @@ -10,23 +11,47 @@ typedef std::set DrawGroups; class Block; +class ItemDef; class Content; +enum class contenttype { + none, block, item +}; + +class contentindexreuse_error: public std::runtime_error { + contenttype type; +public: + contentindexreuse_error(const std::string& msg, contenttype type) + : std::runtime_error(msg), type(type) {} + + inline contenttype getType() const { + return type; + } +}; + class ContentBuilder { std::unordered_map blockDefs; std::vector blockIds; + + std::unordered_map itemDefs; + std::vector itemIds; public: void add(Block* def); + void add(ItemDef* def); + + void checkIdentifier(std::string id); + contenttype checkContentType(std::string id); Content* build(); }; /* Runtime defs cache: indices */ class ContentIndices { - // blockDefs must be a plain vector with block id used as index std::vector blockDefs; + std::vector itemDefs; public: - ContentIndices(std::vector blockDefs); + ContentIndices(std::vector blockDefs, + std::vector itemDefs); inline Block* getBlockDef(blockid_t id) const { if (id >= blockDefs.size()) @@ -34,19 +59,34 @@ public: return blockDefs[id]; } + inline ItemDef* getItemDef(itemid_t id) const { + if (id >= itemDefs.size()) + return nullptr; + return itemDefs[id]; + } + inline size_t countBlockDefs() const { return blockDefs.size(); } + inline size_t countItemDefs() const { + return itemDefs.size(); + } + // use this for critical spots to prevent range check overhead const Block* const* getBlockDefs() const { return blockDefs.data(); } + + const ItemDef* const* getItemDefs() const { + return itemDefs.data(); + } }; /* Content is a definitions repository */ class Content { std::unordered_map blockDefs; + std::unordered_map itemDefs; public: ContentIndices* const indices; DrawGroups* const drawGroups; @@ -57,6 +97,9 @@ public: Block* findBlock(std::string id) const; Block* requireBlock(std::string id) const; + + ItemDef* findItem(std::string id) const; + ItemDef* requireItem(std::string id) const; }; #endif // CONTENT_CONTENT_H_ \ No newline at end of file diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 6fd0a6f5..271f2e52 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -7,6 +7,7 @@ #include #include "Content.h" +#include "ItemDef.h" #include "../util/listutil.h" #include "../voxels/Block.h" #include "../files/files.h" @@ -21,10 +22,56 @@ namespace fs = std::filesystem; ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) { } +bool ContentLoader::fixPackIndices(fs::path folder, + json::JObject* indicesRoot, + std::string contentSection) { + + std::vector detected; + std::vector indexed; + if (fs::is_directory(folder)) { + for (auto entry : fs::directory_iterator(folder)) { + fs::path file = entry.path(); + if (fs::is_regular_file(file) && file.extension() == ".json") { + std::string name = file.stem().string(); + if (name[0] == '_') + continue; + detected.push_back(name); + } + } + } + + bool modified = false; + if (!indicesRoot->has(contentSection)) { + indicesRoot->putArray(contentSection); + } + json::JArray* arr = indicesRoot->arr(contentSection); + if (arr) { + for (uint i = 0; i < arr->size(); i++) { + std::string name = arr->str(i); + if (!util::contains(detected, name)) { + arr->remove(i); + i--; + modified = true; + continue; + } + indexed.push_back(name); + } + } + for (auto name : detected) { + if (!util::contains(indexed, name)) { + arr->put(name); + modified = true; + } + } + return modified; +} + void ContentLoader::fixPackIndices() { auto folder = pack->folder; auto indexFile = pack->getContentFile(); auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER; + auto itemsFolder = folder/ContentPack::ITEMS_FOLDER; + std::unique_ptr root; if (fs::is_regular_file(indexFile)) { root.reset(files::read_json(indexFile)); @@ -32,43 +79,11 @@ void ContentLoader::fixPackIndices() { root.reset(new json::JObject()); } - std::vector detectedBlocks; - std::vector indexedBlocks; - if (fs::is_directory(blocksFolder)) { - for (auto entry : fs::directory_iterator(blocksFolder)) { - fs::path file = entry.path(); - if (fs::is_regular_file(file) && file.extension() == ".json") { - std::string name = file.stem().string(); - if (name[0] == '_') - continue; - detectedBlocks.push_back(name); - } - } - } - bool modified = false; - if (!root->has("blocks")) { - root->putArray("blocks"); - } - json::JArray* blocksarr = root->arr("blocks"); - if (blocksarr) { - for (uint i = 0; i < blocksarr->size(); i++) { - std::string name = blocksarr->str(i); - if (!util::contains(detectedBlocks, name)) { - blocksarr->remove(i); - i--; - modified = true; - continue; - } - indexedBlocks.push_back(name); - } - } - for (auto name : detectedBlocks) { - if (!util::contains(indexedBlocks, name)) { - blocksarr->put(name); - modified = true; - } - } + + modified |= fixPackIndices(blocksFolder, root.get(), "blocks"); + modified |= fixPackIndices(itemsFolder, root.get(), "items"); + if (modified){ // rewrite modified json std::cout << indexFile << std::endl; @@ -165,6 +180,39 @@ Block* ContentLoader::loadBlock(std::string name, fs::path file) { return def.release(); } +ItemDef* ContentLoader::loadItem(std::string name, std::filesystem::path file) { + std::unique_ptr root(files::read_json(file)); + std::unique_ptr def(new ItemDef(name)); + + return def.release(); +} + +Block* ContentLoader::loadBlock(std::string name) { + auto folder = pack->folder; + + std::string prefix = pack->id+":"+name; + fs::path configFile = folder/fs::path("blocks/"+name+".json"); + fs::path scriptfile = folder/fs::path("scripts/"+name+".lua"); + Block* def = loadBlock(prefix, configFile); + if (fs::is_regular_file(scriptfile)) { + scripting::load_block_script(prefix, scriptfile, &def->rt.funcsset); + } + return def; +} + +ItemDef* ContentLoader::loadItem(std::string name) { + auto folder = pack->folder; + + std::string prefix = pack->id+":"+name; + fs::path configFile = folder/fs::path("items/"+name+".json"); + fs::path scriptfile = folder/fs::path("scripts/"+name+".lua"); + ItemDef* def = loadItem(prefix, configFile); + if (fs::is_regular_file(scriptfile)) { + scripting::load_item_script(prefix, scriptfile, &def->rt.funcsset); + } + return def; +} + void ContentLoader::load(ContentBuilder* builder) { std::cout << "-- loading pack [" << pack->id << "]" << std::endl; @@ -178,15 +226,14 @@ void ContentLoader::load(ContentBuilder* builder) { json::JArray* blocksarr = root->arr("blocks"); if (blocksarr) { for (uint i = 0; i < blocksarr->size(); i++) { - std::string name = blocksarr->str(i); - std::string prefix = pack->id+":"+name; - fs::path blockfile = folder/fs::path("blocks/"+name+".json"); - Block* block = loadBlock(prefix, blockfile); - builder->add(block); - fs::path scriptfile = folder/fs::path("scripts/"+name+".lua"); - if (fs::is_regular_file(scriptfile)) { - scripting::load_block_script(prefix, scriptfile, &block->rt.funcsset); - } + builder->add(loadBlock(blocksarr->str(i))); + } + } + + json::JArray* itemsarr = root->arr("items"); + if (itemsarr) { + for (uint i = 0; i < itemsarr->size(); i++) { + builder->add(loadItem(itemsarr->str(i))); } } } diff --git a/src/content/ContentLoader.h b/src/content/ContentLoader.h index 7961da86..983ce581 100644 --- a/src/content/ContentLoader.h +++ b/src/content/ContentLoader.h @@ -5,16 +5,28 @@ #include class Block; +class ItemDef; class ContentPack; class ContentBuilder; +namespace json { + class JObject; +} + class ContentLoader { const ContentPack* pack; + + Block* loadBlock(std::string name); + ItemDef* loadItem(std::string name); public: ContentLoader(ContentPack* pack); + bool fixPackIndices(std::filesystem::path folder, + json::JObject* indicesRoot, + std::string contentSection); void fixPackIndices(); Block* loadBlock(std::string name, std::filesystem::path file); + ItemDef* loadItem(std::string name, std::filesystem::path file); void load(ContentBuilder* builder); }; diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index e79a664a..ae3ea758 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -11,6 +11,7 @@ namespace fs = std::filesystem; const std::string ContentPack::PACKAGE_FILENAME = "package.json"; const std::string ContentPack::CONTENT_FILENAME = "content.json"; const fs::path ContentPack::BLOCKS_FOLDER = "blocks"; +const fs::path ContentPack::ITEMS_FOLDER = "items"; contentpack_error::contentpack_error( std::string packId, diff --git a/src/content/ContentPack.h b/src/content/ContentPack.h index 98ef7db9..e7bcf335 100644 --- a/src/content/ContentPack.h +++ b/src/content/ContentPack.h @@ -31,6 +31,7 @@ struct ContentPack { static const std::string PACKAGE_FILENAME; static const std::string CONTENT_FILENAME; static const std::filesystem::path BLOCKS_FOLDER; + static const std::filesystem::path ITEMS_FOLDER; static bool is_pack(std::filesystem::path folder); static ContentPack read(std::filesystem::path folder); diff --git a/src/content/ItemDef.cpp b/src/content/ItemDef.cpp new file mode 100644 index 00000000..c5bef078 --- /dev/null +++ b/src/content/ItemDef.cpp @@ -0,0 +1,4 @@ +#include "ItemDef.h" + +ItemDef::ItemDef(std::string name) : name(name) { +} diff --git a/src/content/ItemDef.h b/src/content/ItemDef.h new file mode 100644 index 00000000..2223c1e0 --- /dev/null +++ b/src/content/ItemDef.h @@ -0,0 +1,25 @@ +#ifndef CONTENT_ITEM_DEF_H_ +#define CONTENT_ITEM_DEF_H_ + +#include +#include + +#include "../typedefs.h" + +struct item_funcs_set { + bool init: 1; +}; + +class ItemDef { +public: + std::string name; + + struct { + itemid_t id; + item_funcs_set funcsset {}; + } rt; + + ItemDef(std::string name); +}; + +#endif //CONTENT_ITEM_DEF_H_ diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 10ceb1ec..254e3188 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -18,6 +18,8 @@ #include "../coders/json.h" #include "../constants.h" +#include "../content/ItemDef.h" + #include #include #include @@ -461,12 +463,21 @@ void WorldFiles::writePacks(const World* world) { void WorldFiles::writeIndices(const ContentIndices* indices) { json::JObject root; + uint count; json::JArray& blocks = root.putArray("blocks"); - uint count = indices->countBlockDefs(); + count = indices->countBlockDefs(); for (uint i = 0; i < count; i++) { const Block* def = indices->getBlockDef(i); blocks.put(def->name); } + + json::JArray& items = root.putArray("items"); + count = indices->countItemDefs(); + for (uint i = 0; i < count; i++) { + const ItemDef* def = indices->getItemDef(i); + items.put(def->name); + } + files::write_string(getIndicesFile(), json::stringify(&root, true, " ")); } 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 6685535f..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,44 +16,45 @@ InventoryView::InventoryView( int columns, - Player* player, - const Assets* assets, const ContentIndices* indices, - const ContentGfxCache* cache, - std::vector blocks) - : player(player), - assets(assets), - indices(indices), - cache(cache), + LevelFrontend* frontend, + std::vector blocks) + : indices(indices), blocks(blocks), - invColumns(columns) { - blocksPreview = new BlocksPreview(assets->getShader("ui3d"), - assets->getAtlas("blocks"), - cache); + frontend(frontend), + columns(columns) { +} + +InventoryView::~InventoryView() { } void InventoryView::setPosition(int x, int y) { - this->invX = x; - this->invY = y; + position.x = x; + position.y = y; } int InventoryView::getWidth() const { - return invColumns * iconSize + (invColumns-1) * interval + padX * 2; + return columns * iconSize + (columns-1) * interval + padding.x * 2; } int InventoryView::getHeight() const { - uint inv_rows = ceildiv(blocks.size(), invColumns); - return inv_rows * iconSize + (inv_rows-1) * interval + padY * 2; + uint inv_rows = ceildiv(blocks.size(), columns); + return inv_rows * iconSize + (inv_rows-1) * interval + padding.y * 2; +} + +void InventoryView::setSlotConsumer(slotconsumer consumer) { + this->consumer = consumer; } void InventoryView::actAndDraw(const GfxContext* ctx) { + Assets* assets = frontend->getAssets(); Shader* uiShader = assets->getShader("ui"); auto viewport = ctx->getViewport(); uint inv_w = getWidth(); uint inv_h = getHeight(); - int xs = invX + padX; - int ys = invY + padY; + int xs = position.x + padding.x; + int ys = position.y + padding.y; glm::vec4 tint (1.0f); int mx = Events::cursor.x; @@ -62,15 +64,17 @@ void InventoryView::actAndDraw(const GfxContext* ctx) { auto batch = ctx->getBatch2D(); batch->texture(nullptr); batch->color = glm::vec4(0.0f, 0.0f, 0.0f, 0.5f); - batch->rect(invX, invY, inv_w, inv_h); + batch->rect(position.x, position.y, inv_w, inv_h); batch->render(); // blocks & items if (Events::scroll) { - inventoryScroll -= Events::scroll * (iconSize+interval); + scroll -= Events::scroll * (iconSize+interval); } - inventoryScroll = std::min(inventoryScroll, int(inv_h-viewport.getHeight())); - inventoryScroll = std::max(inventoryScroll, 0); + scroll = std::min(scroll, int(inv_h-viewport.getHeight())); + scroll = std::max(scroll, 0); + + auto blocksPreview = frontend->getBlocksPreview(); blocksPreview->begin(&ctx->getViewport()); { Window::clearDepth(); @@ -80,8 +84,8 @@ void InventoryView::actAndDraw(const GfxContext* ctx) { uint index = 0; for (uint i = 0; i < blocks.size(); i++) { Block* cblock = indices->getBlockDef(blocks[i]); - int x = xs + (iconSize+interval) * (index % invColumns); - int y = ys + (iconSize+interval) * (index / invColumns) - inventoryScroll; + int x = xs + (iconSize+interval) * (index % columns); + int y = ys + (iconSize+interval) * (index / columns) - scroll; if (y < -int(iconSize+interval) || y >= int(viewport.getHeight())) { index++; continue; @@ -91,7 +95,9 @@ void InventoryView::actAndDraw(const GfxContext* ctx) { tint.g *= 1.2f; tint.b *= 1.2f; if (Events::jclicked(mousecode::BUTTON_1)) { - player->chosenBlock = blocks[i]; + if (consumer) { + consumer(blocks[i]); + } } } else { tint = glm::vec4(1.0f); diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index 792fec42..5cbd8b75 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -2,44 +2,45 @@ #define FRONTEND_INVENTORY_VIEW_H_ #include +#include +#include #include "../typedefs.h" -class Player; class Assets; class GfxContext; class ContentIndices; -class BlocksPreview; -class ContentGfxCache; +class LevelFrontend; + +typedef std::function slotconsumer; class InventoryView { - Player* player; - const Assets* assets; const ContentIndices* indices; - const ContentGfxCache* const cache; std::vector blocks; - BlocksPreview* blocksPreview; + slotconsumer consumer = nullptr; + LevelFrontend* frontend; - int inventoryScroll = 0; - int invColumns; + int scroll = 0; + int columns; uint iconSize = 48; uint interval = 4; - int padX = interval; - int padY = interval; - int invX = 0; - int invY = 0; + glm::ivec2 padding {interval, interval}; + glm::ivec2 position {0, 0}; public: InventoryView( int columns, - Player* player, - 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); }; #endif // FRONTEND_INVENTORY_VIEW_H_ 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..179f9eb8 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; @@ -36,14 +36,14 @@ using glm::mat4; using std::string; using std::shared_ptr; -WorldRenderer::WorldRenderer(Engine* engine, - Level* level, - const ContentGfxCache* cache) +WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend) : engine(engine), - level(level), + level(frontend->getLevel()), 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..a1ecf2c5 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, LevelFrontend* frontend); ~WorldRenderer(); void draw(const GfxContext& context, Camera* camera); diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 2e8b7b3c..af8d0520 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -39,98 +39,66 @@ #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