c++ - SDL PollEvent() never returns anything -
i'm trying sdl detect keyboard activity c++ console application/game on os x. not sdl_pollevent()
not returning keyboard activity, far can tell debugging sdl_event*
it's supposed update never updated @ all. experience sdl amounts few minutes of tutorials on web, i'm sure there's didn't correctly when setting sdl that's causing problem. below class wrote manages polling events, , supposed notify objects via callback when requested keypress detected (see listenforkeyevents()
). since depends on sdl_pollevent()
, however, it's not doing @ all.
using namespace std ; template<class t> struct keyinputregister { /** * string representing keyboard key * client wishes listen */ const char * requestedchar ; t * caller ; /** * pointer function called * when requested keyboard input 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) ; (auto = 0 ; < keyinputregistry->size() ; i++) { if (ch == (keyinputregistry->at(i).requestedchar)) { t * callr = keyinputregistry->at(i).caller ; void (t::*callbak)() = (keyinputregistry->at(i).callback) ; (callr->*callbak)(); } } } } } } /* example use of keyinputregister , 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 above main(), redefined sdl sdl_main() */ return r ; }
i don't see calls sdl_init in code, have @ http://www.friedspace.com/cprogramming/sdlbasic.php shows basic sample how sdl application supposed set up. must sure sdl running correctly before can poll events.
Comments
Post a Comment