]> gitweb.ps.run Git - npengine/commitdiff
update for win11 main
authorpatrick-scho <patrick.schoenberger@posteo.de>
Wed, 26 Nov 2025 00:07:55 +0000 (01:07 +0100)
committerpatrick-scho <patrick.schoenberger@posteo.de>
Wed, 26 Nov 2025 00:07:55 +0000 (01:07 +0100)
src/main.cpp

index 888035d5eef6f39cbcce52cf640aa0c7677a4aea..61fda8fb4b6a9032eae185de053e6005508e6cc8 100644 (file)
@@ -1,5 +1,6 @@
 #include <fstream>\r
 #include <stdio.h>\r
 #include <fstream>\r
 #include <stdio.h>\r
+#include <stdint.h>\r
 #include <string>\r
 #include <time.h>\r
 #include <windows.h>\r
 #include <string>\r
 #include <time.h>\r
 #include <windows.h>\r
@@ -41,13 +42,12 @@ HWND hwnd_edit = NULL;
 \r
 string get_text() {\r
   int len = SendMessageA(hwnd_edit, WM_GETTEXTLENGTH, 0, 0);\r
 \r
 string get_text() {\r
   int len = SendMessageA(hwnd_edit, WM_GETTEXTLENGTH, 0, 0);\r
-  char *buffer = new char[len + 1];\r
-  int read = SendMessageA(hwnd_edit, WM_GETTEXT, len + 1, (LPARAM)buffer);\r
+  string result;\r
+  result.resize(len + 1);\r
+  int read = SendMessageA(hwnd_edit, WM_GETTEXT, len + 1, (LPARAM)result.data());\r
   if (read != len)\r
     puts("???");\r
   if (read != len)\r
     puts("???");\r
-  buffer[read] = 0;\r
-  string result(buffer);\r
-  delete[] buffer;\r
+  result.data()[read] = 0;\r
   return result;\r
 }\r
 \r
   return result;\r
 }\r
 \r
@@ -68,7 +68,7 @@ void replace(string str) {
 }\r
 \r
 int get_pos(int x, int y) {\r
 }\r
 \r
 int get_pos(int x, int y) {\r
-  return (y + 1) * (WIDTH + 4) + x + 1;\r
+  return (y + 1) * (WIDTH + 3) + x + 1;\r
 }\r
 \r
 char get_block(int x, int y) {\r
 }\r
 \r
 char get_block(int x, int y) {\r
@@ -141,7 +141,7 @@ std::string read_map(int lvl) {
   ifs.close();\r
   buffer[length] = 0;\r
   std::string result(buffer);\r
   ifs.close();\r
   buffer[length] = 0;\r
   std::string result(buffer);\r
-  delete buffer;\r
+  delete[] buffer;\r
   return result;\r
 }\r
 \r
   return result;\r
 }\r
 \r
