SFML и C++ Уроки \ Разработка игр › Форумы › SFML System, Window и другие вопросы › Вывод кириллицы › Ответ в теме: Вывод кириллицы
Я проблему с русским текстом решил так:
Весь текст держу как wchar_t и всё норм.
C++
1 2 3 4 5 6 7 8 |
wchar_t* TextTitle = L"Русские буквы"; // Из wchar_t* в sf:String переводится так: sf::String sfStr = sf::String(TextTitle); sf::Text text; text.setString(sf::String(TextTitle)); // Для других случаев у меня есть StringHelper: std::string stdStr = StringHelper::WideStringToAnsi(TextTitle); text.setString(StringHelper::WideStringToAnsi(TextTitle) + " - v0.1"); |
Сам StringHelper выглядит так:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
class StringHelper { public: static wchar_t* ToWCHAR(const char *c, UINT CodePage = CP_ACP) { static wchar_t* result; int nChars = MultiByteToWideChar(CodePage, 0, c, -1, NULL, 0); result = new WCHAR[nChars]; MultiByteToWideChar(CodePage, 0, c, -1, (LPWSTR)result, nChars); return result; } static const char* ToCHAR(wchar_t *c, UINT CodePage = CP_ACP) { static char* result; int nChars = WideCharToMultiByte(CodePage, 0, c, -1, NULL, 0, NULL, NULL); result = new CHAR[nChars]; WideCharToMultiByte(CodePage, 0, c, -1, (LPSTR)result, nChars, NULL, NULL); return result; } static std::wstring AnsiStringToWide(std::string const &Str, UINT CodePage = CP_ACP) { DWORD const BuffSize = MultiByteToWideChar(CodePage, 0, Str.c_str(), -1, NULL, 0); if (!BuffSize) return NULL; std::vector<wchar_t> Buffer; Buffer.resize(BuffSize); if (!MultiByteToWideChar(CodePage, 0, Str.c_str(), -1, &Buffer[0], BuffSize)) return NULL; return (&Buffer[0]); } static std::string WideStringToAnsi(std::wstring const &Str, UINT CodePage = CP_ACP) { DWORD const BuffSize = WideCharToMultiByte(CodePage, 0, Str.c_str(), -1, NULL, 0, NULL, NULL); if (!BuffSize) return NULL; std::vector<char> Buffer; Buffer.resize(BuffSize); if (!WideCharToMultiByte(CodePage, 0, Str.c_str(), -1, &Buffer[0], BuffSize, NULL, NULL)) return NULL; return (&Buffer[0]); } }; |