This commit is contained in:
MihailRis 2024-04-28 22:33:34 +03:00
parent 167388aefb
commit ef28a368cb
12 changed files with 54 additions and 51 deletions

View File

@ -96,7 +96,7 @@ assetload::postfunc assetload::atlas(
continue;
}
std::set<std::string> names = builder.getNames();
Atlas* atlas = builder.build(2, false);
Atlas* atlas = builder.build(2, false).release();
return [=](auto assets) {
atlas->prepare();
assets->store(atlas, name);
@ -247,7 +247,7 @@ static bool animation(
continue;
}
std::unique_ptr<Atlas> srcAtlas (builder.build(2));
auto srcAtlas = builder.build(2);
srcAtlas->prepare();
Texture* srcTex = srcAtlas->getTexture();

View File

@ -136,7 +136,7 @@ void SlotView::draw(const GfxContext* pctx, Assets* assets) {
batch->setColor(glm::vec4(1.0f));
auto previews = frontend->getBlocksAtlas();
auto previews = assets->getAtlas("block-previews");
auto indices = content->getIndices();
ItemDef* item = indices->getItemDef(stack.getItemId());
@ -181,7 +181,7 @@ void SlotView::draw(const GfxContext* pctx, Assets* assets) {
int x = pos.x+slotSize-text.length()*8;
int y = pos.y+slotSize-16;
batch->setColor(glm::vec4(0, 0, 0, 1.0f));
batch->setColor({0, 0, 0, 1.0f});
font->draw(batch, text, x+1, y+1);
batch->setColor(glm::vec4(1.0f));
font->draw(batch, text, x, y);
@ -266,13 +266,12 @@ void SlotView::onFocus(gui::GUI* gui) {
void SlotView::bind(
int64_t inventoryid,
ItemStack& stack,
LevelFrontend* frontend,
const Content* content,
InventoryInteraction* interaction
) {
this->inventoryid = inventoryid;
bound = &stack;
content = frontend->getLevel()->content;
this->frontend = frontend;
this->content = content;
this->interaction = interaction;
}
@ -332,21 +331,18 @@ void InventoryView::bind(
slot->bind(
inventory->getId(),
inventory->getSlot(slot->getLayout().index),
frontend, interaction
content, interaction
);
}
}
void InventoryView::unbind() {
if (inventory && inventory->isVirtual()) {
frontend->getLevel()->inventories->remove(inventory->getId());
}
inventory = nullptr;
}
void InventoryView::setSelected(int index) {
for (int i = 0; i < int(slots.size()); i++) {
auto slot = slots[i];
slot->setHighlighted(i == index);
for (size_t i = 0; i < slots.size(); i++) {
slots[i]->setHighlighted(static_cast<int>(i) == index);
}
}

View File

@ -59,7 +59,6 @@ struct SlotLayout {
};
class SlotView : public gui::UINode {
LevelFrontend* frontend = nullptr;
InventoryInteraction* interaction = nullptr;
const Content* content;
SlotLayout layout;
@ -81,7 +80,7 @@ public:
void bind(
int64_t inventoryid,
ItemStack& stack,
LevelFrontend* frontend,
const Content* content,
InventoryInteraction* interaction
);

View File

@ -15,9 +15,12 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets)
: level(controller->getLevel()),
controller(controller),
assets(assets),
contentCache(std::make_unique<ContentGfxCache>(level->content, assets)),
blocksAtlas(BlocksPreview::build(contentCache.get(), assets, level->content))
contentCache(std::make_unique<ContentGfxCache>(level->content, assets))
{
assets->store(
BlocksPreview::build(contentCache.get(), assets, level->content).release(),
"block-previews"
);
controller->getPlayerController()->listenBlockInteraction(
[=](Player*, glm::ivec3 pos, const Block* def, BlockInteraction type) {
auto material = level->content->findBlockMaterial(def->material);
@ -79,10 +82,6 @@ ContentGfxCache* LevelFrontend::getContentGfxCache() const {
return contentCache.get();
}
Atlas* LevelFrontend::getBlocksAtlas() const {
return blocksAtlas.get();
}
LevelController* LevelFrontend::getController() const {
return controller;
}

View File

@ -3,10 +3,8 @@
#include <memory>
class Atlas;
class Level;
class Assets;
class BlocksPreview;
class ContentGfxCache;
class LevelController;
@ -15,7 +13,6 @@ class LevelFrontend {
LevelController* controller;
Assets* assets;
std::unique_ptr<ContentGfxCache> contentCache;
std::unique_ptr<Atlas> blocksAtlas;
public:
LevelFrontend(LevelController* controller, Assets* assets);
~LevelFrontend();
@ -23,8 +20,6 @@ public:
Level* getLevel() const;
Assets* getAssets() const;
ContentGfxCache* getContentGfxCache() const;
Atlas* getBlocksAtlas() const;
LevelController* getController() const;
};

View File

@ -153,7 +153,7 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
grabbedItemView->bind(
0,
interaction->getGrabbedItem(),
frontend,
frontend->getLevel()->content,
interaction.get()
);
grabbedItemView->setColor(glm::vec4());

View File

@ -50,7 +50,7 @@ bool AtlasBuilder::has(const std::string& name) const {
return names.find(name) != names.end();
}
Atlas* AtlasBuilder::build(uint extrusion, bool prepare, uint maxResolution) {
std::unique_ptr<Atlas> AtlasBuilder::build(uint extrusion, bool prepare, uint maxResolution) {
if (maxResolution == 0) {
maxResolution = Texture::MAX_RESOLUTION;
}
@ -99,5 +99,5 @@ Atlas* AtlasBuilder::build(uint extrusion, bool prepare, uint maxResolution) {
unitX * x, unitY * y, unitX * (x + w), unitY * (y + h)
);
}
return new Atlas(std::move(canvas), regions, prepare);
return std::make_unique<Atlas>(std::move(canvas), regions, prepare);
}

View File

@ -55,7 +55,7 @@ public:
/// (greather is less mip-mapping artifacts)
/// @param prepare generate atlas texture (calls .prepare())
/// @param maxResolution max atlas resolution
Atlas* build(uint extrusion, bool prepare=true, uint maxResolution=0);
std::unique_ptr<Atlas> build(uint extrusion, bool prepare=true, uint maxResolution=0);
};
#endif // GRAPHICS_CORE_ATLAS_HPP_

View File

@ -55,9 +55,11 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
break;
case BlockModel::custom:
{
glm::vec3 pmul = glm::vec3(size * 0.63f);
glm::vec3 hitbox = glm::vec3();
for (const auto& box : def->modelBoxes)
for (const auto& box : def->modelBoxes) {
hitbox = glm::max(hitbox, box.size());
}
offset.y += (1.0f - hitbox).y * 0.5f;
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
for (size_t i = 0; i < def->modelBoxes.size(); i++) {
@ -69,28 +71,39 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
def->modelUVs[i * 6 + 4],
def->modelUVs[i * 6 + 5]
};
batch->cube(def->modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * glm::vec3(size * 0.63f), def->modelBoxes[i].size() * glm::vec3(size * 0.63f), texfaces, glm::vec4(1.0f), !def->rt.emissive);
batch->cube(
def->modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
def->modelBoxes[i].size() * pmul,
texfaces, glm::vec4(1.0f), !def->rt.emissive
);
}
auto& points = def->modelExtraPoints;
glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
for (size_t i = 0; i < def->modelExtraPoints.size() / 4; i++) {
const UVRegion& reg = def->modelUVs[def->modelBoxes.size() * 6 + i];
batch->point((def->modelExtraPoints[i * 4 + 0] - glm::vec3(0.0f, 0.0f, 1.0f)) * glm::vec3(size * 0.63f), glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
batch->point((def->modelExtraPoints[i * 4 + 1] - glm::vec3(0.0f, 0.0f, 1.0f)) * glm::vec3(size * 0.63f), glm::vec2(reg.u2, reg.v1), glm::vec4(1.0));
batch->point((def->modelExtraPoints[i * 4 + 2] - glm::vec3(0.0f, 0.0f, 1.0f)) * glm::vec3(size * 0.63f), glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
batch->point((def->modelExtraPoints[i * 4 + 0] - glm::vec3(0.0f, 0.0f, 1.0f)) * glm::vec3(size * 0.63f), glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
batch->point((def->modelExtraPoints[i * 4 + 2] - glm::vec3(0.0f, 0.0f, 1.0f)) * glm::vec3(size * 0.63f), glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
batch->point((def->modelExtraPoints[i * 4 + 3] - glm::vec3(0.0f, 0.0f, 1.0f)) * glm::vec3(size * 0.63f), glm::vec2(reg.u1, reg.v2), glm::vec4(1.0));
batch->point((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
batch->point((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0));
batch->point((points[i * 4 + 2] - poff) * pmul, glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
batch->point((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
batch->point((points[i * 4 + 2] - poff) * pmul, glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
batch->point((points[i * 4 + 3] - poff) * pmul, glm::vec2(reg.u1, reg.v2), glm::vec4(1.0));
}
batch->flush();
}
break;
case BlockModel::xsprite: {
glm::vec3 right = glm::normalize(glm::vec3(1.f, 0.f, -1.f));
batch->sprite(right*float(size)*0.43f+glm::vec3(0, size*0.4f, 0),
glm::vec3(0.f, 1.f, 0.f),
right,
size*0.5f, size*0.6f,
texfaces[0],
glm::vec4(1.0f));
batch->sprite(
right*float(size)*0.43f+glm::vec3(0, size*0.4f, 0),
glm::vec3(0.f, 1.f, 0.f),
right,
size*0.5f, size*0.6f,
texfaces[0],
glm::vec4(1.0f)
);
batch->flush();
break;
}
@ -141,6 +154,5 @@ std::unique_ptr<Atlas> BlocksPreview::build(
fbo.unbind();
Window::viewport(0, 0, Window::width, Window::height);
auto newAtlas = std::unique_ptr<Atlas>(builder.build(2));
return newAtlas;
return builder.build(2);
}

View File

@ -26,7 +26,9 @@ std::shared_ptr<Inventory> Inventories::createVirtual(size_t size) {
auto inv = std::make_shared<Inventory>(id, size);
store(inv);
return inv;
return std::shared_ptr<Inventory>(inv.get(), [this](Inventory* ptr) {
remove(ptr->getId());
});
}
void Inventories::store(std::shared_ptr<Inventory> inv) {

View File

@ -3,7 +3,7 @@
#include <glm/ext.hpp>
Camera::Camera(glm::vec3 position, float fov) : fov(fov), position(position), zoom(1.0f), rotation(1.0f) {
Camera::Camera(glm::vec3 position, float fov) : fov(fov), position(position) {
updateVectors();
}

View File

@ -14,8 +14,8 @@ public:
glm::vec3 position;
float zoom;
glm::mat4 rotation;
float zoom = 1.0f;
glm::mat4 rotation {1.0f};
bool perspective = true;
bool flipped = false;
float aspect = 0.0f;