The only difficulty left is SAVING this choice and reloading it when
your app loads. The nearest I can come to this being easy is what
you feared: a long list of cases… Is there an easier way to get a
keysym from a character and vice-versa?
Is there any reason you can’t just write out the keysym?
[keyboard_settings]
jump=182
fire=188
(so you read your config file in as a text file, convert the values to
ints and store them in an array, and then in your mainloop do something
like…)
typedef enum {
JUMP,
FIRE,
// etc.
total_actions
} Actions;
// This gets filled in with the data from the config file, so
// actions[JUMP] will be the keysym for jumping, etc…
static SDL_keysym actions[total_actions];
//checking for events...
case SDL_KEYDOWN:
if (event.key.keysym.sym == actions[JUMP])
make_player_jump();
else if (event.key.keysym.sym == actions[FIRE])
make_player_shoot();
else {
// not a key player has configured to do something, ignore.
}
break;
(or something like that.)
You could go a step further and have an array of structs that contain a
function pointer:
// however you want to set this up...
actions[JUMP].sym = 188;
actions[JUMP].func = make_player_jump;
So your event loop looks like this:
//checking for events...
case SDL_KEYDOWN:
for (int i = 0; i < total_actions; i++) {
if actions[i].sym == event.key.keysym.sym)
actions[i].func();
}
break;
Extra credit for restructuring the data to not need a for loop on each
keypress.
(but really, the big block of if-statements are probably just as good.
Even in something with as many buttons as a flight simulator, you aren’t
going to check that many actions.)
–ryan.