@@ -172,24 +172,10 @@ void load_level(int l) {
 STARTUPINFOA si;\r
 PROCESS_INFORMATION pi;\r
 \r
 STARTUPINFOA si;\r
 PROCESS_INFORMATION pi;\r
 \r
-BOOL CALLBACK ew_cb(HWND hwnd, LPARAM lp) {\r
-  DWORD pid;\r
-  DWORD tid = GetWindowThreadProcessId(hwnd, &pid);\r
-  if (pi.dwProcessId == pid && pi.dwThreadId == tid) {\r
-    hwnd_notepad = hwnd;\r
-    return false;\r
-  }\r
-  return true;\r
-}\r
-BOOL CALLBACK ecw_cb(HWND child, LPARAM in) {\r
-  char buffer[1024 + 1];\r
-  int len = GetClassNameA(child, buffer, 1024);\r
-  buffer[len] = 0;\r
-  if (strcmp(buffer, "Edit") == 0) {\r
-    hwnd_edit = child;\r
-    return false;\r
-  }\r
-  return true;\r
+bool check_class(HWND hwnd, const char *name) {\r
+    char b[64];\r
+    int n = GetClassNameA(hwnd, b, 64);\r
+    return (n && strncmp(b, name, n) == 0);\r
 }\r
 \r
 void start_notepad() {\r
 }\r
 \r
 void start_notepad() {\r
@@ -198,7 +184,7 @@ void start_notepad() {
   ZeroMemory(&pi, sizeof(pi));\r
 \r
   if (!CreateProcessA(NULL,  // No module name (use command line)\r
   ZeroMemory(&pi, sizeof(pi));\r
 \r
   if (!CreateProcessA(NULL,  // No module name (use command line)\r
-                      "notepad.exe",   // Command line\r
+                      "notepad.exe lvl/0.txt",   // Command line\r
                       NULL,  // Process handle not inheritable\r
                       NULL,  // Thread handle not inheritable\r
                       FALSE, // Set handle inheritance to FALSE\r
                       NULL,  // Process handle not inheritable\r
                       NULL,  // Thread handle not inheritable\r
                       FALSE, // Set handle inheritance to FALSE\r
@@ -210,15 +196,44 @@ void start_notepad() {
   ) {\r
     printf("CreateProcess failed (%d).\n", GetLastError());\r
   }\r
   ) {\r
     printf("CreateProcess failed (%d).\n", GetLastError());\r
   }\r
-  Sleep(100);\r
-  EnumWindows(ew_cb, 0);\r
-  EnumChildWindows(hwnd_notepad, ecw_cb, 0);\r
+  Sleep(1000);\r
+  EnumWindows([](HWND hwnd, LPARAM lp)->BOOL{\r
+    if (check_class(hwnd, "Notepad")) {\r
+      *(HWND*)lp = hwnd;\r
+      return false;\r
+    }\r
+    return true;\r
+  }, (LPARAM)&hwnd_notepad);\r
+  EnumChildWindows(hwnd_notepad, [](HWND hwnd, LPARAM lp)->BOOL{\r
+    printf("looking at %p\n", hwnd);\r
+    if (check_class(hwnd, "NotepadTextBox")) {\r
+      EnumChildWindows(hwnd, [](HWND hwnd, LPARAM lp)->BOOL{\r
+        printf("  looking at %p\n", hwnd);\r
+        const size_t size = (WIDTH+4)*(HEIGHT+2);\r
+        char b[size];\r
+        DWORD_PTR dwResult;\r
+        b[0] = 0;\r
+        SendMessageTimeoutA(hwnd, WM_GETTEXT, size, (LPARAM)b,\r
+                SMTO_ABORTIFHUNG, 100, &dwResult);\r
+        if (b[0] == '+') {\r
+          // TODO: check lvl\r
+          printf("%.*s.\n", size, b);\r
+          *(HWND*)lp = hwnd;\r
+          return false;\r
+        }\r
+        return true;\r
+      }, lp);\r
+      if (lp) return false;\r
+    }\r
+    return true;\r
+  }, (LPARAM)&hwnd_edit);\r
   SendMessage(hwnd_edit, EM_SETREADONLY, TRUE, NULL);\r
 }\r
 void close_notepad() {\r
   SendMessage(hwnd_edit, EM_SETREADONLY, TRUE, NULL);\r
 }\r
 void close_notepad() {\r
-  TerminateProcess(pi.hProcess, 0);\r
-  CloseHandle(pi.hProcess);\r
-  CloseHandle(pi.hThread);\r
+  SendMessage(hwnd_edit, EM_SETREADONLY, FALSE, NULL);\r
+  //TerminateProcess(pi.hProcess, 0);\r
+  //CloseHandle(pi.hProcess);\r
+  //CloseHandle(pi.hThread);\r
 }\r
 \r
 // Keys\r
 }\r
 \r
 // Keys\r
@@ -329,7 +344,7 @@ void intro() {
   static int progress = 0;\r
   switch (progress) {\r
   case 0:\r
   static int progress = 0;\r
   switch (progress) {\r
   case 0:\r
-    print_text(4, 2, "Move with left/right.", text_speed);\r
+    print_text(4, 2, "Move with A/D.", text_speed);\r
 \r
     progress++;\r
     break;\r
 \r
     progress++;\r
     break;\r
@@ -426,4 +441,4 @@ int WinMain(HINSTANCE a0, HINSTANCE a1, LPSTR a2, int a3) {
   close_notepad();\r
 \r
   return 0;\r
   close_notepad();\r
 \r
   return 0;\r
-}
\ No newline at end of file
+}\r