inventory render optimized

This commit is contained in:
MihailRis 2024-01-21 23:35:41 +03:00
parent 714a49685f
commit db2108171c
12 changed files with 285 additions and 255 deletions

View File

@ -17,6 +17,8 @@ const int CHUNK_D = 16;
const uint VOXEL_USER_BITS = 8; const uint VOXEL_USER_BITS = 8;
constexpr uint VOXEL_USER_BITS_OFFSET = sizeof(blockstate_t)*8-VOXEL_USER_BITS; constexpr uint VOXEL_USER_BITS_OFFSET = sizeof(blockstate_t)*8-VOXEL_USER_BITS;
const int ITEM_ICON_SIZE = 48;
/* Chunk volume (count of voxels per Chunk) */ /* Chunk volume (count of voxels per Chunk) */
constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D); constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D);

View File

@ -8,47 +8,23 @@
#include "../graphics/Texture.h" #include "../graphics/Texture.h"
#include "../graphics/Atlas.h" #include "../graphics/Atlas.h"
#include "../graphics/Batch3D.h" #include "../graphics/Batch3D.h"
#include "../graphics/Framebuffer.h"
#include "../graphics/GfxContext.h"
#include "../window/Window.h"
#include "../window/Camera.h" #include "../window/Camera.h"
#include "../voxels/Block.h" #include "../voxels/Block.h"
#include "../content/Content.h"
#include "../constants.h"
#include "ContentGfxCache.h" #include "ContentGfxCache.h"
BlocksPreview::BlocksPreview(Assets* assets, const ContentGfxCache* cache) ImageData* BlocksPreview::draw(
: shader(assets->getShader("ui3d")), const ContentGfxCache* cache,
atlas(assets->getAtlas("blocks")), Framebuffer* fbo,
cache(cache) { Batch3D* batch,
batch = std::make_unique<Batch3D>(1024); const Block* def,
} int size
){
BlocksPreview::~BlocksPreview() { Window::clear();
}
void BlocksPreview::begin(const Viewport* viewport) {
this->viewport = viewport;
shader->use();
shader->uniformMatrix("u_projview",
glm::ortho(0.0f, float(viewport->getWidth()),
0.0f, float(viewport->getHeight()),
-100.0f, 100.0f) *
glm::lookAt(glm::vec3(2, 2, 2), glm::vec3(0.0f), glm::vec3(0, 1, 0)));
atlas->getTexture()->bind();
}
/* Draw one block preview at given screen position */
void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tint) {
uint width = viewport->getWidth();
uint height = viewport->getHeight();
y = height - y - 1 - 35 /* magic garbage */;
x += 2;
if (def->model == BlockModel::aabb) {
x += (1.0f - def->hitbox.size()).x * size * 0.5f;
y += (1.0f - def->hitbox.size()).y * size * 0.25f;
}
glm::vec3 offset (x/float(width) * 2, y/float(height) * 2, 0.0f);
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
blockid_t id = def->rt.id; blockid_t id = def->rt.id;
const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1), const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1),
cache->getRegion(id, 2), cache->getRegion(id, 3), cache->getRegion(id, 2), cache->getRegion(id, 3),
@ -59,11 +35,11 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin
// something went wrong... // something went wrong...
break; break;
case BlockModel::block: case BlockModel::block:
batch->blockCube(glm::vec3(size * 0.63f), texfaces, tint, !def->rt.emissive); batch->blockCube(glm::vec3(size * 0.63f), texfaces, glm::vec4(1.0f), !def->rt.emissive);
break; break;
case BlockModel::aabb: case BlockModel::aabb:
batch->blockCube(def->hitbox.size() * glm::vec3(size * 0.63f), batch->blockCube(def->hitbox.size() * glm::vec3(size * 0.63f),
texfaces, tint, !def->rt.emissive); texfaces, glm::vec4(1.0f), !def->rt.emissive);
break; break;
case BlockModel::custom: case BlockModel::custom:
case BlockModel::xsprite: { case BlockModel::xsprite: {
@ -73,10 +49,64 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin
right, right,
size*0.5f, size*0.6f, size*0.5f, size*0.6f,
texfaces[0], texfaces[0],
tint); glm::vec4(1.0f));
break; break;
} }
} }
batch->flush(); batch->flush();
return fbo->texture->readData();
}
std::unique_ptr<Atlas> BlocksPreview::build(
const ContentGfxCache* cache,
Assets* assets,
const Content* content
) {
auto indices = content->getIndices();
size_t count = indices->countBlockDefs();
size_t iconSize = ITEM_ICON_SIZE;
Shader* shader = assets->getShader("ui3d");
Atlas* atlas = assets->getAtlas("blocks");
Viewport viewport(iconSize, iconSize);
GfxContext pctx(nullptr, viewport, nullptr);
GfxContext ctx = pctx.sub();
ctx.cullFace(true);
ctx.depthTest(true);
Framebuffer fbo(iconSize, iconSize, true);
Batch3D batch(1024);
batch.begin();
shader->use();
shader->uniformMatrix("u_projview",
glm::ortho(0.0f, float(iconSize), 0.0f, float(iconSize),
-100.0f, 100.0f) *
glm::lookAt(glm::vec3(2, 2, 2),
glm::vec3(0.0f),
glm::vec3(0, 1, 0)));
AtlasBuilder builder;
Window::viewport(0, 0, iconSize, iconSize);
Window::setBgColor(glm::vec4(0.0f));
fbo.bind();
for (size_t i = 0; i < count; i++) {
auto def = indices->getBlockDef(i);
glm::vec3 offset(0.1f, 0.5f, 0.1f);
if (def->model == BlockModel::aabb) {
offset.y += (1.0f - def->hitbox.size()).y * 0.5f;
}
atlas->getTexture()->bind();
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
builder.add(def->name, draw(cache, &fbo, &batch, def, iconSize));
}
fbo.unbind();
Window::viewport(0, 0, Window::width, Window::height);
return std::unique_ptr<Atlas>(builder.build(2));
} }

