For anyone who knows sdl, a little help?

What is wrong with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SDL_Scancode Game::keys[4] = {SDL_SCANCODE_W, SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_D};
keypressed = SDL_GetKeyboardState(NULL);
void Game::Update()
{
    if(keypressed[keys[0]])//keypressed[SDL_SCANCODE_W]
    {
        player->Update(keys);
    }
}

//playerclass
void Player::Update(SDL_Scancode key[])
{
    if(key[3])
    {
        destSpritesheet.y -= speed;
    }
}


If i press W, as stated in if(keypressed[keys[0]])
why does the character goes up even if there is no key[0]?
1
2
3
4
if(key[3]) //here should be key[0] for W
    {
        destSpritesheet.y -= speed;
    }
If key is the same as Game::keys, then if(key[3]) will always run because Game::keys[3] is not zero.

Are you saying if(keypressed[keys[0]]) is also not working? If you are not using SDL_PollEvent in your program you will have to use SDL_PumpEvents(), otherwise the array will not get updated.
Last edited on
if(keypressed[keys[0]]) is working.

But i'm trying to make controls on the game class and just passing the "key" to the player class
like this pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if(keypressed[key[0]]) //witch should be SDL_SCANCODE_W
player->Update(keypressed) //but in this case in the player class i should have
Update(const Uint8 *key) //this can be any key, so i got to specify it in my player class:
{
if(key[SDL_SCANCODE_W]
//doStuff
}

//So i can do this:

if(keypressed[key[0]]) //If SDL_SCANCODE_W
player->Update(key[0]); //meaning SDL_SCANCODE_W

//in witch:
Update(SDL_Scancode key[])
{
if(key[0]) // if is SDL_SCANCODE_W
 //doStuff
}
Last edited on
Solved by myself
1
2
3
4
5
6
7
8
9
10
11
void Player::Update(SDL_Scancode key[], const Uint8 *keypressed)
{
    if(keypressed[key[0]])
    {
        destSpritesheet.y -= speed;
    }
    if(keypressed[key[2]])
    {
        destSpritesheet.y += speed;
    }
}


Since i'm passing the key pressed and the keys(witch the player can change) i need to pass keypressed. thanks!
Topic archived. No new replies allowed.