move SDL shims to a new DFSDL module
parent
8e62a46009
commit
4e51e02924
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue