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

Author Topic: SFML2 Utf template is kinda confusing  (Read 3304 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
SFML2 Utf template is kinda confusing
« on: January 06, 2011, 11:09:28 pm »
Aight So I'm currently working on my engine where it is going to convert the event values into javascript values. And I were stopped at TextEntered. I wanted to convert the value given to a string in javascript but the v8::String seem to only support up to Utf-16 so I thought I could use SFML to convert it but when I reached the documentation page... it was all way too confusing.

Hoped you could help me out explaining how you use it?

V8 String Documentation: http://bespin.cz/~ondras/html/classv8_1_1String.html

You create a new instance trough the  v8::String::New methods.

*EDIT*
For now I did this, but I think I loose potential data(I usually don't work with these kind of stuff):
Code: [Select]
int Script::Converters::ConvertTextEnteredSignal(const SignalBase &aSignal, v8::Handle<v8::Value> *someArguments)
{
const TextEnteredSignal &signal = static_cast<const TextEnteredSignal &>(aSignal);

// <-- Where I try to convert the char but in a bad way -->
sf::String str = signal.GetChar(); // Returns the sf::Uint32
someArguments[0] = v8::String::New(str.ToAnsiString().c_str(), str.GetSize());
// <-- And here it ends -->

v8::Handle<v8::Object> windowWrapper = localWindowTemplate->NewInstance();
windowWrapper->SetInternalField(0, v8::External::New(signal.GetWindow()));
someArguments[1] = windowWrapper;
return 2;
}
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML2 Utf template is kinda confusing
« Reply #1 on: January 07, 2011, 09:11:43 am »
First, you don't need to use a sf::String (it doesn't provide functions for UTF conversions). You have to use the low-level interface, sf::Utf.

To convert to UTF-8:
Code: [Select]
char utf8[4]; // a single character may be encoded on up to 4 bytes in UTF-8
char* begin = &utf8[0];
char* end = sf::Utf8::Encode(event.TextEntered.Unicode, begin);
someArguments[0] = v8::String::New(utf8, begin - end);


UTF-16 is similar:
Code: [Select]
uint16_t utf16[2]; // a single character may be encoded on up to 2 16-bits elements in UTF-16
uint16_t* begin = &utf16[0];
uint16_t* end = sf::Utf16::Encode(event.TextEntered.Unicode, begin);
someArguments[0] = v8::String::New(utf16, begin - end);
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
SFML2 Utf template is kinda confusing
« Reply #2 on: January 07, 2011, 09:16:15 am »
Thanks, I'll add it when I get back home.

Just thinking, when you come to create tutorials for SFML2 will you need any help?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML2 Utf template is kinda confusing
« Reply #3 on: January 07, 2011, 09:19:52 am »
Quote
Just thinking, when you come to create tutorials for SFML2 will you need any help?

No, I prefer writing them myself. I'm sorry because it will take a long time, but I really want to do it myself ;)
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
SFML2 Utf template is kinda confusing
« Reply #4 on: January 07, 2011, 09:32:41 am »
NP :wink :
I just wanted to offer my help.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything