Question

I got a problem with my code, I'm trying to make a First Person 3D Camera. I use SDL_GetKeyboardState(NULL) to get the pressed keys.

When I press one of the defined keys nothing happens, why?

Camera (Controll):

void Control(float movevel, float mousevel, bool mi, SDL_Window* window)
{
if (mi)  //if the mouse is in the screen
{
    int MidX = 640 / 2;   //middle of the screen
    int MidY = 480 / 2;
    SDL_ShowCursor(SDL_DISABLE);    //we don't show the cursor
    int tmpx, tmpy;
    SDL_GetMouseState(&tmpx, &tmpy); //get the current position of the cursor
    camYaw += mousevel*(MidX - tmpx);   //get the rotation, for example, if the mouse current position is 315, than 5*0.2, this is for Y
    camPitch += mousevel*(MidY - tmpy); //this is for X
    lockCamera();
    //SDL_WarpMouse(MidX, MidY);       //move back the cursor to the center of the screen
    SDL_WarpMouseInWindow(window, MidX, MidY);

    const Uint8* kstate = SDL_GetKeyboardState(NULL);

    if (kstate[SDLK_w])
    {
        if (camPitch != 90 && camPitch != -90)
        {       //if we are facing directly up or down, we don't go forward, it will be commented out, when there will be gravity
            moveCamera(movevel, 0.0);        //move forward
        }

        moveCameraUp(movevel, 0.0);      //move up/down
    }

    if (kstate[SDLK_s])
    {
        //same, just we use 180 degrees, so we move at the different direction (move back)
        if (camPitch != 90 && camPitch != -90)
        {
            moveCamera(movevel, 180.0);
        }

        moveCameraUp(movevel, 180.0);
    }

    if (kstate[SDLK_a])
    {       //move left
        moveCamera(movevel, 90.0);
    }

    if (kstate[SDLK_d])
    {  //move right
        moveCamera(movevel, 270);
    }
}

glRotatef(-camPitch, 1.0, 0.0, 0.0);
glRotatef(-camYaw, 0.0, 1.0, 0.0);
}

Main (Loop)

while (running)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    SDL_Event ev;
    while (SDL_PollEvent(&ev))
    {
        switch (ev.type)
        {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_KEYDOWN:
                int x, y;

                SDL_GetMouseState(&x, &y);
                Main::handleKeys(ev.key.keysym.scancode, x, y);
                break;
        }
    }

    Main::update(window);
    Main::render(window);

    SDL_GL_SwapWindow(window);
}

Main (Update):

void Main::update(SDL_Window* window)
{
Control(0.2, 0.2, mousein, window);
UpdateCamera(0.2); //move the camera to the new location
}
Was it helpful?

Solution 2

Use SDL_PumpEvents() to update the state array. (From the SDL_GetKeyboardState() wiki). I believe that if you do:

SDL_PumpEvents();
SDL_GetKeyboardState(NULL);

you should get the results you are looking for. (You need to SDL_PumpEvents every loop as well as SDL_GetKeyboardState obviously)

(EDIT) Another option:

    case SDL_KEYDOWN:
        switch(event.key.keysym.sym){
            case SDLK_w
                (... code)
                break;
            case SDLK_s:
                (... code)
                break;
            case SDLK_a:
                (... code)
                break;
            case SDLK_d:
                (... code)
                break;

You can also send the event.key.keysym.sym to a function and use it their ofcourse.

OTHER TIPS

you should call the glRotatef functions before you translate the camera, otherwise it will be rotated about the origin and not its position

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top