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

Author Topic: [sf::Event::Text] Problem with national characters.  (Read 4290 times)

0 Members and 2 Guests are viewing this topic.

rapide

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
[sf::Event::Text] Problem with national characters.
« on: August 01, 2012, 12:58:45 pm »
Hello!

I have a little problem. I want do display my text from input. Look at code:

void WriteDisplayer::writing()
{
  if(isActive)
  {
    sf::Event currentEvent = window->getEvent();

    if (currentEvent.Type == sf::Event::TextEntered && window->checkEventUniqueness())
    {
      if (currentEvent.Text.Unicode < 128)
        mainStr.push_back(currentEvent.Text.Unicode);
    }
    else if(currentEvent.Key.Code == sf::Key::Code::Back && window->checkEventUniqueness())
    {
      if(!mainStr.empty())
        mainStr.pop_back();
    }

    sfStr.SetText(mainStr);
  }
}

It works great but I want to get polish characters like ąĄśŚćĆżŻźŹóÓęĘłŁ etc. It should be easy but it don't.
I tryed to write that characters and check value of currentEvent.Text.Unicode by debugger but when I changed the condition then program was writing out wrong characters. What's going on ? :<

For example ...  I chose 'ł' but it displayed 'B'
« Last Edit: August 01, 2012, 01:02:33 pm by rapide »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [sf::Event::Text] Problem with national characters.
« Reply #1 on: August 01, 2012, 01:20:03 pm »
Could you write a complete and minimal code that reproduces the problem? This way we can see how you store and draw your string, and we can test it if nothing seems to be wrong.
Laurent Gomila - SFML developer

rapide

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [sf::Event::Text] Problem with national characters.
« Reply #2 on: August 01, 2012, 01:36:13 pm »
Here you go...

// rapide's development
#pragma once
#include "UpgradedSprite.h"
#include "WindowMenager.h"
#include "ImageStorage.h"

// *******************************************************************************************
// ********************************** class WriteDisplayer ***********************************
// *******************************************************************************************
// Class which is console to get input.
class WriteDisplayer
{
// ------------------------------------------ Variables ------------------------------------------
private:
  WindowMenager* window;                             // Pointer to the main window.
  UpgradedSprite console;                            // Sprite with the screener.
  std::string mainStr;                              // String with text.
  sf::String sfStr;                                  // String which will be display on screen.
  float positionX;                                   // Position X.
  float positionY;                                   // Position Y.
  bool isActive;

// --------------------------------- Constructors and destructor ---------------------------------
public:
  WriteDisplayer(WindowMenager* argWindow, float fontSize, unsigned int fontIndex,
    unsigned int consoleIndex, ImageStorage *images);
  ~WriteDisplayer() {}

// --------------------------------------- Standard methods --------------------------------------
public:
  bool checkState();
  // Function for checking if displayer is active now.
  // Returns: True if write displayer is active.

  void writing();
  // Function for checking events and describe them for charakters.
  // Returns: Void.

  void clear();
  // Function for clearing displayer's console and main string.
  // Returns: Void.

  float getWidth();
  // Function for getting displayer's width.
  // Returns: Width of displayer.

  float getHeight();
  // Function for getting displayer's height.
  // Returns: Height of displayer.

  std::string getString() const;
  // Function for getting main string from displayer.
  // Returns: Main string.

  void setPosition(float posX, float posY);
  // Function for setting displayer's position.
  // Returns: Void.

  void updateWordsPosition();
  // Function for updating words position because when we adding more characters then rectangle of sf::string is bigger.
  // Returns: Void.

  void setStringAtBegin(std::string& argStr);
  // Function for setting main string at begin.
  // 1st argument: Reference to string.
  // Returns: Void.

  void draw();
  // Function for drawing write displayer.
  // Returns: Void.
};

// Inside *.cpp
#include "WriteDisplayer.h"


// *******************************************************************************************
// ***************************** Constructors and destructor *********************************
// *******************************************************************************************

// ---------------------------------------- Constructor ------------------------------------------
WriteDisplayer::WriteDisplayer(WindowMenager* argWindow, float sizeFont, unsigned int fontIndex,
  unsigned int consoleIndex, ImageStorage* images) : window(argWindow)
{
  console.SetImage(*images->getImage(consoleIndex));
  sfStr.SetFont(*images->getFont(fontIndex));
  sfStr.SetColor(sf::Color::Black);
  sfStr.SetSize(sizeFont);
  isActive = false;
}

