Merge pull request #2548 from myk002/myk_logo

Display DFHack logo for the hotkeys overlay widget
develop
Myk 2023-01-04 19:06:05 -08:00 committed by GitHub
commit 78be3a6463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 464 additions and 41 deletions

@ -10,6 +10,9 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/quickfort/
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/orders/
DESTINATION dfhack-config/orders/library)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/art/
DESTINATION "${DFHACK_DATA_DESTINATION}/data/art")
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/examples/
DESTINATION "${DFHACK_DATA_DESTINATION}/examples")

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@ -40,6 +40,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Misc Improvements
- Scrollable widgets now react to mouse wheel events when the mouse is over the widget
- the ``dfhack-config/scripts/`` folder is now searched for scripts by default
- `hotkeys`: overlay hotspot widget now shows the DFHack logo in graphics mode and "DFHack" in text mode
## Documentation
- `overlay-dev-guide`: added troubleshooting tips and common development workflows

@ -124,6 +124,7 @@ set(MODULE_HEADERS
include/modules/Buildings.h
include/modules/Burrows.h
include/modules/Constructions.h
include/modules/DFSDL.h
include/modules/Designations.h
include/modules/EventManager.h
include/modules/Filesystem.h
@ -141,6 +142,7 @@ set(MODULE_HEADERS
include/modules/Random.h
include/modules/Renderer.h
include/modules/Screen.h
include/modules/Textures.h
include/modules/Translation.h
include/modules/Units.h
include/modules/World.h
@ -150,6 +152,7 @@ set(MODULE_SOURCES
modules/Buildings.cpp
modules/Burrows.cpp
modules/Constructions.cpp
modules/DFSDL.cpp
modules/Designations.cpp
modules/EventManager.cpp
modules/Filesystem.cpp
@ -166,6 +169,7 @@ set(MODULE_SOURCES
modules/Random.cpp
modules/Renderer.cpp
modules/Screen.cpp
modules/Textures.cpp
modules/Translation.cpp
modules/Units.cpp
modules/World.cpp

@ -49,9 +49,11 @@ using namespace std;
#include "VersionInfo.h"
#include "PluginManager.h"
#include "ModuleFactory.h"
#include "modules/DFSDL.h"
#include "modules/EventManager.h"
#include "modules/Filesystem.h"
#include "modules/Gui.h"
#include "modules/Textures.h"
#include "modules/World.h"
#include "modules/Persistence.h"
#include "RemoteServer.h"
@ -1670,8 +1672,15 @@ bool Core::Init()
return false;
}
cerr << "Binding to SDL.\n";
if (!DFSDL::init(con)) {
fatal("cannot bind SDL libraries");
return false;
}
cerr << "Initializing textures.\n";
Textures::init(con);
// create mutex for syncing with interactive tasks
cerr << "Initializing Plugins.\n";
cerr << "Initializing plugins.\n";
// create plugin manager
plug_mgr = new PluginManager(this);
plug_mgr->init();
@ -1765,12 +1774,7 @@ bool Core::Init()
cerr << "DFHack is running.\n";
{
auto L = Lua::Core::State;
Lua::StackUnwinder top(L);
Lua::CallLuaModuleFunction(con, L, "script-manager", "reload");
onStateChange(con, SC_CORE_INITIALIZED);
}
return true;
}
@ -2138,6 +2142,13 @@ void Core::onStateChange(color_ostream &out, state_change_event event)
switch (event)
{
case SC_CORE_INITIALIZED:
{
auto L = Lua::Core::State;
Lua::StackUnwinder top(L);
Lua::CallLuaModuleFunction(con, L, "script-manager", "reload");
}
break;
case SC_WORLD_LOADED:
case SC_WORLD_UNLOADED:
case SC_MAP_LOADED:
@ -2171,6 +2182,10 @@ void Core::onStateChange(color_ostream &out, state_change_event event)
evtlog << std::endl;
}
}
break;
case SC_VIEWSCREEN_CHANGED:
Textures::init(out);
break;
default:
break;
}
@ -2248,6 +2263,8 @@ int Core::Shutdown ( void )
}
// invalidate all modules
allModules.clear();
Textures::cleanup();
DFSDL::cleanup();
memset(&(s_mods), 0, sizeof(s_mods));
d.reset();
return -1;

