C++/SDL: keypresses query

closed account (3CXz8vqX)
Firstly, I don't think this is suitable for the beginner forum as SDL involves classes and stuff which aren't necessarily beginner friendly. (At least, not to a beginner who can't work a function let alone a pointer anyway...).

I'm having a bit of a brain seizure while going through LazyFoo's tuts and probably jumped ahead a little. After all, I was more keen on getting a blue box moving than focusing on the lessons. >_> *may have been the reason I didn't hit it off with university so well*. Well, now I have the blue box moving.

It's a rather rudimentary moving bit of code.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
while( SDL_PollEvent( &event ) )
        {
            play_button.handle_events();
            if( event.type == SDL_QUIT )
            {
                quit = true;
            }

            if( event.type == SDL_KEYDOWN )
            {
                switch( event.key.keysym.sym)
                {
                    case SDLK_ESCAPE: quit = true; break;
                    case SDLK_UP: player_pos_y -= 10; break;
                    case SDLK_DOWN: player_pos_y += 10; break;
                    case SDLK_LEFT: player_pos_x -= 10; break;
                    case SDLK_RIGHT: player_pos_x += 10; break;
                    default: break;
                }
            }
        }


This moves my character just fine, but I think I might get RSI if I have to keep tapping the left button..... would keystates solve my dilemma or is there a better way to code it instead?

Just for a whacky crazy thing, I tried a while loop instead of if. It didn't have the intended consequences...*crashed my computer due to overheating*.
Keystates would do what you want however I normally use the same style you are using however make it change the players velocity not the position. You are going to have to work with player velocity sooner or later anyway, but it's your call there.

If you change it by velocity then somewhere else in the code you would have a move function that would look something like this (and you would call it every frame).

1
2
3
4
5
void Move()
{
      x += xVel;
      y += yVel;
}


I believe LazyFoo does everything in this style in his tutorials too (which I do recommend you read through carefully sooner or later just because they are very well done.)
Last edited on
closed account (3CXz8vqX)
Thanks very much! I'll keep following the tutorials and just wait for now.

...you mean except for his earlier ones, where you have to write out clip[].x = x about sixteen times... >.>; (for looped it).

Topic archived. No new replies allowed.