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

Author Topic: Convert sf::UInt32 (event.text.unicode) to std::string or something I can use  (Read 5244 times)

0 Members and 1 Guest are viewing this topic.

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
Hey, trying to convert the event.text.unicode value into something printable, preferably std::string related...

(SFML 2.0)

http://www.sfml-dev.org/documentation/2.0/structsf_1_1Event_1_1TextEvent.php
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Utf_3_0132_01_4.php#ae274c5057c343ba353253bd2c531a283


    std::string str = "";
    sf::Utf32::encodeAnsi(event->text.unicode, std::back_inserter(str), 'e');
    std::cout << str << std::endl;

However, the codepoints seem to be way off for my locale or something. e.g. i slam on A-Z and i get out blank spaces. I hit the forwardslash (/) and it returns 4. I hit enter and it returns ":" (colon).

what's up?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Hi

Could you provide a complete and minimal example that reproduces the problem?

And what is the name of the locale that you're using? (std::cout << std::locale().name())
Laurent Gomila - SFML developer

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
Hi

Could you provide a complete and minimal example that reproduces the problem?

And what is the name of the locale that you're using? (std::cout << std::locale().name())

Uhh. everything is kind of bundled and my project is a mess right now :D

Just that code above *should work*. I could try to make a new example, using an sfml test prog..

Says locale is C. Platform is linux fyi.

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
Hm. well that is interesting. made a barebones app to reproduce, unable to. Wonder what's going on with my application now..

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
hmm..tried eliminating most of my game.cpp code and it still happens only in my project. will keep you posted

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
created branch 'broken' as a proof of what's happening:

detached some build deps as well so you can hopefully easily build it. just run cmake at root level

https://gitorious.org/buildarrhea/buildarrhea/trees/broken

just press a button when the window pops up...well, hit many buttons...and you'll see the unicode char outputted. for me the locale seems to be off or something, because I can hit A-Z and i get spaces, and for other symbols i get characters that aren't what i am using..
« Last Edit: November 01, 2012, 01:33:16 am by Predator106 »

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
good news everyone!

debugged it a bit, figured out that it seems to be caused by retrieving the unicode output in the event handling code.

take a look (latest commit vs. 1 prior. the new one is fixed and working, the old commit is where it is broken).

https://gitorious.org/buildarrhea/buildarrhea/commit/0482618280bd5bf00cfea9907b206682de5975a4


Full file where the bug is witnessed, and line it is seen on:
https://gitorious.org/buildarrhea/buildarrhea/blobs/broken/src/game.cpp#line189

NOTE: this doesn't make any fucking sense to me why this would affect anything ;-)
« Last Edit: November 01, 2012, 02:00:45 am by Predator106 »

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
You should check if the event.type was sf::Event::TextEntered before using event.text.unicode or it'll contain garbage.
Back to C++ gamedev with SFML in May 2023

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
wow. that simple, eh? for such a headache of a problem that i had, ahha. thanks.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
This is stressed so much in the tutorial that Laurent should have started French Baguette Massacre by this point :
Quote
The valid member is the one that matches the event type, for example event.key for a KeyPressed event. Trying to read any other member will result in an undefined behaviour (most likely: random or invalid values). So never try to use an event member that doesn't match its type.
(...)
Read the above paragraph once again and make sure that it's printed in your head, the sf::Event union causes too many problems to inadvertent programmers.
Then again in documentation of event class, to make it even more hilarious the example mentions event.text having undefined values:
Quote
For example, if you received a KeyPressed event, then you must read the event.key member, all other members such as event.MouseMove or event.text will have undefined values.
« Last Edit: November 01, 2012, 02:30:49 am by FRex »
Back to C++ gamedev with SFML in May 2023

Predator106

  • Newbie
  • *
  • Posts: 43
    • View Profile
can we add a flashing red banner for this? :-)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Such paragraphs are already marked as "important", and will have a red background in the next version of the website.

Quote
French Baguette Massacre
What's that? ;D
Laurent Gomila - SFML developer