create and delete textures

develop
shevernitskiy 2023-08-28 10:00:39 +03:00
parent 3ce2909309
commit 6f11812729
7 changed files with 179 additions and 40 deletions

@ -203,6 +203,7 @@ Sebastian Wolfertz Enkrod
SeerSkye SeerSkye SeerSkye SeerSkye
seishuuu seishuuu seishuuu seishuuu
Seth Woodworth sethwoodworth Seth Woodworth sethwoodworth
shevernitskiy shevernitskiy
Shim Panze Shim-Panze Shim Panze Shim-Panze
Silver silverflyone Silver silverflyone
simon simon

@ -1775,9 +1775,51 @@ static int textures_getTexposByHandle(lua_State *state)
return 1; 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<TexposHandle> handles;
Lua::GetVector(state, handles);
for (auto& handle: handles) {
Textures::deleteHandle(handle);
}
}
return 0;
}
static int textures_createTile(lua_State *state)
{
std::vector<uint32_t> 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<uint32_t> 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[] = { static const luaL_Reg dfhack_textures_funcs[] = {
{ "loadTileset", textures_loadTileset }, { "loadTileset", textures_loadTileset },
{ "getTexposByHandle", textures_getTexposByHandle }, { "getTexposByHandle", textures_getTexposByHandle },
{ "deleteHandle", textures_deleteHandle },
{ "createTile", textures_createTile },
{ "createTileset", textures_createTileset },
{ NULL, NULL } { NULL, NULL }
}; };

@ -410,6 +410,17 @@ namespace DFHack {namespace Lua {
} }
} }
template<typename T>
requires std::is_arithmetic_v<T>
void GetVector(lua_State *state, std::vector<T> &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<std::string> &pvec, int idx = 1); DFHACK_EXPORT void GetVector(lua_State *state, std::vector<std::string> &pvec, int idx = 1);
DFHACK_EXPORT void CheckPen(lua_State *L, Screen::Pen *pen, int index, bool allow_nil = false, bool allow_color = true); DFHACK_EXPORT void CheckPen(lua_State *L, Screen::Pen *pen, int index, bool allow_nil = false, bool allow_color = true);

@ -48,6 +48,8 @@ DFHACK_EXPORT void DFSDL_FreeSurface(SDL_Surface *surface);
// DFHACK_EXPORT int DFSDL_SemPost(SDL_sem *sem); // DFHACK_EXPORT int DFSDL_SemPost(SDL_sem *sem);
DFHACK_EXPORT int DFSDL_PushEvent(SDL_Event *event); DFHACK_EXPORT int DFSDL_PushEvent(SDL_Event *event);
DFHACK_EXPORT void DFSDL_free(void *ptr); 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 // submitted and returned text is UTF-8
// see wrapper functions below for cp-437 variants // see wrapper functions below for cp-437 variants