// *******************************************************************************************
// ************************************** writing() ******************************************
// *******************************************************************************************
void WriteDisplayer::writing()
{
  if(isActive)
  {
    sf::Event currentEvent = window->getEvent();

    if (currentEvent.Type == sf::Event::TextEntered && window->checkEventUniqueness())
    {
      if (currentEvent.Text.Unicode < 128 || currentEvent.Text.Unicode == 322)  ///-> 322 == '&#322;' ?
        mainStr.push_back(currentEvent.Text.Unicode);
    }
    else if(currentEvent.Key.Code == sf::Key::Code::Back && window->checkEventUniqueness())
    {
      if(!mainStr.empty())
        mainStr.pop_back();
    }
    sfStr.SetText(mainStr);
  }
}

// *******************************************************************************************
// *************************************** draw() ********************************************
// *******************************************************************************************
void WriteDisplayer::draw()
{
  window->draw(console);
  updateWordsPosition();
  window->draw(sfStr);
}

//...

// This is from ImageStorage which contains vector of pointers to the sf::Font.
// ----------------------------------------- loadFontToVector ----------------------------------------
void ImageStorage::loadFontToVector(char* filePath)
{
  sf::Font* temporaryFont = new sf::Font;
  temporaryFont->LoadFromFile(filePath, 30 * 3, polishCharakters());

  fontsContent.push_back(temporaryFont);
}

// ------------------------------------- polishCharakters ------------------------------------
const sf::Unicode::Text ImageStorage::polishCharakters()
{
  std::wstring cPL;
  cPL = L"abcdefghijklmnopqrstuvwxyz";
  cPL += L"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  cPL += L"0123456789";
  cPL += L"&#261;&#263;&#281;&#322;&#324;ó&#347;&#378;&#380;";
  cPL += L"&#260;&#262;&#280;&#321;&#323;Ó&#346;&#377;&#379;";
  cPL += L".,\\/'<>;:[]{}|+=-_()*&^%$#@!~\" \n\t";
  return cPL;
}

// ------------------------------------------- getFont -------------------------------------------
sf::Font* ImageStorage::getFont(unsigned int index) const
{
  return fontsContent[index];
}

I hope that it will brighten your little concept.
« Last Edit: August 01, 2012, 01:50:33 pm by rapide »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: [sf::Event::Text] Problem with national characters.
« Reply #3 on: August 01, 2012, 02:23:24 pm »
That's not a minimal example. ::)
A minimal example would for example be a main function, which loads a font and displays the text. If that doesn't reproduce the problem then you would soon recognize that the problem was somewhere in your code handeling. On the other hand if it still doesn't work, then it might be some SFML related thing or wrong handeling of unicode characters.

Anyways does the font you're loading support those characters?

Quote
...but when I changed the condition...
How did you change it?

What compiler and SFML version do you use?

Please keep this post in mind, when writing a question...
« Last Edit: August 01, 2012, 02:25:09 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

rapide

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [sf::Event::Text] Problem with national characters.
« Reply #4 on: August 01, 2012, 02:54:08 pm »

Anyways does the font you're loading support those characters?

Quote
...but when I changed the condition...
How did you change it?

What compiler and SFML version do you use?

Please keep this post in mind, when writing a question...

You wouldn't understand my main function without other classes describes. My mechanics of those class works great and displaying other characters like a, b, c, d... etc but I can't describe polish characters in those if, here:

 if (currentEvent.Text.Unicode < 128 || currentEvent.Text.Unicode == 322)  ///-> 322 == '&#38;#322;' ?
        mainStr.push_back(currentEvent.Text.Unicode);

I pressed key 'ł' and debugger showed me that currentEvent.Text.Unicode contains 322. After I added new condition (means: currentEvent.Text.Unicode == 322) my 'if' accept condition but currentEvent.Text.Unicode contains 'B' character. I use Visual Studio 10 and SFML 1.6. 
« Last Edit: August 01, 2012, 02:57:57 pm by rapide »

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: [sf::Event::Text] Problem with national characters.
« Reply #5 on: August 22, 2012, 06:51:09 pm »
What he means is that you should write a new main function that makes the problem show up, with as little code as possible.