与节点FFI创建SDL2操纵杆绑定的问题



我一直试图编写一个对象,使用FFI模块使用NodeJS的SDL2库中的Joycle类,但一直遇到问题。它似乎在大约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" 

有什么想法吗?

我怀疑上面的代码中存在太多问题,无法使其按预期工作。最后,我想出了一个不同的解决方案。。。

我发现了一个静态的、较低级别的输入库-http://forums.tigsource.com/index.php?topic=10675.0-在上面写一个骨架库,链接它,然后应用它…

它工作得很好,比SDL轻得多。。。

如果我有时间的话,我必须在接下来的几天内把它放在GIT或Bitbucket上。

最新更新