简体   繁体   English

SDL PollEvent()从不返回任何内容

[英]SDL PollEvent() never returns anything

I'm trying to get SDL to detect keyboard activity for a C++ console application/game on OS X. But not only is SDL_PollEvent() not returning any keyboard activity, as far as I can tell from debugging the SDL_Event* it's supposed to update is never updated at all. 我正在尝试让SDL检测OS X上C ++控制台应用程序/游戏的键盘活动。但是据我调试SDL_Event*知道的,不仅SDL_PollEvent()不返回任何键盘活动,根本不会更新。 My experience with SDL amounts to a few minutes of tutorials on the web, so I'm sure there's something I didn't do correctly when setting up SDL that's causing this problem. 我对SDL的经验相当于网上的几分钟教程,因此,我确定设置SDL时会导致某些问题,但我没有正确执行某些操作。 Below is the class I wrote that manages polling for events, and is supposed to notify objects via callback when the requested keypress is detected (see listenForKeyEvents() ). 下面是我编写的用于管理事件轮询的类,应该在检测到请求的按键时通过回调通知对象(请参见listenForKeyEvents() )。 Since it depends on SDL_PollEvent() , however, it's not currently doing anything at all. 由于它依赖于SDL_PollEvent() ,因此,它目前根本不做任何事情。

using namespace std ;

template<class T>
struct KeyInputRegister {

    /**
     * The string representing the keyboard key
     * the client wishes to listen for
     */
    const char * requestedChar ;

    T * caller ;

    /**
     * A pointer to the function to be called
     * when the requested keyboard input is detected.
     */
    void (T::*callBack)() ;

    KeyInputRegister(const char* ch, T * callr, void (T::*cb)()) :
        requestedChar(ch), caller(callr), callBack(cb) {}

} ;

template <class T>
class InputController {

protected:
    static SDL_Event * event ;
    static std::thread * keyEventsThread ;

    static vector<KeyInputRegister<T>> * keyInputRegistry ;

    static void listenForKeyEvents() ;
    static void listenForKeyEvents_thread_start() ;

public:

    static void init() ;
    static void registerForKeypress(KeyInputRegister<T> & reg) ;
    static void exit() ;
} ;

template <class T>
void InputController<T>::init() {
    SDL_Init(SDL_INIT_EVERYTHING) ;
    keyInputRegistry = new vector<KeyInputRegister<T>> ;
    event = new SDL_Event() ;
    listenForKeyEvents_thread_start() ; 
}

template <class T>
void InputController<T>::listenForKeyEvents_thread_start() {
    void (*listenPtr)() = InputController<T>::listenForKeyEvents ;
    InputController<T>::keyEventsThread = new std::thread(listenPtr) ;
}

template <class T>
void InputController<T>::registerForKeypress(KeyInputRegister<T> & reg) {
    keyInputRegistry->push_back(reg) ;
}

template <class T>
void InputController<T>::listenForKeyEvents() {

    while (*GLOBAL_CONTINUE_SIGNAL) {
        if (SDL_PollEvent(event) == 1) {
            cout << "event detected!" << endl ;
            if (event->type == SDL_KEYDOWN) {

                auto key = event->key.keysym.sym ;
                const char * ch = SDL_GetKeyName(key) ;

                for (auto i = 0 ; i < keyInputRegistry->size() ; i++) {
                    if (ch == (keyInputRegistry->at(i).requestedChar)) {

                        T * callr = keyInputRegistry->at(i).caller ;

                        void (T::*callBak)() = (keyInputRegistry->at(i).callBack) ;

                        (callr->*callBak)();

                    }
                }
            }
        }
    }
}


/* An example use of KeyInputRegister and InputController: */

class GameObject {
    void moveForward() { cout << "moved forward!" << endl ; }

    void mfRegForCallback() {
        void (GameObject::*mvForwPtr)() = &GameObject::moveForward ;
        KeyInputRegister<GameObject> regMvF("w", this, mvForwPtr) ;
        InputController<GameObject>::registerForKeypress(regMvF) ;
    }
}


extern bool * GLOBAL_CONTINUE_SIGNAL = new bool(true) ;

#ifdef __cplusplus
extern "C"
#endif
int main(int argc, char ** argv) {
    InputController<GameObject>::init() ;
    GameObject player0 ;
    player0.mfregForCallback() ;
    usleep(1e9) ;
    *GLOBAL_CONTINUE_SIGNAL = false ;
    //other cleanup, etc.
    return 0;
}

#ifdef main
#undef main
#endif
int main(int argc, char ** argv) {
    int r = SDL_main(argc, argv) ; /*actually calls the above main(), which is redefined by SDL to be   
                                   SDL_main() */
    return r ;
}

I don't see any calls to SDL_Init in your code, have a look at http://www.friedspace.com/cprogramming/sdlbasic.php it shows a very basic sample how an SDL Application is supposed to be set up. 我没有在您的代码中看到对SDL_Init的任何调用,请访问http://www.friedspace.com/cprogramming/sdlbasic.php,它显示了一个非常基本的示例,说明了应该如何设置SDL应用程序。 You must be sure that SDL is running correctly before you can poll for events. 您必须先确保SDL在正确运行,然后才能轮询事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM