SDL input problems...

Started by
15 comments, last by Kylotan 18 years, 4 months ago
[source lang=C++]
if (SDL_Init( SDL_INIT_EVERYTHING) < 0)
   return cWUtility::ErrorLog.Write("cWInput::Init()");

SDL_EVENT   m_Event;
Uint8       *m_KeyState;
Uint8       m_MouseState;
long        m_AbsX, m_AbsY, m_RelX, m_RelY;


SDL_PollEvent( &m_Event );
SDL_EnableUNICODE( 1 );

m_KeyState = SDL_GetKeyState(NULL);

while (loop)
{
   SDL_PollEvent( &m_Event );
   SDL_PumpEvents();
   m_MouseState = SDL_GetMouseState((int*)m_AbsX, (int*)m_AbsY);
   SDL_GetRelativeMouseState((int*)m_RelX, (int*)m_RelY);

   if ( MYGetKeyState(SDL_ESCAPE))
      exit();
}

bool MYGetKeyState(long Key)
{
   if (m_KeyState[Key] == 1 )
     return true;
   return false;
}

for some reason none of my keyboard and mouse events are working.. it's not seeing that anything is being pressed (of course this is a very simplified version of what i really have coded) is there anything wrong with that?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
The problem with your keyboard inputs can be explained because the
m_KeyState = SDL_GetKeyState(NULL) should be within your main loop.

Also, I think you're calling SDL_GetMouseState incorrectly. It should look something like:
m_MouseState = SDL_GetMouseState( &m_AbsX, &m_AbsY );

Cheers,
--Brian
ohhh, ok. what about pollevent() and pumpevents()? should i have those in my main loop?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
If you're not using that SDL_Event object, you don't need SDL_PollEvent at all. You will need SDL_PumpEvents, however.
Alright. How's the order go in the loop? should i pump events THEN getkeystate? for vice-versa?

Also, what's the purpose of using the SDL_EVENT, and polling the event?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
Alright. How's the order go in the loop? should i pump events THEN getkeystate? for vice-versa?

Also, what's the purpose of using the SDL_EVENT, and polling the event?


the event queue is central to SDL's event handling. if you do not empty the queue( by polling over every element and dealing with those you want ) your application will become unresponsive. you need to do that regularly.

the "snapshot" input functions can then be used, but they are not a complete replacement for polling
alright, i get the part about pollevent().. kinda, but what if there are 2 buttons being pressed at the same time?

can someone post some examples using pollevent() AND pumpevents() and event getkeystate()?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
alright, i get the part about pollevent().. kinda, but what if there are 2 buttons being pressed at the same time?

can someone post some examples using pollevent() AND pumpevents() and event getkeystate()?


I've tried SDL_Event with double key presses. It _WONT_ work. However, SDL_GetKeyState(...) will: (from my code) (not really, from my _previous_ code)
bool Run() {    static SDL_Event s_Event;    while (SDL_PollEvent(&s_Event)) {        if (s_Event.type == SDL_KEYDOWN) {            switch (s_Event.key.keysym.sym) {                case SDLK_ESCAPE: return false; break;                default: // whatever            }        }    }    static unsigned char *s_Keys = SDL_GetKeyState(NULL);    // Just for my sake ;)    static unsigned int SDLK_ALT = (SDLK_LALT | SDLK_RALT);    if (s_Keys[SDLK_ALT] && s_Keys[SDLK_F4])        return false;    SDL_PumpEvents();    return true;}


As you can see, the polling will NOT take two or more keys. However, the snapshot version of the keys will, as you can see.

If you have any more questions, say so.

C++
hmm... would i be able to just have something that looks like this?

[source lang=C++]bool Run() {    static SDL_Event s_Event;        // main loop    while (1)    {      SDL_PollEvent(&s_Event);      static unsigned char *s_Keys = SDL_GetKeyState(NULL);      static unsigned int SDLK_ALT = (SDLK_LALT | SDLK_RALT);      if (s_Keys[SDLK_ALT] && s_Keys[SDLK_F4])          break;      SDL_PumpEvents();    }    return true;}


also:
so the pollevent() needs to be at the top with the getkeystate() and the pumpevents() needs to be at the bottom once i'm done checking the states and whatnot?

edit:
since the SDL_EVENT only checks one key, and i'm going to use the getkeystate() anyway? could i just avoid using pollevent() completely? or would i have to do just what i'm doing in that code i posted and just ahve a call to it?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
hmm... would i be able to just have something that looks like this?

*** Source Snippet Removed ***

also:
so the pollevent() needs to be at the top with the getkeystate() and the pumpevents() needs to be at the bottom once i'm done checking the states and whatnot?

edit:
since the SDL_EVENT only checks one key, and i'm going to use the getkeystate() anyway? could i just avoid using pollevent() completely? or would i have to do just what i'm doing in that code i posted and just ahve a call to it?


Basicly, yes, you can have code like that. Take this as a note though, it DOESN'T have to look like that, it's just how I wrote some code. What you should do is:
while (gameRunning) // or whatever your main loop is{    while (SDL_PollEvent(&yourEvent))    {        // event stuff    }    // other stuff}

My previous code was just an example.

No, it doesn't have to be in that order. It's just the order that I and many others prefer because of it's readability.

SDL_Event will hold events. Each key press, and any other message is an event. However, when you press 'w' and key holding it and press 'd' you will get a message for 'd', and not for 'w'. This is not what you wan't in games because your character then won't be able to move diagnoally. However, this is good for menus. This is where SDL_GetKeyState is nice. It get's a snapshot of the keys. So it can capture many keys at once. This way, you can have diagnol movement.

C++

This topic is closed to new replies.

Advertisement