Merge pull request #486 from MihailRis/canvas_shares_texture

add canvas:create_texture(...)
This commit is contained in:
MihailRis 2025-03-17 16:13:28 +03:00 committed by GitHub
commit 63f508d430
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 47 additions and 7 deletions

View File

@ -184,6 +184,7 @@ Here, *color* can be specified in the following ways:
| data:clear(*color*) | fills the canvas with the specified RGBA color |
| data:update() | applies changes to the canvas and uploads it to the GPU |
| data:set_data(data: table<int>) | replaces pixel data (width * height * 4 numbers) |
| data:create_texture(name: str) | creates and shares texture to renderer |
## Inventory

View File

@ -185,6 +185,7 @@ document["worlds-panel"]:clear()
| data:clear(*цвет*) | заполняет холст указанным RGBA цветом |
| data:update() | применяет изменения и загружает холст в видеопамять |
| data:set_data(data: table<int>) | заменяет данные пикселей (ширина * высота * 4 чисел) |
| data:create_texture(name: str) | создаёт и делится текстурой с рендерером |
## Inventory (inventory)

View File

@ -72,6 +72,11 @@ public:
assets[typeid(T)][name].reset(asset.release());
}
template <class T>
void store(std::shared_ptr<T> asset, const std::string& name) {
assets[typeid(T)][name] = std::move(asset);
}
template <class T>
T* get(const std::string& name) const {
const auto& mapIter = assets.find(typeid(T));

View File

@ -165,7 +165,7 @@ assetload::postfunc assetload::font(
textures.emplace_back(nullptr);
} else {
auto texture = Texture::from(page.get());
texture->setMipMapping(false);
texture->setMipMapping(false, true);
textures.emplace_back(std::move(texture));
}
}

View File

@ -71,14 +71,20 @@ void GLTexture::setNearestFilter() {
glBindTexture(GL_TEXTURE_2D, 0);
}
void GLTexture::setMipMapping(bool flag) {
void GLTexture::setMipMapping(bool flag, bool pixelated) {
bind();
if (flag) {
glTexParameteri(
GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST
GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER,
pixelated ? GL_NEAREST : GL_LINEAR_MIPMAP_NEAREST
);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(
GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER,
pixelated ? GL_NEAREST : GL_LINEAR
);
}
glBindTexture(GL_TEXTURE_2D, 0);
}

View File

@ -18,7 +18,7 @@ public:
virtual void reload(const ImageData& image) override;
virtual void setMipMapping(bool flag) override;
virtual void setMipMapping(bool flag, bool pixelated) override;
virtual std::unique_ptr<ImageData> readData() override;
virtual uint getId() const override;

View File

@ -34,7 +34,7 @@ public:
virtual uint getId() const = 0;
virtual void setMipMapping(bool flag) = 0;
virtual void setMipMapping(bool flag, bool pixelated) = 0;
static std::unique_ptr<Texture> from(const ImageData* image);
};

View File

@ -114,7 +114,7 @@ void PrecipitationRenderer::render(
batch->begin();
auto& texture = assets.require<Texture>(weather.fall.texture);
texture.setMipMapping(false);
texture.setMipMapping(false, true);
batch->setTexture(&texture, {});
const struct {

View File

@ -118,6 +118,12 @@ namespace lua {
return mTexture != nullptr;
}
auto shareTexture() const {
return mTexture;
}
void createTexture();
static int createMetatable(lua::State*);
inline static std::string TYPENAME = "Canvas";
private:

View File

@ -4,6 +4,8 @@
#include "graphics/core/Texture.hpp"
#include "logic/scripting/lua/lua_custom_types.hpp"
#include "logic/scripting/lua/lua_util.hpp"
#include "engine/Engine.hpp"
#include "assets/Assets.hpp"
using namespace lua;
@ -13,6 +15,11 @@ LuaCanvas::LuaCanvas(
: mTexture(std::move(inTexture)), mData(std::move(inData)) {
}
void LuaCanvas::createTexture() {
mTexture = Texture::from(mData.get());
mTexture->setMipMapping(false, true);
}
union RGBA {
struct {
uint8_t r, g, b, a;
@ -166,6 +173,19 @@ static int l_update(State* L) {
return 0;
}
static int l_create_texture(State* L) {
if (auto canvas = touserdata<LuaCanvas>(L, 1)) {
if (!canvas->hasTexture()) {
canvas->createTexture();
}
if (canvas->hasTexture()) {
std::string name = require_string(L, 2);
scripting::engine->getAssets()->store(canvas->shareTexture(), name);
}
}
return 0;
}
static std::unordered_map<std::string, lua_CFunction> methods {
{"at", lua::wrap<l_at>},
{"set", lua::wrap<l_set>},
@ -173,6 +193,7 @@ static std::unordered_map<std::string, lua_CFunction> methods {
{"blit", lua::wrap<l_blit>},
{"clear", lua::wrap<l_clear>},
{"update", lua::wrap<l_update>},
{"create_texture", lua::wrap<l_create_texture>},
{"_set_data", lua::wrap<l_set_data>},
};