View File

@ -6,25 +6,27 @@
#include <memory> #include <memory>
class Assets; class Assets;
class Viewport; class ImageData;
class Shader;
class Atlas; class Atlas;
class Framebuffer;
class Batch3D; class Batch3D;
class Block; class Block;
class Content;
class ContentGfxCache; class ContentGfxCache;
class BlocksPreview { class BlocksPreview {
Shader* shader;
Atlas* atlas;
std::unique_ptr<Batch3D> batch;
const ContentGfxCache* const cache;
const Viewport* viewport;
public: public:
BlocksPreview(Assets* assets, const ContentGfxCache* cache); static ImageData* draw(
~BlocksPreview(); const ContentGfxCache* cache,
Framebuffer* framebuffer,
Batch3D* batch,
const Block* block,
int size);
void begin(const Viewport* viewport); static std::unique_ptr<Atlas> build(
void draw(const Block* block, int x, int y, int size, glm::vec4 tint); const ContentGfxCache* cache,
Assets* assets,
const Content* content);
}; };
#endif // FRONTEND_BLOCKS_PREVIEW_H_ #endif // FRONTEND_BLOCKS_PREVIEW_H_

View File

@ -153,31 +153,26 @@ void SlotView::draw(Batch2D* batch, Assets* assets) {
batch->color = glm::vec4(1.0f); batch->color = glm::vec4(1.0f);
Shader* uiShader = assets->getShader("ui");
Viewport viewport(Window::width, Window::height); Viewport viewport(Window::width, Window::height);
GfxContext ctx(nullptr, viewport, batch); GfxContext ctx(nullptr, viewport, batch);
auto preview = frontend->getBlocksPreview(); auto previews = frontend->getBlocksAtlas();
auto indices = content->getIndices(); auto indices = content->getIndices();
ItemDef* item = indices->getItemDef(stack.getItemId()); ItemDef* item = indices->getItemDef(stack.getItemId());
switch (item->iconType) { switch (item->iconType) {
case item_icon_type::none: case item_icon_type::none:
break; break;
case item_icon_type::block: case item_icon_type::block: {
batch->render(); Block* cblock = content->requireBlock(item->icon);
{ batch->texture(previews->getTexture());
GfxContext subctx = ctx.sub();
subctx.depthTest(true);
subctx.cullFace(true);
Block* cblock = content->requireBlock(item->icon); UVRegion region = previews->get(cblock->name);
preview->begin(&subctx.getViewport()); batch->rect(
preview->draw(cblock, coord.x, coord.y, slotSize, tint); coord.x, coord.y, slotSize, slotSize,
} 0, 0, 0, region, false, true, tint);
uiShader->use();
batch->begin();
break; break;
}
case item_icon_type::sprite: { case item_icon_type::sprite: {
size_t index = item->icon.find(':'); size_t index = item->icon.find(':');
std::string name = item->icon.substr(index+1); std::string name = item->icon.substr(index+1);
@ -327,12 +322,6 @@ InventoryLayout* InventoryView::getLayout() const {
return layout.get(); return layout.get();
} }
// performance disaster x2
void InventoryView::draw(Batch2D* batch, Assets* assets) {
Container::draw(batch, assets);
Window::clearDepth();
}
void InventoryView::drawBackground(Batch2D* batch, Assets* assets) { void InventoryView::drawBackground(Batch2D* batch, Assets* assets) {
glm::vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
batch->texture(nullptr); batch->texture(nullptr);

View File

@ -126,7 +126,6 @@ public:
void build(); void build();
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void drawBackground(Batch2D* batch, Assets* assets) override; virtual void drawBackground(Batch2D* batch, Assets* assets) override;
void setInventory(std::shared_ptr<Inventory> inventory); void setInventory(std::shared_ptr<Inventory> inventory);
@ -138,7 +137,7 @@ public:
void setSelected(int index); void setSelected(int index);
static const int SLOT_INTERVAL = 4; static const int SLOT_INTERVAL = 4;
static const int SLOT_SIZE = 48; static const int SLOT_SIZE = ITEM_ICON_SIZE;
}; };
class InventoryInteraction { class InventoryInteraction {

View File

@ -2,6 +2,7 @@
#include "../world/Level.h" #include "../world/Level.h"
#include "../assets/Assets.h" #include "../assets/Assets.h"
#include "../graphics/Atlas.h"
#include "BlocksPreview.h" #include "BlocksPreview.h"
#include "ContentGfxCache.h" #include "ContentGfxCache.h"
@ -9,8 +10,7 @@ LevelFrontend::LevelFrontend(Level* level, Assets* assets)
: level(level), : level(level),
assets(assets), assets(assets),
contentCache(std::make_unique<ContentGfxCache>(level->content, assets)), contentCache(std::make_unique<ContentGfxCache>(level->content, assets)),
blocksPreview(std::make_unique<BlocksPreview>(assets, contentCache.get())) { blocksAtlas(BlocksPreview::build(contentCache.get(), assets, level->content)) {
} }
LevelFrontend::~LevelFrontend() { LevelFrontend::~LevelFrontend() {
@ -24,10 +24,10 @@ Assets* LevelFrontend::getAssets() const {
return assets; return assets;
} }
BlocksPreview* LevelFrontend::getBlocksPreview() const {
return blocksPreview.get();
}
ContentGfxCache* LevelFrontend::getContentGfxCache() const { ContentGfxCache* LevelFrontend::getContentGfxCache() const {
return contentCache.get(); return contentCache.get();
} }
Atlas* LevelFrontend::getBlocksAtlas() const {
return blocksAtlas.get();
}

View File

@ -3,6 +3,7 @@
#include <memory> #include <memory>
class Atlas;
class Level; class Level;
class Assets; class Assets;
class BlocksPreview; class BlocksPreview;
@ -12,15 +13,15 @@ class LevelFrontend {
Level* level; Level* level;
Assets* assets; Assets* assets;
std::unique_ptr<ContentGfxCache> contentCache; std::unique_ptr<ContentGfxCache> contentCache;
std::unique_ptr<BlocksPreview> blocksPreview; std::unique_ptr<Atlas> blocksAtlas;
public: public:
LevelFrontend(Level* level, Assets* assets); LevelFrontend(Level* level, Assets* assets);
~LevelFrontend(); ~LevelFrontend();
Level* getLevel() const; Level* getLevel() const;
Assets* getAssets() const; Assets* getAssets() const;
BlocksPreview* getBlocksPreview() const;
ContentGfxCache* getContentGfxCache() const; ContentGfxCache* getContentGfxCache() const;
Atlas* getBlocksAtlas() const;
}; };

View File

@ -52,106 +52,106 @@ using glm::vec4;
using namespace gui; using namespace gui;
inline std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) { inline std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) {
auto label = std::make_shared<Label>(L"-"); auto label = std::make_shared<Label>(L"-");
label->textSupplier(supplier); label->textSupplier(supplier);
return label; return label;
} }
void HudRenderer::createDebugPanel(Engine* engine) { void HudRenderer::createDebugPanel(Engine* engine) {
auto level = frontend->getLevel(); auto level = frontend->getLevel();
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f); Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
debugPanel = std::shared_ptr<UINode>(panel); debugPanel = std::shared_ptr<UINode>(panel);
panel->listenInterval(1.0f, [this]() { panel->listenInterval(1.0f, [this]() {
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin); fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
fpsMin = fps; fpsMin = fps;
fpsMax = fps; fpsMax = fps;
}); });
panel->setCoord(vec2(10, 10)); panel->setCoord(vec2(10, 10));
panel->add(create_label([this](){ return L"fps: "+this->fpsString;})); panel->add(create_label([this](){ return L"fps: "+this->fpsString;}));
panel->add(create_label([this](){ panel->add(create_label([this](){
return L"meshes: " + std::to_wstring(Mesh::meshesCount); return L"meshes: " + std::to_wstring(Mesh::meshesCount);
})); }));
panel->add(create_label([=](){ panel->add(create_label([=](){
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
bool culling = settings.graphics.frustumCulling; bool culling = settings.graphics.frustumCulling;
return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off"); return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off");
})); }));
panel->add(create_label([=]() { panel->add(create_label([=]() {
return L"chunks: "+std::to_wstring(level->chunks->chunksCount)+ return L"chunks: "+std::to_wstring(level->chunks->chunksCount)+
L" visible: "+std::to_wstring(level->chunks->visible); L" visible: "+std::to_wstring(level->chunks->visible);
})); }));
panel->add(create_label([=](){ panel->add(create_label([=](){
auto player = level->player; auto player = level->player;
auto* indices = level->content->getIndices(); auto* indices = level->content->getIndices();
auto def = indices->getBlockDef(player->selectedVoxel.id); auto def = indices->getBlockDef(player->selectedVoxel.id);
std::wstringstream stream; std::wstringstream stream;
stream << std::hex << level->player->selectedVoxel.states; stream << std::hex << level->player->selectedVoxel.states;
if (def) { if (def) {
stream << L" (" << util::str2wstr_utf8(def->name) << L")"; stream << L" (" << util::str2wstr_utf8(def->name) << L")";
} }
return L"block: "+std::to_wstring(player->selectedVoxel.id)+ return L"block: "+std::to_wstring(player->selectedVoxel.id)+
L" "+stream.str(); L" "+stream.str();
})); }));
panel->add(create_label([=](){ panel->add(create_label([=](){
return L"seed: "+std::to_wstring(level->world->seed); return L"seed: "+std::to_wstring(level->world->seed);
})); }));
for (int ax = 0; ax < 3; ax++){ for (int ax = 0; ax < 3; ax++){
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f)); Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
sub->orientation(Orientation::horizontal); sub->orientation(Orientation::horizontal);
std::wstring str = L"x: "; std::wstring str = L"x: ";
str[0] += ax; str[0] += ax;
Label* label = new Label(str); Label* label = new Label(str);
label->margin(vec4(2, 3, 2, 3)); label->margin(vec4(2, 3, 2, 3));
sub->add(label); sub->add(label);
sub->color(vec4(0.0f)); sub->color(vec4(0.0f));
// Coord input // Coord input
TextBox* box = new TextBox(L""); TextBox* box = new TextBox(L"");
box->textSupplier([=]() { box->textSupplier([=]() {
Hitbox* hitbox = level->player->hitbox.get(); Hitbox* hitbox = level->player->hitbox.get();
return util::to_wstring(hitbox->position[ax], 2); return util::to_wstring(hitbox->position[ax], 2);
}); });
box->textConsumer([=](std::wstring text) { box->textConsumer([=](std::wstring text) {
try { try {
vec3 position = level->player->hitbox->position; vec3 position = level->player->hitbox->position;
position[ax] = std::stoi(text); position[ax] = std::stoi(text);
level->player->teleport(position); level->player->teleport(position);
} catch (std::invalid_argument& _){ } catch (std::invalid_argument& _){
} }
}); });
box->setOnEditStart([=](){ box->setOnEditStart([=](){
Hitbox* hitbox = level->player->hitbox.get(); Hitbox* hitbox = level->player->hitbox.get();
box->text(std::to_wstring(int(hitbox->position[ax]))); box->text(std::to_wstring(int(hitbox->position[ax])));
}); });
sub->add(box); sub->add(box);
panel->add(sub); panel->add(sub);
} }
panel->add(create_label([=](){ panel->add(create_label([=](){
int hour, minute, second; int hour, minute, second;
timeutil::from_value(level->world->daytime, hour, minute, second); timeutil::from_value(level->world->daytime, hour, minute, second);
std::wstring timeString = std::wstring timeString =
util::lfill(std::to_wstring(hour), 2, L'0') + L":" + util::lfill(std::to_wstring(hour), 2, L'0') + L":" +
util::lfill(std::to_wstring(minute), 2, L'0'); util::lfill(std::to_wstring(minute), 2, L'0');
return L"time: "+timeString; return L"time: "+timeString;
})); }));
{ {
TrackBar* bar = new TrackBar(0.0f, 1.0f, 1.0f, 0.005f, 8); TrackBar* bar = new TrackBar(0.0f, 1.0f, 1.0f, 0.005f, 8);
bar->supplier([=]() {return level->world->daytime;}); bar->supplier([=]() {return level->world->daytime;});
bar->consumer([=](double val) {level->world->daytime = val;}); bar->consumer([=](double val) {level->world->daytime = val;});
panel->add(bar); panel->add(bar);
} }
{ {
TrackBar* bar = new TrackBar(0.0f, 1.0f, 0.0f, 0.005f, 8); TrackBar* bar = new TrackBar(0.0f, 1.0f, 0.0f, 0.005f, 8);
bar->supplier([=]() {return WorldRenderer::fog;}); bar->supplier([=]() {return WorldRenderer::fog;});
bar->consumer([=](double val) {WorldRenderer::fog = val;}); bar->consumer([=](double val) {WorldRenderer::fog = val;});
panel->add(bar); panel->add(bar);
} }
{ {
auto checkbox = new FullCheckBox(L"Show Chunk Borders", vec2(400, 32)); auto checkbox = new FullCheckBox(L"Show Chunk Borders", vec2(400, 32));
checkbox->supplier([=]() { checkbox->supplier([=]() {
return engine->getSettings().debug.showChunkBorders; return engine->getSettings().debug.showChunkBorders;
@ -160,8 +160,8 @@ void HudRenderer::createDebugPanel(Engine* engine) {
engine->getSettings().debug.showChunkBorders = checked; engine->getSettings().debug.showChunkBorders = checked;
}); });
panel->add(checkbox); panel->add(checkbox);
} }
panel->refresh(); panel->refresh();
} }
std::shared_ptr<InventoryView> HudRenderer::createContentAccess() { std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
@ -192,10 +192,10 @@ std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
auto contentAccess = std::make_shared<InventoryView>( auto contentAccess = std::make_shared<InventoryView>(
content, content,
frontend, frontend,
interaction.get(), interaction.get(),
accessInventory, accessInventory,
std::move(layout) std::move(layout)
); );
contentAccess->build(); contentAccess->build();
return contentAccess; return contentAccess;
@ -215,10 +215,10 @@ std::shared_ptr<InventoryView> HudRenderer::createHotbar() {
layout->setOrigin(glm::vec2(layout->getSize().x/2, 0)); layout->setOrigin(glm::vec2(layout->getSize().x/2, 0));
auto view = std::make_shared<InventoryView>( auto view = std::make_shared<InventoryView>(
content, content,
frontend, frontend,
interaction.get(), interaction.get(),
inventory, inventory,
std::move(layout) std::move(layout)
); );
view->build(); view->build();
view->setInteractive(false); view->setInteractive(false);
@ -255,13 +255,13 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
gui(engine->getGUI()), gui(engine->getGUI()),
frontend(frontend) frontend(frontend)
{ {
auto menu = gui->getMenu(); auto menu = gui->getMenu();
interaction = std::make_unique<InventoryInteraction>(); interaction = std::make_unique<InventoryInteraction>();
grabbedItemView = std::make_shared<SlotView>( grabbedItemView = std::make_shared<SlotView>(
interaction->getGrabbedItem(), interaction->getGrabbedItem(),
frontend, frontend,
interaction.get(), interaction.get(),
frontend->getLevel()->content, frontend->getLevel()->content,
SlotLayout(glm::vec2(), false, false, nullptr, nullptr) SlotLayout(glm::vec2(), false, false, nullptr, nullptr)
); );
@ -279,14 +279,14 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
hotbarView = createHotbar(); hotbarView = createHotbar();
inventoryView = createInventory(); inventoryView = createInventory();
uicamera = new Camera(vec3(), 1); uicamera = new Camera(vec3(), 1);
uicamera->perspective = false; uicamera->perspective = false;
uicamera->flipped = true; uicamera->flipped = true;
createDebugPanel(engine); createDebugPanel(engine);
menu->reset(); menu->reset();
gui->add(debugPanel); gui->add(debugPanel);
gui->add(contentAccessPanel); gui->add(contentAccessPanel);
gui->add(hotbarView); gui->add(hotbarView);
gui->add(inventoryView); gui->add(inventoryView);
@ -298,48 +298,48 @@ HudRenderer::~HudRenderer() {
gui->remove(inventoryView); gui->remove(inventoryView);
gui->remove(hotbarView); gui->remove(hotbarView);
gui->remove(contentAccessPanel); gui->remove(contentAccessPanel);
gui->remove(debugPanel); gui->remove(debugPanel);
delete uicamera; delete uicamera;
} }
void HudRenderer::drawDebug(int fps){ void HudRenderer::drawDebug(int fps){
this->fps = fps; this->fps = fps;
fpsMin = min(fps, fpsMin); fpsMin = min(fps, fpsMin);
fpsMax = max(fps, fpsMax); fpsMax = max(fps, fpsMax);
} }
void HudRenderer::update(bool visible) { void HudRenderer::update(bool visible) {
auto level = frontend->getLevel(); auto level = frontend->getLevel();
auto player = level->player; auto player = level->player;
auto menu = gui->getMenu(); auto menu = gui->getMenu();
menu->visible(pause); menu->visible(pause);
if (!visible && inventoryOpen) { if (!visible && inventoryOpen) {
inventoryOpen = false; inventoryOpen = false;
} }
if (pause && menu->current().panel == nullptr) { if (pause && menu->current().panel == nullptr) {
pause = false; pause = false;
} }
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) { if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
if (pause) { if (pause) {
pause = false; pause = false;
menu->reset(); menu->reset();
} else if (inventoryOpen) { } else if (inventoryOpen) {
inventoryOpen = false; inventoryOpen = false;
} else { } else {
pause = true; pause = true;
menu->set("pause"); menu->set("pause");
} }
} }
if (visible && Events::jactive(BIND_HUD_INVENTORY)) { if (visible && Events::jactive(BIND_HUD_INVENTORY)) {
if (!pause) { if (!pause) {
inventoryOpen = !inventoryOpen; inventoryOpen = !inventoryOpen;
} }
} }
if ((pause || inventoryOpen) == Events::_cursor_locked) { if ((pause || inventoryOpen) == Events::_cursor_locked) {
Events::toggleCursor(); Events::toggleCursor();
} }
glm::vec2 invSize = contentAccessPanel->size(); glm::vec2 invSize = contentAccessPanel->size();
inventoryView->visible(inventoryOpen); inventoryView->visible(inventoryOpen);
@ -365,10 +365,10 @@ void HudRenderer::update(bool visible) {
} }
void HudRenderer::drawOverlay(const GfxContext& ctx) { void HudRenderer::drawOverlay(const GfxContext& ctx) {
if (pause) { if (pause) {
Shader* uishader = assets->getShader("ui"); Shader* uishader = assets->getShader("ui");
uishader->use(); uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjView()); uishader->uniformMatrix("u_projview", uicamera->getProjView());
const Viewport& viewport = ctx.getViewport(); const Viewport& viewport = ctx.getViewport();
const uint width = viewport.getWidth(); const uint width = viewport.getWidth();
@ -376,48 +376,48 @@ void HudRenderer::drawOverlay(const GfxContext& ctx) {
auto batch = ctx.getBatch2D(); auto batch = ctx.getBatch2D();
batch->begin(); batch->begin();
// draw fullscreen dark overlay // draw fullscreen dark overlay
batch->texture(nullptr); batch->texture(nullptr);
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.5f); batch->color = vec4(0.0f, 0.0f, 0.0f, 0.5f);
batch->rect(0, 0, width, height); batch->rect(0, 0, width, height);
batch->render(); batch->render();
} }
} }
void HudRenderer::draw(const GfxContext& ctx){ void HudRenderer::draw(const GfxContext& ctx){
auto level = frontend->getLevel(); auto level = frontend->getLevel();
const Viewport& viewport = ctx.getViewport(); const Viewport& viewport = ctx.getViewport();
const uint width = viewport.getWidth(); const uint width = viewport.getWidth();
const uint height = viewport.getHeight(); const uint height = viewport.getHeight();
Player* player = level->player; Player* player = level->player;
debugPanel->visible(player->debug); debugPanel->visible(player->debug);
uicamera->setFov(height); uicamera->setFov(height);
auto batch = ctx.getBatch2D(); auto batch = ctx.getBatch2D();
batch->begin(); batch->begin();
Shader* uishader = assets->getShader("ui"); Shader* uishader = assets->getShader("ui");
uishader->use(); uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjView()); uishader->uniformMatrix("u_projview", uicamera->getProjView());
// Draw selected item preview // Draw selected item preview
hotbarView->setCoord(glm::vec2(width/2, height-65)); hotbarView->setCoord(glm::vec2(width/2, height-65));
hotbarView->setSelected(player->getChosenSlot()); hotbarView->setSelected(player->getChosenSlot());
// Crosshair // Crosshair
batch->begin(); batch->begin();
if (!pause && Events::_cursor_locked && !level->player->debug) { if (!pause && Events::_cursor_locked && !level->player->debug) {
batch->lineWidth(2); batch->lineWidth(2);
batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f); batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f); batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f); batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f); batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
} }
if (inventoryOpen) { if (inventoryOpen) {
auto caLayout = contentAccess->getLayout(); auto caLayout = contentAccess->getLayout();
auto invLayout = inventoryView->getLayout(); auto invLayout = inventoryView->getLayout();
float caWidth = caLayout->getSize().x; float caWidth = caLayout->getSize().x;
@ -430,15 +430,15 @@ void HudRenderer::draw(const GfxContext& ctx){
height/2-invSize.y/2 height/2-invSize.y/2
)); ));
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0)); contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
} }
grabbedItemView->setCoord(glm::vec2(Events::cursor)); grabbedItemView->setCoord(glm::vec2(Events::cursor));
batch->render(); batch->render();
} }
bool HudRenderer::isInventoryOpen() const { bool HudRenderer::isInventoryOpen() const {
return inventoryOpen; return inventoryOpen;
} }
bool HudRenderer::isPause() const { bool HudRenderer::isPause() const {
return pause; return pause;
} }

