SFML community forums
Help => Window => Topic started by: Chumper on November 28, 2010, 09:42:41 pm
-
Hi everybody,
I am currently writing a small Game with Sfml 1.6.
It is a Clone of Ipcurve.
I managed dynamic Keybindings, but at the Moment they are still hardcoded.
That's why I want to save this Keybindings to a .ini-File.
But how can I do that?
I have used this tutorial for the Keybindings:
http://www.sfml-dev.org/wiki/en/tutoriels/bindevents_en
but how can i save that to an .ini-File like this?
[PLAYER1]
up=w
left=a
right=d
R=255
G=0
B=0
in short:
How can I get from
a string "w" to Event::Key::W?
And how can I save Event::Key::W to a File?
Cheers Nils
-
If you look here:
http://sfml-dev.org/documentation/2.0/group__window.htm
The key enums are actually the character that they represent. so by writing:
char key = static_cast< char >( keyCode );
we convert the key code into the character. Though keep in mind that this only works with the letters and numbers. In order to cover the entire keyboard I would instead use numbers instead of characters. Just to keep it easy.
-
So there is no other chance to make it easier than to enumerate the whole Keyboard and transfer the string "w" to Event::Key::W.
Your Idea would be to represent the whole keyboard with numbers, but that is still a lot of work, just think about the numblock, the F-Keys and the normal Controlkeys like Shift, Control, Alt, Left, Down, etc...
Anyways, thanks for your thoughts, i think i will use the numbers.
-
The enum is already a number so it isn't a lot of work for your side. If you really want it to be text that is printed to the configuration file then you could solve it by some fancy work with an array.
Anyway to iterate trough the enums you do like this:
for( sf::Key::Code code = sf::Key::A; code <= sf::Key::Z; code++ )
{
// Code
}
for( sf::Key::Code code = sf::Key::Num0; code <= sf::Key::Num9; code++ )
{
// Code
}
for( sf::Key::Code code = sf::Key::Escape; code < sf::Key::Count; code++ )
{
// Code
}
Depending on the compiler, it might complain about adding 1 to your enum. Visual Studio don't.