Skip to content

Commit 7ff8e02

Browse files
committed
feat: use sol2 and expose tex manipulation
sol2 lets us do way more C++:y bindings to Lua, here used for exposing the texture manipulation class used to stack DDS textures and query image information.
1 parent a283091 commit 7ff8e02

File tree

6 files changed

+62
-19
lines changed

6 files changed

+62
-19
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ find_package(gli CONFIG REQUIRED)
115115
find_package(LuaJIT REQUIRED)
116116
find_package(PkgConfig REQUIRED)
117117
find_package(re2 CONFIG REQUIRED)
118+
find_package(sol2 CONFIG REQUIRED)
118119
find_package(Threads REQUIRED)
119120
find_package(zstd REQUIRED)
120121
find_package(ZLIB REQUIRED)
@@ -212,8 +213,10 @@ target_link_libraries(SimpleGraphic
212213
PkgConfig::libjxl
213214
PkgConfig::libjxl_threads
214215
re2::re2
216+
sol2
215217
Threads::Threads
216218
ZLIB::ZLIB
219+
zstd::libzstd_shared
217220
)
218221

219222
install(FILES $<TARGET_RUNTIME_DLLS:SimpleGraphic> DESTINATION ".")

ui_api.cpp

+45-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
#include "ui_local.h"
88

99
#include <filesystem>
10+
#include <fstream>
1011
#include <zlib.h>
1112

