diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2019-10-12 09:02:45 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2019-10-12 09:02:45 +0200 |
commit | d08d7dbe709aba1aec95c3d6de29c5304fc493be (patch) | |
tree | 12041aae413b6a99f6e18f29029a9616da3c63b6 /GdiRadar | |
parent | 339c4f9b53192e65588d8bf8c5c35967f9f75d61 (diff) |
basic GdiRadar lib
Diffstat (limited to 'GdiRadar')
-rw-r--r-- | GdiRadar/GdiRadar.cpp | 216 | ||||
-rw-r--r-- | GdiRadar/GdiRadar.h | 39 | ||||
-rw-r--r-- | GdiRadar/GdiRadar.vcxproj | 2 | ||||
-rw-r--r-- | GdiRadar/GdiRadar.vcxproj.filters | 6 | ||||
-rw-r--r-- | GdiRadar/GdiRadarMain.cpp | 156 |
5 files changed, 343 insertions, 76 deletions
diff --git a/GdiRadar/GdiRadar.cpp b/GdiRadar/GdiRadar.cpp index f6639e3..c88536e 100644 --- a/GdiRadar/GdiRadar.cpp +++ b/GdiRadar/GdiRadar.cpp @@ -1,35 +1,43 @@ #include "pch.h" +#include "GdiRadar.h" + #include <iostream> #include <vector> -#include <Windows.h> -#include <time.h> - -#pragma comment(lib, "Gdi32.lib") - #define SCALE_X(posx) ((int)((float)GameMapWidth * (1000.0f / posx))) #define SCALE_Y(posy) ((int)((float)GameMapHeight * (1000.0f / posy))) -static HWND myDrawWnd = NULL; -static HINSTANCE hInstance = NULL; -static WNDCLASS wc = { 0 }; -static float GameMapWidth = 0; -static float GameMapHeight = 0; -enum entity_color { - EC_RED +struct gdi_radar_drawing +{ + HBRUSH EnemyBrush; + HBRUSH BackgroundBrush; + COLORREF TextCOLOR; + HFONT HFONT_Hunt; + RECT DC_Dimensions; + HDC hdc; }; -struct entity { - float pos[2]; - float health; - enum entity_color color; - const char *name; +struct gdi_radar_context +{ + PWSTR className; + PWSTR windowName; + HWND myDrawWnd; + HINSTANCE hInstance; + WNDCLASS wc; + + clock_t minimumUpdateTime; + clock_t lastTimeUpdated; + float GameMapWidth; + float GameMapHeight; + size_t reservedEntities; + + struct gdi_radar_drawing drawing; + std::vector<struct entity> entities; }; -std::vector<struct entity> entities; -static void draw_entity(HDC hdc, float posx, float posy, float health, enum entity_color color, const char *name) +static void draw_entity(struct gdi_radar_context * const ctx, float posx, float posy, float health, enum entity_color color, const char *name) { #if 0 RECT healthRect = { posx - 10, posy - 10, posx + 10, posy - 5 }; @@ -42,45 +50,59 @@ static void draw_entity(HDC hdc, float posx, float posy, float health, enum enti switch (color) { case EC_RED: - SetDCBrushColor(hdc, RGB(255, 0, 0)); + SetDCBrushColor(ctx->drawing.hdc, RGB(255, 0, 0)); break; } - std::cout << GameMapWidth << ", " << SCALE_X(posx) << std::endl; - Ellipse(hdc, SCALE_X(posx), SCALE_Y(posy), SCALE_X(posx + 5), SCALE_Y(posy + 5)); + Ellipse(ctx->drawing.hdc, posx, posy, posx + 5, posy + 5); } -LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { - static HBRUSH EnemyBrush = NULL; - static HBRUSH BackgroundBrush = NULL; - static COLORREF TextCOLOR = NULL; - static HFONT HFONT_Hunt = NULL; - static RECT DC_Dimensions = {}; - static HDC hdc = NULL; + struct gdi_radar_context * const ctx = (gdi_radar_context * const)lparam; + + if (!ctx) + { + return DefWindowProc(hwnd, message, wparam, lparam); + } + + struct gdi_radar_drawing * const drawing = &ctx->drawing; + if (!drawing) + { + return DefWindowProc(hwnd, message, wparam, lparam); + } switch (message) { case WM_CREATE: std::cout << "WM_CREATE\n"; - hdc = GetDC(hwnd); - EnemyBrush = CreateSolidBrush(RGB(255, 0, 0)); - BackgroundBrush = CreateSolidBrush(RGB(0, 0, 0)); - TextCOLOR = RGB(0, 255, 0); - SetBkMode(hdc, TRANSPARENT); + 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; + case WM_PAINT: { std::cout << "WM_PAINT\n"; PAINTSTRUCT ps; BeginPaint(hwnd, &ps); - for (auto& entity : entities) { - draw_entity(hdc, entity.pos[0], entity.pos[1], entity.health, entity.color, entity.name); + for (auto& entity : ctx->entities) { + draw_entity(ctx, entity.pos[0], entity.pos[1], entity.health, entity.color, entity.name); } EndPaint(hwnd, &ps); return 0; } break; + case WM_LBUTTONDOWN: std::cout << "WM_LBUTTONDOWN\n"; return 0; @@ -95,62 +117,104 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) return 0; case WM_SIZE: std::cout << "WM_SIZE\n"; - GetClientRect(hwnd, &DC_Dimensions); - FillRect(hdc, &DC_Dimensions, BackgroundBrush); - return 0; - case WM_DESTROY: - std::cout << "WM_DESTROY\n"; - PostQuitMessage(0); + GetClientRect(hwnd, &drawing->DC_Dimensions); + FillRect(drawing->hdc, &drawing->DC_Dimensions, drawing->BackgroundBrush); return 0; } + return DefWindowProc(hwnd, message, wparam, lparam); } -int main() +struct gdi_radar_context * const + gdi_radar_configure(struct gdi_radar_config const * const cfg, + HINSTANCE hInst) { - static double last_time_called = 0.0; - double current_time; - - std::cout << "Init\n"; - - GameMapWidth = 1000.0f; - GameMapHeight = 1000.0f; - - hInstance = (HINSTANCE)GetWindowLongW(GetActiveWindow(), -6); - wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); - wc.hCursor = LoadCursor(hInstance, IDC_ARROW); - wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION); - wc.hInstance = hInstance; - wc.lpfnWndProc = WndProc; - wc.lpszClassName = L"peter"; - wc.style = CS_HREDRAW | CS_VREDRAW; + struct gdi_radar_context * result = new gdi_radar_context; + + /* config params */ + result->className = _wcsdup(cfg->className); + result->windowName = _wcsdup(cfg->windowName); + result->minimumUpdateTime = 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.lpfnWndProc = WndProc; + result->wc.lpszClassName = result->className; + result->wc.style = CS_HREDRAW | CS_VREDRAW; + + return result; +} - UnregisterClassW(L"peter", hInstance); - if (!RegisterClass(&wc)) +bool gdi_radar_init(struct gdi_radar_context * const ctx) +{ + UnregisterClassW(ctx->className, ctx->hInstance); + if (!RegisterClass(&ctx->wc)) { - return 1; + return false; } - myDrawWnd = CreateWindowW(L"peter", - L"the window", + ctx->myDrawWnd = CreateWindowW(ctx->className, ctx->windowName, WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_MAXIMIZEBOX | WS_SIZEBOX, 50, 50, 640, 480, - NULL, NULL, hInstance, NULL); - ShowWindow(myDrawWnd, SW_SHOWNORMAL); - UpdateWindow(myDrawWnd); + NULL, NULL, ctx->hInstance, ctx); + if (!ctx->myDrawWnd) + { + return false; + } + if (!ShowWindow(ctx->myDrawWnd, SW_SHOWNORMAL)) + { + return false; + } + if (!UpdateWindow(ctx->myDrawWnd)) + { + return false; + } - last_time_called = clock(); + ctx->lastTimeUpdated = clock(); + return true; +} - entities.push_back(entity{ 0.0f, 0.0f, 100.0f, EC_RED, "test" }); - entities.push_back(entity{ 1000.0f, 1000.0f, 50.0f, EC_RED, "m0wL" }); - entities.push_back(entity{ 500.0f, 500.0f, 80.0f, EC_RED, "whiteshirt" }); +void gdi_radar_add_entity(struct gdi_radar_context * const ctx, + struct entity const * const ent) +{ + ctx->entities.push_back(*ent); +} - MSG msg; - while (GetMessageA(&msg, myDrawWnd, 0, 0)) +void gdi_radar_clear_entities(struct gdi_radar_context * const ctx) +{ + ctx->entities.clear(); +} + +static inline LRESULT +gdi_process_events(struct gdi_radar_context * const ctx, MSG * const msg) +{ + if (!TranslateMessage(msg)) { - TranslateMessage(&msg); - DispatchMessage(&msg); + return NULL; } + return DispatchMessageW(msg); +} - return 0; +void gdi_radar_process_window_events_blocking(struct gdi_radar_context * const ctx) +{ + MSG msg; + while (GetMessageW(&msg, ctx->myDrawWnd, 0, 0)) + { + gdi_process_events(ctx, &msg); + } } + +void gdi_radar_process_window_events_nonblocking(struct gdi_radar_context * const ctx) +{ + MSG msg; + while (PeekMessageW(&msg, ctx->myDrawWnd, 0, 0, PM_REMOVE)) + { + gdi_process_events(ctx, &msg); + } +}
\ No newline at end of file diff --git a/GdiRadar/GdiRadar.h b/GdiRadar/GdiRadar.h new file mode 100644 index 0000000..8081aba --- /dev/null +++ b/GdiRadar/GdiRadar.h @@ -0,0 +1,39 @@ +#pragma once + +#include <time.h> +#include <Windows.h> + + +struct gdi_radar_config { + LPCWSTR className; + LPCWSTR windowName; + clock_t minimumUpdateTime; + size_t reservedEntities; +}; + +struct gdi_radar_context; + + +struct gdi_radar_context * const + gdi_radar_configure(struct gdi_radar_config const * const cfg, + HINSTANCE hInst); +bool gdi_radar_init(struct gdi_radar_context * const ctx); + + +enum entity_color { + EC_RED +}; + +struct entity { + float pos[2]; + float health; + enum entity_color color; + const char *name; +}; + + +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_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 diff --git a/GdiRadar/GdiRadar.vcxproj b/GdiRadar/GdiRadar.vcxproj index e389f18..caec31e 100644 --- a/GdiRadar/GdiRadar.vcxproj +++ b/GdiRadar/GdiRadar.vcxproj @@ -155,10 +155,12 @@ </Link> </ItemDefinitionGroup> <ItemGroup> + <ClInclude Include="GdiRadar.h" /> <ClInclude Include="pch.h" /> </ItemGroup> <ItemGroup> <ClCompile Include="GdiRadar.cpp" /> + <ClCompile Include="GdiRadarMain.cpp" /> <ClCompile Include="pch.cpp"> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader> diff --git a/GdiRadar/GdiRadar.vcxproj.filters b/GdiRadar/GdiRadar.vcxproj.filters index f856747..c7b0464 100644 --- a/GdiRadar/GdiRadar.vcxproj.filters +++ b/GdiRadar/GdiRadar.vcxproj.filters @@ -18,11 +18,17 @@ <ClInclude Include="pch.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="GdiRadar.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="pch.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="GdiRadarMain.cpp"> + <Filter>Source Files</Filter> + </ClCompile> <ClCompile Include="GdiRadar.cpp"> <Filter>Source Files</Filter> </ClCompile> diff --git a/GdiRadar/GdiRadarMain.cpp b/GdiRadar/GdiRadarMain.cpp new file mode 100644 index 0000000..f6639e3 --- /dev/null +++ b/GdiRadar/GdiRadarMain.cpp @@ -0,0 +1,156 @@ +#include "pch.h" +#include <iostream> +#include <vector> + +#include <Windows.h> +#include <time.h> + +#pragma comment(lib, "Gdi32.lib") + +#define SCALE_X(posx) ((int)((float)GameMapWidth * (1000.0f / posx))) +#define SCALE_Y(posy) ((int)((float)GameMapHeight * (1000.0f / posy))) + +static HWND myDrawWnd = NULL; +static HINSTANCE hInstance = NULL; +static WNDCLASS wc = { 0 }; +static float GameMapWidth = 0; +static float GameMapHeight = 0; + +enum entity_color { + EC_RED +}; + +struct entity { + float pos[2]; + float health; + enum entity_color color; + const char *name; +}; +std::vector<struct entity> entities; + + +static void draw_entity(HDC hdc, float posx, float posy, float health, enum entity_color color, const char *name) +{ +#if 0 + RECT healthRect = { posx - 10, posy - 10, posx + 10, posy - 5 }; + FillRect(hdc, &rect, color); + + RECT textRect = { posx, posy, posx + 10, posy - 5 }; + DrawText(hdc, TEXT("Michael Morrison"), -1, &rect, + DT_SINGLELINE | DT_CENTER | DT_VCENTER); +#endif + + switch (color) { + case EC_RED: + SetDCBrushColor(hdc, RGB(255, 0, 0)); + break; + } + std::cout << GameMapWidth << ", " << SCALE_X(posx) << std::endl; + Ellipse(hdc, SCALE_X(posx), SCALE_Y(posy), SCALE_X(posx + 5), SCALE_Y(posy + 5)); +} + +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) +{ + static HBRUSH EnemyBrush = NULL; + static HBRUSH BackgroundBrush = NULL; + static COLORREF TextCOLOR = NULL; + static HFONT HFONT_Hunt = NULL; + static RECT DC_Dimensions = {}; + static HDC hdc = NULL; + + switch (message) + { + case WM_CREATE: + std::cout << "WM_CREATE\n"; + hdc = GetDC(hwnd); + EnemyBrush = CreateSolidBrush(RGB(255, 0, 0)); + BackgroundBrush = CreateSolidBrush(RGB(0, 0, 0)); + TextCOLOR = RGB(0, 255, 0); + SetBkMode(hdc, TRANSPARENT); + return 0; + case WM_PAINT: + { + std::cout << "WM_PAINT\n"; + PAINTSTRUCT ps; + + BeginPaint(hwnd, &ps); + for (auto& entity : entities) { + draw_entity(hdc, entity.pos[0], entity.pos[1], entity.health, entity.color, entity.name); + } + EndPaint(hwnd, &ps); + return 0; + } + break; + case WM_LBUTTONDOWN: + std::cout << "WM_LBUTTONDOWN\n"; + return 0; + case WM_NCLBUTTONDOWN: + std::cout << "WM_NCLBUTTONDOWN\n"; + break; + case WM_CHAR: + std::cout << "WM_CHAR\n"; + return 0; + case WM_MOVE: + std::cout << "WM_MOVE\n"; + return 0; + case WM_SIZE: + std::cout << "WM_SIZE\n"; + GetClientRect(hwnd, &DC_Dimensions); + FillRect(hdc, &DC_Dimensions, BackgroundBrush); + return 0; + case WM_DESTROY: + std::cout << "WM_DESTROY\n"; + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, message, wparam, lparam); +} + +int main() +{ + static double last_time_called = 0.0; + double current_time; + + std::cout << "Init\n"; + + GameMapWidth = 1000.0f; + GameMapHeight = 1000.0f; + + hInstance = (HINSTANCE)GetWindowLongW(GetActiveWindow(), -6); + wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); + wc.hCursor = LoadCursor(hInstance, IDC_ARROW); + wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION); + wc.hInstance = hInstance; + wc.lpfnWndProc = WndProc; + wc.lpszClassName = L"peter"; + wc.style = CS_HREDRAW | CS_VREDRAW; + + UnregisterClassW(L"peter", hInstance); + if (!RegisterClass(&wc)) + { + return 1; + } + + myDrawWnd = CreateWindowW(L"peter", + L"the window", + WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_MAXIMIZEBOX | WS_SIZEBOX, + 50, 50, 640, 480, + NULL, NULL, hInstance, NULL); + ShowWindow(myDrawWnd, SW_SHOWNORMAL); + UpdateWindow(myDrawWnd); + + last_time_called = clock(); + + entities.push_back(entity{ 0.0f, 0.0f, 100.0f, EC_RED, "test" }); + entities.push_back(entity{ 1000.0f, 1000.0f, 50.0f, EC_RED, "m0wL" }); + entities.push_back(entity{ 500.0f, 500.0f, 80.0f, EC_RED, "whiteshirt" }); + + MSG msg; + while (GetMessageA(&msg, myDrawWnd, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + return 0; +} |