Weird bug
OK. This one has me stumped. It looks like it is something very simple, but for the life of me I cannot figure out what's going on. First, here is the code:
Now, when 'w' is pressed, this is output:
---------------------------------------------------- key: w
Key pressed correctly? yes
before idle, is w pressed? no
What's going on? I've commented out all code in that section which sets w to false, and yet it somehow manages to become false at the end. I've clean built and everything.
Code:
while ( SDL_PollEvent (&event) )
{
switch (event.type)
{
case SDL_MOUSEMOTION:
//mousex += event.motion.xrel;
//mousey += event.motion.yrel;
//m_GameState.SetMouse(mousex, mousey);
break;
case SDL_MOUSEBUTTONDOWN:
m_GameState.SetMouseState(true);
if(m_CurrentGameScene->AcceptsMouse())
m_CurrentGameScene->Mouse(1, true, mousex, mousey);
break;
case SDL_MOUSEBUTTONUP:
m_GameState.SetMouseState(false);
break;
case SDL_KEYDOWN:
/* Any keypress quits the app... */
if(event.key.keysym.sym == SDLK_q)
done = 1;
key = ConvertKey(event.key.keysym.sym);
std::cout << "---------------------------------------------------- key: " << key << std::endl;
m_GameState.SetKey(key, true);
std::cout << "\tKey pressed correctly? " << (m_GameState.GetKey(key) ? "yes" : "no") << std::endl;
//if(m_CurrentGameScene->AcceptsKeyboard())
// m_CurrentGameScene->Keyboard(key, mousex, mousey);
break;
case SDL_KEYUP:
//key = ConvertKey(event.key.keysym.sym);
//std::cout << "----------------------------------------------------------keyup: " << key << std::endl;
//m_GameState.SetKey(key, false);
//if(m_CurrentGameScene->AcceptsKeyboardUp())
// m_CurrentGameScene->KeyboardUp(key, mousex, mousey);
break;
case SDL_QUIT:
done = 1;
break;
default:
std::cout << "unknown event" << std::endl;
break;
}
}
// Draw at 24 hz
// This approach is not normally recommended - it is better to
// use time-based animation and run as fast as possible
//m_GameState.SetKey('w', true);
std::cout << "before idle, is w pressed? " << (m_GameState.GetKey('w') ? "yes" : "no") << std::endl;
Now, when 'w' is pressed, this is output:
---------------------------------------------------- key: w
Key pressed correctly? yes
before idle, is w pressed? no
What's going on? I've commented out all code in that section which sets w to false, and yet it somehow manages to become false at the end. I've clean built and everything.
Nevermind. I got it. It was with the use of unsigned chars. I was using unsigned chars because of the conversion from glut to SDL. GLUT's keyboard callbacks are all unsigned chars, but somehow this doesn't work here. I converted all to regular chars, and it is now working fine.
Quote:GLUT's keyboard callbacks are all unsigned chars, but somehow this doesn't work here.I'm pretty sure you should use Sint16 for any key codes, anyway; if you take a glance at SDL_keysym.h, you'll see that the key codes exceed 255 (such as SDLK_UNDO == 322.)
Mark Bishop
--
Student and freelance OS X & iOS developer