Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Key input in SDL

Options
  • 03-03-2007 10:08pm
    #1
    Closed Accounts Posts: 882 ✭✭✭


    Hey guys, i've a project in C++ in college to make a retro game through SDL such as space invaders or similar. I'm trying to take an input from the keyboard to make the ship move.

    I can get it to move, but it's really slow in reacting and most of the time doesn't at all.

    I'm using a class and loading a sprite to the screen and using the usual x and y position and velocity variables and updated them everytime the screen refreshes. I'm using a function called keyinput() to take the input from the keyboard, then update the velocity variables. This then updates the position of the image and it moves.

    I got the code from a tutorial on the web, but i'm having trouble with it. Our lecturer is going to give us material on this next week, but i'm trying to get it out of the way so i can move onto other parts of the game.

    It does work, but not very well.

    Here's a snippet of code:


    Uint8* keys;
    keys = SDL_GetKeyState(NULL);

    SDL_Event event;


    bool move=true;

    while (move)
    {
    if (SDL_PollEvent(&event))
    {

    if (event.type == SDL_KEYDOWN)
    {

    SDLKey keyPressed = event.key.keysym.sym;


    switch (keyPressed)
    {
    case SDLK_UP:
    ship_vy=-1;
    move= false;
    break;
    case SDLK_DOWN:
    ship_vy=1;
    move= false;
    break;
    case SDLK_RIGHT:
    ship_vx=1;
    move= false;
    break;
    case SDLK_LEFT:
    ship_vx=-1;
    move= false;
    break;
    }

    }
    }


    cheers for any advice.


Advertisement