13+
#include "core/core_tex_manipulation.h"
14+
1215
/* OnFrame()
1316
** OnChar("<char>")
1417
** OnKeyDown("<keyName>")
@@ -36,6 +39,18 @@
3639
** imgHandle:SetLoadingPriority(pri)
3740
** width, height = imgHandle:ImageSize()
3841
**
42+
** texHandle = NewTexHandle()
43+
** texHandle:Allocate(format, width, height, layerCount, mipCount)
44+
** texHandle:Load("<fileName>")
45+
** texHandle:Save("<fileName>")
46+
** info = texHandle:Info()
47+
** isvalid = texHandle:IsValid()
48+
** texHandle:StackTextures({tex1, tex2, .. texN}) -- all textures must be same shape and format
49+
** -- texHandle:SetLayer(srcTexHandle, layer)
50+
** -- texHandle:CopyImage(srcTexHandle, targetX, targetY)
51+
** -- texHandle:Transcode(newFormat)
52+
** -- texHandle:GenerateMipmaps()
53+
**
3954
** RenderInit(["flag1"[, "flag2"...]]) flag:{"DPI_AWARE"}
4055
** width, height = GetScreenSize()
4156
** scaleFactor = GetScreenScale()
@@ -94,7 +109,7 @@
94109
// Grab UI main pointer from the registry
95110
static ui_main_c* GetUIPtr(lua_State* L)
96111
{
97-
lua_rawgeti(L, LUA_REGISTRYINDEX, 0);
112+
lua_geti(L, LUA_REGISTRYINDEX, ui_main_c::REGISTRY_KEY);
98113
ui_main_c* ui = (ui_main_c*)lua_touserdata(L, -1);
99114
lua_pop(L, 1);
100115
return ui;
@@ -296,7 +311,7 @@ SG_LUA_CPP_FUN_BEGIN(NewArtHandle)
296311
return 0;
297312

298313
const auto comp = component_count(format);
299-
if (comp != 1 || comp != 3 || comp != 4)
314+
if (comp != 1 && comp != 3 && comp != 4)
300315
return 0;
301316

302317
artHandle_s* artHandle = (artHandle_s*)lua_newuserdata(L, sizeof(artHandle_s));
@@ -1751,6 +1766,7 @@ static int l_Exit(lua_State* L)
17511766

17521767
int ui_main_c::InitAPI(lua_State* L)
17531768
{
1769+
sol::state_view lua(L);
17541770
luaL_openlibs(L);
17551771

17561772
// Add "lua/" subdir for non-JIT Lua
@@ -1814,6 +1830,33 @@ int ui_main_c::InitAPI(lua_State* L)
18141830
lua_setfield(L, -2, "Size");
18151831
lua_setfield(L, LUA_REGISTRYINDEX, "uiarthandlemeta");
18161832

1833+
sol::usertype<Texture_c> textureType = lua.new_usertype<Texture_c>("Texture",
1834+
sol::constructors<Texture_c()>());
1835+
1836+
textureType["Allocate"] = sol::overload(
1837+
sol::resolve<bool(gli::format,int,int,int,int)>(&Texture_c::Allocate),
1838+
sol::resolve<bool(std::string_view,int,int,int,int)>(&Texture_c::Allocate));
1839+
textureType["Load"] = &Texture_c::Load;
1840+
textureType["Save"] = &Texture_c::Save;
1841+
textureType["Info"] = &Texture_c::Info;
1842+
textureType["IsValid"] = &Texture_c::IsValid;
1843+
1844+
//textureType["SetLayer"] = &Texture_c::SetLayer;
1845+
//textureType["CopyImage"] = &Texture_c::CopyImage;
1846+
//textureType["Transcode"] = &Texture_c::Transcode;
1847+
//textureType["GenerateMipmaps"] = &Texture_c::Transcode;
1848+
1849+
textureType["StackTextures"] = &Texture_c::StackTextures;
1850+
1851+
sol::usertype<TextureInfo_s> textureInfoType = lua.new_usertype<TextureInfo_s>("TextureInfo");
1852+
1853+
textureInfoType["formatId"] = &TextureInfo_s::formatId;
1854+
textureInfoType["formatStr"] = &TextureInfo_s::formatStr;
1855+
textureInfoType["width"] = &TextureInfo_s::width;
1856+
textureInfoType["height"] = &TextureInfo_s::height;
1857+
textureInfoType["layerCount"] = &TextureInfo_s::layerCount;
1858+
textureInfoType["mipCount"] = &TextureInfo_s::mipCount;
1859+
18171860
// Rendering
18181861
ADDFUNC(RenderInit);
18191862
ADDFUNC(GetScreenSize);

ui_local.h

+4-13
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,12 @@
1111

1212
#include "ui.h"
1313

14-
extern "C" {
15-
#ifdef _WIN32
16-
#include <luajit/lua.h>
17-
#include <luajit/lauxlib.h>
18-
#include <luajit/lualib.h>
19-
#include <luajit/luajit.h>
20-
#else
21-
#include <lua.h>
22-
#include <lauxlib.h>
23-
#include <lualib.h>
24-
#endif
25-
}
14+
#define SOL_ALL_SAFETIES_ON 1
15+
#define SOL_USING_CXX_LUAJIT 1
16+
#include <sol/sol.hpp>
2617

2718
#include "ui_console.h"
2819
#include "ui_debug.h"
2920
#include "ui_subscript.h"
3021

31-
#include "ui_main.h"
22+
#include "ui_main.h"

ui_main.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static int traceback (lua_State *L) {
189189

190190
static int l_panicFunc(lua_State* L)
191191
{
192-
lua_rawgeti(L, LUA_REGISTRYINDEX, 0);
192+
lua_rawgeti(L, LUA_REGISTRYINDEX, ui_main_c::REGISTRY_KEY);
193193
ui_main_c* ui = (ui_main_c*)lua_touserdata(L, -1);
194194
lua_pop(L, 1);
195195
ui->sys->Error("Unprotected Lua error:\n%s", lua_tostring(L, -1));
@@ -289,11 +289,12 @@ void ui_main_c::ScriptInit()
289289

290290
// Initialise Lua
291291
sys->con->Printf("Initialising Lua...\n");
292-
L = luaL_newstate();
292+
solState.emplace();
293+
L = solState->lua_state();
293294
if ( !L ) sys->Error("Error: unable to create Lua state.");
294295
lua_atpanic(L, l_panicFunc);
295296
lua_pushlightuserdata(L, this);
296-
lua_rawseti(L, LUA_REGISTRYINDEX, 0);
297+
lua_seti(L, LUA_REGISTRYINDEX, ui_main_c::REGISTRY_KEY);
297298
lua_pushcfunction(L, traceback);
298299
lua_pushvalue(L, -1);
299300
lua_setfield(L, LUA_REGISTRYINDEX, "traceback");
@@ -444,8 +445,8 @@ void ui_main_c::ScriptShutdown()
444445
ui_IDebug::FreeHandle(debug);
445446

446447
// Shutdown Lua
447-
lua_close(L);
448448
L = NULL;
449+
solState.reset();
449450
}
450451

451452
void ui_main_c::Shutdown()

ui_main.h

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ui_main_c: public ui_IMain {
3434
dword subScriptSize = 0;
3535
ui_ISubScript** subScriptList = nullptr;
3636

37+
std::optional<sol::state> solState;
3738
lua_State* L = nullptr;
3839
char* scriptName = nullptr;
3940
char* scriptCfg = nullptr;
@@ -65,4 +66,6 @@ class ui_main_c: public ui_IMain {
6566
void CallKeyHandler(const char* hname, int key, bool dblclk);
6667
const char* NameForKey(int key);
6768
int KeyForName(const char* name);
69+
70+
enum { REGISTRY_KEY = 1 };
6871
};

vcpkg.json

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"luajit",
1212
"pkgconf",
1313
"re2",
14+
"sol2",
15+
"zstd",
1416
"zlib"
1517
]
1618
}

0 commit comments

Comments
 (0)