dfhack/library/modules/Textures.cpp

463 lines
16 KiB
C++

2023-09-01 23:38:33 -06:00
#include <algorithm>
2023-09-01 09:12:59 -06:00
#include <atomic>
2023-08-11 00:14:05 -06:00
#include <mutex>
2023-08-28 01:00:39 -06:00
#include <numeric>
2023-08-11 00:14:05 -06:00
#include <unordered_map>
#include "Internal.h"
2023-01-04 18:15:32 -07:00
#include "modules/DFSDL.h"
#include "modules/Textures.h"
#include "Debug.h"
#include "PluginManager.h"
2023-08-11 00:14:05 -06:00
#include "VTableInterpose.h"
#include "df/enabler.h"
2023-08-11 00:14:05 -06:00
#include "df/viewscreen_adopt_regionst.h"
#include "df/viewscreen_loadgamest.h"
#include "df/viewscreen_new_arenast.h"
2023-08-12 07:11:42 -06:00
#include "df/viewscreen_new_regionst.h"
2023-05-22 16:07:19 -06:00
2023-08-29 22:38:21 -06:00
#include <SDL_pixels.h>
2023-08-12 23:33:31 -06:00
#include <SDL_surface.h>
using df::global::enabler;
using namespace DFHack;
2023-01-04 18:15:32 -07:00
using namespace DFHack::DFSDL;
namespace DFHack {
2023-08-12 23:33:31 -06:00
DBG_DECLARE(core, textures, DebugCategory::LINFO);
}
2023-09-01 09:31:19 -06:00
struct ReservedRange {
void init(int32_t start) {
this->start = start;
this->end = start + ReservedRange::size;
this->current = start;
2023-09-01 23:38:33 -06:00
this->is_installed = true;
2023-09-01 09:31:19 -06:00
}
long get_new_texpos() {
if (this->current == this->end)
return -1;
2023-09-10 02:17:24 -06:00
return this->current++;
2023-09-01 09:31:19 -06:00
}
static const int32_t size = 10000; // size of reserved texpos buffer
int32_t start = -1;
int32_t end = -1;
long current = -1;
2023-09-01 23:38:33 -06:00
bool is_installed = false;
2023-09-01 09:31:19 -06:00
};
static ReservedRange reserved_range{};
2023-08-11 00:14:05 -06:00
static std::unordered_map<TexposHandle, long> g_handle_to_texpos;
2023-09-01 09:12:59 -06:00
static std::unordered_map<TexposHandle, long> g_handle_to_reserved_texpos;
2023-08-11 00:14:05 -06:00
static std::unordered_map<TexposHandle, SDL_Surface*> g_handle_to_surface;
2023-09-01 09:12:59 -06:00
static std::unordered_map<std::string, std::vector<TexposHandle>> g_tileset_to_handles;
2023-09-01 23:38:33 -06:00
static std::vector<TexposHandle> g_delayed_regs;
2023-08-11 00:14:05 -06:00
static std::mutex g_adding_mutex;
2023-09-01 09:12:59 -06:00
static std::atomic<bool> loading_state = false;
static SDL_Surface* dummy_surface = NULL;
2023-09-01 09:12:59 -06:00
// 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.
// It also deletes the source surface.
//
// It uses the same pixel format (RGBA, R at lowest address) regardless of
// hardware.
2023-08-11 12:40:41 -06:00
SDL_Surface* canonicalize_format(SDL_Surface* src) {
2023-08-29 22:38:21 -06:00
// even though we have null check after DFIMG_Load
2023-08-28 01:00:39 -06:00
// in loadTileset() (the only consumer of this method)
// it's better put nullcheck here as well
if (!src)
return src;
2023-08-29 22:38:21 -06:00
auto fmt = DFSDL_AllocFormat(SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32);
2023-08-28 01:00:39 -06:00
SDL_Surface* tgt = DFSDL_ConvertSurface(src, fmt, SDL_SWSURFACE);
2023-08-12 07:11:42 -06:00
DFSDL_FreeSurface(src);
for (int x = 0; x < tgt->w; ++x) {
for (int y = 0; y < tgt->h; ++y) {
Uint8* p = (Uint8*)tgt->pixels + y * tgt->pitch + x * 4;
if (p[3] == 0) {
for (int c = 0; c < 3; c++) {
p[c] = 0;
}
}
}
}
2023-08-28 01:00:39 -06:00
2023-08-12 07:11:42 -06:00
return tgt;
}
2023-08-11 00:14:05 -06:00
// register surface in texture raws, get a texpos
static long add_texture(SDL_Surface* surface) {
std::lock_guard<std::mutex> lg_add_texture(g_adding_mutex);
auto texpos = enabler->textures.raws.size();
enabler->textures.raws.push_back(surface);
return texpos;
}
// register surface in texture raws to specific texpos
2023-09-01 09:12:59 -06:00
static void insert_texture(SDL_Surface* surface, long texpos) {
std::lock_guard<std::mutex> lg_add_texture(g_adding_mutex);
enabler->textures.raws[texpos] = surface;
}
2023-08-28 01:00:39 -06:00
// delete surface from texture raws
static void delete_texture(long texpos) {
std::lock_guard<std::mutex> lg_add_texture(g_adding_mutex);
2023-08-28 01:46:59 -06:00
auto pos = static_cast<size_t>(texpos);
if (pos >= enabler->textures.raws.size())
2023-08-28 01:00:39 -06:00
return;
enabler->textures.raws[texpos] = NULL;
}
2023-08-11 00:14:05 -06:00
2023-08-28 01:00:39 -06:00
// create new surface with RGBA32 format and pixels as data
SDL_Surface* create_texture(std::vector<uint32_t>& pixels, int texture_px_w, int texture_px_h) {
2023-08-29 22:38:21 -06:00
auto surface = DFSDL_CreateRGBSurfaceWithFormat(0, texture_px_w, texture_px_h, 32,
SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32);
2023-08-28 01:56:08 -06:00
auto canvas_length = static_cast<size_t>(texture_px_w * texture_px_h);
for (size_t i = 0; i < pixels.size() && i < canvas_length; i++) {
2023-08-28 01:00:39 -06:00
uint32_t* p = (uint32_t*)surface->pixels + i;
*p = pixels[i];
}
return surface;
2023-08-11 00:14:05 -06:00
}
2023-08-28 01:00:39 -06:00
// convert single surface into tiles according w/h
// register tiles in texture raws and return handles
2023-09-01 09:12:59 -06:00
std::vector<TexposHandle> slice_tileset(SDL_Surface* surface, int tile_px_w, int tile_px_h,
bool reserved) {
2023-08-11 12:40:41 -06:00
std::vector<TexposHandle> handles{};
2023-08-28 01:00:39 -06:00
if (!surface)
2023-08-11 12:40:41 -06:00
return handles;
int dimx = surface->w / tile_px_w;
int dimy = surface->h / tile_px_h;
2023-08-28 01:00:39 -06:00
2023-09-01 23:38:33 -06:00
if (reserved && (dimx * dimy > reserved_range.end - reserved_range.current)) {
WARN(textures).print(
"there is not enough space in reserved range for whole tileset, using dynamic range\n");
reserved = false;
}
2023-08-11 12:40:41 -06:00
for (int y = 0; y < dimy; y++) {
for (int x = 0; x < dimx; x++) {
2023-08-12 07:11:42 -06:00
SDL_Surface* tile = DFSDL_CreateRGBSurface(
0, tile_px_w, tile_px_h, 32, surface->format->Rmask, surface->format->Gmask,
surface->format->Bmask, surface->format->Amask);
SDL_Rect vp{tile_px_w * x, tile_px_h * y, tile_px_w, tile_px_h};
2023-08-11 12:40:41 -06:00
DFSDL_UpperBlit(surface, &vp, tile, NULL);
2023-09-01 09:12:59 -06:00
auto handle = Textures::loadTexture(tile, reserved);
2023-08-11 12:40:41 -06:00
handles.push_back(handle);
}
}
DFSDL_FreeSurface(surface);
2023-08-28 01:00:39 -06:00
return handles;
}
2023-09-01 09:12:59 -06:00
TexposHandle Textures::loadTexture(SDL_Surface* surface, bool reserved) {
if (!surface || !enabler)
2023-08-28 01:00:39 -06:00
return 0; // should be some error, i guess
2023-09-01 23:38:33 -06:00
if (loading_state)
reserved = true; // use reserved range during loading for all textures
2023-08-28 01:00:39 -06:00
auto handle = reinterpret_cast<uintptr_t>(surface);
g_handle_to_surface.emplace(handle, surface);
surface->refcount++; // prevent destruct on next FreeSurface by game
2023-09-01 23:38:33 -06:00
if (reserved && reserved_range.is_installed) {
2023-09-01 09:12:59 -06:00
auto texpos = reserved_range.get_new_texpos();
2023-09-01 23:38:33 -06:00
if (texpos != -1) {
insert_texture(surface, texpos);
g_handle_to_reserved_texpos.emplace(handle, texpos);
dummy_surface->refcount--;
2023-09-01 23:38:33 -06:00
return handle;
}
if (loading_state) { // if we in loading state and reserved range is full -> error
2023-09-01 09:12:59 -06:00
ERR(textures).printerr("reserved range limit has been reached, use dynamic range\n");
return 0;
}
2023-09-01 23:38:33 -06:00
}
// if we here in loading state = true, then it should be dynamic range -> delay reg
if (loading_state) {
g_delayed_regs.push_back(handle);
2023-09-01 09:12:59 -06:00
} else {
auto texpos = add_texture(surface);
g_handle_to_texpos.emplace(handle, texpos);
}
2023-09-01 23:38:33 -06:00
2023-08-28 01:00:39 -06:00
return handle;
}
std::vector<TexposHandle> Textures::loadTileset(const std::string& file, int tile_px_w,
2023-09-01 09:12:59 -06:00
int tile_px_h, bool reserved) {
if (g_tileset_to_handles.contains(file))
return g_tileset_to_handles[file];
2023-09-10 05:50:03 -06:00
if (!enabler)
return std::vector<TexposHandle>{};
2023-08-28 01:00:39 -06:00
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);
2023-09-01 09:12:59 -06:00
auto handles = slice_tileset(surface, tile_px_w, tile_px_h, reserved);
2023-08-28 01:00:39 -06:00
2023-09-01 23:38:33 -06:00
DEBUG(textures).print("loaded %zd textures from '%s'\n", handles.size(), file.c_str());
2023-09-01 09:12:59 -06:00
g_tileset_to_handles[file] = handles;
2023-09-01 23:38:33 -06:00
2023-08-11 12:40:41 -06:00
return handles;
}
long Textures::getTexposByHandle(TexposHandle handle) {
if (!handle || !enabler)
2023-08-11 12:40:41 -06:00
return -1;
2023-08-11 00:14:05 -06:00
2023-09-01 09:12:59 -06:00
if (g_handle_to_reserved_texpos.contains(handle))
return g_handle_to_reserved_texpos[handle];
2023-08-11 12:40:41 -06:00
if (g_handle_to_texpos.contains(handle))
return g_handle_to_texpos[handle];
2023-09-01 23:38:33 -06:00
if (std::find(g_delayed_regs.begin(), g_delayed_regs.end(), handle) != g_delayed_regs.end())
return 0;
2023-08-11 12:40:41 -06:00
if (g_handle_to_surface.contains(handle)) {
g_handle_to_surface[handle]->refcount++; // prevent destruct on next FreeSurface by game
2023-09-01 23:38:33 -06:00
if (loading_state) { // reinit dor dynamic range during loading -> delayed
g_delayed_regs.push_back(handle);
return 0;
}
2023-08-11 12:40:41 -06:00
auto texpos = add_texture(g_handle_to_surface[handle]);
g_handle_to_texpos.emplace(handle, texpos);
return texpos;
2023-08-11 00:14:05 -06:00
}
2023-08-11 12:40:41 -06:00
return -1;
2023-08-11 00:14:05 -06:00
}
2023-09-01 09:12:59 -06:00
TexposHandle Textures::createTile(std::vector<uint32_t>& pixels, int tile_px_w, int tile_px_h,
bool reserved) {
if (!enabler)
return 0;
2023-08-28 01:00:39 -06:00
auto texture = create_texture(pixels, tile_px_w, tile_px_h);
2023-09-01 09:12:59 -06:00
auto handle = Textures::loadTexture(texture, reserved);
2023-08-28 01:00:39 -06:00
return handle;
}
std::vector<TexposHandle> Textures::createTileset(std::vector<uint32_t>& pixels, int texture_px_w,
2023-09-01 09:12:59 -06:00
int texture_px_h, int tile_px_w, int tile_px_h,
bool reserved) {
if (!enabler)
return std::vector<TexposHandle>{};
2023-08-28 01:00:39 -06:00
auto texture = create_texture(pixels, texture_px_w, texture_px_h);
2023-09-01 09:12:59 -06:00
auto handles = slice_tileset(texture, tile_px_w, tile_px_h, reserved);
2023-08-28 01:00:39 -06:00
return handles;
}
void Textures::deleteHandle(TexposHandle handle) {
if (!enabler)
return;
2023-08-28 01:00:39 -06:00
auto texpos = Textures::getTexposByHandle(handle);
if (texpos > 0)
delete_texture(texpos);
2023-09-01 09:12:59 -06:00
if (g_handle_to_reserved_texpos.contains(handle))
g_handle_to_reserved_texpos.erase(handle);
2023-08-28 01:00:39 -06:00
if (g_handle_to_texpos.contains(handle))
g_handle_to_texpos.erase(handle);
2023-09-02 02:05:28 -06:00
if (auto it = std::find(g_delayed_regs.begin(), g_delayed_regs.end(), handle);
it != g_delayed_regs.end())
g_delayed_regs.erase(it);
2023-08-28 01:00:39 -06:00
if (g_handle_to_surface.contains(handle)) {
auto surface = g_handle_to_surface[handle];
2023-08-29 22:38:21 -06:00
while (surface->refcount)
2023-08-28 01:00:39 -06:00
DFSDL_FreeSurface(surface);
g_handle_to_surface.erase(handle);
}
}
2023-08-11 00:14:05 -06:00
static void reset_texpos() {
DEBUG(textures).print("resetting texture mappings\n");
2023-08-11 00:14:05 -06:00
g_handle_to_texpos.clear();
}
2023-09-01 09:12:59 -06:00
static void reset_reserved_texpos() {
DEBUG(textures).print("resetting reserved texture mappings\n");
g_handle_to_reserved_texpos.clear();
}
static void reset_tilesets() {
DEBUG(textures).print("resetting tileset to handle mappings\n");
g_tileset_to_handles.clear();
}
2023-08-11 00:14:05 -06:00
static void reset_surface() {
2023-09-01 09:12:59 -06:00
DEBUG(textures).print("deleting cached surfaces\n");
2023-08-11 00:14:05 -06:00
for (auto& entry : g_handle_to_surface) {
DFSDL_FreeSurface(entry.second);
}
g_handle_to_surface.clear();
}
2023-09-01 23:38:33 -06:00
static void register_delayed_handles() {
DEBUG(textures).print("register delayed handles, size %zd\n", g_delayed_regs.size());
for (auto& handle : g_delayed_regs) {
auto texpos = add_texture(g_handle_to_surface[handle]);
g_handle_to_texpos.emplace(handle, texpos);
}
g_delayed_regs.clear();
}
2023-08-11 00:14:05 -06:00
// reset point on New Game
struct tracking_stage_new_region : df::viewscreen_new_regionst {
typedef df::viewscreen_new_regionst interpose_base;
DEFINE_VMETHOD_INTERPOSE(void, logic, ()) {
if (this->m_raw_load_stage != this->raw_load_stage) {
2023-09-01 09:12:59 -06:00
TRACE(textures).print("raw_load_stage %d -> %d\n", this->m_raw_load_stage,
this->raw_load_stage);
2023-09-01 23:38:33 -06:00
bool tmp_state = loading_state;
2023-09-01 09:12:59 -06:00
loading_state = this->raw_load_stage >= 0 && this->raw_load_stage < 3 ? true : false;
2023-09-01 23:38:33 -06:00
if (tmp_state != loading_state && !loading_state)
register_delayed_handles();
2023-08-11 00:14:05 -06:00
this->m_raw_load_stage = this->raw_load_stage;
if (this->m_raw_load_stage == 1)
reset_texpos();
}
INTERPOSE_NEXT(logic)();
}
private:
inline static int m_raw_load_stage = -2; // not valid state at the start
};
IMPLEMENT_VMETHOD_INTERPOSE(tracking_stage_new_region, logic);
// reset point on New Game in Existing World
struct tracking_stage_adopt_region : df::viewscreen_adopt_regionst {
typedef df::viewscreen_adopt_regionst interpose_base;
DEFINE_VMETHOD_INTERPOSE(void, logic, ()) {
if (this->m_cur_step != this->cur_step) {
TRACE(textures).print("step %d -> %d\n", this->m_cur_step, this->cur_step);
2023-09-01 23:38:33 -06:00
bool tmp_state = loading_state;
2023-09-01 09:12:59 -06:00
loading_state = this->cur_step >= 0 && this->cur_step < 3 ? true : false;
2023-09-01 23:38:33 -06:00
if (tmp_state != loading_state && !loading_state)
register_delayed_handles();
2023-08-11 00:14:05 -06:00
this->m_cur_step = this->cur_step;
2023-08-27 13:28:03 -06:00
if (this->m_cur_step == 1)
2023-08-11 00:14:05 -06:00
reset_texpos();
}
INTERPOSE_NEXT(logic)();
}
private:
inline static int m_cur_step = -2; // not valid state at the start
};
IMPLEMENT_VMETHOD_INTERPOSE(tracking_stage_adopt_region, logic);
// reset point on Load Game
struct tracking_stage_load_region : df::viewscreen_loadgamest {
typedef df::viewscreen_loadgamest interpose_base;
DEFINE_VMETHOD_INTERPOSE(void, logic, ()) {
if (this->m_cur_step != this->cur_step) {
TRACE(textures).print("step %d -> %d\n", this->m_cur_step, this->cur_step);
2023-09-01 23:38:33 -06:00
bool tmp_state = loading_state;
2023-09-01 09:12:59 -06:00
loading_state = this->cur_step >= 0 && this->cur_step < 3 ? true : false;
2023-09-01 23:38:33 -06:00
if (tmp_state != loading_state && !loading_state)
register_delayed_handles();
2023-08-11 00:14:05 -06:00
this->m_cur_step = this->cur_step;
2023-08-12 07:11:42 -06:00
if (this->m_cur_step == 1)
2023-08-11 00:14:05 -06:00
reset_texpos();
}
INTERPOSE_NEXT(logic)();
}
private:
inline static int m_cur_step = -2; // not valid state at the start
};
IMPLEMENT_VMETHOD_INTERPOSE(tracking_stage_load_region, logic);
// reset point on New Arena
struct tracking_stage_new_arena : df::viewscreen_new_arenast {
typedef df::viewscreen_new_arenast interpose_base;
DEFINE_VMETHOD_INTERPOSE(void, logic, ()) {
if (this->m_cur_step != this->cur_step) {
TRACE(textures).print("step %d -> %d\n", this->m_cur_step, this->cur_step);
2023-09-01 23:38:33 -06:00
bool tmp_state = loading_state;
2023-09-01 09:12:59 -06:00
loading_state = this->cur_step >= 0 && this->cur_step < 3 ? true : false;
2023-09-01 23:38:33 -06:00
if (tmp_state != loading_state && !loading_state)
register_delayed_handles();
2023-08-11 00:14:05 -06:00
this->m_cur_step = this->cur_step;
if (this->m_cur_step == 0)
reset_texpos();
}
INTERPOSE_NEXT(logic)();
}
private:
inline static int m_cur_step = -2; // not valid state at the start
};
IMPLEMENT_VMETHOD_INTERPOSE(tracking_stage_new_arena, logic);
2023-08-11 12:40:41 -06:00
static void install_reset_point() {
2023-08-11 00:14:05 -06:00
INTERPOSE_HOOK(tracking_stage_new_region, logic).apply();
INTERPOSE_HOOK(tracking_stage_adopt_region, logic).apply();
INTERPOSE_HOOK(tracking_stage_load_region, logic).apply();
INTERPOSE_HOOK(tracking_stage_new_arena, logic).apply();
}
static void uninstall_reset_point() {
INTERPOSE_HOOK(tracking_stage_new_region, logic).remove();
INTERPOSE_HOOK(tracking_stage_adopt_region, logic).remove();
INTERPOSE_HOOK(tracking_stage_load_region, logic).remove();
INTERPOSE_HOOK(tracking_stage_new_arena, logic).remove();
}
2023-09-01 09:12:59 -06:00
static void reserve_static_range() {
2023-09-01 23:38:33 -06:00
if (static_cast<size_t>(enabler->textures.init_texture_size) != enabler->textures.raws.size()) {
WARN(textures).print(
"reserved range can't be installed! all textures will be loaded to dynamic range!");
return;
}
2023-09-01 09:12:59 -06:00
reserved_range.init(enabler->textures.init_texture_size);
dummy_surface =
DFSDL_CreateRGBSurfaceWithFormat(0, 0, 0, 32, SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32);
dummy_surface->refcount += ReservedRange::size;
2023-09-01 09:31:19 -06:00
for (int32_t i = 0; i < ReservedRange::size; i++) {
2023-09-01 09:12:59 -06:00
add_texture(dummy_surface);
}
2023-09-01 09:31:19 -06:00
enabler->textures.init_texture_size += ReservedRange::size;
2023-09-01 09:12:59 -06:00
}
2023-08-12 07:11:42 -06:00
void Textures::init(color_ostream& out) {
if (!enabler)
return;
2023-09-01 09:12:59 -06:00
reserve_static_range();
2023-08-12 07:11:42 -06:00
install_reset_point();
2023-09-01 09:12:59 -06:00
DEBUG(textures, out)
.print("dynamic texture loading ready, reserved range %d-%d\n", reserved_range.start,
reserved_range.end);
}
void Textures::cleanup() {
if (!enabler)
return;
2023-08-11 12:40:41 -06:00
reset_texpos();
2023-09-01 09:12:59 -06:00
reset_reserved_texpos();
reset_tilesets();
2023-08-11 12:40:41 -06:00
reset_surface();
uninstall_reset_point();
2023-08-11 00:48:03 -06:00
}