我一直在尝试编写一个对象以使用 SDL2 库中的 Joystick 类和 NodeJS 使用 FFI 模块,但一直遇到问题。它似乎在大约 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"
有任何想法吗?