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

Author Topic: Save Inputs to File  (Read 2324 times)

0 Members and 1 Guest are viewing this topic.

Chumper

  • Newbie
  • *
  • Posts: 2
    • ICQ Messenger - 285551970
    • View Profile
Save Inputs to File
« 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?

Code: [Select]

[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

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Save Inputs to File
« Reply #1 on: November 28, 2010, 09:50:10 pm »
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:
Code: [Select]
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.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Chumper

  • Newbie
  • *
  • Posts: 2
    • ICQ Messenger - 285551970
    • View Profile
Save Inputs to File
« Reply #2 on: November 28, 2010, 10:11:41 pm »
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.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Save Inputs to File
« Reply #3 on: November 28, 2010, 11:18:29 pm »
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:

Code: [Select]

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.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio