Skip to main content
gamedev.net
🔒 Locked

SDL input

Started by andyZER0 May 1, 2004 at 10:02 PM 4 replies 500+ views
Original Post
andyZER0
andyZER0
So I''ve been reading up on nehe.gamedev.net to learn OpenGL. Also, I''ve been looking at the SDL port for some cross-platform coding. The code that handles input pretty much looks like this: // beginning of code while(SDL_PollEvent(&event)) { switch (event.type) { /* case SDL_KEYDOWN: handleKeyPress( &event.key.keysym ); break; */ case SDL_QUIT: progRun = 0; break; case SDL_VIDEORESIZE: /* handle resize event */ surface = SDL_SetVideoMode( event.resize.w, event.resize.h, 32, videoFlags ); if ( !surface ) { fprintf( stderr, "Could not get a surface after resize: %s\n", SDL_GetError( ) ); Quit( 1 ); } resizeWindow( event.resize.w, event.resize.h ); break; default: break; } } //end of code handleKeyPress(SDL_keysym *keysym) handles all the keys with switch statements as well. However, this input doesn''t do what I expect it to do. Pressing left speeds up the Y rotation speed of the box, but when I hold it down, it doesn''t continue to do so. If I''m not specific enough, an example is how holding down the key to go forward in a first person shooter makes you go forward as long as you hold the key down. In my case, pressing and holding the key only modifies it once, and doesn''t continue to increase. Thus, I gotta keep tapping it repeatedly. This code seems to work: //begin if (event.type == SDL_KEYDOWN) handleKeyPress( &event.key.keysym ); //end Yet, it''s very sensitive. Like pressing ''L'' enables or disables light; the slightest tap makes the light flash a couple times a second lol. How do I solve my input problem? Thanks very much in advance.
Squirell
Squirell
put in a delay or something? like wait 5 milliseconds or something like that
jdhardy
jdhardy
There are three ways:
1) After a SDL_KEYDOWN, set a boolean flag. After a SDL_KEYUP, unset it.

2) Read the key array every frame.

EDIT:
3) Enable key repeats.

The SDL doc page for input is here. Make sure to read the comments as well.

[edited by - ze_jackal on May 1, 2004 12:47:46 AM]
Spudder
Spudder
Try SDL_WaitEvent() instead, this doesn''t return until an event has been received unlike SDL_PollEvent() and should solve your sensitivity problem.
Pres
Pres
Example of solution 1) which works well


static void handle_key_down(SDL_keysym* keysym)
{
switch(keysym->sym)
{

case SDLK_ESCAPE:
quit_game(0);
break;
case SDLK_p:
PAUSE=PAUSE^1;
break;
case SDLK_g:
break;
case SDLK_w:
flags=flags|1;
break;
case SDLK_s:
flags=flags|2;
break;
case SDLK_l:
flags=flags|4;
break;
case SDLK_k:
flags=flags|8;
break;
case SDLK_a:
flags=flags|16;
break;
case SDLK_d:
flags=flags|32;
break;
case SDLK_n:
flags=flags|64;
break;
case SDLK_m:
flags=flags|128;
break;
case SDLK_b:
flags=flags|256;
break;
default:
break;
}
}

static void handle_key_up(SDL_keysym* keysym)
{
switch(keysym->sym)
{
case SDLK_w:
flags=(flags & (flags - 1));
break;
case SDLK_s:
flags=(flags & (flags - 2));
break;
case SDLK_l:
flags=(flags & (flags - 4));
break;
case SDLK_k:
flags=(flags & (flags - 8));
break;
case SDLK_a:
flags=(flags & (flags - 16));
break;
case SDLK_d:
flags=(flags & (flags - 32));
break;
case SDLK_n:
flags=(flags & (flags - 64));
break;
case SDLK_m:
flags=(flags & (flags - 128));
break;
case SDLK_b:
flags=(flags & (flags - 256));
break;
default:
break;
}
}
void update_input(void)
{
if((flags & 1) == 1)
{

}

/* etcetera */

}

Fingers_
Fingers_
You probably don''t want to use the events to do this at all.

Read the documentation on the SDL_GetKeyState function. This will let you poll the state of each key. In your case, you''d check each frame if the arrow key is down, and if it is then change your rotation rate.

Of course, if you have "toggle" keys that require something to happen only when you first press it you should use the keydown event for those.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.