From da4b99e98392aee91fce23139321db34334b90fe Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Wed, 3 Nov 2021 12:53:04 +0300 Subject: [PATCH] Added Unicode support in high score dialog. Cherry picked form https://github.com/Raymai97/SpaceCadetPinball/commit/ecea9c812aefa918fa7375273353eabe9e9dd27f. --- SpaceCadetPinball/high_score.cpp | 79 ++++++++++++++++---------------- SpaceCadetPinball/high_score.h | 10 ++-- SpaceCadetPinball/options.cpp | 37 +++++++++++++++ SpaceCadetPinball/options.h | 2 + SpaceCadetPinball/pb.cpp | 8 ++-- 5 files changed, 88 insertions(+), 48 deletions(-) diff --git a/SpaceCadetPinball/high_score.cpp b/SpaceCadetPinball/high_score.cpp index 5896106..0eac6b7 100644 --- a/SpaceCadetPinball/high_score.cpp +++ b/SpaceCadetPinball/high_score.cpp @@ -10,7 +10,7 @@ int high_score::dlg_enter_name; int high_score::dlg_score; int high_score::dlg_position; -LPCSTR high_score::default_name; +LPCWSTR high_score::default_name; high_score_struct* high_score::dlg_hst; winhelp_entry high_score::help[21] @@ -40,7 +40,8 @@ winhelp_entry high_score::help[21] int high_score::read(high_score_struct* table, int* ptrToSmth) { - char Buffer[20]; + char scoreBuffer[20]; + wchar_t nameBuffer[20]; int scoreSum = 0; clear_table(table); @@ -52,18 +53,17 @@ int high_score::read(high_score_struct* table, int* ptrToSmth) for (auto position = 0; position < 5; ++position) { auto tablePtr = &table[position]; - _itoa_s(position, Buffer, 10); - lstrcatA(Buffer, ".Name"); - options::get_string(optPath, Buffer, buf1, "", 32); - buf1[32] = 0; - lstrcpyA(tablePtr->Name, buf1); - _itoa_s(position, Buffer, 10); - lstrcatA(Buffer, ".Score"); - options::get_string(optPath, Buffer, buf1, "", 300); + + wsprintfW(nameBuffer, L"%d.Name", position); + options::get_string(optPath, nameBuffer, tablePtr->Name, L"", 32); + tablePtr->Name[31] = 0; + + sprintf_s(scoreBuffer, "%d.Score", position); + options::get_string(optPath, scoreBuffer, buf1, "", 300); tablePtr->Score = atol(buf1); - for (int i = lstrlenA(tablePtr->Name); --i >= 0; scoreSum += tablePtr->Name[i]) - { - } + + for (auto i = lstrlenW(tablePtr->Name) - 1; i >= 0; i--) + scoreSum += tablePtr->Name[i]; scoreSum += tablePtr->Score; } @@ -78,29 +78,30 @@ int high_score::read(high_score_struct* table, int* ptrToSmth) int high_score::write(high_score_struct* table, int* ptrToSmth) { - char Buffer[20]; + char scoreBuffer[20]; + wchar_t nameBuffer[20]; - high_score_struct* tablePtr = table; int scoreSum = 0; CHAR* buf = memory::allocate(300u); if (!buf) return 1; - const CHAR* optPath = pinball::get_rc_string(166, 0); + auto optPath = pinball::get_rc_string(166, 0); for (auto position = 0; position < 5; ++position) { - _itoa_s(position, Buffer, 10); - lstrcatA(Buffer, ".Name"); - options::set_string(optPath, Buffer, tablePtr->Name); - _itoa_s(position, Buffer, 10); - lstrcatA(Buffer, ".Score"); + auto tablePtr = &table[position]; + + wsprintfW(nameBuffer, L"%d.Name", position); + options::set_string(optPath, nameBuffer, tablePtr->Name); + + sprintf_s(scoreBuffer, "%d.Score", position); _ltoa_s(tablePtr->Score, buf, 300, 10); - options::set_string(optPath, Buffer, buf); - for (int i = lstrlenA(tablePtr->Name); --i >= 0; scoreSum += tablePtr->Name[i]) - { - } + options::set_string(optPath, scoreBuffer, buf); + + for (auto i = lstrlenW(tablePtr->Name) - 1; i >= 0; i--) + scoreSum += tablePtr->Name[i]; scoreSum += tablePtr->Score; - ++tablePtr; } + scramble_number_string(scoreSum, buf); options::set_string(optPath, "Verification", buf); memory::free(buf); @@ -130,7 +131,7 @@ int high_score::get_score_position(high_score_struct* table, int score) return -1; } -int high_score::place_new_score_into(high_score_struct* table, int score, LPSTR scoreStr, int position) +int high_score::place_new_score_into(high_score_struct* table, int score, LPWSTR name, int position) { if (position >= 0) { @@ -148,9 +149,9 @@ int high_score::place_new_score_into(high_score_struct* table, int score, LPSTR } high_score_struct* posTable = &table[position]; posTable->Score = score; - if (lstrlenA(scoreStr) >= 31) - scoreStr[31] = 0; - lstrcpyA(posTable->Name, scoreStr); + if (lstrlenW(name) >= 31) + name[31] = 0; + lstrcpyW(posTable->Name, name); posTable->Name[31] = 0; } return position; @@ -169,7 +170,7 @@ void high_score::show_high_score_dialog(high_score_struct* table) DialogBoxParamW(winmain::hinst, L"dlg_highscores", winmain::hwnd_frame, HighScore, 0); } -void high_score::show_and_set_high_score_dialog(high_score_struct* table, int score, int pos, LPCSTR defaultName) +void high_score::show_and_set_high_score_dialog(high_score_struct* table, int score, int pos, LPCWSTR defaultName) { dlg_position = pos; dlg_score = score; @@ -185,7 +186,7 @@ INT_PTR high_score::HighScore(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { HWND parent; int nIDDlgItem; - CHAR name[32]; + wchar_t name[32]; switch (msg) { @@ -203,7 +204,7 @@ INT_PTR high_score::HighScore(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) show_high_scores(hWnd, dlg_hst); for (nIDDlgItem = DLG_HIGHSCORES_EditName1; nIDDlgItem < 611; ++nIDDlgItem) { - ShowWindow(GetDlgItem(hWnd, nIDDlgItem), 0); + ShowWindow(GetDlgItem(hWnd, nIDDlgItem), SW_HIDE); } if (dlg_enter_name == 1) { @@ -213,12 +214,12 @@ INT_PTR high_score::HighScore(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) return 1; } HWND nameTextBox = GetDlgItem(hWnd, dlg_position + DLG_HIGHSCORES_EditName1); - ShowWindow(nameTextBox, 5); + ShowWindow(nameTextBox, SW_SHOW); EnableWindow(nameTextBox, 1); SetFocus(nameTextBox); if (default_name) { - SetWindowTextA(nameTextBox, default_name); + SetWindowTextW(nameTextBox, default_name); SendMessageA(nameTextBox, EM_SETSEL, 0, -1); } SendMessageA(nameTextBox, EM_SETLIMITTEXT, 31u, 0); @@ -239,7 +240,7 @@ INT_PTR high_score::HighScore(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { break; } - GetDlgItemTextA(hWnd, dlg_position + DLG_HIGHSCORES_EditName1, name, 32); + GetDlgItemTextW(hWnd, dlg_position + DLG_HIGHSCORES_EditName1, name, 32); name[31] = 0; place_new_score_into(dlg_hst, dlg_score, name, dlg_position); break; @@ -276,7 +277,7 @@ void high_score::show_high_scores(HWND hDlg, high_score_struct* table) { if (dlg_enter_name == 1 && dlg_position == i) { - hsdlg_show_score(hDlg, " ", dlg_score, i); + hsdlg_show_score(hDlg, L" ", dlg_score, i); nextPosition = 1; } hsdlg_show_score(hDlg, tablePtr->Name, tablePtr->Score, i + nextPosition); @@ -284,7 +285,7 @@ void high_score::show_high_scores(HWND hDlg, high_score_struct* table) } } -void high_score::hsdlg_show_score(HWND hDlg, LPCSTR name, int score, int position) +void high_score::hsdlg_show_score(HWND hDlg, LPCWSTR name, int score, int position) { CHAR scoreStr[36]; if (position < 5) @@ -292,7 +293,7 @@ void high_score::hsdlg_show_score(HWND hDlg, LPCSTR name, int score, int positio score::string_format(score, scoreStr); if (scoreStr[0]) { - SetWindowTextA(GetDlgItem(hDlg, position + DLG_HIGHSCORES_StaticName1), name); + SetWindowTextW(GetDlgItem(hDlg, position + DLG_HIGHSCORES_StaticName1), name); SetWindowTextA(GetDlgItem(hDlg, position + DLG_HIGHSCORES_Score1), scoreStr); } } diff --git a/SpaceCadetPinball/high_score.h b/SpaceCadetPinball/high_score.h index 842226e..9915f04 100644 --- a/SpaceCadetPinball/high_score.h +++ b/SpaceCadetPinball/high_score.h @@ -3,7 +3,7 @@ struct high_score_struct { - char Name[32]; + wchar_t Name[32]; int Score; }; @@ -15,19 +15,19 @@ public: static int write(high_score_struct* table, int* ptrToSmth); static void clear_table(high_score_struct* table); static int get_score_position(high_score_struct* table, int score); - static int place_new_score_into(high_score_struct* table, int score, LPSTR scoreStr, int position); + static int place_new_score_into(high_score_struct* table, int score, LPWSTR name, int position); static void scramble_number_string(int Value, char* Buffer); static void show_high_score_dialog(high_score_struct* table); - static void show_and_set_high_score_dialog(high_score_struct* table, int score, int pos, LPCSTR defaultName); + static void show_and_set_high_score_dialog(high_score_struct* table, int score, int pos, LPCWSTR defaultName); static INT_PTR __stdcall HighScore(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); static void show_high_scores(HWND hDlg, high_score_struct* table); - static void hsdlg_show_score(HWND hDlg, LPCSTR name, int score, int position); + static void hsdlg_show_score(HWND hDlg, LPCWSTR name, int score, int position); private : static int dlg_enter_name; static int dlg_score; static int dlg_position; - static LPCSTR default_name; + static LPCWSTR default_name; static high_score_struct* dlg_hst; static winhelp_entry help[21]; }; diff --git a/SpaceCadetPinball/options.cpp b/SpaceCadetPinball/options.cpp index ff1de4a..85ca203 100644 --- a/SpaceCadetPinball/options.cpp +++ b/SpaceCadetPinball/options.cpp @@ -351,6 +351,43 @@ void options::set_string(LPCSTR optPath, LPCSTR lpValueName, LPCSTR value) path_free(); } +void options::get_string(LPCSTR optPath, LPCWSTR lpValueName, LPWSTR dst, LPCWSTR defaultValue, int iMaxLength) +{ + DWORD dwDisposition; + HKEY hKey; + + lstrcpynW(dst, defaultValue, iMaxLength); + if (!OptionsRegPath) + return; + + auto regPath = path(optPath); + if (!RegCreateKeyExA(HKEY_CURRENT_USER, regPath, 0, nullptr, 0, KEY_ALL_ACCESS, nullptr, &hKey, &dwDisposition)) + { + DWORD bufferSize = iMaxLength * sizeof(wchar_t); + RegQueryValueExW(hKey, lpValueName, nullptr, nullptr, reinterpret_cast(dst), &bufferSize); + RegCloseKey(hKey); + } + path_free(); +} + +void options::set_string(LPCSTR optPath, LPCWSTR lpValueName, LPCWSTR value) +{ + DWORD dwDisposition; + HKEY hKey; + + if (!OptionsRegPath) + return; + + auto regPath = path(optPath); + if (!RegCreateKeyExA(HKEY_CURRENT_USER, regPath, 0, nullptr, 0, KEY_ALL_ACCESS, nullptr, &hKey, &dwDisposition)) + { + DWORD bufferSize = (lstrlenW(value) + 1) * sizeof(wchar_t); + RegSetValueExW(hKey, lpValueName, 0, 1u, LPBYTE(value), bufferSize); + RegCloseKey(hKey); + } + path_free(); +} + void options::menu_check(UINT uIDCheckItem, int check) { diff --git a/SpaceCadetPinball/options.h b/SpaceCadetPinball/options.h index 0cbc57c..9f347e4 100644 --- a/SpaceCadetPinball/options.h +++ b/SpaceCadetPinball/options.h @@ -75,6 +75,8 @@ public: static void set_int(LPCSTR optPath, LPCSTR lpValueName, int data); static void get_string(LPCSTR optPath, LPCSTR lpValueName, LPSTR dst, LPCSTR defaultValue, int iMaxLength); static void set_string(LPCSTR optPath, LPCSTR lpValueName, LPCSTR value); + static void get_string(LPCSTR optPath, LPCWSTR lpValueName, LPWSTR dst, LPCWSTR defaultValue, int iMaxLength); + static void set_string(LPCSTR optPath, LPCWSTR lpValueName, LPCWSTR value); static void menu_check(UINT uIDCheckItem, int check); static void menu_set(UINT uIDEnableItem, int enable); static void toggle(UINT uIDCheckItem); diff --git a/SpaceCadetPinball/pb.cpp b/SpaceCadetPinball/pb.cpp index 2f061c7..4f0b2e1 100644 --- a/SpaceCadetPinball/pb.cpp +++ b/SpaceCadetPinball/pb.cpp @@ -502,8 +502,8 @@ void pb::keydown(int key) ball->Acceleration.X = 0.0; break; case 'H': - char String1[200]; - lstrcpyA(String1, pinball::get_rc_string(26, 0)); + wchar_t String1[200]; + lstrcpyW(String1, pinball::get_rc_Wstring(26, 0)); high_score::show_and_set_high_score_dialog(highscore_table, 1000000000, 1, String1); break; case 'M': @@ -556,7 +556,7 @@ void pb::end_game() { int scores[4]{}; int scoreIndex[4]{}; - char String1[200]; + wchar_t String1[200]; mode_change(2); int playerCount = MainTable->PlayerCount; @@ -593,7 +593,7 @@ void pb::end_game() int position = high_score::get_score_position(highscore_table, scores[i]); if (position >= 0) { - lstrcpyA(String1, pinball::get_rc_string(scoreIndex[i] + 26, 0)); + lstrcpyW(String1, pinball::get_rc_Wstring(scoreIndex[i] + 26, 0)); high_score::show_and_set_high_score_dialog(highscore_table, scores[i], position, String1); } }