me he familiarizado con la programación de OpenGL usando SDL en Ubuntu usando C++. Después de mirar alrededor y experimentar estoy empezando a comprender. Necesito consejos sobre el manejo de eventos de teclado con SDL.¿Cómo manejar múltiples pulsaciones de teclas a la vez con SDL?
Tengo una cámara en 1ra persona, y puedo caminar hacia adelante, atrás, atacar a izquierda y derecha y usar el mouse para mirar alrededor, lo cual es genial. Aquí está mi función processEvents:
void processEvents()
{
int mid_x = screen_width >> 1;
int mid_y = screen_height >> 1;
int mpx = event.motion.x;
int mpy = event.motion.y;
float angle_y = 0.0f;
float angle_z = 0.0f;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_w:
objCamera.Move_Camera(CAMERASPEED);
break;
case SDLK_s:
objCamera.Move_Camera(-CAMERASPEED);
break;
case SDLK_d:
objCamera.Strafe_Camera(CAMERASPEED);
break;
case SDLK_a:
objCamera.Strafe_Camera(-CAMERASPEED);
break;
default:
break;
}
break;
case SDL_MOUSEMOTION:
if((mpx == mid_x) && (mpy == mid_y)) return;
SDL_WarpMouse(mid_x, mid_y);
// Get the direction from the mouse cursor, set a resonable maneuvering speed
angle_y = (float)((mid_x - mpx))/1000;
angle_z = (float)((mid_y - mpy))/1000;
// The higher the value is the faster the camera looks around.
objCamera.mView.y += angle_z * 2;
// limit the rotation around the x-axis
if((objCamera.mView.y - objCamera.mPos.y) > 8) objCamera.mView.y = objCamera.mPos.y + 8;
if((objCamera.mView.y - objCamera.mPos.y) <-8) objCamera.mView.y = objCamera.mPos.y - 8;
objCamera.Rotate_View(-angle_y);
break;
case SDL_QUIT:
quit = true;
break;
case SDL_VIDEORESIZE:
screen = SDL_SetVideoMode(event.resize.w, event.resize.h, screen_bpp, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE);
screen_width = event.resize.w;
screen_height = event.resize.h;
init_opengl();
std::cout << "Resized to width: " << event.resize.w << " height: " << event.resize.h << std::endl;
break;
default:
break;
}
}
}
ahora que esto está funcionando, tiene algunas limitaciones. El más grande y el propósito de mi pregunta es que parece que solo procesa la última clave que se presionó. Entonces, si sostengo 's' para caminar hacia atrás y presiono 'd' para atacar a la derecha, termino ametrallando a la derecha pero no retrocediendo.
¿Puede alguien señalarme en la dirección correcta para un mejor manejo del teclado con SDL, soporte para múltiples pulsaciones de teclas a la vez, etc.?
Gracias
Realmente una buena solución, esto se puede utilizar en casi todos los juegos – devsaw