aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDannyDaemonic <DannyDaemonic@gmail.com>2023-05-20 00:40:02 -0700
committerGitHub <noreply@github.com>2023-05-20 00:40:02 -0700
commitd2c59b8ba498ab01e65203dde6fe95236d20f6e7 (patch)
treee006c71f14fdd69aa8b6b9880d9a122207a06d66 /examples
parent503db28849d8641d66244385e7e9649608a2e4d0 (diff)
Fix for mingw (#1462)
Diffstat (limited to 'examples')
-rw-r--r--examples/common.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/examples/common.cpp b/examples/common.cpp
index e89df53..1308f84 100644
--- a/examples/common.cpp
+++ b/examples/common.cpp
@@ -578,6 +578,37 @@ void console_set_color(console_state & con_st, console_color_t color) {
}
char32_t getchar32() {
+#if defined(_WIN32)
+ HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
+ wchar_t high_surrogate = 0;
+
+ while (true) {
+ INPUT_RECORD record;
+ DWORD count;
+ if (!ReadConsoleInputW(hConsole, &record, 1, &count) || count == 0) {
+ return WEOF;
+ }
+
+ if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
+ wchar_t wc = record.Event.KeyEvent.uChar.UnicodeChar;
+ if (wc == 0) {
+ continue;
+ }
+
+ if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate
+ high_surrogate = wc;
+ continue;
+ } else if ((wc >= 0xDC00) && (wc <= 0xDFFF)) { // Check if wc is a low surrogate
+ if (high_surrogate != 0) { // Check if we have a high surrogate
+ return ((high_surrogate - 0xD800) << 10) + (wc - 0xDC00) + 0x10000;
+ }
+ }
+
+ high_surrogate = 0; // Reset the high surrogate
+ return static_cast<char32_t>(wc);
+ }
+ }
+#else
wchar_t wc = getwchar();
if (static_cast<wint_t>(wc) == WEOF) {
return WEOF;
@@ -596,6 +627,7 @@ char32_t getchar32() {
#endif
return static_cast<char32_t>(wc);
+#endif
}
void pop_cursor(console_state & con_st) {