gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:55:04 24 4
gpt4 key购买 nike

我正在尝试让 SDL 检测 OS X 上 C++ 控制台应用程序/游戏的键盘事件。但不仅是 SDL_PollEvent() 没有返回任何键盘事件,据我所知通过调试它应该更新的 SDL_Event* 根本不会更新。我使用 SDL 的经验相当于网络上几分钟的教程,所以我确信在设置 SDL 时我没有做正确的事情导致了这个问题。下面是我编写的管理事件轮询的类,应该在检测到请求的按键时通过回调通知对象(参见 listenForKeyEvents())。但是,由于它依赖于 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 ;
}

最佳答案

我在您的代码中没有看到任何对 SDL_Init 的调用,请查看 http://www.friedspace.com/cprogramming/sdlbasic.php它显示了一个非常基本的示例,说明应该如何设置 SDL 应用程序。在轮询事件之前,您必须确保 SDL 正确运行。

关于c++ - SDL PollEvent() 从不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23288933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com