View File

@ -3,13 +3,15 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "Texture.h" #include "Texture.h"
Framebuffer::Framebuffer(uint width, uint height) : width(width), height(height) { Framebuffer::Framebuffer(uint width, uint height, bool alpha)
: width(width), height(height) {
glGenFramebuffers(1, &fbo); glGenFramebuffers(1, &fbo);
bind(); bind();
GLuint tex; GLuint tex;
glGenTextures(1, &tex); glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex); glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); GLuint format = alpha ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

View File

@ -12,7 +12,7 @@ public:
uint width; uint width;
uint height; uint height;
Texture* texture; Texture* texture;
Framebuffer(uint width, uint height); Framebuffer(uint width, uint height, bool alpha=false);
~Framebuffer(); ~Framebuffer();
void bind(); void bind();

View File

@ -179,6 +179,10 @@ void Window::setBgColor(glm::vec3 color) {
glClearColor(color.r, color.g, color.b, 1.0f); glClearColor(color.r, color.g, color.b, 1.0f);
} }
void Window::setBgColor(glm::vec4 color) {
glClearColor(color.r, color.g, color.b, color.a);
}
void Window::viewport(int x, int y, int width, int height){ void Window::viewport(int x, int y, int width, int height){
glViewport(x, y, width, height); glViewport(x, y, width, height);
} }

View File

@ -48,6 +48,7 @@ public:
static void clear(); static void clear();
static void clearDepth(); static void clearDepth();
static void setBgColor(glm::vec3 color); static void setBgColor(glm::vec3 color);
static void setBgColor(glm::vec4 color);
static double time(); static double time();
static const char* getClipboardText(); static const char* getClipboardText();
static DisplaySettings* getSettings(); static DisplaySettings* getSettings();