From 6f11812729e42e720544e7157b29e728cf4fc667 Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Mon, 28 Aug 2023 10:00:39 +0300 Subject: [PATCH] create and delete textures --- docs/about/Authors.rst | 1 + library/LuaApi.cpp | 42 ++++++++++ library/include/LuaTools.h | 11 +++ library/include/modules/DFSDL.h | 2 + library/include/modules/Textures.h | 21 +++++ library/modules/DFSDL.cpp | 15 +++- library/modules/Textures.cpp | 127 ++++++++++++++++++++--------- 7 files changed, 179 insertions(+), 40 deletions(-) diff --git a/docs/about/Authors.rst b/docs/about/Authors.rst index cf74c6412..ba556ab64 100644 --- a/docs/about/Authors.rst +++ b/docs/about/Authors.rst @@ -203,6 +203,7 @@ Sebastian Wolfertz Enkrod SeerSkye SeerSkye seishuuu seishuuu Seth Woodworth sethwoodworth +shevernitskiy shevernitskiy Shim Panze Shim-Panze Silver silverflyone simon diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index a1821d2fb..241d1888b 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1775,9 +1775,51 @@ static int textures_getTexposByHandle(lua_State *state) return 1; } +static int textures_deleteHandle(lua_State *state) +{ + if (lua_isinteger(state,1)) { + auto handle = luaL_checkunsigned(state, 1); + Textures::deleteHandle(handle); + } else if (lua_istable(state,1)) { + std::vector handles; + Lua::GetVector(state, handles); + for (auto& handle: handles) { + Textures::deleteHandle(handle); + } + } + return 0; +} + +static int textures_createTile(lua_State *state) +{ + std::vector pixels; + Lua::GetVector(state, pixels); + auto tile_w = luaL_checkint(state, 2); + auto tile_h = luaL_checkint(state, 3); + auto handle = Textures::createTile(pixels, tile_w, tile_h); + Lua::Push(state, handle); + return 1; +} + +static int textures_createTileset(lua_State *state) +{ + std::vector pixels; + Lua::GetVector(state, pixels); + auto texture_w = luaL_checkint(state, 2); + auto texture_h = luaL_checkint(state, 3); + auto tile_w = luaL_checkint(state, 4); + auto tile_h = luaL_checkint(state, 5); + auto handles = Textures::createTileset(pixels, texture_w, texture_h, tile_w, tile_h); + Lua::PushVector(state, handles); + return 1; +} + static const luaL_Reg dfhack_textures_funcs[] = { { "loadTileset", textures_loadTileset }, { "getTexposByHandle", textures_getTexposByHandle }, + { "deleteHandle", textures_deleteHandle }, + { "createTile", textures_createTile }, + { "createTileset", textures_createTileset }, { NULL, NULL } }; diff --git a/library/include/LuaTools.h b/library/include/LuaTools.h index cab3ee9cc..188d63854 100644 --- a/library/include/LuaTools.h +++ b/library/include/LuaTools.h @@ -410,6 +410,17 @@ namespace DFHack {namespace Lua { } } + template + requires std::is_arithmetic_v + void GetVector(lua_State *state, std::vector &pvec, int idx = 1) { + lua_pushnil(state); // first key + while (lua_next(state, idx) != 0) + { + pvec.push_back(lua_tointeger(state, -1)); + lua_pop(state, 1); // remove value, leave key + } + } + DFHACK_EXPORT void GetVector(lua_State *state, std::vector &pvec, int idx = 1); DFHACK_EXPORT void CheckPen(lua_State *L, Screen::Pen *pen, int index, bool allow_nil = false, bool allow_color = true); diff --git a/library/include/modules/DFSDL.h b/library/include/modules/DFSDL.h index 36dd641d5..b70255ec8 100644 --- a/library/include/modules/DFSDL.h +++ b/library/include/modules/DFSDL.h @@ -48,6 +48,8 @@ DFHACK_EXPORT void DFSDL_FreeSurface(SDL_Surface *surface); // DFHACK_EXPORT int DFSDL_SemPost(SDL_sem *sem); DFHACK_EXPORT int DFSDL_PushEvent(SDL_Event *event); DFHACK_EXPORT void DFSDL_free(void *ptr); +DFHACK_EXPORT SDL_PixelFormat* DFSDL_AllocFormat(uint32_t pixel_format); +DFHACK_EXPORT SDL_Surface* DFSDL_CreateRGBSurfaceWithFormat(uint32_t flags, int width, int height, int depth, uint32_t format); // submitted and returned text is UTF-8 // see wrapper functions below for cp-437 variants diff --git a/library/include/modules/Textures.h b/library/include/modules/Textures.h index fa1f743be..e371df90e 100644 --- a/library/include/modules/Textures.h +++ b/library/include/modules/Textures.h @@ -43,6 +43,27 @@ DFHACK_EXPORT std::vector loadTileset(const std::string& file, */ DFHACK_EXPORT long getTexposByHandle(TexposHandle handle); +/** + * Delete all info about texture by TexposHandle + */ +DFHACK_EXPORT void deleteHandle(TexposHandle handle); + +/** + * Create new texture with RGBA32 format and pixels as data. + * Register this texture and return TexposHandle. + */ +DFHACK_EXPORT TexposHandle createTile(std::vector& pixels, int tile_px_w = TILE_WIDTH_PX, + int tile_px_h = TILE_HEIGHT_PX); + +/** + * Create new textures as tileset with RGBA32 format and pixels as data. + * Register this textures and return vector of TexposHandle. + */ +DFHACK_EXPORT std::vector createTileset(std::vector& pixels, + int texture_px_w, int texture_px_h, + int tile_px_w = TILE_WIDTH_PX, + int tile_px_h = TILE_HEIGHT_PX); + /** * Call this on DFHack init just once to setup interposed handlers and * init static assets. diff --git a/library/modules/DFSDL.cpp b/library/modules/DFSDL.cpp index 72105b2e6..398a9c8b2 100644 --- a/library/modules/DFSDL.cpp +++ b/library/modules/DFSDL.cpp @@ -41,6 +41,8 @@ SDL_bool (*g_SDL_HasClipboardText)(); int (*g_SDL_SetClipboardText)(const char *text); char * (*g_SDL_GetClipboardText)(); void (*g_SDL_free)(void *); +SDL_PixelFormat* (*g_SDL_AllocFormat)(uint32_t pixel_format) = nullptr; +SDL_Surface* (*g_SDL_CreateRGBSurfaceWithFormat)(uint32_t flags, int width, int height, int depth, uint32_t format) = nullptr; bool DFSDL::init(color_ostream &out) { for (auto &lib_str : SDL_LIBS) { @@ -81,7 +83,9 @@ bool DFSDL::init(color_ostream &out) { bind(g_sdl_handle, SDL_SetClipboardText); bind(g_sdl_handle, SDL_GetClipboardText); bind(g_sdl_handle, SDL_free); - #undef bind + bind(g_sdl_handle, SDL_AllocFormat); + bind(g_sdl_handle, SDL_CreateRGBSurfaceWithFormat); +#undef bind DEBUG(dfsdl,out).print("sdl successfully loaded\n"); return true; @@ -147,6 +151,15 @@ int DFSDL::DFSDL_SetClipboardText(const char *text) { return g_SDL_SetClipboardText(text); } +SDL_PixelFormat* DFSDL::DFSDL_AllocFormat(uint32_t pixel_format) { + return g_SDL_AllocFormat(pixel_format); +} + +SDL_Surface* DFSDL::DFSDL_CreateRGBSurfaceWithFormat(uint32_t flags, int width, int height, int depth, uint32_t format) { + return g_SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format); +} + + DFHACK_EXPORT std::string DFHack::getClipboardTextCp437() { if (!g_sdl_handle || g_SDL_HasClipboardText() != SDL_TRUE) return ""; diff --git a/library/modules/Textures.cpp b/library/modules/Textures.cpp index 48bc71e3c..26a769e48 100644 --- a/library/modules/Textures.cpp +++ b/library/modules/Textures.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "Internal.h" @@ -30,6 +31,10 @@ static std::unordered_map g_handle_to_texpos; static std::unordered_map g_handle_to_surface; static std::mutex g_adding_mutex; +// it is SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32 +// initialized inplace to avoid including SDL_pixels.h +static const uint32_t RGBA32 = 376840196; + // Converts an arbitrary Surface to something like the display format // (32-bit RGBA), and converts magenta to transparency if convert_magenta is set // and the source surface didn't already have an alpha channel. @@ -38,28 +43,14 @@ static std::mutex g_adding_mutex; // It uses the same pixel format (RGBA, R at lowest address) regardless of // hardware. SDL_Surface* canonicalize_format(SDL_Surface* src) { - SDL_PixelFormat fmt; - fmt.palette = NULL; - fmt.BitsPerPixel = 32; - fmt.BytesPerPixel = 4; - fmt.Rloss = fmt.Gloss = fmt.Bloss = fmt.Aloss = 0; -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - fmt.Rshift = 24; - fmt.Gshift = 16; - fmt.Bshift = 8; - fmt.Ashift = 0; -#else - fmt.Rshift = 0; - fmt.Gshift = 8; - fmt.Bshift = 16; - fmt.Ashift = 24; -#endif - fmt.Rmask = 255 << fmt.Rshift; - fmt.Gmask = 255 << fmt.Gshift; - fmt.Bmask = 255 << fmt.Bshift; - fmt.Amask = 255 << fmt.Ashift; - - SDL_Surface* tgt = DFSDL_ConvertSurface(src, &fmt, SDL_SWSURFACE); + // however we have null check after DFIMG_Load + // in loadTileset() (the only consumer of this method) + // it's better put nullcheck here as well + if (!src) + return src; + + auto fmt = DFSDL_AllocFormat(RGBA32); + SDL_Surface* tgt = DFSDL_ConvertSurface(src, fmt, SDL_SWSURFACE); DFSDL_FreeSurface(src); for (int x = 0; x < tgt->w; ++x) { for (int y = 0; y < tgt->h; ++y) { @@ -71,6 +62,7 @@ SDL_Surface* canonicalize_format(SDL_Surface* src) { } } } + return tgt; } @@ -82,31 +74,34 @@ static long add_texture(SDL_Surface* surface) { return texpos; } -TexposHandle Textures::loadTexture(SDL_Surface* surface) { - if (!surface) - return 0; // should be some error, i guess +// delete surface from texture raws +static void delete_texture(long texpos) { + std::lock_guard lg_add_texture(g_adding_mutex); + if (texpos >= enabler->textures.raws.size()) + return; + enabler->textures.raws[texpos] = NULL; +} - 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); - g_handle_to_texpos.emplace(handle, texpos); - return handle; +// create new surface with RGBA32 format and pixels as data +SDL_Surface* create_texture(std::vector& pixels, int texture_px_w, int texture_px_h) { + auto surface = DFSDL_CreateRGBSurfaceWithFormat(0, texture_px_w, texture_px_h, 32, RGBA32); + for (auto i = 0; i < pixels.size() && i < texture_px_w * texture_px_h; i++) { + uint32_t* p = (uint32_t*)surface->pixels + i; + *p = pixels[i]; + } + return surface; } -std::vector Textures::loadTileset(const std::string& file, int tile_px_w, - int tile_px_h) { +// convert single surface into tiles according w/h +// register tiles in texture raws and return handles +std::vector slice_tileset(SDL_Surface* surface, int tile_px_w, int tile_px_h) { std::vector handles{}; - - SDL_Surface* surface = DFIMG_Load(file.c_str()); - if (!surface) { - ERR(textures).printerr("unable to load textures from '%s'\n", file.c_str()); + if (!surface) return handles; - } - surface = canonicalize_format(surface); int dimx = surface->w / tile_px_w; int dimy = surface->h / tile_px_h; + for (int y = 0; y < dimy; y++) { for (int x = 0; x < dimx; x++) { SDL_Surface* tile = DFSDL_CreateRGBSurface( @@ -120,6 +115,32 @@ std::vector Textures::loadTileset(const std::string& file, int til } DFSDL_FreeSurface(surface); + return handles; +} + +TexposHandle Textures::loadTexture(SDL_Surface* surface) { + if (!surface) + return 0; // should be some error, i guess + + 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); + g_handle_to_texpos.emplace(handle, texpos); + return handle; +} + +std::vector Textures::loadTileset(const std::string& file, int tile_px_w, + int tile_px_h) { + SDL_Surface* surface = DFIMG_Load(file.c_str()); + if (!surface) { + ERR(textures).printerr("unable to load textures from '%s'\n", file.c_str()); + return std::vector{}; + } + + surface = canonicalize_format(surface); + auto handles = slice_tileset(surface, tile_px_w, tile_px_h); + DEBUG(textures).print("loaded %zd textures from '%s'\n", handles.size(), file.c_str()); return handles; @@ -142,6 +163,34 @@ long Textures::getTexposByHandle(TexposHandle handle) { return -1; } +TexposHandle Textures::createTile(std::vector& pixels, int tile_px_w, int tile_px_h) { + auto texture = create_texture(pixels, tile_px_w, tile_px_h); + auto handle = Textures::loadTexture(texture); + return handle; +} + +std::vector Textures::createTileset(std::vector& pixels, int texture_px_w, + int texture_px_h, int tile_px_w, int tile_px_h) { + auto texture = create_texture(pixels, texture_px_w, texture_px_h); + auto handles = slice_tileset(texture, tile_px_w, tile_px_h); + return handles; +} + +void Textures::deleteHandle(TexposHandle handle) { + auto texpos = Textures::getTexposByHandle(handle); + if (texpos > 0) + delete_texture(texpos); + if (g_handle_to_texpos.contains(handle)) + g_handle_to_texpos.erase(handle); + if (g_handle_to_surface.contains(handle)) { + auto surface = g_handle_to_surface[handle]; + for (auto i = 0; i < surface->refcount; i++) { + DFSDL_FreeSurface(surface); + } + g_handle_to_surface.erase(handle); + } +} + static void reset_texpos() { g_handle_to_texpos.clear(); }