0

我一直在尝试编写一个对象以使用 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" 

有任何想法吗?

4

1 回答 1

0

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

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

它工作得很好,而且比 SDL 轻得多……

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

于 2013-08-13T13:27:07.943 回答