0

remove_joystick()除了不断调用then之外,有没有办法在运行时更新插入的操纵杆数量install_joystick?这被证明是非常缓慢的(从 60 FPS 到 5 左右)。

Allegro 4.2 只回答请...

void Joystick::Update() {
    //If joystick input was lost, attempt to reacquire.
    if(GetNumJoysticks() == 0) {
        throw InputNotAvailableException("Joystick");
    }
    //If all joysticks were deleted remove input and do nothing.
    if(_numjoysticks == 0) {
        remove_joystick();
        return;
    }

    //Update state information
    if(poll_joystick() < 0) {
        throw InputNotAvailableException("Joystick");
    }
    for(int i = 0; i < _numButtons; ++i) {
        _prevButtons[i].b = _curButtons[i].b;
        _prevButtons[i].name = _curButtons[i].name;

        _curButtons[i].b = joy[_joyNumber].button[i].b;
        _curButtons[i].name = joy[_joyNumber].button[i].name;
    }
    for(int i = 0; i < _numSticks; ++i) {
        for(int j = 0; j < joy[_joyNumber].stick[i].num_axis; ++j) {
            _prevSticks[i].axis[j].name = _curSticks[i].axis[j].name;
            _prevSticks[i].axis[j].pos = _curSticks[i].axis[j].pos;
            _prevSticks[i].axis[j].d1 = _curSticks[i].axis[j].d1;
            _prevSticks[i].axis[j].d2 = _curSticks[i].axis[j].d2;

            _curSticks[i].axis[j].name = joy[_joyNumber].stick[i].axis[j].name;
            _curSticks[i].axis[j].pos = joy[_joyNumber].stick[i].axis[j].pos;
            _curSticks[i].axis[j].d1 = joy[_joyNumber].stick[i].axis[j].d1;
            _curSticks[i].axis[j].d2 = joy[_joyNumber].stick[i].axis[j].d2;
        }
        _prevSticks[i].flags = _curSticks[i].flags;
        _prevSticks[i].name = _curSticks[i].name;

        _curSticks[i].flags = joy[_joyNumber].stick[i].flags;
        _curSticks[i].name = joy[_joyNumber].stick[i].name;
    }
}
int Joystick::GetNumJoysticks() {
    remove_joystick();
    if(install_joystick(JOY_TYPE_DIRECTX)) {
        return 0;
    }
    return (num_joysticks);
}
4

1 回答 1

2

4.x 系列没有。5.x 系列可以。

您必须使用自定义平台特定代码(假设存在此类事件)侦听本机操作系统事件,并且仅在检测到更改时调用 Allegro deinit/init 函数,或者要求用户手动启动操纵杆刷新。

在 Linux 下,您可以inotify_add_watch()/dev/input 检查更改。查看 4.4 Allegro 代码,您似乎想要调用 Win32 函数joyGetNumDevs()joyGetPos(). 就像是:

int WIN_MAX_JOYSTICKS = joyGetNumDevs(); // this should never change
JOYINFO ji;
int pluggedin_count = 0;
for (int i = 0; i < WIN_MAX_JOYSTICKS; ++i)
  if (joyGetPos(i, &ji) == JOYERR_NOERROR) ++pluggedin_count;

if (pluggedin_count != last_pluggedin_count) /* reinit Allegro */

您必须每 N 秒执行一次。

这些joy* 函数是Windows 函数,因此请阅读MSDN 文档以了解如何使用它们。

于 2012-07-07T17:55:30.083 回答