@ -43,6 +43,27 @@ DFHACK_EXPORT std::vector<TexposHandle> loadTileset(const std::string& file,
*/ */
DFHACK_EXPORT long getTexposByHandle(TexposHandle handle); 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<uint32_t>& 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<TexposHandle> createTileset(std::vector<uint32_t>& 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 * Call this on DFHack init just once to setup interposed handlers and
* init static assets. * init static assets.

@ -41,6 +41,8 @@ SDL_bool (*g_SDL_HasClipboardText)();
int (*g_SDL_SetClipboardText)(const char *text); int (*g_SDL_SetClipboardText)(const char *text);
char * (*g_SDL_GetClipboardText)(); char * (*g_SDL_GetClipboardText)();
void (*g_SDL_free)(void *); 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) { bool DFSDL::init(color_ostream &out) {
for (auto &lib_str : SDL_LIBS) { for (auto &lib_str : SDL_LIBS) {
@ -81,6 +83,8 @@ bool DFSDL::init(color_ostream &out) {
bind(g_sdl_handle, SDL_SetClipboardText); bind(g_sdl_handle, SDL_SetClipboardText);
bind(g_sdl_handle, SDL_GetClipboardText); bind(g_sdl_handle, SDL_GetClipboardText);
bind(g_sdl_handle, SDL_free); bind(g_sdl_handle, SDL_free);
bind(g_sdl_handle, SDL_AllocFormat);
bind(g_sdl_handle, SDL_CreateRGBSurfaceWithFormat);
#undef bind #undef bind
DEBUG(dfsdl,out).print("sdl successfully loaded\n"); DEBUG(dfsdl,out).print("sdl successfully loaded\n");
@ -147,6 +151,15 @@ int DFSDL::DFSDL_SetClipboardText(const char *text) {
return g_SDL_SetClipboardText(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() { DFHACK_EXPORT std::string DFHack::getClipboardTextCp437() {
if (!g_sdl_handle || g_SDL_HasClipboardText() != SDL_TRUE) if (!g_sdl_handle || g_SDL_HasClipboardText() != SDL_TRUE)
return ""; return "";

@ -1,4 +1,5 @@
#include <mutex> #include <mutex>
#include <numeric>
#include <unordered_map> #include <unordered_map>
#include "Internal.h" #include "Internal.h"
@ -30,6 +31,10 @@ static std::unordered_map<TexposHandle, long> g_handle_to_texpos;
static std::unordered_map<TexposHandle, SDL_Surface*> g_handle_to_surface; static std::unordered_map<TexposHandle, SDL_Surface*> g_handle_to_surface;
static std::mutex g_adding_mutex; 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 // Converts an arbitrary Surface to something like the display format
// (32-bit RGBA), and converts magenta to transparency if convert_magenta is set // (32-bit RGBA), and converts magenta to transparency if convert_magenta is set
// and the source surface didn't already have an alpha channel. // 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 // It uses the same pixel format (RGBA, R at lowest address) regardless of
// hardware. // hardware.
SDL_Surface* canonicalize_format(SDL_Surface* src) { SDL_Surface* canonicalize_format(SDL_Surface* src) {
SDL_PixelFormat fmt; // however we have null check after DFIMG_Load
fmt.palette = NULL; // in loadTileset() (the only consumer of this method)
fmt.BitsPerPixel = 32; // it's better put nullcheck here as well
fmt.BytesPerPixel = 4; if (!src)
fmt.Rloss = fmt.Gloss = fmt.Bloss = fmt.Aloss = 0; return src;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
fmt.Rshift = 24; auto fmt = DFSDL_AllocFormat(RGBA32);
fmt.Gshift = 16; SDL_Surface* tgt = DFSDL_ConvertSurface(src, fmt, SDL_SWSURFACE);
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);
DFSDL_FreeSurface(src); DFSDL_FreeSurface(src);
for (int x = 0; x < tgt->w; ++x) { for (int x = 0; x < tgt->w; ++x) {
for (int y = 0; y < tgt->h; ++y) { for (int y = 0; y < tgt->h; ++y) {
@ -71,6 +62,7 @@ SDL_Surface* canonicalize_format(SDL_Surface* src) {
} }
} }
} }
return tgt; return tgt;
} }
@ -82,31 +74,34 @@ static long add_texture(SDL_Surface* surface) {
return texpos; return texpos;
} }
TexposHandle Textures::loadTexture(SDL_Surface* surface) { // delete surface from texture raws
if (!surface) static void delete_texture(long texpos) {
return 0; // should be some error, i guess std::lock_guard<std::mutex> lg_add_texture(g_adding_mutex);
if (texpos >= enabler->textures.raws.size())
return;
enabler->textures.raws[texpos] = NULL;
}
auto handle = reinterpret_cast<uintptr_t>(surface); // create new surface with RGBA32 format and pixels as data
g_handle_to_surface.emplace(handle, surface); SDL_Surface* create_texture(std::vector<uint32_t>& pixels, int texture_px_w, int texture_px_h) {
surface->refcount++; // prevent destruct on next FreeSurface by game auto surface = DFSDL_CreateRGBSurfaceWithFormat(0, texture_px_w, texture_px_h, 32, RGBA32);
auto texpos = add_texture(surface); for (auto i = 0; i < pixels.size() && i < texture_px_w * texture_px_h; i++) {
g_handle_to_texpos.emplace(handle, texpos); uint32_t* p = (uint32_t*)surface->pixels + i;
return handle; *p = pixels[i];
}
return surface;
} }
std::vector<TexposHandle> Textures::loadTileset(const std::string& file, int tile_px_w, // convert single surface into tiles according w/h
int tile_px_h) { // register tiles in texture raws and return handles
std::vector<TexposHandle> slice_tileset(SDL_Surface* surface, int tile_px_w, int tile_px_h) {
std::vector<TexposHandle> handles{}; std::vector<TexposHandle> handles{};
if (!surface)
SDL_Surface* surface = DFIMG_Load(file.c_str());
if (!surface) {
ERR(textures).printerr("unable to load textures from '%s'\n", file.c_str());
return handles; return handles;
}
surface = canonicalize_format(surface);
int dimx = surface->w / tile_px_w; int dimx = surface->w / tile_px_w;
int dimy = surface->h / tile_px_h; int dimy = surface->h / tile_px_h;
for (int y = 0; y < dimy; y++) { for (int y = 0; y < dimy; y++) {
for (int x = 0; x < dimx; x++) { for (int x = 0; x < dimx; x++) {
SDL_Surface* tile = DFSDL_CreateRGBSurface( SDL_Surface* tile = DFSDL_CreateRGBSurface(
@ -120,6 +115,32 @@ std::vector<TexposHandle> Textures::loadTileset(const std::string& file, int til
} }
DFSDL_FreeSurface(surface); 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<uintptr_t>(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<TexposHandle> 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<TexposHandle>{};
}
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()); DEBUG(textures).print("loaded %zd textures from '%s'\n", handles.size(), file.c_str());
return handles; return handles;
@ -142,6 +163,34 @@ long Textures::getTexposByHandle(TexposHandle handle) {
return -1; return -1;
} }
TexposHandle Textures::createTile(std::vector<uint32_t>& 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<TexposHandle> Textures::createTileset(std::vector<uint32_t>& 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() { static void reset_texpos() {
g_handle_to_texpos.clear(); g_handle_to_texpos.clear();
} }