Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Better way to get sf::Keyboard:: codes  (Read 4650 times)

0 Members and 1 Guest are viewing this topic.

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Better way to get sf::Keyboard:: codes
« 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Better way to get sf::Keyboard:: codes
« Reply #1 on: August 24, 2012, 09:55:22 pm »
Hi, I implemented conversion functions in my library Thor:
#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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Better way to get sf::Keyboard:: codes
« Reply #2 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 ^.^

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Better way to get sf::Keyboard:: codes
« Reply #3 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 for that. ;)
If you have it figured out once it will be an easy and very usefull tool.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/