Getting out of continuous loop

I thought this code would exit the loop when up arrow is pressed - but it does not. What am I doing wrong? TIA. Bill S.

#include <SDL2/SDL.h>
#include <stdio.h>
#include

int main()
{
SDL_Init(SDL_INIT_EVERYTHING);

const Uint8 *state = SDL_GetKeyboardState(NULL);
while (!(state[SDL_SCANCODE_UP]))
{
printf(“just burning time in a loop \n”);
}
printf(" is pressed.\n");
SDL_Quit();
}

When in doubt, check the docs https://wiki.libsdl.org/SDL_GetKeyboardState.
Docs say you need to be calling SDL_PumpEvents(); to properly use
SDL_GetKeyboardState. SDL_PumpEvents is also called inside
SDL_PollEvent(), which you’d want if you want to deal with OS-leve events.

-AlexOn Wed, Mar 4, 2015 at 6:25 PM, bilsch01 wrote:

I thought this code would exit the loop when up arrow is pressed - but
it does not. What am I doing wrong? TIA. Bill S.

#include
#include
#include

int main()
{
SDL_Init(SDL_INIT_EVERYTHING);

const Uint8 *state = SDL_GetKeyboardState(NULL);
while (!(state[SDL_SCANCODE_UP]))
{
printf(“just burning time in a loop \n”);
}
printf(" is pressed.\n");
SDL_Quit();
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

The docs are a struggle for me. I dont have a feel for this kind of programming. Can you tell me do I need SDL_Pump and SDL_Poll in each iteration of the loop?

Yes to each iteration of the loop. Unless you are dealing with “special” circumstances, you should probably stick with using just SDL_Poll. (Note that SDL_Poll calls SDL_PumpEvents internally, so no need to call both).

Jeffrey Carpenter
@Jeffrey_Carpenter> On Mar 4, 2015, at 20:31, bilsch01 wrote:

The docs are a struggle for me. I dont have a feel for this kind of programming. Can you tell me do I need SDL_Pump and SDL_Poll in each iteration of the loop?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Sorry, didn?t read through the whole thread until just now ?

With SDL_GetKeyboardState, yes, you should call SDL_PumpEvents first during each iteration of the loop.

Cheers,
Jeffrey Carpenter <@Jeffrey_Carpenter>

-------------- next part --------------
A non-text attachment was scrubbed…
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1572 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20150304/8333f2ea/attachment.bin

thanks.

You forgot to put
Code:
state = SDL_GetKeyboardState(NULL);

INSIDE the loop code.------------------------
Wut