I have created a cube in opengl using SDL. Now I am trying to move the camera in X direction if user presses left or right arrow keys and Y direction if user presses up or down arrow keys. Right now the camera moves by a fixed amount in a particular direction if the key gets pressed once. It doesn't matter how long does a person hold the key down, it moves by that fixed amount only. I want the camera to move continuously as long as the key is pressed.
Below is the code for keyboard polling.
while ( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_ACTIVEEVENT:
if ( event.active.gain == 0 )
isActive = FALSE;
else
isActive = TRUE;
break;
case SDL_VIDEORESIZE:
surface = SDL_SetVideoMode( event.resize.w,
event.resize.h,
16, 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;
case SDL_KEYDOWN:
handleKeyPress( &event.key.keysym );
break;
case SDL_QUIT:
done = TRUE;
break;
default:
break;
}
}
And below i have mentioned the keypress handler function
void handleKeyPress( SDL_keysym *keysym )
{
switch( (keysym->sym) ){
case SDLK_ESCAPE:
Quit(0);
break;
case SDLK_LEFT:
eye[0] -= 0.1;
break;
case SDLK_RIGHT:
eye[0] += 0.1;
break;
case SDLK_UP:
eye[1] -= 0.1;
break;
case SDLK_DOWN:
eye[1] += 0.1;
break;
default:
break;
}
return;
}
What changes should I make in my code to handle continuous keystroke detection?
Note: The following code does the required job but I don't understand why
if ( ( SDL_EnableKeyRepeat( 100, SDL_DEFAULT_REPEAT_INTERVAL ) ) )
{
fprintf( stderr, "Setting keyboard repeat failed: %s\n",
SDL_GetError( ) );
Quit( 1 );
}