No, you're not.
It helps if you understand how event queues work. When something of interest happens, SDL will add an event to the queue.
This code:
1 2 3 4 5
|
while (SDL_PollEvent ( &event )){
switch (event.type){
//...
}
}
|
SDL_PollEvent tells SDL "okay, give me the next event that's in the queue". And the switch says "what kind of event is it?"
Now if there's no events in the queue, that loop isn't going to run, because SDL_PollEvent will have no event to give you.
With that in mind, when you press a key, SDL sends 1 event. When you release a key, SDL sends another event. It does
not continually send events as you hold the key down. So any code that you put in the
case SDL_KEYDOWN:
section will run only when the key is
first pressed.
Conversely, SDL_GetKeyState does not use the event system, and instead gives you the realtime pressed/released state of any key. You can check it any time and it will tell you whether the key is pressed or not.
With that in mind, think about what your code does...
When you first press the Up key:
- SDL_PollEvent will give you the SDL_KEYDOWN event
- your switch will process it
- SDL_GetKeyState will tell you that the Up key is pressed
- you will return 2 (correct behavior)
The next frame - when up is still being held:
- SDL_PollEvent has no pending events, so it will give you nothing
- your switch will not be run, since there's no event to check
- (SDL_GetKeyState is never checked)
- you will return 0 (incorrect behavior)