Jump to content

  • Log In with Google      Sign In   
  • Create Account

Changeable Input


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 hnderk   Members   -  Reputation: 100

Like
0Likes
Like

Posted 14 January 2012 - 05:59 AM

Hi all !
I'm writing a simple 2d game using SDL. I would like my game has got a changeable input from user. For the example: left arrow on the keyboard moves sth to the left. User change it, now Joystick axis motion moves sth to the left. I have no idea how do that. Can anybody help me ?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9599

Like
1Likes
Like

Posted 14 January 2012 - 09:18 AM

Exact details depend on what programming language you are using, but there are a number of ways to do this. One way is to create a map or associative array of event objects to function pointers or function objects. Then in each event handling block invoke the functor for the received events. Alternately you can use a map of events to tokens and then have your input handling functionality dispatch tokens to game state objects.

#3 hnderk   Members   -  Reputation: 100

Like
0Likes
Like

Posted 14 January 2012 - 09:58 AM

I'm using C++, could you explain it a little more with short code example ?

#4 SimonForsman   Crossbones+   -  Reputation: 6112

Like
1Likes
Like

Posted 14 January 2012 - 10:26 AM

I'm using C++, could you explain it a little more with short code example ?


typdef void (*action)(Player&);

void Action1(Player &p) {
code
}
void Action2(Player &p) {
code
}
...
...
...
...
std::map<int,action> actions = new std::map<int,action>();

actions[SDLK_SPACE] = Action1;
actions[SDLK_LEFT] = Action2;

(Assigning and reassigning can be done however you want)
....

then you just read input and call:
actions[keyThatWasPressed](playerobject);

i might have messed up the function pointer syntax somewhere but you should get the general idea.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#5 hnderk   Members   -  Reputation: 100

Like
0Likes
Like

Posted 14 January 2012 - 12:41 PM

Thanks, but how can I do it for SDL_Event structure ?
I'd like to use joystick too, it seems that is for keyboard only.

Exact details depend on what programming language you are using, but there are a number of ways to do this. One way is to create a map or associative array of event objects to function pointers or function objects. Then in each event handling block invoke the functor for the received events. Alternately you can use a map of events to tokens and then have your input handling functionality dispatch tokens to game state objects.


Could you write how to use a map of events ? I tried it myself but it doesn't work.

#6 SimonForsman   Crossbones+   -  Reputation: 6112

Like
1Likes
Like

Posted 15 January 2012 - 10:34 AM

Thanks, but how can I do it for SDL_Event structure ?
I'd like to use joystick too, it seems that is for keyboard only.


Exact details depend on what programming language you are using, but there are a number of ways to do this. One way is to create a map or associative array of event objects to function pointers or function objects. Then in each event handling block invoke the functor for the received events. Alternately you can use a map of events to tokens and then have your input handling functionality dispatch tokens to game state objects.


Could you write how to use a map of events ? I tried it myself but it doesn't work.


The key part of the map has to have a comparison operator defined (the < operator in this case) which SDL_Event doesn't have, You could write your own EventKey(ok, maybe not the best name) class that stores the necessary information and which overloads the < operator or you can pass a comparison function to the map.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#7 rip-off   Moderators   -  Reputation: 8228

Like
2Likes
Like

Posted 15 January 2012 - 11:57 AM

You can use event.key.keysym.sym as the lookup key for key events:
// Somewhere handy for inclusion

typedef void (*KeyFunction)(Player &player);
typedef void (*MouseFunction)(Player &player, int x, int y);

typedef std::map<SDL_KeySym, KeyFunction> KeyMap;
typedef std::map<Uint8, MouseFunction> MouseMap;

// Later, during setup in main().

KeyMap keyMap;
keyMap[SDLK_UP] = &playerForward;
keyMap[SDLK_DOWN] = &playerBackward;
keyMap[SDLK_LEFT] = &playerLeft;
keyMap[SDLK_RIGHT] = &playerRight;

MouseMap mouseMap;
mouseMap[SDL_BUTTON(1)] = &playerShoot;

// Later again, during game loop...

bool running = true;
while(running)
{
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)
        {
            running = false;
        }
        else if(event.type == SDL_KEYDOWN)
        {
            KeyMap::iterator i = keyMap.find(event.key.keysym.sym);
            if(i != keyMap.end())
            {
                KeyFunction action = i->second;
                action(player);
            }
        }
        else if(event.type == SDL_MOUSEBUTTONDOWN)
        {
            MouseMap::iterator i = mouseMap.find(event.button.button);
            if(i != mouseMap.end())
            {
                MouseFunction action = i->second;
                action(player, event.button.x, event.button.y);
            }
        }
    }

    // ...
}

Having a general event mapping system is a little more difficult. For most games, you might want to map key presses or releases, and mouse clicks. Mapping complicated, stateful actions like mouse dragging will take a bit more thought.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS