Merge branch 'dev' into generated-pcm-stream

This commit is contained in:
MihailRis 2025-11-06 12:18:47 +03:00
commit 219d5aab31
19 changed files with 226 additions and 36 deletions

View File

@ -25,4 +25,12 @@ assets.parse_model(
-- Model name after loading
name: str
)
-- Creates a Canvas from a loaded texture.
assets.to_canvas(
-- The name of the loaded texture.
-- Both standalone textures ("texture_name") and
-- those in an atlas ("atlas:texture_name") are supported
name: str
) --> Canvas
```

View File

@ -25,4 +25,12 @@ assets.parse_model(
-- Имя модели после загрузки
name: str
)
-- Создаёт холст (Canvas) из загруженной текстуры
assets.to_canvas(
-- Имя загруженной текстуры.
-- Поддерживается как отдельные ("имя_текстуры"),
-- так и находящиеся в атласе ("атлас:имя_текстуры").
name: str
) --> Canvas
```

View File

@ -298,7 +298,9 @@ function __vc_on_hud_open()
end)
input.add_callback("key:escape", function()
if menu.page ~= "" then
menu:reset()
if not menu:back() then
menu:reset()
end
elseif hud.is_inventory_open() then
hud.close_inventory()
else

View File

@ -184,7 +184,7 @@ assetload::postfunc assetload::atlas(
if (!append_atlas(builder, file)) continue;
}
std::set<std::string> names = builder.getNames();
Atlas* atlas = builder.build(2, false).release();
Atlas* atlas = builder.build(ATLAS_EXTRUSION, false).release();
return [=](auto assets) {
atlas->prepare();
assets->store(std::unique_ptr<Atlas>(atlas), name);
@ -501,7 +501,7 @@ static bool load_animation(
}
if (!append_atlas(builder, file)) continue;
}
auto srcAtlas = builder.build(2, true);
auto srcAtlas = builder.build(ATLAS_EXTRUSION, true);
if (frameList.empty()) {
for (const auto& frameName : builder.getNames()) {
frameList.emplace_back(frameName, 0);

View File

@ -61,6 +61,8 @@ inline constexpr int ITEM_ICON_SIZE = 48;
inline constexpr int TRANSLUCENT_BLOCKS_SORT_INTERVAL = 8;
inline constexpr int ATLAS_EXTRUSION = 2;
inline const std::string SHADERS_FOLDER = "shaders";
inline const std::string TEXTURES_FOLDER = "textures";
inline const std::string FONTS_FOLDER = "fonts";

View File

@ -26,6 +26,7 @@
#include "graphics/core/ImageData.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/ui/GUI.hpp"
#include "graphics/ui/elements/Menu.hpp"
#include "objects/rigging.hpp"
#include "logic/EngineController.hpp"
#include "logic/CommandsInterpreter.hpp"
@ -154,6 +155,14 @@ void Engine::initializeClient() {
},
true
));
keepAlive(this->input->addKeyCallback(Keycode::ESCAPE, [this]() {
auto& menu = *gui->getMenu();
if (menu.hasOpenPage() && menu.back()) {
return true;
}
return false;
}));
}
void Engine::initialize(CoreParameters coreParameters) {

View File

@ -9,6 +9,7 @@
#include "graphics/ui/elements/TextBox.hpp"
#include "graphics/ui/elements/TrackBar.hpp"
#include "graphics/ui/elements/InputBindBox.hpp"
#include "graphics/ui/GUI.hpp"
#include "graphics/render/WorldRenderer.hpp"
#include "graphics/render/ParticlesRenderer.hpp"
#include "graphics/render/ChunksRenderer.hpp"
@ -43,10 +44,15 @@ static std::shared_ptr<Label> create_label(GUI& gui, wstringsupplier supplier) {
return label;
}
static bool should_keep_previous(GUI& gui) {
return !gui.getInput().isCursorLocked();
}
// TODO: move to xml
// TODO: move to xml finally
// TODO: move to xml finally
// TODO: move to xml finally
// TODO: move to xml finally
std::shared_ptr<UINode> create_debug_panel(
Engine& engine,
Level& level,
@ -81,7 +87,7 @@ std::shared_ptr<UINode> create_debug_panel(
fpsMax = fps;
});
panel->listenInterval(1.0f, [&engine]() {
panel->listenInterval(1.0f, [&engine, &gui]() {
const auto& network = engine.getNetwork();
size_t totalDownload = network.getTotalDownload();
size_t totalUpload = network.getTotalUpload();
@ -135,7 +141,16 @@ std::shared_ptr<UINode> create_debug_panel(
std::to_wstring(player.getId());
}));
panel->add(create_label(gui, [&]() -> std::wstring {
const auto& vox = player.selection.vox;
// TODO: move to xml finally
static voxel prevVox = {BLOCK_VOID, {}};
auto vox = player.selection.vox;
if (vox.id == BLOCK_VOID && should_keep_previous(gui)) {
vox = prevVox;
} else {
prevVox = vox;
}
std::wstringstream stream;
stream << "r:" << vox.state.rotation << " s:"
<< std::bitset<3>(vox.state.segment) << " u:"
@ -148,8 +163,17 @@ std::shared_ptr<UINode> create_debug_panel(
}
}));
panel->add(create_label(gui, [&]() -> std::wstring {
const auto& selection = player.selection;
// TODO: move to xml finally
static CursorSelection prevSelection {};
auto selection = player.selection;
const auto& vox = selection.vox;
if (vox.id == BLOCK_VOID && should_keep_previous(gui)) {
selection = prevSelection;
} else {
prevSelection = selection;
}
if (vox.id == BLOCK_VOID) {
return L"x: - y: - z: -";
}
@ -158,7 +182,16 @@ std::shared_ptr<UINode> create_debug_panel(
L" z: " + std::to_wstring(selection.actualPosition.z);
}));
panel->add(create_label(gui, [&]() {
// TODO: move to xml finally
static entityid_t prevEid = ENTITY_NONE;
auto eid = player.getSelectedEntity();
if (eid == ENTITY_NONE && should_keep_previous(gui)) {
eid = prevEid;
} else {
prevEid = eid;
}
if (eid == ENTITY_NONE) {
return std::wstring {L"entity: -"};
} else if (auto entity = level.entities->get(eid)) {

View File

@ -49,6 +49,14 @@ ImageData* Atlas::getImage() const {
return image.get();
}
std::shared_ptr<Texture> Atlas::shareTexture() const {
return texture;
}
std::shared_ptr<ImageData> Atlas::shareImageData() const {
return image;
}
void AtlasBuilder::add(const std::string& name, std::unique_ptr<ImageData> image) {
entries.push_back(atlasentry{name, std::shared_ptr<ImageData>(image.release())});
names.insert(name);

View File

@ -14,8 +14,8 @@ class ImageData;
class Texture;
class Atlas {
std::unique_ptr<Texture> texture;
std::unique_ptr<ImageData> image;
std::shared_ptr<Texture> texture;
std::shared_ptr<ImageData> image;
std::unordered_map<std::string, UVRegion> regions;
public:
/// @param image atlas raster
@ -36,6 +36,9 @@ public:
Texture* getTexture() const;
ImageData* getImage() const;
std::shared_ptr<Texture> shareTexture() const;
std::shared_ptr<ImageData> shareImageData() const;
};
struct atlasentry {

View File

@ -95,6 +95,17 @@ void ImageData::blit(const ImageData& image, int x, int y) {
throw std::runtime_error("mismatching format");
}
std::unique_ptr<ImageData> ImageData::cropped(int x, int y, int width, int height) const {
width = std::min<int>(width, this->width - x);
height = std::min<int>(height, this->height - y);
if (width <= 0 || height <= 0) {
throw std::runtime_error("invalid crop dimensions");
}
auto subImage = std::make_unique<ImageData>(format, width, height);
subImage->blitMatchingFormat(*this, -x, -y);
return subImage;
}
static bool clip_line(int& x1, int& y1, int& x2, int& y2, int width, int height) {
const int left = 0;
const int right = width;

View File

@ -33,6 +33,8 @@ public:
void extrude(int x, int y, int w, int h);
void fixAlphaColor();
std::unique_ptr<ImageData> cropped(int x, int y, int width, int height) const;
ubyte* getData() const {
return data.get();
}

View File

@ -51,6 +51,14 @@ void Texture::reload(const ubyte* data) {
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(data));
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}
void Texture::reloadPartial(const ImageData& image, uint x, uint y, uint w, uint h) {
glBindTexture(GL_TEXTURE_2D, id);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image.getData());
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}

View File

@ -19,6 +19,7 @@ public:
virtual void bind() const;
virtual void unbind() const;
void reload(const ubyte* data);
void reloadPartial(const ImageData& image, uint x, uint y, uint w, uint h);
void setNearestFilter();

View File

@ -137,5 +137,5 @@ std::unique_ptr<Atlas> BlocksPreview::build(
builder.add(def.name, draw(cache, shader, fbo, batch, def, iconSize));
}
fbo.unbind();
return builder.build(2);
return builder.build(ATLAS_EXTRUSION);
}

View File

@ -7,7 +7,9 @@
#include "engine/Engine.hpp"
#include "graphics/commons/Model.hpp"
#include "graphics/core/Texture.hpp"
#include "graphics/core/Atlas.hpp"
#include "util/Buffer.hpp"
#include "../usertypes/lua_type_canvas.hpp"
using namespace scripting;
@ -64,8 +66,47 @@ static int l_parse_model(lua::State* L) {
return 0;
}
static int l_to_canvas(lua::State* L) {
auto& assets = *engine->getAssets();
auto alias = lua::require_lstring(L, 1);
size_t sep = alias.rfind(':');
if (sep == std::string::npos) {
auto texture = assets.getShared<Texture>(std::string(alias));
if (texture != nullptr) {
auto image = texture->readData();
return lua::newuserdata<lua::LuaCanvas>(
L, texture, std::move(image)
);
}
return 0;
}
auto atlasName = alias.substr(0, sep);
if (auto atlas = assets.get<Atlas>(std::string(atlasName))) {
auto textureName = std::string(alias.substr(sep + 1));
auto image = atlas->shareImageData();
auto texture = atlas->shareTexture();
if (auto region = atlas->getIf(textureName)) {
UVRegion uvRegion = *region;
int atlasWidth = static_cast<int>(image->getWidth());
int atlasHeight = static_cast<int>(image->getHeight());
int x = static_cast<int>(uvRegion.u1 * atlasWidth);
int y = static_cast<int>(uvRegion.v1 * atlasHeight);
int w = static_cast<int>(uvRegion.getWidth() * atlasWidth);
int h = static_cast<int>(uvRegion.getHeight() * atlasHeight);
return lua::newuserdata<lua::LuaCanvas>(
L, std::move(texture), image->cropped(x, y, w, h), uvRegion
);
}
}
return 0;
}
const luaL_Reg assetslib[] = {
{"load_texture", lua::wrap<l_load_texture>},
{"parse_model", lua::wrap<l_parse_model>},
{"to_canvas", lua::wrap<l_to_canvas>},
{nullptr, nullptr}
};

View File

@ -58,7 +58,7 @@ DocumentNode get_document_node(lua::State* L, int idx) {
static int l_menu_back(lua::State* L) {
auto node = get_document_node(L);
if (auto menu = dynamic_cast<Menu*>(node.node.get())) {
menu->back();
return lua::pushboolean(L, menu->back());
}
return 0;
}

View File

@ -11,14 +11,59 @@
using namespace lua;
LuaCanvas::LuaCanvas(
std::shared_ptr<Texture> inTexture, std::shared_ptr<ImageData> inData
std::shared_ptr<Texture> texture,
std::shared_ptr<ImageData> data,
UVRegion region
)
: mTexture(std::move(inTexture)), mData(std::move(inData)) {
: texture(std::move(texture)),
data(std::move(data)),
region(std::move(region)) {
}
void LuaCanvas::update(int extrusion) {
if (!hasTexture()) {
return;
}
if (region.isFull()) {
texture->reload(*data);
} else {
uint texWidth = texture->getWidth();
uint texHeight = texture->getHeight();
uint imgWidth = data->getWidth();
uint imgHeight = data->getHeight();
uint x = static_cast<uint>(region.u1 * texWidth);
uint y = static_cast<uint>(region.v1 * texHeight);
uint w = static_cast<uint>((region.u2 - region.u1) * texWidth);
uint h = static_cast<uint>((region.v2 - region.v1) * texHeight);
w = std::min<uint>(w, imgWidth);
h = std::min<uint>(h, imgHeight);
if (extrusion > 0) {
auto extruded = std::make_unique<ImageData>(
data->getFormat(),
w + extrusion * 2,
h + extrusion * 2
);
extruded->blit(*data, extrusion, extrusion);
extruded->extrude(0, 0, w + extrusion * 2, h + extrusion * 2);
texture->reloadPartial(
*extruded,
x - extrusion,
y - extrusion,
w + extrusion * 2,
h + extrusion * 2
);
} else {
texture->reloadPartial(*data, x, y, w, h);
}
}
}
void LuaCanvas::createTexture() {
mTexture = Texture::from(mData.get());
mTexture->setMipMapping(false, true);
texture = Texture::from(data.get());
texture->setMipMapping(false, true);
}
union RGBA {
@ -42,7 +87,7 @@ static RGBA* get_at(const ImageData& data, uint x, uint y) {
static RGBA* get_at(State* L, uint x, uint y) {
if (auto canvas = touserdata<LuaCanvas>(L, 1)) {
return get_at(canvas->data(), x, y);
return get_at(canvas->getData(), x, y);
}
return nullptr;
}
@ -98,7 +143,7 @@ static LuaCanvas& require_canvas(State* L, int idx) {
static int l_clear(State* L) {
auto& canvas = require_canvas(L, 1);
auto& image = canvas.data();
auto& image = canvas.getData();
ubyte* data = image.getData();
RGBA rgba {};
if (gettop(L) == 1) {
@ -123,7 +168,7 @@ static int l_line(State* L) {
RGBA rgba = get_rgba(L, 6);
if (auto canvas = touserdata<LuaCanvas>(L, 1)) {
auto& image = canvas->data();
auto& image = canvas->getData();
image.drawLine(
x1, y1, x2, y2, glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a}
);
@ -136,13 +181,13 @@ static int l_blit(State* L) {
auto& src = require_canvas(L, 2);
int dst_x = tointeger(L, 3);
int dst_y = tointeger(L, 4);
dst.data().blit(src.data(), dst_x, dst_y);
dst.getData().blit(src.getData(), dst_x, dst_y);
return 0;
}
static int l_set_data(State* L) {
auto& canvas = require_canvas(L, 1);
auto& image = canvas.data();
auto& image = canvas.getData();
auto data = image.getData();
if (lua::isstring(L, 2)) {
@ -167,9 +212,7 @@ static int l_set_data(State* L) {
static int l_update(State* L) {
if (auto canvas = touserdata<LuaCanvas>(L, 1)) {
if (canvas->hasTexture()) {
canvas->texture().reload(canvas->data());
}
canvas->update();
}
return 0;
}
@ -203,7 +246,7 @@ static int l_meta_index(State* L) {
if (texture == nullptr) {
return 0;
}
auto& data = texture->data();
auto& data = texture->getData();
if (isnumber(L, 2)) {
if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) {
return pushinteger(L, pixel->rgba);
@ -232,7 +275,7 @@ static int l_meta_newindex(State* L) {
if (texture == nullptr) {
return 0;
}
auto& data = texture->data();
auto& data = texture->getData();
if (isnumber(L, 2) && isnumber(L, 3)) {
if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) {
pixel->rgba = static_cast<uint>(tointeger(L, 3));

View File

@ -1,6 +1,8 @@
#pragma once
#include "../lua_commons.hpp"
#include "maths/UVRegion.hpp"
#include "constants.hpp"
class Texture;
class ImageData;
@ -9,8 +11,9 @@ namespace lua {
class LuaCanvas : public Userdata {
public:
explicit LuaCanvas(
std::shared_ptr<Texture> inTexture,
std::shared_ptr<ImageData> inData
std::shared_ptr<Texture> texture,
std::shared_ptr<ImageData> data,
UVRegion region = UVRegion(0, 0, 1, 1)
);
~LuaCanvas() override = default;
@ -18,29 +21,32 @@ namespace lua {
return TYPENAME;
}
[[nodiscard]] auto& texture() const {
return *mTexture;
[[nodiscard]] auto& getTexture() const {
return *texture;
}
[[nodiscard]] auto& data() const {
return *mData;
[[nodiscard]] auto& getData() const {
return *data;
}
[[nodiscard]] bool hasTexture() const {
return mTexture != nullptr;
return texture != nullptr;
}
auto shareTexture() const {
return mTexture;
return texture;
}
void update(int extrusion = ATLAS_EXTRUSION);
void createTexture();
static int createMetatable(lua::State*);
inline static std::string TYPENAME = "Canvas";
private:
std::shared_ptr<Texture> mTexture; // nullable
std::shared_ptr<ImageData> mData;
std::shared_ptr<Texture> texture; // nullable
std::shared_ptr<ImageData> data;
UVRegion region;
};
static_assert(!std::is_abstract<LuaCanvas>());
}

View File

@ -1,8 +1,7 @@
#pragma once
#include <cmath>
#include <glm/vec2.hpp>
#include <glm/vec4.hpp>
#include <glm/glm.hpp>
struct UVRegion {
float u1;
@ -69,4 +68,10 @@ struct UVRegion {
copy.scale(scale);
return copy;
}
bool isFull() const {
const auto e = 1e-7;
return glm::abs(u1 - 0.0) < e && glm::abs(v1 - 0.0) < e &&
glm::abs(u2 - 1.0) < e && glm::abs(v2 - 1.0) < e;
}
};