SFML community forums

Help => System => Topic started by: legacyblade on August 24, 2012, 08:13:36 pm

Title: Better way to get sf::Keyboard:: codes
Post by: legacyblade on August 24, 2012, 08:13:36 pm
Hello. I'm working on the input system for my game. And I've run into a problem. I'm storing the keybindings for each "player" in separate ini files. The whole system is actually working out pretty well. Except that telling the program what sf::Keyboard:: thing to look for is a huge pain. I get my data from the INI file in the form of strings. Right now, I have a function that looks up that string and returns the proper sf::Keyboard::Key.

It looks something like this (except it has an entry for every keyboard key x.x)

                if ( stringholder.at(i) == "A" ) {
                        results.push_back(sf::Keyboard::A);
                } else if ( stringholder.at(i) == "B" ) {
                        results.push_back(sf::Keyboard::B);
                } else if ( stringholder.at(i) == "C" ) {
                        results.push_back(sf::Keyboard::C);
                } else if ( stringholder.at(i) == "D" ) {
                        results.push_back(sf::Keyboard::D);
                } else if ( stringholder.at(i) == "E" ) {
                        results.push_back(sf::Keyboard::E);
                } else if ( stringholder.at(i) == "F" ) {
                        results.push_back(sf::Keyboard::F);
                } else if ( stringholder.at(i) == "G" ) {
                        results.push_back(sf::Keyboard::G);

 

(A switch statement would be a little less messy, but C++ doesn't allow those with strings. x.x Also, I'm using vectors so that I can have multiple keys bound to the same action.)

Is there a better way to do this? I can change what strings are in the INI file. I can even try to find a way to simplify them to a single letter so I can use a switch statement. But the problem is that that still requires massive amounts of conditions. And it's easy to create tiny errors.

Is there a better way to do this? Is there a way to lookup a keyboard key by something and return it, rather than explicitly stating which keyboard key is to be used for a specific string?

Thanks for reading this post. Any advice you can offer would be great.
Title: Re: Better way to get sf::Keyboard:: codes
Post by: Nexus on August 24, 2012, 09:55:22 pm
Hi, I implemented conversion functions in my library Thor (http://www.bromeon.ch/libraries/thor/v2.0/doc/_input_names_8hpp.html):
#include <Thor/Events/InputNames.hpp>

...
sf::Keyboard::Key key = thor::ToKeyboardKey(stringholder[i]);
results.push_back(key);

By the way, I would use std::vector's operator[] instead of at(), otherwise you have useless checks. Wrong indices are almost always logic errors that are already caught by assertions in operator[], exception cannot be meaningfully handled.
Title: Re: Better way to get sf::Keyboard:: codes
Post by: legacyblade on August 24, 2012, 10:10:45 pm
Sweet. Thanks. I'll try and figure out how to use cMake. I didn't have any luck with it when installing SFML, so I just download the already compiled versions.

This is a very useful resource. Thank you so much ^.^
Title: Re: Better way to get sf::Keyboard:: codes
Post by: eXpl0it3r on August 24, 2012, 10:28:15 pm
Sweet. Thanks. I'll try and figure out how to use cMake. I didn't have any luck with it when installing SFML, so I just download the already compiled versions.
There's quite a good official tutorial (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-installation.html) for that. ;)
If you have it figured out once it will be an easy and very usefull tool.