diff options
| author | kkard2 <[email protected]> | 2025-09-06 15:04:28 +0200 |
|---|---|---|
| committer | kkard2 <[email protected]> | 2025-09-06 15:04:28 +0200 |
| commit | ee158180231bafb2d41bd1c6aa5de545556faed8 (patch) | |
| tree | 285b7d8f9fba54f0cf80e52264b35dc90d0eb8c9 | |
| parent | 0b86e0d8e96ccb313bd8033aec013f29fe8a8b99 (diff) | |
add resizing to win32
| -rw-r--r-- | platform_win32.c | 69 | ||||
| -rw-r--r-- | run.bat | 2 |
2 files changed, 44 insertions, 27 deletions
diff --git a/platform_win32.c b/platform_win32.c index a41b135..1d7ff57 100644 --- a/platform_win32.c +++ b/platform_win32.c @@ -1,5 +1,6 @@ #define UNICODE #include <windows.h> +#include <assert.h> #define SPONGE_IMPLEMENTATION #include "sponge.h" @@ -7,10 +8,35 @@ #include "example.h" // TODO(kard): ofc some resizing stuff -#define WIDTH 256 -#define HEIGHT 256 +#define DEFAULT_WIDTH 256 +#define DEFAULT_HEIGHT 256 -static uint32_t pixel_buffer[WIDTH * HEIGHT]; +static size_t pixels_buffer_count; +static sponge_Texture canvas; +static BITMAPINFO bmi; + +int update_canvas(sponge_Texture *canvas, uint32_t new_width, uint32_t new_height) { + size_t new_count = new_width * new_height; + if (new_count > pixels_buffer_count) { + uint32_t *new_pixels_buffer = malloc(new_count * sizeof(uint32_t)); + if (!new_pixels_buffer) + return 0; + free(canvas->pixels); + canvas->pixels = new_pixels_buffer; + pixels_buffer_count = new_count; + } + canvas->width = new_width; + canvas->height = new_height; + canvas->stride = new_width; // TODO(kard): think about this if we want image to stay the same without redraw + + ZeroMemory(&bmi, sizeof(bmi)); + bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bmi.bmiHeader.biWidth = new_width; + bmi.bmiHeader.biHeight = -(int32_t)new_height; // negative = top-down + bmi.bmiHeader.biPlanes = 1; + bmi.bmiHeader.biBitCount = 32; + bmi.bmiHeader.biCompression = BI_RGB; +} LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { @@ -18,6 +44,12 @@ LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage(0); return 0; } + case WM_SIZE: { + uint32_t width = LOWORD(lParam); + uint32_t height = HIWORD(lParam); + // TODO(kard): handle properly? (what does that even mean) + assert(update_canvas(&canvas, width, height)); + } default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } @@ -43,8 +75,8 @@ int WinMain( 0, CLASS_NAME, L"sponge", - WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, // bla bla you can't resize window basically - CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, DEFAULT_WIDTH, DEFAULT_HEIGHT, NULL, NULL, hInstance, @@ -54,23 +86,9 @@ int WinMain( if (hwnd == NULL) return 1; - ShowWindow(hwnd, nShowCmd); - - BITMAPINFO bmi; - ZeroMemory(&bmi, sizeof(bmi)); - bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); - bmi.bmiHeader.biWidth = WIDTH; - bmi.bmiHeader.biHeight = -HEIGHT; // negative = top-down - bmi.bmiHeader.biPlanes = 1; - bmi.bmiHeader.biBitCount = 32; - bmi.bmiHeader.biCompression = BI_RGB; + update_canvas(&canvas, DEFAULT_WIDTH, DEFAULT_HEIGHT); - sponge_Texture canvas = { - .pixels = pixel_buffer, - .width = WIDTH, - .height = HEIGHT, - .stride = WIDTH, - }; + ShowWindow(hwnd, nShowCmd); MSG msg = {0}; BOOL running = TRUE; @@ -88,16 +106,15 @@ int WinMain( if (!running) break; - - // NOTE(kard): ACTUAL RENDERING CODE HERE !!! draw_frame(canvas); HDC hdc = GetDC(hwnd); StretchDIBits( hdc, - 0, 0, WIDTH, HEIGHT, // dest rectangle - 0, 0, WIDTH, HEIGHT, // src rectangle - pixel_buffer, + // TODO(kard): check what happens if canvas is bigger than window (it shouldn't differ currently) + 0, 0, canvas.width, canvas.height, // dest rectangle + 0, 0, canvas.width, canvas.height, // src rectangle + canvas.pixels, &bmi, DIB_RGB_COLORS, SRCCOPY @@ -1,3 +1,3 @@ del out.exe -cl platform_win32.c "examples\%1" user32.lib gdi32.lib /Fe:out.exe +cl platform_win32.c "examples\%1" user32.lib gdi32.lib /W3 /Fe:out.exe out.exe |
