2023-01-04 18:15:32 -07:00
|
|
|
#include "Internal.h"
|
|
|
|
|
|
|
|
#include "modules/DFSDL.h"
|
|
|
|
|
|
|
|
#include "Debug.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
|
2023-07-05 23:34:25 -06:00
|
|
|
#include <SDL_stdinc.h>
|
2023-07-02 19:04:06 -06:00
|
|
|
|
2023-01-04 18:15:32 -07:00
|
|
|
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 {
|
2023-05-22 16:07:19 -06:00
|
|
|
"SDL2.dll",
|
2023-01-04 18:15:32 -07:00
|
|
|
"SDL.framework/Versions/A/SDL",
|
|
|
|
"SDL.framework/SDL",
|
2023-05-22 16:07:19 -06:00
|
|
|
"libSDL2-2.0.so.0"
|
2023-01-04 18:15:32 -07:00
|
|
|
};
|
|
|
|
static const std::vector<std::string> SDL_IMAGE_LIBS {
|
2023-05-22 16:07:19 -06:00
|
|
|
"SDL2_image.dll",
|
2023-01-04 18:15:32 -07:00
|
|
|
"SDL_image.framework/Versions/A/SDL_image",
|
|
|
|
"SDL_image.framework/SDL_image",
|
2023-05-22 16:07:19 -06:00
|
|
|
"libSDL2_image-2.0.so.0"
|
2023-01-04 18:15:32 -07:00
|
|
|
};
|
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
SDL_Surface * (*g_IMG_Load)(const char *) = nullptr;
|
|
|
|
SDL_Surface * (*g_SDL_CreateRGBSurface)(uint32_t, int, int, int, uint32_t, uint32_t, uint32_t, uint32_t) = nullptr;
|
|
|
|
SDL_Surface * (*g_SDL_CreateRGBSurfaceFrom)(void *pixels, int width, int height, int depth, int pitch, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask) = nullptr;
|
|
|
|
int (*g_SDL_UpperBlit)(SDL_Surface *, const SDL_Rect *, SDL_Surface *, SDL_Rect *) = nullptr;
|
|
|
|
SDL_Surface * (*g_SDL_ConvertSurface)(SDL_Surface *, const SDL_PixelFormat *, uint32_t) = nullptr;
|
|
|
|
void (*g_SDL_FreeSurface)(SDL_Surface *) = nullptr;
|
|
|
|
// int (*g_SDL_SemWait)(DFSDL_sem *) = nullptr;
|
|
|
|
// int (*g_SDL_SemPost)(DFSDL_sem *) = nullptr;
|
|
|
|
int (*g_SDL_PushEvent)(SDL_Event *) = nullptr;
|
2023-07-02 19:04:06 -06:00
|
|
|
SDL_bool (*g_SDL_HasClipboardText)();
|
|
|
|
int (*g_SDL_SetClipboardText)(const char *text);
|
|
|
|
char * (*g_SDL_GetClipboardText)();
|
|
|
|
void (*g_SDL_free)(void *);
|
2023-01-04 18:15:32 -07:00
|
|
|
|
|
|
|
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_CreateRGBSurface);
|
2023-02-26 22:17:25 -07:00
|
|
|
bind(g_sdl_handle, SDL_CreateRGBSurfaceFrom);
|
2023-01-04 18:15:32 -07:00
|
|
|
bind(g_sdl_handle, SDL_UpperBlit);
|
|
|
|
bind(g_sdl_handle, SDL_ConvertSurface);
|
|
|
|
bind(g_sdl_handle, SDL_FreeSurface);
|
2023-05-22 16:07:19 -06:00
|
|
|
// bind(g_sdl_handle, SDL_SemWait);
|
|
|
|
// bind(g_sdl_handle, SDL_SemPost);
|
2023-02-26 16:37:54 -07:00
|
|
|
bind(g_sdl_handle, SDL_PushEvent);
|
2023-07-02 19:04:06 -06:00
|
|
|
bind(g_sdl_handle, SDL_HasClipboardText);
|
|
|
|
bind(g_sdl_handle, SDL_SetClipboardText);
|
|
|
|
bind(g_sdl_handle, SDL_GetClipboardText);
|
|
|
|
bind(g_sdl_handle, SDL_free);
|
2023-01-04 18:15:32 -07:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
SDL_Surface * DFSDL::DFIMG_Load(const char *file) {
|
2023-01-04 18:15:32 -07:00
|
|
|
return g_IMG_Load(file);
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
SDL_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) {
|
2023-01-04 18:15:32 -07:00
|
|
|
return g_SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask);
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
SDL_Surface * DFSDL::DFSDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask) {
|
2023-02-26 22:17:25 -07:00
|
|
|
return g_SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask);
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
int DFSDL::DFSDL_UpperBlit(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect) {
|
2023-01-04 18:15:32 -07:00
|
|
|
return g_SDL_UpperBlit(src, srcrect, dst, dstrect);
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
SDL_Surface * DFSDL::DFSDL_ConvertSurface(SDL_Surface *src, const SDL_PixelFormat *fmt, uint32_t flags) {
|
2023-01-04 18:15:32 -07:00
|
|
|
return g_SDL_ConvertSurface(src, fmt, flags);
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
void DFSDL::DFSDL_FreeSurface(SDL_Surface *surface) {
|
2023-01-04 18:15:32 -07:00
|
|
|
g_SDL_FreeSurface(surface);
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
// int DFSDL::DFSDL_SemWait(DFSDL_sem *sem) {
|
|
|
|
// return g_SDL_SemWait(sem);
|
|
|
|
// }
|
2023-01-04 18:15:32 -07:00
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
// int DFSDL::DFSDL_SemPost(DFSDL_sem *sem) {
|
|
|
|
// return g_SDL_SemPost(sem);
|
|
|
|
// }
|
2023-02-26 16:37:54 -07:00
|
|
|
|
2023-05-22 16:07:19 -06:00
|
|
|
int DFSDL::DFSDL_PushEvent(SDL_Event *event) {
|
2023-02-26 16:37:54 -07:00
|
|
|
return g_SDL_PushEvent(event);
|
|
|
|
}
|
2023-07-02 19:04:06 -06:00
|
|
|
|
2023-07-05 13:08:18 -06:00
|
|
|
void DFSDL::DFSDL_free(void *ptr) {
|
|
|
|
g_SDL_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
char * DFSDL::DFSDL_GetClipboardText() {
|
|
|
|
return g_SDL_GetClipboardText();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DFSDL::DFSDL_SetClipboardText(const char *text) {
|
|
|
|
return g_SDL_SetClipboardText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
DFHACK_EXPORT std::string DFHack::getClipboardTextCp437() {
|
|
|
|
if (!g_sdl_handle || g_SDL_HasClipboardText() != SDL_TRUE)
|
2023-07-02 19:04:06 -06:00
|
|
|
return "";
|
|
|
|
char *text = g_SDL_GetClipboardText();
|
2023-07-21 16:03:06 -06:00
|
|
|
// convert tabs to spaces so they don't get converted to '?'
|
|
|
|
for (char *c = text; *c; ++c) {
|
|
|
|
if (*c == '\t')
|
|
|
|
*c = ' ';
|
|
|
|
}
|
2023-07-05 13:08:18 -06:00
|
|
|
std::string textcp437 = UTF2DF(text);
|
|
|
|
DFHack::DFSDL::DFSDL_free(text);
|
|
|
|
return textcp437;
|
2023-07-02 19:04:06 -06:00
|
|
|
}
|
|
|
|
|
2023-07-05 13:08:18 -06:00
|
|
|
DFHACK_EXPORT bool DFHack::setClipboardTextCp437(std::string text) {
|
|
|
|
if (!g_sdl_handle)
|
|
|
|
return false;
|
|
|
|
return 0 == DFHack::DFSDL::DFSDL_SetClipboardText(DF2UTF(text).c_str());
|
2023-07-02 19:04:06 -06:00
|
|
|
}
|