From 6567f85eb3a33931dc59c6c0e59f8623979432b7 Mon Sep 17 00:00:00 2001 From: hailey Date: Mon, 10 Mar 2025 10:29:55 -0500 Subject: [PATCH] Updated rectangle border rendering --- examples/win32_gdi/main.c | 6 +- renderers/win32_gdi/clay_renderer_gdi.c | 81 +++++++++++++++++++++---- 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/examples/win32_gdi/main.c b/examples/win32_gdi/main.c index f197a1f..761c59e 100644 --- a/examples/win32_gdi/main.c +++ b/examples/win32_gdi/main.c @@ -151,11 +151,7 @@ int APIENTRY WinMain( uint64_t clayRequiredMemory = Clay_MinMemorySize(); Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(clayRequiredMemory, malloc(clayRequiredMemory)); Clay_Initialize(clayMemory, (Clay_Dimensions){.width = 800, .height = 600}, (Clay_ErrorHandler){HandleClayErrors}); // This final argument is new since the video was published - - // Font fonts[1]; - // fonts[FONT_ID_BODY_16] = LoadFontEx("resources/Roboto-Regular.ttf", 48, 0, 400); - - Clay_SetMeasureTextFunction(Clay_Win32_MeasureText, NULL); // was gettings fonts[] passed in + Clay_SetMeasureTextFunction(Clay_Win32_MeasureText, NULL); ZeroMemory(&wc, sizeof wc); wc.hInstance = hInstance; diff --git a/renderers/win32_gdi/clay_renderer_gdi.c b/renderers/win32_gdi/clay_renderer_gdi.c index 5bd85e3..e031189 100644 --- a/renderers/win32_gdi/clay_renderer_gdi.c +++ b/renderers/win32_gdi/clay_renderer_gdi.c @@ -1,7 +1,6 @@ #include #include "../../clay.h" - HDC renderer_hdcMem = {0}; HBITMAP renderer_hbmMem = {0}; HANDLE renderer_hOld = {0}; @@ -115,18 +114,75 @@ void Clay_Win32_Render(HWND hwnd, Clay_RenderCommandArray renderCommands) break; } - // // The renderer should draw a colored border inset into the bounding box. - // case CLAY_RENDER_COMMAND_TYPE_BORDER: - // { - // // TODO: - // break; - // } + // The renderer should draw a colored border inset into the bounding box. + case CLAY_RENDER_COMMAND_TYPE_BORDER: + { + Clay_BorderRenderData brd = renderCommand->renderData.border; + RECT r = rc; - // case CLAY_RENDER_COMMAND_TYPE_IMAGE: - // { - // // TODO: - // break; - // } + r.left = boundingBox.x; + r.top = boundingBox.y; + r.right = boundingBox.x + boundingBox.width; + r.bottom = boundingBox.y + boundingBox.height; + + HPEN topPen = CreatePen(PS_SOLID, brd.width.top, RGB(brd.color.r, brd.color.g, brd.color.b)); + HPEN leftPen = CreatePen(PS_SOLID, brd.width.left, RGB(brd.color.r, brd.color.g, brd.color.b)); + HPEN bottomPen = CreatePen(PS_SOLID, brd.width.bottom, RGB(brd.color.r, brd.color.g, brd.color.b)); + HPEN rightPen = CreatePen(PS_SOLID, brd.width.right, RGB(brd.color.r, brd.color.g, brd.color.b)); + + HPEN oldPen = SelectObject(renderer_hdcMem, topPen); + + if (brd.cornerRadius.topLeft == 0) + { + MoveToEx(renderer_hdcMem, r.left, r.top, NULL); + LineTo(renderer_hdcMem, r.right, r.top); + + SelectObject(renderer_hdcMem, leftPen); + MoveToEx(renderer_hdcMem, r.left, r.top, NULL); + LineTo(renderer_hdcMem, r.left, r.bottom); + + SelectObject(renderer_hdcMem, bottomPen); + MoveToEx(renderer_hdcMem, r.left, r.bottom, NULL); + LineTo(renderer_hdcMem, r.right, r.bottom); + + SelectObject(renderer_hdcMem, rightPen); + MoveToEx(renderer_hdcMem, r.right, r.top, NULL); + LineTo(renderer_hdcMem, r.right, r.bottom); + } + else + { + // todo: i should be rounded + MoveToEx(renderer_hdcMem, r.left, r.top, NULL); + LineTo(renderer_hdcMem, r.right, r.top); + + SelectObject(renderer_hdcMem, leftPen); + MoveToEx(renderer_hdcMem, r.left, r.top, NULL); + LineTo(renderer_hdcMem, r.left, r.bottom); + + SelectObject(renderer_hdcMem, bottomPen); + MoveToEx(renderer_hdcMem, r.left, r.bottom, NULL); + LineTo(renderer_hdcMem, r.right, r.bottom); + + SelectObject(renderer_hdcMem, rightPen); + MoveToEx(renderer_hdcMem, r.right, r.top, NULL); + LineTo(renderer_hdcMem, r.right, r.bottom); + + } + + SelectObject(renderer_hdcMem, oldPen); + DeleteObject(topPen); + DeleteObject(leftPen); + DeleteObject(bottomPen); + DeleteObject(rightPen); + + break; + } + + // case CLAY_RENDER_COMMAND_TYPE_IMAGE: + // { + // // TODO: i couldnt get the win 32 api to load a bitmap.... So im punting on this one :( + // break; + // } default: printf("Unhandled render command %d\r\n", renderCommand->commandType); @@ -144,7 +200,6 @@ void Clay_Win32_Render(HWND hwnd, Clay_RenderCommandArray renderCommands) EndPaint(hwnd, &ps); } - /* Hacks due to the windows api not making sence to use.... may measure too large, but never too small */