我一直在尝试使用FFI模块编写一个对象来使用带有NodeJS的SDL2库中的Joystick类,但是仍然遇到问题。它似乎在大约50%的时间内按预期工作,但在其他时候,程序声称它无法找到连接的操纵杆(使用SDL_GetError())。
以下是代码示例:
// Constructor...
function Joystick(deviceId){
this.joystickPointer = ref.refType('pointer');
this.SDL = ffi.Library("SDL2.dll", {
"SDL_Init": ["Uint32", ["string"]],
"SDL_Quit": ["void", []],
"SDL_JoystickOpen": ["pointer", ["int"]],
"SDL_NumJoysticks": ["int", []],
"SDL_JoystickName": ["string", [this.joystickPointer]],
"SDL_JoystickNumButtons": ["int", [this.joystickPointer]],
"SDL_JoystickGetButton": ["Uint8", [this.joystickPointer, "int"]],
"SDL_JoystickNumAxes": ["int", [this.joystickPointer]],
"SDL_JoystickGetAxis": ["int16", [this.joystickPointer, "int"]],
"SDL_JoystickGetAttached": ["bool", [this.joystickPointer]],
"SDL_JoystickClose": ["void", [this.joystickPointer]],
"SDL_JoystickUpdate": ["void", []],
"SDL_GetError": ["string", []]
});
// Setup
this.deviceId = deviceId || 0;
this.SDL.SDL_Init("SDL_INIT_JOYSTICK");
this.joystickObject = this.SDL.SDL_JoystickOpen(this.deviceId);
// Poll Joystick
this.deviceCount = this.SDL.SDL_NumJoysticks();
this.buttons = this.SDL.SDL_JoystickNumButtons(this.joystickObject);
this.name = this.SDL.SDL_JoystickName(this.joystickObject);
// Cleanup
this.SDL.SDL_JoystickClose(this.joystickObject);
this.SDL.SDL_Quit();
return false;
}
var testJoystick = new Joystick(0);
console.log(testJoystick.name);
当它失败时,SDL_GetError()会给我以下错误消息:
"Joystick hasn't been opened yet"
有什么想法吗?
答案 0 :(得分:0)
我怀疑上面的代码中存在太多问题,无法按预期工作。 最后,我想出了一个不同的解决方案......
我发现了一个静态的低级输入库 - http://forums.tigsource.com/index.php?topic=10675.0 - 在它的顶部写了一个骨架库,将其链接起来然后应用它......
效果很好,比SDL轻得多......
如果我有时间的话,必须在接下来的几天内把它放在GIT或Bitbucket上。