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

Author Topic: Wwedish vowels in sf::Event::TextEntered, problem  (Read 1772 times)

0 Members and 1 Guest are viewing this topic.

ingwik

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Wwedish vowels in sf::Event::TextEntered, problem
« on: October 07, 2011, 01:02:30 pm »
Environment
VC++ 2010 Express
Windows 7 enterprise 64
SFML 1.6

I have a simple hangman game going on, but I've run into a nasty problem. In Sweden, we have three vowels not used in the english language, but could be called Å/AO, Ä/AE and Ö/OE.
I have changed VC++ to use swedish language by adding

        locale swedish("swedish");
        locale::global(swedish);

in the main code.

The wovels are placed on the keyboard as key 1 (Ö) and 2 (Ä) right of "L" and the (Å) key above those two.

If I write this code, I'll get the swedish wovels, but all other keys appear too.

Code: [Select]
if (Event.Type == sf::Event::KeyPressed)
 {
 if ( Event.Type == sf::Event::TextEntered )
        cout << "Char= " << (char)Event.Text.Unicode <<endl;  
 }




If I use this code instead to sort the other keys away, I won't get the swedish vowels

Code: [Select]
if (Event.Type == sf::Event::KeyPressed)
 {
     if ( Event.Text.Unicode < 0x80 ) // it's printable
           {

 if ( Event.Type == sf::Event::TextEntered )
                         
        cout << "Char= " << (char)Event.Text.Unicode <<endl;    
           }
}


I assume the "Unicode < 0x80" is the culprit to the behaviour. I just can't figure out what to write instead. Is there a solution to this problem?

ChronicRat

  • Sr. Member
  • ****
  • Posts: 327
  • C++ programmer
    • View Profile
    • My blog
Wwedish vowels in sf::Event::TextEntered, problem
« Reply #1 on: October 07, 2011, 01:19:30 pm »
You can't convert unicode value (Uint32) by just typecasting to char. You have to use special functions wich are convert from UTF-32, like "wcstombs".

ingwik

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Wwedish vowels in sf::Event::TextEntered, problem
« Reply #2 on: October 07, 2011, 01:47:11 pm »
Sooo, just leaving it open and compare every character typed to a list with allowed character and store typed, allowed character in an array is the fastest hack, I assume? Then I don't have to typecast anything.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Wwedish vowels in sf::Event::TextEntered, problem
« Reply #3 on: October 07, 2011, 01:49:19 pm »
Is this really your code?? Obviously not, Event.Type cannot be both TextEntered and KeyPressed.

I'd like to see your real code please.

And... what do you want to do? I can't give you a solution if you don't tell me what you want to do with the characters.
Laurent Gomila - SFML developer

Haikarainen

  • Hero Member
  • *****
  • Posts: 545
    • MSN Messenger - fredrik.haikarainen@gmail.com
    • View Profile
    • Email
Wwedish vowels in sf::Event::TextEntered, problem
« Reply #4 on: October 07, 2011, 05:04:50 pm »
Dont really get what you want to do, but swedish characters work for me with this code (im just copypasting the textinput code from my games console)

Code: [Select]
if(Event.Type == sf::Event::TextEntered){
if(this->isDown()){
switch(Event.Key.Code){
case 8:
// backspace
this->RmChar();
break;
case 13:
// enter
if(strcmp(this->GetBuffer().c_str(), "") != 0){
this->Root->RunLine(this->GetBuffer());
}
this->ClearBuffer();
break;
case 9:
// tab
this->AutoComplete();
break;
default:
// or else
std::string tmp;
tmp+= static_cast<char>(Event.Text.Unicode);
this->AddChar(tmp);
break;

}
}
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Wwedish vowels in sf::Event::TextEntered, problem
« Reply #5 on: October 07, 2011, 06:06:31 pm »
Quote
swedish characters work for me with this code

Converting to char assumes that the current locale (or whatever will be used to interpret the resulting string) is a 8-bits subset of UTF-32. Which means ASCII, or Latin-1. So this is a very dangerous operation ;)
Laurent Gomila - SFML developer

ingwik

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Wwedish vowels in sf::Event::TextEntered, problem
« Reply #6 on: October 07, 2011, 09:49:24 pm »
Quote from: "Laurent"
Is this really your code?? Obviously not, Event.Type cannot be both TextEntered and KeyPressed.

I'd like to see your real code please.
 


Sad to say Mr Laurent, it was my original code. First, I didn't realize that TextEntered was an event type of its own but thought it was an event fired by key down. That code was buggy to say the least...

I did like this, in code and pseudo code to keep it short.
In this way, I' get the swedish wovels but reject anything but letters. I never used:
Code: [Select]
if ( Event.Text.Unicode < 0x80 ) // it's printable

Since I want a single word for hang man game (hardly more than 255 letters long :), it is sufficient for me.


lower2upper(char letterin); //= function to make an a char to an A char.
int i=0;
char tempchar;
char secretword[255];//the word you want to find out
char alphabet[30]{all the letters in the alphabet A-Ö and - for combined words};


if ( Event.Type == sf::Event::TextEntered)
 {
tempchar= ((char)Event.Text.Unicode;
tempchar = lower2upper(tempchar);
if (the charexists in the alphabet[]) //pseudocode
 {
 secretword = tempchar;
 i++
 }

}

 

anything