1

I recently switched from SFML to libtcod for my rougelike, because i find libtcod more convinient for those kinds of games. One problem i cant find any solution for is how to store keyboard bound functions.

In SFML i could do this for my keyfunctions

std::map<sf::Keyboard::Key, std::function<void()>> keyboardCommands

In libtcod there's the TCOD_Key_t struct (that would replace sf::Keyboard::Key in the map) which works for special keys like F-keys, Esc, etc. but "nothing" for letters.

Is there a way with libtcod i could put all my keyboard functions in one map?

val
  • 729
  • 6
  • 19

1 Answers1

1

DISCLAIMER: I have never used this library!

I found this:

Any other (printable) key :
TCODK_CHAR

So when you receive TCODK_CHAR, you look for char c; from this struct:

typedef struct {
    TCOD_keycode_t vk;
    char c;
    unsigned pressed : 1;
    unsigned lalt : 1;
    unsigned lctrl : 1;
    unsigned ralt : 1;
    unsigned rctrl : 1;
    unsigned shift : 1;
} TCOD_key_t;

I believe it will contain character pressed.

UPDATE

It's a C library, so intended usage is probably was something like: (key here is of type TCOD_key_t)

switch(key.vk) {
    case TCODK_UP : onUp(); break;
    case TCODK_CHAR: onChar(key.c); break
    default: break;
}

With map you could do something like:

if(TCODK_CHAR == key.vk)
    onChar(key.c);
else
    specialCommands[key.vk]();

(where specialCommands is std::map<TCOD_keycode_t, std::function<...> > )

You might put char callbacks tinto the map, however, if some of the members of TCOD_keycode_t enumeration will match character code, you will have a key collision (i.e. if, hypothetically TCODK_UP has value 65, it will be the same key as char 'a' (ASCII code 65)).

You could also have another map or array that maps characters (key.c) to different functions.

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • How would i put that into one map? Special keys would need TCOD_keycode_t and characters would need c. What i can think about is that i'd have a map for TCOD_keycode_t-s and one for chars. and in my event cheking, check if c is empty, and then check the appropriate map. – val Dec 15 '15 at 23:29
  • @val added dome more info – Ivan Aksamentov - Drop Dec 15 '15 at 23:43