Frage

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. 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. 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()). Since it depends on SDL_PollEvent(), however, it's not currently doing anything at all.

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 ;
}
War es hilfreich?

Lösung

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. You must be sure that SDL is running correctly before you can poll for events.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top