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