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
#include <optional>
#include "Export.h"
#include "ColorText.h"
#include <SDL_surface.h>
using TexposHandle = uintptr_t;
namespace DFHack {
/**
@ -12,6 +18,19 @@ namespace DFHack {
*/
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
* and reindex textures as needed.

@ -1,3 +1,6 @@
#include <mutex>
#include <unordered_map>
#include "Internal.h"
#include "modules/DFSDL.h"
@ -5,10 +8,13 @@
#include "Debug.h"
#include "PluginManager.h"
#include "VTableInterpose.h"
#include "df/enabler.h"
#include <SDL_surface.h>
#include "df/viewscreen_adopt_regionst.h"
#include "df/viewscreen_loadgamest.h"
#include "df/viewscreen_new_regionst.h"
#include "df/viewscreen_new_arenast.h"
using df::global::enabler;
using namespace DFHack;
@ -19,6 +25,7 @@ namespace DFHack {
}
static bool g_loaded = false;
static bool g_dynamic_loaded = false;
static long g_num_dfhack_textures = 0;
static long g_dfhack_logo_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_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
// (32-bit RGBA), and converts magenta to transparency if convert_magenta is set
// and the source surface didn't already have an alpha channel.
@ -72,10 +83,145 @@ SDL_Surface * canonicalize_format(SDL_Surface *src) {
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_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,
int tile_w = TILE_WIDTH_PX,
int tile_h = TILE_HEIGHT_PX) {
@ -121,6 +267,11 @@ static size_t load_textures(color_ostream & out, const char * fname,
// unloaded.
//
void Textures::init(color_ostream& out) {
if (!g_dynamic_loaded) {
g_dynamic_loaded = true;
install_reset_point();
}
if (!enabler)
return;
@ -134,31 +285,31 @@ void Textures::init(color_ostream &out) {
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_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_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_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_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_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_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_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_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_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_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_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_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);
DEBUG(textures,out).print("loaded %ld textures\n", g_num_dfhack_textures);
@ -190,6 +341,13 @@ void Textures::cleanup() {
g_loaded = false;
g_num_dfhack_textures = 0;
g_dfhack_logo_texpos_start = -1;
if (g_dynamic_loaded) {
g_dynamic_loaded = false;
reset_texpos();
reset_surface();
uninstall_reset_point();
}
}
long Textures::getDfhackLogoTexposStart() {