mkxp-sunshine/binding-mri/chroma-binding.cpp
2026-05-15 23:56:06 +04:00

96 lines
2.7 KiB
C++

#include "binding-util.h"
#include "binding-types.h"
#include "debugwriter.h"
#include "chromasdk/ChromaApi.h"
bool INIT_SUCCESS = false;
#ifdef _WIN32
HINSTANCE hLib = NULL;
#else
int* hLib = NULL;
#endif
//dynamic-loaded proc pointers
PluginPlayAnimation _playAnimation = NULL;
PluginOpenAnimation _openAnimation = NULL;
PluginIsInitialized _pluginIsInitialized = NULL;
PluginInit _pluginInit = NULL;
PluginUninit _pluginUninit = NULL;
int lastAnimId = -1;
void _attemptLinkSdk() {
Debug() << "[_attempLinkSdk] Attempting to bind Chroma SDK";
#ifdef _WIN32
hLib = LoadLibrary(L"ChromaApi.dll");
if (hLib != NULL) {
Debug() << "[_attempLinkSdk] Chroma Impl @" << hLib;
INIT_SUCCESS = true;
_playAnimation = (PluginPlayAnimation) GetProcAddress(hLib, "PluginPlayAnimation");
INIT_SUCCESS &= _playAnimation != NULL;
Debug() << "[_attempLinkSdk] Plugin method @" << reinterpret_cast<void*>(_playAnimation);
_openAnimation = (PluginOpenAnimation) GetProcAddress(hLib, "PluginOpenAnimation");
INIT_SUCCESS &= _openAnimation != NULL;
_pluginInit = (PluginInit) GetProcAddress(hLib, "PluginInit");
INIT_SUCCESS &= _pluginInit != NULL;
_pluginIsInitialized = (PluginIsInitialized) GetProcAddress(hLib, "PluginIsInitialized");
INIT_SUCCESS &= _pluginInit != NULL;
if (INIT_SUCCESS && !_pluginIsInitialized()) {
Debug() << "[_attempLinkSdk] Initializing chroma plugin";
_pluginInit();
}
_pluginUninit = (PluginUninit) GetProcAddress(hLib, "PluginUninit");
INIT_SUCCESS &= _pluginUninit != NULL;
Debug() << "[_attempLinkSdk] Plugin init success:" << INIT_SUCCESS;
}
#else
// Currently the Razor Chroma API is Windows-only.
// So, if we are not building for Windows platform,
// then don't bother with Chroma stuff.
Debug() << "[_attempLinkSdk] Chroma: Unsupported Platform";
#endif
}
RB_METHOD(chromaPlayAnimation) {
RB_UNUSED_PARAM;
Debug() << "[chromaPlayAnimation] Chroma: Attempting to play animation.";
const char* animfile;
bool loop;
rb_get_args(argc, argv, "zb", &animfile, &loop RB_ARG_END);
if (INIT_SUCCESS) {
lastAnimId = _openAnimation(animfile);
Debug() << "[chromaPlayAnimation] Opening animation:" << animfile;
Debug() << "[chromaPlayAnimation] ID:" << lastAnimId;
_playAnimation(lastAnimId);
}
return Qnil;
}
void chromaBindingInit() {
_attemptLinkSdk();
VALUE module = rb_define_module("Chroma");
_rb_define_module_function(module, "playAnim", chromaPlayAnimation);
}
void chromaBindingRelease() {
#ifdef _WIN32
if (hLib != NULL) {
_pluginUninit();
FreeLibrary(hLib);
hLib = NULL;
INIT_SUCCESS = false;
}
#endif
}