@ -57,6 +57,7 @@ distribution.
#include "modules/Materials.h"
#include "modules/Random.h"
#include "modules/Screen.h"
#include "modules/Textures.h"
#include "modules/Translation.h"
#include "modules/Units.h"
#include "modules/World.h"
@ -1670,6 +1671,13 @@ static const luaL_Reg dfhack_job_funcs[] = {
{ NULL, NULL }
};
/***** Textures module *****/
static const LuaWrapper::FunctionReg dfhack_textures_module[] = {
WRAPM(Textures, getDfhackLogoTexposStart),
{ NULL, NULL }
};
/***** Units module *****/
static const LuaWrapper::FunctionReg dfhack_units_module[] = {
@ -3357,6 +3365,7 @@ void OpenDFHackApi(lua_State *state)
luaL_setfuncs(state, dfhack_funcs, 0);
OpenModule(state, "gui", dfhack_gui_module, dfhack_gui_funcs);
OpenModule(state, "job", dfhack_job_module, dfhack_job_funcs);
OpenModule(state, "textures", dfhack_textures_module);
OpenModule(state, "units", dfhack_units_module, dfhack_units_funcs);
OpenModule(state, "items", dfhack_items_module, dfhack_items_funcs);
OpenModule(state, "maps", dfhack_maps_module, dfhack_maps_funcs);

@ -0,0 +1,92 @@
#pragma once
#include "Export.h"
#include "ColorText.h"
namespace DFHack
{
// SDL stand-in type definitions
typedef signed short SINT16;
typedef void DFSDL_sem;
typedef struct
{
int16_t x, y;
uint16_t w, h;
} DFSDL_Rect;
typedef struct
{
void *palette; // SDL_Palette*
uint8_t BitsPerPixel;
uint8_t BytesPerPixel;
uint8_t Rloss;
uint8_t Gloss;
uint8_t Bloss;
uint8_t Aloss;
uint8_t Rshift;
uint8_t Gshift;
uint8_t Bshift;
uint8_t Ashift;
uint32_t Rmask;
uint32_t Gmask;
uint32_t Bmask;
uint32_t Amask;
uint32_t colorkey;
uint8_t alpha;
} DFSDL_PixelFormat;
typedef struct
{
uint32_t flags;
DFSDL_PixelFormat* format;
int w, h;
int pitch;
void* pixels;
void* userdata; // as far as i could see DF doesnt use this
int locked;
void* lock_data;
DFSDL_Rect clip_rect;
void* map;
int refcount;
} DFSDL_Surface;
// =========
struct DFTileSurface
{
bool paintOver; // draw over original tile?
DFSDL_Surface* surface; // from where it should be drawn
DFSDL_Rect* rect; // from which coords (NULL to draw whole surface)
DFSDL_Rect* dstResize; // if not NULL dst rect will be resized (x/y/w/h will be added to original dst)
};
/**
* The DFSDL module - provides access to SDL functions without actually
* requiring build-time linkage to SDL
* \ingroup grp_modules
* \ingroup grp_dfsdl
*/
namespace DFSDL
{
/**
* Call this on DFHack init so we can load the SDL functions. Returns false on
* failure.
*/
bool init(DFHack::color_ostream &out);
/**
* Call this when DFHack is being unloaded.
*/
void cleanup();
DFHACK_EXPORT DFSDL_Surface * DFIMG_Load(const char *file);
DFHACK_EXPORT int DFSDL_SetAlpha(DFSDL_Surface *surface, uint32_t flag, uint8_t alpha);
DFHACK_EXPORT DFSDL_Surface * DFSDL_CreateRGBSurface(uint32_t flags, int width, int height, int depth, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask);
DFHACK_EXPORT int DFSDL_UpperBlit(DFSDL_Surface *src, const DFSDL_Rect *srcrect, DFSDL_Surface *dst, DFSDL_Rect *dstrect);
DFHACK_EXPORT DFSDL_Surface * DFSDL_ConvertSurface(DFSDL_Surface *src, const DFSDL_PixelFormat *fmt, uint32_t flags);
DFHACK_EXPORT void DFSDL_FreeSurface(DFSDL_Surface *surface);
DFHACK_EXPORT int DFSDL_SemWait(DFSDL_sem *sem);
DFHACK_EXPORT int DFSDL_SemPost(DFSDL_sem *sem);
}
}

@ -33,42 +33,11 @@ distribution.
#include <stdint.h>
#include "Export.h"
#include "Module.h"
#include "DFSDL.h"
#include <vector>
namespace DFHack
{
// SDL stuff
typedef signed short SINT16;
typedef struct
{
int16_t x, y;
uint16_t w, h;
} DFSDL_Rect;
typedef struct
{
uint32_t flags;
void* format; // PixelFormat*
int w, h;
int pitch;
void* pixels;
void* userdata; // as far as i could see DF doesnt use this
int locked;
void* lock_data;
DFSDL_Rect clip_rect;
void* map;
int refcount;
} DFSDL_Surface;
// =========
struct DFTileSurface
{
bool paintOver; // draw over original tile?
DFSDL_Surface* surface; // from where it should be drawn
DFSDL_Rect* rect; // from which coords (NULL to draw whole surface)
DFSDL_Rect* dstResize; // if not NULL dst rect will be resized (x/y/w/h will be added to original dst)
};
class DFHACK_EXPORT Graphic : public Module
{
public:
@ -83,7 +52,6 @@ namespace DFHack
private:
std::vector<DFTileSurface* (*)(int, int)> funcs;
};
}
#endif

@ -0,0 +1,34 @@
#pragma once
#include "Export.h"
#include "ColorText.h"
namespace DFHack {
/**
* The Textures module - loads and provides access to DFHack textures
* \ingroup grp_modules
* \ingroup grp_textures
*/
namespace Textures {
/**
* Call this on DFHack init and on every viewscreen change so we can reload
* and reindex textures as needed.
*/
void init(DFHack::color_ostream &out);
/**
* Call this when DFHack is being unloaded.
*
*/
void cleanup();
/**
* Get first texpos for the DFHack logo. This texpos and the next 11 make up the
* 4x3 grid of logo textures that can be displayed on the UI layer.
*/
DFHACK_EXPORT long getDfhackLogoTexposStart();
}
}

@ -0,0 +1,120 @@
#include "Internal.h"
#include "modules/DFSDL.h"
#include "Debug.h"
#include "PluginManager.h"
namespace DFHack {
DBG_DECLARE(core, dfsdl, DebugCategory::LINFO);
}
using namespace DFHack;
static DFLibrary *g_sdl_handle = nullptr;
static DFLibrary *g_sdl_image_handle = nullptr;
static const std::vector<std::string> SDL_LIBS {
"SDLreal.dll", // TODO: change to SDL.dll once we move to dfhooks
"SDL.framework/Versions/A/SDL",
"SDL.framework/SDL",
"libSDL-1.2.so.0"
};
static const std::vector<std::string> SDL_IMAGE_LIBS {
"SDL_image.dll",
"SDL_image.framework/Versions/A/SDL_image",
"SDL_image.framework/SDL_image",
"libSDL_image-1.2.so.0"
};
DFSDL_Surface * (*g_IMG_Load)(const char *) = nullptr;
int (*g_SDL_SetAlpha)(DFSDL_Surface *, uint32_t, uint8_t) = nullptr;
DFSDL_Surface * (*g_SDL_CreateRGBSurface)(uint32_t, int, int, int, uint32_t, uint32_t, uint32_t, uint32_t);
int (*g_SDL_UpperBlit)(DFSDL_Surface *, const DFSDL_Rect *, DFSDL_Surface *, DFSDL_Rect *);
DFSDL_Surface * (*g_SDL_ConvertSurface)(DFSDL_Surface *, const DFSDL_PixelFormat *, uint32_t);
void (*g_SDL_FreeSurface)(DFSDL_Surface *);
int (*g_SDL_SemWait)(DFSDL_sem *);
int (*g_SDL_SemPost)(DFSDL_sem *);
bool DFSDL::init(color_ostream &out) {
for (auto &lib_str : SDL_LIBS) {
if ((g_sdl_handle = OpenPlugin(lib_str.c_str())))
break;
}
if (!g_sdl_handle) {
out.printerr("DFHack could not find SDL\n");
return false;
}
for (auto &lib_str : SDL_IMAGE_LIBS) {
if ((g_sdl_image_handle = OpenPlugin(lib_str.c_str())))
break;
}
if (!g_sdl_image_handle) {
out.printerr("DFHack could not find SDL_image\n");
return false;
}
#define bind(handle, name) \
g_##name = (decltype(g_##name))LookupPlugin(handle, #name); \
if (!g_##name) { \
out.printerr("DFHack could not find: " #name "\n"); \
return false; \
}
bind(g_sdl_image_handle, IMG_Load);
bind(g_sdl_handle, SDL_SetAlpha);
bind(g_sdl_handle, SDL_CreateRGBSurface);
bind(g_sdl_handle, SDL_UpperBlit);
bind(g_sdl_handle, SDL_ConvertSurface);
bind(g_sdl_handle, SDL_FreeSurface);
bind(g_sdl_handle, SDL_SemWait);
bind(g_sdl_handle, SDL_SemPost);
#undef bind
DEBUG(dfsdl,out).print("sdl successfully loaded\n");
return true;
}
// It's ok to leave NULLs in the raws list (according to usage in g_src)
void DFSDL::cleanup() {
if (g_sdl_handle) {
ClosePlugin(g_sdl_handle);
g_sdl_handle = nullptr;
}
if (g_sdl_image_handle) {
ClosePlugin(g_sdl_image_handle);
g_sdl_image_handle = nullptr;
}
}
DFSDL_Surface * DFSDL::DFIMG_Load(const char *file) {
return g_IMG_Load(file);
}
int DFSDL::DFSDL_SetAlpha(DFSDL_Surface *surface, uint32_t flag, uint8_t alpha) {
return g_SDL_SetAlpha(surface, flag, alpha);
}
DFSDL_Surface * DFSDL::DFSDL_CreateRGBSurface(uint32_t flags, int width, int height, int depth, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask) {
return g_SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask);
}
int DFSDL::DFSDL_UpperBlit(DFSDL_Surface *src, const DFSDL_Rect *srcrect, DFSDL_Surface *dst, DFSDL_Rect *dstrect) {
return g_SDL_UpperBlit(src, srcrect, dst, dstrect);
}
DFSDL_Surface * DFSDL::DFSDL_ConvertSurface(DFSDL_Surface *src, const DFSDL_PixelFormat *fmt, uint32_t flags) {
return g_SDL_ConvertSurface(src, fmt, flags);
}
void DFSDL::DFSDL_FreeSurface(DFSDL_Surface *surface) {
g_SDL_FreeSurface(surface);
}
int DFSDL::DFSDL_SemWait(DFSDL_sem *sem) {
return g_SDL_SemWait(sem);
}
int DFSDL::DFSDL_SemPost(DFSDL_sem *sem) {
return g_SDL_SemPost(sem);
}

@ -0,0 +1,148 @@
#include "Internal.h"
#include "modules/DFSDL.h"
#include "modules/Textures.h"
#include "Debug.h"
#include "PluginManager.h"
#include "df/enabler.h"
using df::global::enabler;
using namespace DFHack;
using namespace DFHack::DFSDL;
namespace DFHack {
DBG_DECLARE(core, textures, DebugCategory::LINFO);
}
static bool g_loaded = false;
static long g_num_dfhack_textures = 0;
static long g_dfhack_logo_texpos_start = -1;
// 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.
DFSDL_Surface * canonicalize_format(DFSDL_Surface *src) {
DFSDL_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;
fmt.colorkey = 0;
fmt.alpha = 255;
DFSDL_Surface *tgt = DFSDL_ConvertSurface(src, &fmt, 0); // SDL_SWSURFACE
DFSDL_FreeSurface(src);
return tgt;
}
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,
long *texpos_start) {
DFSDL_Surface *s = DFIMG_Load(fname);
if (!s) {
out.printerr("unable to load textures from '%s'\n", fname);
return 0;
}
s = canonicalize_format(s);
DFSDL_SetAlpha(s, 0, 255);
int dimx = s->w / TILE_WIDTH_PX;
int dimy = s->h / TILE_HEIGHT_PX;
long count = 0;
for (int y = 0; y < dimy; y++) {
for (int x = 0; x < dimx; x++) {
DFSDL_Surface *tile = DFSDL_CreateRGBSurface(0, // SDL_SWSURFACE
TILE_WIDTH_PX, TILE_HEIGHT_PX, 32,
s->format->Rmask, s->format->Gmask, s->format->Bmask,
s->format->Amask);
DFSDL_SetAlpha(tile, 0,255);
DFSDL_Rect vp;
vp.x = TILE_WIDTH_PX * x;
vp.y = TILE_HEIGHT_PX * y;
vp.w = TILE_WIDTH_PX;
vp.h = TILE_HEIGHT_PX;
DFSDL_UpperBlit(s, &vp, tile, NULL);
if (!count++)
*texpos_start = enabler->textures.raws.size();
enabler->textures.raws.push_back(tile);
}
}
DFSDL_FreeSurface(s);
DEBUG(textures,out).print("loaded %zd textures from '%s'\n", count, fname);
return count;
}
// DFHack could conceivably be loaded at any time, so we need to be able to
// handle loading textures before or after a world is loaded.
// If a world is already loaded, then append our textures to the raws. they'll
// be freed when the world is unloaded and we'll reload when we get to the title
// screen. If it's pre-world, append our textures and then adjust the "init"
// texture count so our textures will no longer be freed when worlds are
// unloaded.
//
void Textures::init(color_ostream &out) {
auto & textures = enabler->textures;
long num_textures = textures.raws.size();
if (num_textures <= g_dfhack_logo_texpos_start)
g_loaded = false;
if (g_loaded)
return;
bool is_pre_world = num_textures == textures.init_texture_size;
g_num_dfhack_textures = load_textures(out, "hack/data/art/dfhack.png",
&g_dfhack_logo_texpos_start);
DEBUG(textures,out).print("loaded %zd textures\n", g_num_dfhack_textures);
if (is_pre_world)
textures.init_texture_size += g_num_dfhack_textures;
// NOTE: when GL modes are supported, we'll have to re-upload textures here
g_loaded = true;
}
// It's ok to leave NULLs in the raws list (according to usage in g_src)
void Textures::cleanup() {
if (!g_loaded)
return;
auto & textures = enabler->textures;
auto &raws = textures.raws;
size_t texpos_end = g_dfhack_logo_texpos_start + g_num_dfhack_textures;
for (size_t idx = g_dfhack_logo_texpos_start; idx <= texpos_end; ++idx) {
DFSDL_FreeSurface((DFSDL_Surface *)raws[idx]);
raws[idx] = NULL;
}
if (g_dfhack_logo_texpos_start == textures.init_texture_size - g_num_dfhack_textures)
textures.init_texture_size -= g_num_dfhack_textures;
g_loaded = false;
g_num_dfhack_textures = 0;
g_dfhack_logo_texpos_start = -1;
}
long Textures::getDfhackLogoTexposStart() {
return g_dfhack_logo_texpos_start;
}

