From 1409af67de7cc818ae3c29def1c3d813f3c7ce1a Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Sun, 13 Aug 2023 20:09:12 +0300 Subject: [PATCH] expose api to lua --- library/LuaApi.cpp | 18 +++++++++++++++- library/include/modules/Textures.h | 2 +- library/modules/Textures.cpp | 7 +++---- plugins/lua/hotkeys.lua | 33 +++++++++++++++--------------- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 61a7eb83d..095d432ee 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1709,8 +1709,24 @@ static const luaL_Reg dfhack_job_funcs[] = { /***** Textures module *****/ +static int textures_loadTileset(lua_State *state) +{ + std::string file = luaL_checkstring(state, 1); + auto tile_w = luaL_checkint(state, 2); + auto tile_h = luaL_checkint(state, 3); + auto handles = Textures::loadTileset(file, tile_w, tile_h); + Lua::PushVector(state, handles); + return 1; +} + static const LuaWrapper::FunctionReg dfhack_textures_module[] = { WRAPM(Textures, getAsset), + WRAPM(Textures, getTexposByHandle), + { NULL, NULL } +}; + +static const luaL_Reg dfhack_textures_funcs[] = { + { "loadTileset", textures_loadTileset }, { NULL, NULL } }; @@ -3680,7 +3696,7 @@ void OpenDFHackApi(lua_State *state) luaL_setfuncs(state, dfhack_funcs, 0); OpenModule(state, "gui", dfhack_gui_module, dfhack_gui_funcs); OpenModule(state, "job", dfhack_job_module, dfhack_job_funcs); - OpenModule(state, "textures", dfhack_textures_module); + OpenModule(state, "textures", dfhack_textures_module, dfhack_textures_funcs); OpenModule(state, "units", dfhack_units_module, dfhack_units_funcs); OpenModule(state, "military", dfhack_military_module); OpenModule(state, "items", dfhack_items_module, dfhack_items_funcs); diff --git a/library/include/modules/Textures.h b/library/include/modules/Textures.h index 9166e7607..d8a256b2b 100644 --- a/library/include/modules/Textures.h +++ b/library/include/modules/Textures.h @@ -8,7 +8,7 @@ struct SDL_Surface; -typedef SDL_Surface* TexposHandle; +typedef uintptr_t TexposHandle; namespace DFHack { diff --git a/library/modules/Textures.cpp b/library/modules/Textures.cpp index 0eb4e442d..10ccbf59d 100644 --- a/library/modules/Textures.cpp +++ b/library/modules/Textures.cpp @@ -35,8 +35,7 @@ static std::vector empty{}; static std::tuple, int, int> basic{empty, Textures::TILE_WIDTH_PX, Textures::TILE_HEIGHT_PX}; static std::unordered_map, int, int>> - g_static_assets{{"hack/data/art/dfhack.png", basic}, - {"hack/data/art/green-pin.png", basic}, + g_static_assets{{"hack/data/art/green-pin.png", basic}, {"hack/data/art/red-pin.png", basic}, {"hack/data/art/icons.png", basic}, {"hack/data/art/on-off.png", basic}, @@ -105,7 +104,7 @@ TexposHandle Textures::loadTexture(SDL_Surface* surface) { if (!surface) return 0; // should be some error, i guess - auto handle = surface; + auto handle = reinterpret_cast(surface); g_handle_to_surface.emplace(handle, surface); surface->refcount++; // prevent destruct on next FreeSurface by game auto texpos = add_texture(surface); @@ -139,7 +138,7 @@ std::vector Textures::loadTileset(const std::string& file, int til } DFSDL_FreeSurface(surface); - DEBUG(textures).print("loaded %ld textures from '%s'\n", handles.size(), file.c_str()); + DEBUG(textures).print("loaded %i textures from '%s'\n", handles.size(), file.c_str()); return handles; } diff --git a/plugins/lua/hotkeys.lua b/plugins/lua/hotkeys.lua index e2bef331b..ce4342ea0 100644 --- a/plugins/lua/hotkeys.lua +++ b/plugins/lua/hotkeys.lua @@ -50,6 +50,7 @@ HotspotMenuWidget.ATTRS{ function HotspotMenuWidget:init() self.mouseover = false + self.textures = dfhack.textures.loadTileset('hack/data/art/dfhack.png', 8, 12) end function HotspotMenuWidget:overlay_onupdate() @@ -70,28 +71,28 @@ local dscreen = dfhack.screen function HotspotMenuWidget:onRenderBody(dc) local x, y = dc.x, dc.y local tp = function(offset) - return dfhack.textures.getAsset('hack/data/art/dfhack.png', offset) + return dfhack.textures.getTexposByHandle(self.textures[offset]) end - if tp(0) == -1 then + if tp(1) == -1 then dscreen.paintString(COLOR_WHITE, x, y + 0, '!DF!') dscreen.paintString(COLOR_WHITE, x, y + 1, '!Ha!') dscreen.paintString(COLOR_WHITE, x, y + 2, '!ck!') else - dscreen.paintTile(COLOR_WHITE, x + 0, y + 0, '!', tp(0)) - dscreen.paintTile(COLOR_WHITE, x + 1, y + 0, 'D', tp(1)) - dscreen.paintTile(COLOR_WHITE, x + 2, y + 0, 'F', tp(2)) - dscreen.paintTile(COLOR_WHITE, x + 3, y + 0, '!', tp(3)) - - dscreen.paintTile(COLOR_WHITE, x + 0, y + 1, '!', tp(4)) - dscreen.paintTile(COLOR_WHITE, x + 1, y + 1, 'H', tp(5)) - dscreen.paintTile(COLOR_WHITE, x + 2, y + 1, 'a', tp(6)) - dscreen.paintTile(COLOR_WHITE, x + 3, y + 1, '!', tp(7)) - - dscreen.paintTile(COLOR_WHITE, x + 0, y + 2, '!', tp(8)) - dscreen.paintTile(COLOR_WHITE, x + 1, y + 2, 'c', tp(9)) - dscreen.paintTile(COLOR_WHITE, x + 2, y + 2, 'k', tp(10)) - dscreen.paintTile(COLOR_WHITE, x + 3, y + 2, '!', tp(11)) + dscreen.paintTile(COLOR_WHITE, x + 0, y + 0, '!', tp(1)) + dscreen.paintTile(COLOR_WHITE, x + 1, y + 0, 'D', tp(2)) + dscreen.paintTile(COLOR_WHITE, x + 2, y + 0, 'F', tp(3)) + dscreen.paintTile(COLOR_WHITE, x + 3, y + 0, '!', tp(4)) + + dscreen.paintTile(COLOR_WHITE, x + 0, y + 1, '!', tp(5)) + dscreen.paintTile(COLOR_WHITE, x + 1, y + 1, 'H', tp(6)) + dscreen.paintTile(COLOR_WHITE, x + 2, y + 1, 'a', tp(7)) + dscreen.paintTile(COLOR_WHITE, x + 3, y + 1, '!', tp(8)) + + dscreen.paintTile(COLOR_WHITE, x + 0, y + 2, '!', tp(9)) + dscreen.paintTile(COLOR_WHITE, x + 1, y + 2, 'c', tp(10)) + dscreen.paintTile(COLOR_WHITE, x + 2, y + 2, 'k', tp(11)) + dscreen.paintTile(COLOR_WHITE, x + 3, y + 2, '!', tp(12)) end end