expose api to lua

develop
shevernitskiy 2023-08-13 20:09:12 +03:00
parent 22b0671038
commit 1409af67de
4 changed files with 38 additions and 22 deletions

@ -1709,8 +1709,24 @@ static const luaL_Reg dfhack_job_funcs[] = {
/***** Textures module *****/ /***** 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[] = { static const LuaWrapper::FunctionReg dfhack_textures_module[] = {
WRAPM(Textures, getAsset), WRAPM(Textures, getAsset),
WRAPM(Textures, getTexposByHandle),
{ NULL, NULL }
};
static const luaL_Reg dfhack_textures_funcs[] = {
{ "loadTileset", textures_loadTileset },
{ NULL, NULL } { NULL, NULL }
}; };
@ -3680,7 +3696,7 @@ void OpenDFHackApi(lua_State *state)
luaL_setfuncs(state, dfhack_funcs, 0); luaL_setfuncs(state, dfhack_funcs, 0);
OpenModule(state, "gui", dfhack_gui_module, dfhack_gui_funcs); OpenModule(state, "gui", dfhack_gui_module, dfhack_gui_funcs);
OpenModule(state, "job", dfhack_job_module, dfhack_job_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, "units", dfhack_units_module, dfhack_units_funcs);
OpenModule(state, "military", dfhack_military_module); OpenModule(state, "military", dfhack_military_module);
OpenModule(state, "items", dfhack_items_module, dfhack_items_funcs); OpenModule(state, "items", dfhack_items_module, dfhack_items_funcs);

@ -8,7 +8,7 @@
struct SDL_Surface; struct SDL_Surface;
typedef SDL_Surface* TexposHandle; typedef uintptr_t TexposHandle;
namespace DFHack { namespace DFHack {

@ -35,8 +35,7 @@ static std::vector<TexposHandle> empty{};
static std::tuple<std::vector<TexposHandle>, int, int> basic{empty, Textures::TILE_WIDTH_PX, static std::tuple<std::vector<TexposHandle>, int, int> basic{empty, Textures::TILE_WIDTH_PX,
Textures::TILE_HEIGHT_PX}; Textures::TILE_HEIGHT_PX};
static std::unordered_map<std::string, std::tuple<std::vector<TexposHandle>, int, int>> static std::unordered_map<std::string, std::tuple<std::vector<TexposHandle>, int, int>>
g_static_assets{{"hack/data/art/dfhack.png", basic}, g_static_assets{{"hack/data/art/green-pin.png", basic},
{"hack/data/art/green-pin.png", basic},
{"hack/data/art/red-pin.png", basic}, {"hack/data/art/red-pin.png", basic},
{"hack/data/art/icons.png", basic}, {"hack/data/art/icons.png", basic},
{"hack/data/art/on-off.png", basic}, {"hack/data/art/on-off.png", basic},
@ -105,7 +104,7 @@ TexposHandle Textures::loadTexture(SDL_Surface* surface) {
if (!surface) if (!surface)
return 0; // should be some error, i guess return 0; // should be some error, i guess
auto handle = surface; auto handle = reinterpret_cast<uintptr_t>(surface);
g_handle_to_surface.emplace(handle, surface); g_handle_to_surface.emplace(handle, surface);
surface->refcount++; // prevent destruct on next FreeSurface by game surface->refcount++; // prevent destruct on next FreeSurface by game
auto texpos = add_texture(surface); auto texpos = add_texture(surface);
@ -139,7 +138,7 @@ std::vector<TexposHandle> Textures::loadTileset(const std::string& file, int til
} }
DFSDL_FreeSurface(surface); 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; return handles;
} }

@ -50,6 +50,7 @@ HotspotMenuWidget.ATTRS{
function HotspotMenuWidget:init() function HotspotMenuWidget:init()
self.mouseover = false self.mouseover = false
self.textures = dfhack.textures.loadTileset('hack/data/art/dfhack.png', 8, 12)
end end
function HotspotMenuWidget:overlay_onupdate() function HotspotMenuWidget:overlay_onupdate()
@ -70,28 +71,28 @@ local dscreen = dfhack.screen
function HotspotMenuWidget:onRenderBody(dc) function HotspotMenuWidget:onRenderBody(dc)
local x, y = dc.x, dc.y local x, y = dc.x, dc.y
local tp = function(offset) local tp = function(offset)
return dfhack.textures.getAsset('hack/data/art/dfhack.png', offset) return dfhack.textures.getTexposByHandle(self.textures[offset])
end 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 + 0, '!DF!')
dscreen.paintString(COLOR_WHITE, x, y + 1, '!Ha!') dscreen.paintString(COLOR_WHITE, x, y + 1, '!Ha!')
dscreen.paintString(COLOR_WHITE, x, y + 2, '!ck!') dscreen.paintString(COLOR_WHITE, x, y + 2, '!ck!')
else else
dscreen.paintTile(COLOR_WHITE, x + 0, y + 0, '!', tp(0)) dscreen.paintTile(COLOR_WHITE, x + 0, y + 0, '!', tp(1))
dscreen.paintTile(COLOR_WHITE, x + 1, y + 0, 'D', tp(1)) dscreen.paintTile(COLOR_WHITE, x + 1, y + 0, 'D', tp(2))
dscreen.paintTile(COLOR_WHITE, x + 2, y + 0, 'F', tp(2)) dscreen.paintTile(COLOR_WHITE, x + 2, y + 0, 'F', tp(3))
dscreen.paintTile(COLOR_WHITE, x + 3, y + 0, '!', tp(3)) dscreen.paintTile(COLOR_WHITE, x + 3, y + 0, '!', tp(4))
dscreen.paintTile(COLOR_WHITE, x + 0, y + 1, '!', tp(4)) dscreen.paintTile(COLOR_WHITE, x + 0, y + 1, '!', tp(5))
dscreen.paintTile(COLOR_WHITE, x + 1, y + 1, 'H', tp(5)) dscreen.paintTile(COLOR_WHITE, x + 1, y + 1, 'H', tp(6))
dscreen.paintTile(COLOR_WHITE, x + 2, y + 1, 'a', tp(6)) dscreen.paintTile(COLOR_WHITE, x + 2, y + 1, 'a', tp(7))
dscreen.paintTile(COLOR_WHITE, x + 3, y + 1, '!', tp(7)) dscreen.paintTile(COLOR_WHITE, x + 3, y + 1, '!', tp(8))
dscreen.paintTile(COLOR_WHITE, x + 0, y + 2, '!', tp(8)) dscreen.paintTile(COLOR_WHITE, x + 0, y + 2, '!', tp(9))
dscreen.paintTile(COLOR_WHITE, x + 1, y + 2, 'c', tp(9)) dscreen.paintTile(COLOR_WHITE, x + 1, y + 2, 'c', tp(10))
dscreen.paintTile(COLOR_WHITE, x + 2, y + 2, 'k', tp(10)) dscreen.paintTile(COLOR_WHITE, x + 2, y + 2, 'k', tp(11))
dscreen.paintTile(COLOR_WHITE, x + 3, y + 2, '!', tp(11)) dscreen.paintTile(COLOR_WHITE, x + 3, y + 2, '!', tp(12))
end end
end end