diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2019-10-13 16:17:30 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2019-10-13 16:17:30 +0200 |
commit | 5ae350f11cf628909eb9fe2d7500080e9e70748f (patch) | |
tree | 38d75af77af49dfa2b7765fb97840890d74623eb | |
parent | d08d7dbe709aba1aec95c3d6de29c5304fc493be (diff) |
basic window events are now correctly processed
-rw-r--r-- | GdiRadar/GdiRadar.cpp | 104 | ||||
-rw-r--r-- | GdiRadar/GdiRadar.h | 14 |
2 files changed, 81 insertions, 37 deletions
diff --git a/GdiRadar/GdiRadar.cpp b/GdiRadar/GdiRadar.cpp index c88536e..54cc1fe 100644 --- a/GdiRadar/GdiRadar.cpp +++ b/GdiRadar/GdiRadar.cpp @@ -4,6 +4,9 @@ #include <iostream> #include <vector> +#pragma comment(lib, "Gdi32.lib") + +#define INVALID_MAP_VALUE ((UINT64)0) #define SCALE_X(posx) ((int)((float)GameMapWidth * (1000.0f / posx))) #define SCALE_Y(posy) ((int)((float)GameMapHeight * (1000.0f / posy))) @@ -11,7 +14,6 @@ struct gdi_radar_drawing { HBRUSH EnemyBrush; - HBRUSH BackgroundBrush; COLORREF TextCOLOR; HFONT HFONT_Hunt; RECT DC_Dimensions; @@ -21,15 +23,15 @@ struct gdi_radar_drawing struct gdi_radar_context { PWSTR className; + ATOM classAtom; PWSTR windowName; HWND myDrawWnd; - HINSTANCE hInstance; - WNDCLASS wc; + WNDCLASSW wc; clock_t minimumUpdateTime; clock_t lastTimeUpdated; - float GameMapWidth; - float GameMapHeight; + UINT64 GameMapWidth; + UINT64 GameMapHeight; size_t reservedEntities; struct gdi_radar_drawing drawing; @@ -58,16 +60,27 @@ static void draw_entity(struct gdi_radar_context * const ctx, float posx, float static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { - struct gdi_radar_context * const ctx = (gdi_radar_context * const)lparam; + struct gdi_radar_context * wnd_ctx = NULL; + + if (message == WM_CREATE) { + LONG_PTR pParent = (LONG_PTR)((LPCREATESTRUCTW)lparam)->lpCreateParams; + SetWindowLongPtrW(hwnd, -21, pParent); + wnd_ctx = (struct gdi_radar_context *)pParent; + } + else { + wnd_ctx = (struct gdi_radar_context *)GetWindowLongPtrW(hwnd, -21); + } - if (!ctx) + if (!wnd_ctx) { + std::cout << "WndProc: ctx NULL!\n"; return DefWindowProc(hwnd, message, wparam, lparam); } - struct gdi_radar_drawing * const drawing = &ctx->drawing; + struct gdi_radar_drawing * const drawing = &wnd_ctx->drawing; if (!drawing) { + std::cout << "WndProc: drawing NULL!\n"; return DefWindowProc(hwnd, message, wparam, lparam); } @@ -77,14 +90,12 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM l std::cout << "WM_CREATE\n"; drawing->hdc = GetDC(hwnd); drawing->EnemyBrush = CreateSolidBrush(RGB(255, 0, 0)); - drawing->BackgroundBrush = CreateSolidBrush(RGB(0, 0, 0)); drawing->TextCOLOR = RGB(0, 255, 0); SetBkMode(drawing->hdc, TRANSPARENT); return 0; case WM_DESTROY: std::cout << "WM_DESTROY\n"; DeleteObject(drawing->EnemyBrush); - DeleteObject(drawing->BackgroundBrush); DeleteDC(drawing->hdc); PostQuitMessage(0); return 0; @@ -95,34 +106,32 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM l PAINTSTRUCT ps; BeginPaint(hwnd, &ps); - for (auto& entity : ctx->entities) { - draw_entity(ctx, entity.pos[0], entity.pos[1], entity.health, entity.color, entity.name); + for (auto& entity : wnd_ctx->entities) { + draw_entity(wnd_ctx, entity.pos[0], entity.pos[1], entity.health, entity.color, entity.name); } EndPaint(hwnd, &ps); - return 0; + break; } - break; case WM_LBUTTONDOWN: std::cout << "WM_LBUTTONDOWN\n"; - return 0; + break; case WM_NCLBUTTONDOWN: std::cout << "WM_NCLBUTTONDOWN\n"; break; case WM_CHAR: std::cout << "WM_CHAR\n"; - return 0; + break; case WM_MOVE: std::cout << "WM_MOVE\n"; - return 0; + break; case WM_SIZE: std::cout << "WM_SIZE\n"; - GetClientRect(hwnd, &drawing->DC_Dimensions); - FillRect(drawing->hdc, &drawing->DC_Dimensions, drawing->BackgroundBrush); - return 0; + break; } - return DefWindowProc(hwnd, message, wparam, lparam); + //std::cout << "Default window proc for message 0x" << std::hex << message << std::endl; + return DefWindowProcW(hwnd, message, wparam, lparam); } struct gdi_radar_context * const @@ -130,49 +139,68 @@ struct gdi_radar_context * const HINSTANCE hInst) { struct gdi_radar_context * result = new gdi_radar_context; + if (!result) + { + return NULL; + } + ZeroMemory(result, sizeof(*result)); /* config params */ result->className = _wcsdup(cfg->className); result->windowName = _wcsdup(cfg->windowName); - result->minimumUpdateTime = cfg->minimumUpdateTime; + result->minimumUpdateTime = (clock_t)cfg->minimumUpdateTime; result->reservedEntities = cfg->reservedEntities; result->entities.reserve(result->reservedEntities); /* other */ - result->hInstance = hInst; - result->wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); - result->wc.hCursor = LoadCursor(result->hInstance, IDC_ARROW); - result->wc.hIcon = LoadIcon(result->hInstance, IDI_APPLICATION); - result->wc.hInstance = result->hInstance; + result->wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 0)); + result->wc.hCursor = LoadCursor(hInst, IDC_ARROW); + result->wc.hIcon = LoadIcon(hInst, IDI_APPLICATION); + result->wc.hInstance = hInst; result->wc.lpfnWndProc = WndProc; result->wc.lpszClassName = result->className; result->wc.style = CS_HREDRAW | CS_VREDRAW; + result->GameMapWidth = INVALID_MAP_VALUE; + result->GameMapHeight = INVALID_MAP_VALUE; return result; } bool gdi_radar_init(struct gdi_radar_context * const ctx) { - UnregisterClassW(ctx->className, ctx->hInstance); - if (!RegisterClass(&ctx->wc)) + if (ctx->GameMapWidth == INVALID_MAP_VALUE || + ctx->GameMapHeight == INVALID_MAP_VALUE) + { + std::cout << "Invalid game map dimensions!\n"; + return false; + } + + UnregisterClassW(ctx->className, ctx->wc.hInstance); + ctx->classAtom = RegisterClassW(&ctx->wc); + if (!ctx->classAtom) { + std::cout << "Register window class failed with 0x" + << std::hex << GetLastError() << "!\n"; return false; } ctx->myDrawWnd = CreateWindowW(ctx->className, ctx->windowName, - WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_MAXIMIZEBOX | WS_SIZEBOX, + WS_OVERLAPPEDWINDOW | WS_THICKFRAME | WS_EX_LAYERED | WS_VISIBLE | + WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_MAXIMIZEBOX | WS_SIZEBOX, 50, 50, 640, 480, - NULL, NULL, ctx->hInstance, ctx); + NULL, NULL, ctx->wc.hInstance, ctx); if (!ctx->myDrawWnd) { + std::cout << "Create window failed!\n"; return false; } if (!ShowWindow(ctx->myDrawWnd, SW_SHOWNORMAL)) { - return false; + std::cout << "Show window failed!\n"; } if (!UpdateWindow(ctx->myDrawWnd)) { + std::cout << "Update window failed!\n"; return false; } @@ -191,13 +219,17 @@ void gdi_radar_clear_entities(struct gdi_radar_context * const ctx) ctx->entities.clear(); } +void gdi_radar_set_game_dimensions(struct gdi_radar_context * const ctx, + UINT64 GameMapWidth, UINT64 GameMapHeight) +{ + ctx->GameMapWidth = GameMapWidth; + ctx->GameMapHeight = GameMapHeight; +} + static inline LRESULT gdi_process_events(struct gdi_radar_context * const ctx, MSG * const msg) { - if (!TranslateMessage(msg)) - { - return NULL; - } + TranslateMessage(msg); return DispatchMessageW(msg); } diff --git a/GdiRadar/GdiRadar.h b/GdiRadar/GdiRadar.h index 8081aba..b8d73f6 100644 --- a/GdiRadar/GdiRadar.h +++ b/GdiRadar/GdiRadar.h @@ -7,13 +7,17 @@ struct gdi_radar_config { LPCWSTR className; LPCWSTR windowName; - clock_t minimumUpdateTime; + double minimumUpdateTime; size_t reservedEntities; }; struct gdi_radar_context; +static HINSTANCE gdi_radar_get_fake_hinstance() +{ + return (HINSTANCE)GetWindowLongW(GetActiveWindow(), -6); +} struct gdi_radar_context * const gdi_radar_configure(struct gdi_radar_config const * const cfg, HINSTANCE hInst); @@ -35,5 +39,13 @@ struct entity { void gdi_radar_add_entity(struct gdi_radar_context * const ctx, struct entity const * const ent); void gdi_radar_clear_entities(struct gdi_radar_context * const ctx); +void gdi_radar_set_game_dimensions(struct gdi_radar_context * const ctx, + UINT64 GameMapWidth, UINT64 GameMapHeight); +static void gdi_radar_set_game_dimensions(struct gdi_radar_context * const ctx, + float GameMapWidth, float GameMapHeight) +{ + gdi_radar_set_game_dimensions(ctx, + (UINT64)GameMapWidth, (UINT64)GameMapHeight); +} void gdi_radar_process_window_events_blocking(struct gdi_radar_context * const ctx); void gdi_radar_process_window_events_nonblocking(struct gdi_radar_context * const ctx);
\ No newline at end of file |