feat: canvas from texture atlas element
This commit is contained in:
@@ -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 "../lua_custom_types.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
@@ -64,8 +66,37 @@ static int l_parse_model(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_to_canvas(lua::State* L) {
|
||||
auto alias = lua::require_lstring(L, 1);
|
||||
size_t sep = alias.rfind(':');
|
||||
if (sep == std::string::npos) {
|
||||
return 0;
|
||||
}
|
||||
auto atlasName = alias.substr(0, sep);
|
||||
auto& assets = *engine->getAssets();
|
||||
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}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user