ContentGfxCache

This commit is contained in:
MihailRis
2023-11-22 14:53:35 +03:00
parent bf4ad5ca58
commit d92347097e
14 changed files with 86 additions and 51 deletions
+26
View File
@@ -0,0 +1,26 @@
#include "ContentGfxCache.h"
#include <string>
#include "../assets/Assets.h"
#include "../content/Content.h"
#include "../graphics/Atlas.h"
#include "../voxels/Block.h"
using std::string;
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) {
const ContentIndices* contentIds = content->indices;
sideregions = new UVRegion[contentIds->countBlockDefs() * 6];
Atlas* atlas = assets->getAtlas("blocks");
for (uint i = 0; i < contentIds->countBlockDefs(); i++) {
Block* def = contentIds->getBlockDef(i);
for (uint side = 0; side < 6; side++) {
string tex = def->textureFaces[side];
if (atlas->has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex);
}
}
}
}
+21
View File
@@ -0,0 +1,21 @@
#ifndef FRONTEND_BLOCKS_GFX_CACHE_H_
#define FRONTEND_BLOCKS_GFX_CACHE_H_
#include "../graphics/UVRegion.h"
#include "../typedefs.h"
class Content;
class Assets;
class ContentGfxCache {
// array of block sides uv regions (6 per block)
UVRegion* sideregions;
public:
ContentGfxCache(const Content* content, Assets* assets);
inline const UVRegion& getRegion(blockid_t id, int side) const {
return sideregions[id * 6 + side];
}
};
#endif // FRONTEND_BLOCKS_GFX_CACHE_H_
+8 -8
View File
@@ -32,6 +32,7 @@
#include "gui/panels.h"
#include "gui/UINode.h"
#include "gui/GUI.h"
#include "ContentGfxCache.h"
#include "screens.h"
#include "../engine.h"
#include "../core_defs.h"
@@ -49,10 +50,11 @@ inline Label* create_label(gui::wstringsupplier supplier) {
return label;
}
HudRenderer::HudRenderer(Engine* engine, Level* level)
HudRenderer::HudRenderer(Engine* engine, Level* level, const ContentGfxCache* cache)
: level(level),
assets(engine->getAssets()),
gui(engine->getGUI()) {
gui(engine->getGUI()),
cache(cache) {
auto menu = gui->getMenu();
batch = new Batch2D(1024);
uicamera = new Camera(vec3(), 1);
@@ -192,10 +194,9 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) {
}
if (cblock->model == BlockModel::block){
batch->blockSprite(x, y, icon_size, icon_size, (const UVRegion*)cblock->uvdata, tint);
batch->blockSprite(x, y, icon_size, icon_size, &cache->getRegion(cblock->id, 0), tint);
} else if (cblock->model == BlockModel::xsprite){
const UVRegion& region = (reinterpret_cast<UVRegion*>(cblock->uvdata))[3];
batch->sprite(x, y, icon_size, icon_size, region, tint);
batch->sprite(x, y, icon_size, icon_size, cache->getRegion(cblock->id, 3), tint);
}
}
}
@@ -267,10 +268,9 @@ void HudRenderer::draw(const GfxContext& ctx){
Block* cblock = contentIds->getBlockDef(player->choosenBlock);
assert(cblock != nullptr);
if (cblock->model == BlockModel::block){
batch->blockSprite(width-56, uicamera->fov - 56, 48, 48, (const UVRegion*)cblock->uvdata, vec4(1.0f));
batch->blockSprite(width-56, uicamera->fov - 56, 48, 48, &cache->getRegion(cblock->id, 0), vec4(1.0f));
} else if (cblock->model == BlockModel::xsprite){
const UVRegion& region = (reinterpret_cast<UVRegion*>(cblock->uvdata))[3];
batch->sprite(width-56, uicamera->fov - 56, 48, 48, region, vec4(1.0f));
batch->sprite(width-56, uicamera->fov - 56, 48, 48, cache->getRegion(cblock->id, 3), vec4(1.0f));
}
}
+3 -1
View File
@@ -13,6 +13,7 @@ class Assets;
class Player;
class Level;
class Engine;
class ContentGfxCache;
namespace gui {
class GUI;
@@ -35,8 +36,9 @@ class HudRenderer {
std::shared_ptr<gui::UINode> debugPanel;
gui::GUI* gui;
const ContentGfxCache* const cache;
public:
HudRenderer(Engine* engine, Level* level);
HudRenderer(Engine* engine, Level* level, const ContentGfxCache* cache);
~HudRenderer();
void update();
+5 -2
View File
@@ -23,6 +23,7 @@
#include "../voxels/Chunk.h"
#include "world_render.h"
#include "hud.h"
#include "ContentGfxCache.h"
#include "gui/GUI.h"
#include "gui/panels.h"
#include "../engine.h"
@@ -89,13 +90,15 @@ void MenuScreen::draw(float delta) {
LevelScreen::LevelScreen(Engine* engine, Level* level)
: Screen(engine),
level(level) {
worldRenderer = new WorldRenderer(engine, level);
hud = new HudRenderer(engine, level);
cache = new ContentGfxCache(level->content, engine->getAssets());
worldRenderer = new WorldRenderer(engine, level, cache);
hud = new HudRenderer(engine, level, cache);
}
LevelScreen::~LevelScreen() {
delete hud;
delete worldRenderer;
delete cache;
std::cout << "-- writing world" << std::endl;
World* world = level->world;
+2
View File
@@ -11,6 +11,7 @@ class HudRenderer;
class Engine;
class Camera;
class Batch2D;
class ContentGfxCache;
/* Screen is a mainloop state */
class Screen {
@@ -38,6 +39,7 @@ class LevelScreen : public Screen {
Level* level;
WorldRenderer* worldRenderer;
HudRenderer* hud;
ContentGfxCache* cache;
bool occlusion = true;
void updateHotkeys();
public:
+3 -22
View File
@@ -26,39 +26,20 @@
#include "../maths/FrustumCulling.h"
#include "../settings.h"
#include "../engine.h"
#include "ContentGfxCache.h"
using glm::vec3;
using std::string;
using std::shared_ptr;
WorldRenderer::WorldRenderer(Engine* engine, Level* level)
WorldRenderer::WorldRenderer(Engine* engine, Level* level, const ContentGfxCache* cache)
: engine(engine), level(level) {
lineBatch = new LineBatch(4096);
renderer = new ChunksRenderer(level);
renderer = new ChunksRenderer(level, cache);
frustumCulling = new Frustum();
level->events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
renderer->unload(chunk);
});
// TODO: move to some proper place
// otrefactoryu dnem
Assets* assets = engine->getAssets();
Atlas* atlas = assets->getAtlas("blocks");
const Content* content = level->content;
const ContentIndices* contentIds = content->indices;
for (uint i = 0; i < contentIds->countBlockDefs(); i++) {
Block* def = contentIds->getBlockDef(i);
for (uint side = 0; side < 6; side++) {
string tex = def->textureFaces[side];
if (atlas->has(tex)) {
UVRegion region = atlas->get(tex);
float* data = reinterpret_cast<float*>(&region);
for (uint j = 0; j < 4; j++) {
def->uvdata[side * 4 + j] = data[j];
}
}
}
}
}
WorldRenderer::~WorldRenderer() {
+2 -1
View File
@@ -21,6 +21,7 @@ class Texture;
class Frustum;
class Engine;
class Chunks;
class ContentGfxCache;
class WorldRenderer {
Engine* engine;
@@ -32,7 +33,7 @@ class WorldRenderer {
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader, bool occlusion);
public:
WorldRenderer(Engine* engine, Level* level);
WorldRenderer(Engine* engine, Level* level, const ContentGfxCache* cache);
~WorldRenderer();
void draw(const GfxContext& context, Camera* camera, bool occlusion);