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

Author Topic: read input from file  (Read 2251 times)

0 Members and 1 Guest are viewing this topic.

short

  • Newbie
  • *
  • Posts: 9
    • View Profile
read input from file
« on: September 26, 2011, 04:06:41 am »
Hey there,

I was wondering if there was a way to convert all keyboard/button names from string to their enum.

I read in the tutorial that you can do a straight char representation -> sf::keyboard::key enum for characters/numbers, but what about things such as "Back."

In my situation, I'm reading key bindings from a file, such as this:

Code: [Select]
<?xml version="1.0" encoding="UTF-8" ?>
<Root>
  <Keyboard>
    <a action="MoveLeft"></a>
    <s action="MoveDown"></s>
    <d action="MoveRight"></d>
    <w action="MoveUp"></w>
    <F12 action="whatevs"></F12>
  </Keyboard>
</Root>


I use xml to parse this the way I want it, but everything comes out as a string. I need to do a conversion from, say std::string("f12") -> sf::keyboard::F12. I didn't see anything, and the only constant-time way I can think to solve this is to create an std::map with all the key/values defined within it (the map of course allowing constant time lookup). This would allow me to do something such as return _myMap.at("F12") and have a sf::keyboard::key returned, or something similar.

This would however require a map with every keyboard / mouse "button/click" in it. I don't mind doing this, if there is no other way I have missed.

Thanks!!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
read input from file
« Reply #1 on: September 26, 2011, 06:00:36 am »
There's no other way, you must write your own function.

Quote
the map of course allowing constant time lookup

Searching in a map is O(ln(N)) ;)
Laurent Gomila - SFML developer

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
read input from file
« Reply #2 on: September 26, 2011, 10:04:23 am »
Quote from: "Laurent"
Quote
the map of course allowing constant time lookup

Searching in a map is O(ln(N)) ;)


Yeah, it's a tree search and pretty fast but it's not constant time. If you want constant time lookup then there's always a hash map (std::tr1::unordered_map or boost::unordered_map).
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: read input from file
« Reply #3 on: September 26, 2011, 11:20:44 am »
Quote from: "short"
I was wondering if there was a way to convert all keyboard/button names from string to their enum.
Since this is an often-needed feature for dynamic key assignment, I plan to add this in Thor. I have already gotten requests about serializations, a "bimap" from keys to strings and back is at least a start.

So if you can wait some time, we don't have to implement it both ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Silvah

  • Guest
read input from file
« Reply #4 on: September 26, 2011, 04:21:30 pm »
Quote from: "MorleyDev"
If you want constant time lookup then there's always a hash map (std::tr1::unordered_map or boost::unordered_map).
Standard hash maps are constant time only if you're extremely lucky. Collision are virtually guaranteed to happen, so the actual complexity is almost-but-not-really-constant.

Are the keys immutable? If they are, I'd consider using a perfect hash function, there are programs like gperf that generate them.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: read input from file
« Reply #5 on: September 30, 2011, 08:10:20 pm »
Quote from: "Nexus"
Since this is an often-needed feature for dynamic key assignment, I plan to add this in Thor.
Done.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: