ContentPackRuntime + refactor

This commit is contained in:
MihailRis
2024-02-08 20:53:12 +03:00
parent 54e6d49847
commit 241d15e349
22 changed files with 224 additions and 169 deletions
+24 -26
View File
@@ -6,36 +6,34 @@
#include "../content/Content.h"
#include "../graphics/Atlas.h"
#include "../voxels/Block.h"
#include "../core_defs.h"
#include "UiDocument.h"
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) {
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : content(content) {
auto indices = content->getIndices();
sideregions = new UVRegion[indices->countBlockDefs() * 6];
Atlas* atlas = assets->getAtlas("blocks");
for (uint i = 0; i < indices->countBlockDefs(); i++) {
Block* def = indices->getBlockDef(i);
for (uint side = 0; side < 6; side++) {
std::string tex = def->textureFaces[side];
if (atlas->has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex);
} else {
if (atlas->has("notfound"))
sideregions[i * 6 + side] = atlas->get("notfound");
}
}
for (uint side = 0; side < def->modelTextures.size(); side++)
{
std::string tex = def->modelTextures[side];
if (atlas->has(tex)) {
def->modelUVs.push_back(atlas->get(tex));
} else {
if (atlas->has("notfound"))
def->modelUVs.push_back(atlas->get("notfound"));
}
}
sideregions = std::make_unique<UVRegion[]>(indices->countBlockDefs() * 6);
Atlas* atlas = assets->getAtlas("blocks");
for (uint i = 0; i < indices->countBlockDefs(); i++) {
Block* def = indices->getBlockDef(i);
for (uint side = 0; side < 6; side++) {
const std::string& tex = def->textureFaces[side];
if (atlas->has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex);
} else if (atlas->has(TEXTURE_NOTFOUND)) {
sideregions[i * 6 + side] = atlas->get(TEXTURE_NOTFOUND);
}
}
for (uint side = 0; side < def->modelTextures.size(); side++) {
const std::string& tex = def->modelTextures[side];
if (atlas->has(tex)) {
def->modelUVs.push_back(atlas->get(tex));
} else if (atlas->has(TEXTURE_NOTFOUND)) {
def->modelUVs.push_back(atlas->get(TEXTURE_NOTFOUND));
}
}
}
}
ContentGfxCache::~ContentGfxCache() {
delete[] sideregions;
}
+12 -1
View File
@@ -1,15 +1,24 @@
#ifndef FRONTEND_BLOCKS_GFX_CACHE_H_
#define FRONTEND_BLOCKS_GFX_CACHE_H_
#include <memory>
#include <string>
#include <unordered_map>
#include "../graphics/UVRegion.h"
#include "../typedefs.h"
class Content;
class Assets;
class UiDocument;
using uidocuments_map = std::unordered_map<std::string, std::shared_ptr<UiDocument>>;
class ContentGfxCache {
const Content* content;
// array of block sides uv regions (6 per block)
UVRegion* sideregions;
std::unique_ptr<UVRegion[]> sideregions;
// all loaded layouts
uidocuments_map layouts;
public:
ContentGfxCache(const Content* content, Assets* assets);
~ContentGfxCache();
@@ -17,6 +26,8 @@ public:
inline const UVRegion& getRegion(blockid_t id, int side) const {
return sideregions[id * 6 + side];
}
std::shared_ptr<UiDocument> getLayout(const std::string& id);
};
#endif // FRONTEND_BLOCKS_GFX_CACHE_H_
+2 -2
View File
@@ -150,10 +150,10 @@ void SlotView::draw(const GfxContext* pctx, Assets* assets) {
case item_icon_type::none:
break;
case item_icon_type::block: {
Block* cblock = content->requireBlock(item->icon);
const Block& cblock = content->requireBlock(item->icon);
batch->texture(previews->getTexture());
UVRegion region = previews->get(cblock->name);
UVRegion region = previews->get(cblock.name);
batch->rect(
coord.x, coord.y, slotSize, slotSize,
0, 0, 0, region, false, true, tint);