如何在 SFML 中使用俄语文本?
How to use russian text in SFML?
我试图在我的 SFML 应用程序中打印俄语文本,但我只收到奇怪的符号。我知道,我能做到:
text.setString(L"blabla")
但是,我想从文件中获取文本。我在我的项目中使用 Unicode。
截图:
为了兼容性和简单性,您可以使用 UTF8 来存储文件。读取文件,然后转换 UTF8 字符串。如果您将文件保存在记事本等中,请确保它以 UTF8 格式保存。如果使用 UTF16 文件,您将 运行 陷入额外的困境。 ANSI 格式已过时,不推荐使用。
#include <fstream>
#include <string>
#include <sstream>
...
std::ofstream fout(L"file.txt"); //Visual Studio allows wide char file name here
fout << u8"Test ελληνικά...";
fout.close();
std::ifstream fin(L"file.txt");
std::stringstream ss;
ss << fin.rdbuf();
std::string utf8 = ss.str();
sf::String str = sf::String::fromUtf8(utf8.begin(), utf8.end());
我试图在我的 SFML 应用程序中打印俄语文本,但我只收到奇怪的符号。我知道,我能做到:
text.setString(L"blabla")
但是,我想从文件中获取文本。我在我的项目中使用 Unicode。 截图:
为了兼容性和简单性,您可以使用 UTF8 来存储文件。读取文件,然后转换 UTF8 字符串。如果您将文件保存在记事本等中,请确保它以 UTF8 格式保存。如果使用 UTF16 文件,您将 运行 陷入额外的困境。 ANSI 格式已过时,不推荐使用。
#include <fstream>
#include <string>
#include <sstream>
...
std::ofstream fout(L"file.txt"); //Visual Studio allows wide char file name here
fout << u8"Test ελληνικά...";
fout.close();
std::ifstream fin(L"file.txt");
std::stringstream ss;
ss << fin.rdbuf();
std::string utf8 = ss.str();
sf::String str = sf::String::fromUtf8(utf8.begin(), utf8.end());