dynamic texture loading

develop
shevernitskiy 2023-08-11 09:14:05 +03:00
parent 34ddf6bed7
commit e21f0f2b69
2 changed files with 217 additions and 40 deletions

@ -1,8 +1,14 @@
#pragma once #pragma once
#include <optional>
#include "Export.h" #include "Export.h"
#include "ColorText.h" #include "ColorText.h"
#include <SDL_surface.h>
using TexposHandle = uintptr_t;
namespace DFHack { namespace DFHack {
/** /**
@ -12,6 +18,19 @@ namespace DFHack {
*/ */
namespace Textures { namespace Textures {
/**
* Load texture and get handle.
* Keep it to obtain valid texpos.
*/
DFHACK_EXPORT TexposHandle loadTexture(SDL_Surface* surface);
/**
* Get texpos by handle.
* Always use this function, if you need to get valid texpos for your texure.
* Texpos can change on game textures reset, but handle will be the same.
*/
DFHACK_EXPORT std::optional<long> getTexposByHandle(TexposHandle handle);
/** /**
* Call this on DFHack init and on every viewscreen change so we can reload * Call this on DFHack init and on every viewscreen change so we can reload
* and reindex textures as needed. * and reindex textures as needed.
@ -71,4 +90,4 @@ DFHACK_EXPORT long getPanelBordersTexposStart();
DFHACK_EXPORT long getWindowBordersTexposStart(); DFHACK_EXPORT long getWindowBordersTexposStart();
} }
} }

@ -1,3 +1,6 @@
#include <mutex>
#include <unordered_map>
#include "Internal.h" #include "Internal.h"
#include "modules/DFSDL.h" #include "modules/DFSDL.h"
@ -5,10 +8,13 @@
#include "Debug.h" #include "Debug.h"
#include "PluginManager.h" #include "PluginManager.h"
#include "VTableInterpose.h"
#include "df/enabler.h" #include "df/enabler.h"
#include "df/viewscreen_adopt_regionst.h"
#include <SDL_surface.h> #include "df/viewscreen_loadgamest.h"
#include "df/viewscreen_new_regionst.h"
#include "df/viewscreen_new_arenast.h"
using df::global::enabler; using df::global::enabler;
using namespace DFHack; using namespace DFHack;
@ -19,6 +25,7 @@ namespace DFHack {
} }
static bool g_loaded = false; static bool g_loaded = false;
static bool g_dynamic_loaded = false;
static long g_num_dfhack_textures = 0; static long g_num_dfhack_textures = 0;
static long g_dfhack_logo_texpos_start = -1; static long g_dfhack_logo_texpos_start = -1;
static long g_green_pin_texpos_start = -1; static long g_green_pin_texpos_start = -1;
@ -34,6 +41,10 @@ static long g_bold_borders_texpos_start = -1;
static long g_panel_borders_texpos_start = -1; static long g_panel_borders_texpos_start = -1;
static long g_window_borders_texpos_start = -1; static long g_window_borders_texpos_start = -1;
static std::unordered_map<TexposHandle, long> g_handle_to_texpos;
static std::unordered_map<TexposHandle, SDL_Surface*> g_handle_to_surface;
static std::mutex g_adding_mutex;
// 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.
@ -41,7 +52,7 @@ static long g_window_borders_texpos_start = -1;
// //
// 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; SDL_PixelFormat fmt;
fmt.palette = NULL; fmt.palette = NULL;
fmt.BitsPerPixel = 32; fmt.BitsPerPixel = 32;
@ -57,7 +68,7 @@ SDL_Surface * canonicalize_format(SDL_Surface *src) {
fmt.Bmask = 255 << fmt.Bshift; fmt.Bmask = 255 << fmt.Bshift;
fmt.Amask = 255 << fmt.Ashift; fmt.Amask = 255 << fmt.Ashift;
SDL_Surface *tgt = DFSDL_ConvertSurface(src, &fmt, SDL_SWSURFACE); 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) {
@ -72,13 +83,148 @@ SDL_Surface * canonicalize_format(SDL_Surface *src) {
return tgt; return tgt;
} }
// 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;
}
TexposHandle Textures::loadTexture(SDL_Surface* surface) {
if (!surface)
return 0; // should be some error, i guess
auto handle = reinterpret_cast<uintptr_t>(surface); // not the best way?
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::optional<long> Textures::getTexposByHandle(TexposHandle handle) {
if (!handle)
return std::nullopt;
if (auto it = g_handle_to_texpos.find(handle); it != g_handle_to_texpos.end())
return it->second;
if (auto it = g_handle_to_surface.find(handle); it != g_handle_to_surface.end()) {
it->second->refcount++; // prevent destruct on next FreeSurface by game
auto texpos = add_texture(it->second);
g_handle_to_texpos.emplace(handle, texpos);
return texpos;
}
return std::nullopt;
}
static void reset_texpos() {
g_handle_to_texpos.clear();
}
static void reset_surface() {
for (auto& entry : g_handle_to_surface) {
DFSDL_FreeSurface(entry.second);
}
g_handle_to_surface.clear();
}
// 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) {
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) {
this->m_cur_step = this->cur_step;
if (this->m_cur_step == df::viewscreen_adopt_regionst::T_cur_step::ProcessingRawData)
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) {
this->m_cur_step = this->cur_step;
if (this->m_cur_step == df::viewscreen_loadgamest::T_cur_step::ProcessingRawData)
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) {
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);
static void
install_reset_point() {
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();
}
const uint32_t TILE_WIDTH_PX = 8; const uint32_t TILE_WIDTH_PX = 8;
const uint32_t TILE_HEIGHT_PX = 12; const uint32_t TILE_HEIGHT_PX = 12;
static size_t load_textures(color_ostream & out, const char * fname, static size_t load_tiles_from_image(color_ostream& out, const char* fname,
long *texpos_start, long* texpos_start,
int tile_w = TILE_WIDTH_PX, int tile_w = TILE_WIDTH_PX,
int tile_h = TILE_HEIGHT_PX) { int tile_h = TILE_HEIGHT_PX) {
SDL_Surface *s = DFIMG_Load(fname); SDL_Surface *s = DFIMG_Load(fname);
if (!s) { if (!s) {
out.printerr("unable to load textures from '%s'\n", fname); out.printerr("unable to load textures from '%s'\n", fname);
@ -91,7 +237,7 @@ static size_t load_textures(color_ostream & out, const char * fname,
long count = 0; long count = 0;
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(0, // SDL_SWSURFACE SDL_Surface* tile = DFSDL_CreateRGBSurface(0, // SDL_SWSURFACE
tile_w, tile_h, 32, tile_w, tile_h, 32,
s->format->Rmask, s->format->Gmask, s->format->Bmask, s->format->Rmask, s->format->Gmask, s->format->Bmask,
s->format->Amask); s->format->Amask);
@ -120,7 +266,12 @@ static size_t load_textures(color_ostream & out, const char * fname,
// texture count so our textures will no longer be freed when worlds are // texture count so our textures will no longer be freed when worlds are
// unloaded. // unloaded.
// //
void Textures::init(color_ostream &out) { void Textures::init(color_ostream& out) {
if (!g_dynamic_loaded) {
g_dynamic_loaded = true;
install_reset_point();
}
if (!enabler) if (!enabler)
return; return;
@ -134,32 +285,32 @@ void Textures::init(color_ostream &out) {
bool is_pre_world = num_textures == textures.init_texture_size; bool is_pre_world = num_textures == textures.init_texture_size;
g_num_dfhack_textures = load_textures(out, "hack/data/art/dfhack.png", g_num_dfhack_textures = load_tiles_from_image(out, "hack/data/art/dfhack.png",
&g_dfhack_logo_texpos_start); &g_dfhack_logo_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/green-pin.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/green-pin.png",
&g_green_pin_texpos_start); &g_green_pin_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/red-pin.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/red-pin.png",
&g_red_pin_texpos_start); &g_red_pin_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/icons.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/icons.png",
&g_icons_texpos_start); &g_icons_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/on-off.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/on-off.png",
&g_on_off_texpos_start); &g_on_off_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/pathable.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/pathable.png",
&g_pathable_texpos_start, 32, 32); &g_pathable_texpos_start, 32, 32);
g_num_dfhack_textures += load_textures(out, "hack/data/art/unsuspend.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/unsuspend.png",
&g_unsuspend_texpos_start, 32, 32); &g_unsuspend_texpos_start, 32, 32);
g_num_dfhack_textures += load_textures(out, "hack/data/art/control-panel.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/control-panel.png",
&g_control_panel_texpos_start); &g_control_panel_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/border-thin.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/border-thin.png",
&g_thin_borders_texpos_start); &g_thin_borders_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/border-medium.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/border-medium.png",
&g_medium_borders_texpos_start); &g_medium_borders_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/border-bold.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/border-bold.png",
&g_bold_borders_texpos_start); &g_bold_borders_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/border-panel.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/border-panel.png",
&g_panel_borders_texpos_start); &g_panel_borders_texpos_start);
g_num_dfhack_textures += load_textures(out, "hack/data/art/border-window.png", g_num_dfhack_textures += load_tiles_from_image(out, "hack/data/art/border-window.png",
&g_window_borders_texpos_start); &g_window_borders_texpos_start);
DEBUG(textures,out).print("loaded %ld textures\n", g_num_dfhack_textures); DEBUG(textures,out).print("loaded %ld textures\n", g_num_dfhack_textures);
@ -176,8 +327,8 @@ void Textures::cleanup() {
if (!g_loaded) if (!g_loaded)
return; return;
auto & textures = enabler->textures; auto& textures = enabler->textures;
auto &raws = textures.raws; auto& raws = textures.raws;
size_t texpos_end = g_dfhack_logo_texpos_start + g_num_dfhack_textures - 1; size_t texpos_end = g_dfhack_logo_texpos_start + g_num_dfhack_textures - 1;
for (size_t idx = g_dfhack_logo_texpos_start; idx <= texpos_end; ++idx) { for (size_t idx = g_dfhack_logo_texpos_start; idx <= texpos_end; ++idx) {
DFSDL_FreeSurface((SDL_Surface *)raws[idx]); DFSDL_FreeSurface((SDL_Surface *)raws[idx]);
@ -190,6 +341,13 @@ void Textures::cleanup() {
g_loaded = false; g_loaded = false;
g_num_dfhack_textures = 0; g_num_dfhack_textures = 0;
g_dfhack_logo_texpos_start = -1; g_dfhack_logo_texpos_start = -1;
if (g_dynamic_loaded) {
g_dynamic_loaded = false;
reset_texpos();
reset_surface();
uninstall_reset_point();
}
} }
long Textures::getDfhackLogoTexposStart() { long Textures::getDfhackLogoTexposStart() {
@ -242,4 +400,4 @@ long Textures::getPanelBordersTexposStart() {
long Textures::getWindowBordersTexposStart() { long Textures::getWindowBordersTexposStart() {
return g_window_borders_texpos_start; return g_window_borders_texpos_start;
} }