SFML community forums

Help => System => Topic started by: Glocke on September 30, 2014, 09:59:18 am

Title: [SOLVED] Save/Load keybindings
Post by: Glocke on September 30, 2014, 09:59:18 am
Hi,

I'm using ini-files to save and load keybindings. Using sf::Joystick is quite easy: I read/write the buttons' uint's. Handling axis is nearly the same, but I'm using a small function mapping "x" to sf::Joystick::Axis::X and so on. But doing this for keyboard (e.g. "w" to sf::Keyboard::W) isn't fun at all. Is there a clever way to do this? I want to avoid a large switch considering the entire sf::Keyboard enum. Maybe there's a way?
Title: AW: Save/Load keybindings
Post by: eXpl0it3r on September 30, 2014, 10:01:42 am
You could take a look at Thor's action system.
Title: Re: Save/Load keybindings
Post by: Nexus on September 30, 2014, 10:23:15 am
More precisely, the Thor header InputNames.hpp (http://www.bromeon.ch/libraries/thor/v2.0/doc/_input_names_8hpp.html) will help you :)
Title: Re: Save/Load keybindings
Post by: Glocke on September 30, 2014, 10:31:42 am
Well, (for me) it seems like Thor creates a hashmap to map sf::Keyboard::Key to std::string and vice versa..

What's about the enum values of sf::Keyboard::Key. Afaik every C-style enum is based on int-values (but aliased by the enum). So the sf::Keyboard::Key's should use int values at the internals. Are these values deterministic? The first two enum elements explicitly carry the values -1 and 0 - this might imply B as 1, C as 2 and so on. Testing this (means: converting to integer and printing to stdout) confirmed this - at least for the first 4-5 elements.
But these values should be deterministic, because of the two initial values, aren't they?
Title: Re: Save/Load keybindings
Post by: Nexus on September 30, 2014, 11:14:49 am
If deterministic means that every compiler translates the enumerator's values to the same integer: yes

If deterministic means that you can serialize the numbers and future versions will still be compatible: no
That's why you should prefer string serialization. It can also be read and written by humans.
Title: Re: Save/Load keybindings
Post by: Glocke on September 30, 2014, 11:22:12 am
If deterministic means that you can serialize the numbers and future versions will still be compatible: no
That's why you should prefer string serialization. It can also be read and written by humans.

Ok thanks. I guess I'll adapt Thor's mapping approach in the near future.
Title: Re: Save/Load keybindings
Post by: Nexus on September 30, 2014, 11:24:41 am
You could also just use it ;)

If you don't want to install and link Thor, you can also copy the files (just keep the license). The advantage is, when SFML supports more keys, I will adapt Thor correspondingly, so your code works automatically with more keys.
Title: Re: Save/Load keybindings
Post by: Glocke on September 30, 2014, 11:36:31 am
You could also just use it ;)

At the moment I don't want to add extra dependencies to my project. But I thinkg if I'll need more Thor-features later, I'll use it ;)

If you don't want to install and link Thor, you can also copy the files (just keep the license). The advantage is, when SFML supports more keys, I will adapt Thor correspondingly, so your code works automatically with more keys.

 8)
Title: Re: Save/Load keybindings
Post by: Rosme on October 10, 2014, 12:37:20 am
You could also just use it ;)
At the moment I don't want to add extra dependencies to my project. But I thinkg if I'll need more Thor-features later, I'll use it ;)
Considering the license and how easy it is to use, the reason of adding extra dependencies shouldn't be. You'll find that Thor offers a lot of thing that can be usefull. This looks to me like a case of NIH (http://en.wikipedia.org/wiki/Not_invented_here).
Title: Re: Save/Load keybindings
Post by: Glocke on October 10, 2014, 09:16:09 am
Well, that's why I finally added Thor to my project to load keybindings :)