@ -19,7 +19,6 @@ HotspotMenuWidget.ATTRS{
}
function HotspotMenuWidget:init()
self:addviews{widgets.Label{text={'!DF!', NEWLINE, '!Ha!', NEWLINE, '!ck!'}}}
self.mouseover = false
end
@ -41,6 +40,34 @@ function HotspotMenuWidget:overlay_trigger()
mouseover=self.mouseover}:show()
end
local dscreen = dfhack.screen
function HotspotMenuWidget:onRenderBody(dc)
local tpos = dfhack.textures.getDfhackLogoTexposStart()
local x, y = dc.x, dc.y
if tpos == -1 then
dscreen.paintString(COLOR_WHITE, x, y+0, '!DF!')
dscreen.paintString(COLOR_WHITE, x, y+1, '!Ha!')
dscreen.paintString(COLOR_WHITE, x, y+2, '!ck!')
else
dscreen.paintTile(COLOR_WHITE, x+0, y+0, '!', tpos+0)
dscreen.paintTile(COLOR_WHITE, x+1, y+0, 'D', tpos+1)
dscreen.paintTile(COLOR_WHITE, x+2, y+0, 'F', tpos+2)
dscreen.paintTile(COLOR_WHITE, x+3, y+0, '!', tpos+3)
dscreen.paintTile(COLOR_WHITE, x+0, y+1, '!', tpos+4)
dscreen.paintTile(COLOR_WHITE, x+1, y+1, 'H', tpos+5)
dscreen.paintTile(COLOR_WHITE, x+2, y+1, 'a', tpos+6)
dscreen.paintTile(COLOR_WHITE, x+3, y+1, '!', tpos+7)
dscreen.paintTile(COLOR_WHITE, x+0, y+2, '!', tpos+8)
dscreen.paintTile(COLOR_WHITE, x+1, y+2, 'c', tpos+9)
dscreen.paintTile(COLOR_WHITE, x+2, y+2, 'k', tpos+10)
dscreen.paintTile(COLOR_WHITE, x+3, y+2, '!', tpos+11)
end
end
-- register the menu hotspot with the overlay
OVERLAY_WIDGETS = {menu=HotspotMenuWidget}