Hi all,
I've been trying to develop a text-editor in sfml based on the built-in text renderer, and I ran into this issue: I can't display uft-8 characters like őúűá etc. here's my code:
#include "keyboard.h"
void keyboard::process_keypress(sf::Key::Code keycode) //if a key is pressed then let's handle it
{
keys[keycode] = true;
if (keys[sf::Key::Back] == true)
{
//backslash pressed
if (the_string.length() > 0)
{
if (!save_state)
{
if (the_pos > 0)
{
the_string = the_string.replace(the_pos - 1, 1, "");
the_pos--;
}
}
else
{
if (the_pos > save_pos)
{
the_string = the_string.replace(the_pos - 1, 1, "");
the_pos--;
}
}
}
}
if (keys[sf::Key::Delete] == true)
{
//delete pressed
if (the_pos < the_string.length())
{
the_string = the_string.replace(the_pos, 1, "");
}
}
if (keys[sf::Key::Return] == true)
{
//enter pressed
the_string.insert(the_pos, 1, '\n');
the_pos += 1;
if (save_state)
{
std::string tmp_str = the_string.substr(the_string.substr(0, the_string.length() - 1).rfind("\n") + 1);
tmp_str = tmp_str.substr(0, tmp_str.length() - 1);
std::fstream f;
f.open(tmp_str.c_str(), std::fstream::out);
if (f.is_open())
{
f << write_out;
f.close();
the_string = write_out + "\nText successfully saved!\n";
the_pos = the_string.length();
}
else
{
the_string = write_out + "\nCouldn't save the text!\n";
the_pos = the_string.length();
}
save_state = false;
}
}
if (keys[sf::Key::Escape] == true)
{
//escape pressed
the_context.Close();
}
if (keys[sf::Key::Left] == true)
{
if (!save_state)
{
if (the_pos > 0)
{
the_pos--;
}
}
else
{
if (the_pos > save_pos)
{
the_pos--;
}
}
}
if (keys[sf::Key::Right] == true)
{
if (the_pos < the_string.length())
{
the_pos++;
}
}
if (keys[sf::Key::RControl] == true || keys[sf::Key::LControl] == true)
{
if (keys[sf::Key::S] == true)
{
write_out = the_string;
the_string += "\nPlease enter the filename to save the text to: \n";
the_pos = the_string.length();
save_pos = the_pos;
save_state = true;
is_not_text = true;
}
}
}
void keyboard::process_keyrelease(sf::Key::Code keycode)
{
keys[keycode] = false;
}
void keyboard::process_text_input(const unsigned int& char_code)
{
for (int c = 0; c < not_text.size(); c++)
{
if (keys[not_text[c]] == true)
{
is_not_text = true;
}
}
if (!is_not_text) //if it is text
{
the_string.insert(the_pos, 1, (wchar_t)char_code); //I think the problem lies here: I convert the incoming unsigned integer to a wchar_t which is supposed to be utf-8 right?
the_pos++;
}
is_not_text = false;
}
I basically pass the character code to the process_text_input(...), in which I convert it to a wchar_t and insert it at the current position into the string I display.
So how can I render the unicode characters, what did I do wrong?
Best regards,
Yours3!f