From f33cd3a03ceaceda089e8ef658650e52a70938a4 Mon Sep 17 00:00:00 2001 From: kkard2 Date: Mon, 1 Sep 2025 20:17:51 +0200 Subject: init --- .gitignore | 2 + platform_win32.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ run.bat | 3 ++ softrender.h | 35 +++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 platform_win32.c create mode 100644 run.bat create mode 100644 softrender.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d969f6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.exe +*.obj diff --git a/platform_win32.c b/platform_win32.c new file mode 100644 index 0000000..7ebd16c --- /dev/null +++ b/platform_win32.c @@ -0,0 +1,115 @@ +#define UNICODE +#include + +#define SOFTRENDER_IMPLEMENTATION +#include "softrender.h" + +// TODO(kard): ofc some resizing stuff +#define WIDTH 256 +#define HEIGHT 256 + +static uint32_t pixel_buffer[WIDTH * HEIGHT]; + +LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { + switch (uMsg) { + case WM_DESTROY: { + PostQuitMessage(0); + return 0; + } + default: + return DefWindowProc(hwnd, uMsg, wParam, lParam); + } +} + +int WinMain( + HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPSTR lpCmdLine, + int nShowCmd +) { + const wchar_t CLASS_NAME[] = L"softrender"; + + WNDCLASS wc = { 0 }; + + wc.lpfnWndProc = WindowProc; + wc.hInstance = hInstance; + wc.lpszClassName = CLASS_NAME; + + RegisterClass(&wc); + + HWND hwnd = CreateWindowEx( + 0, + CLASS_NAME, + L"softrender", + WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, // bla bla you can't resize window basically + CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, + NULL, + NULL, + hInstance, + NULL + ); + + 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; + + soft_Canvas canvas = { + .pixels = pixel_buffer, + .width = WIDTH, + .height = HEIGHT, + .stride = WIDTH, + }; + + MSG msg = {0}; + BOOL running = TRUE; + + while (running) { + while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { + if (msg.message == WM_QUIT) { + running = FALSE; + break; + } + + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + if (!running) break; + + + // NOTE(kard): ACTUAL RENDERING CODE HERE !!! + soft_clear(canvas, 0xFF000000); + soft_draw_rect(canvas, 80, 70, 80 + 20, 70 + 40, 0xFFFF00FF); + soft_draw_rect(canvas, 140, 70, 140 + 20, 70 + 40, 0xFFFF00FF); + + + HDC hdc = GetDC(hwnd); + StretchDIBits( + hdc, + 0, 0, WIDTH, HEIGHT, // dest rectangle + 0, 0, WIDTH, HEIGHT, // src rectangle + pixel_buffer, + &bmi, + DIB_RGB_COLORS, + SRCCOPY + ); + + InvalidateRect(hwnd, NULL, FALSE); + UpdateWindow(hwnd); + + // TODO(kard): proper frame limiting + Sleep(16); + } + + return (int) msg.wParam; +} diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..f6cf655 --- /dev/null +++ b/run.bat @@ -0,0 +1,3 @@ +del platform_win32.exe +cl platform_win32.c user32.lib gdi32.lib +platform_win32.exe diff --git a/softrender.h b/softrender.h new file mode 100644 index 0000000..e417ab2 --- /dev/null +++ b/softrender.h @@ -0,0 +1,35 @@ +#include + +typedef struct { + // TODO(kard): define byte order + uint32_t *pixels; + uint32_t width; + uint32_t height; + uint32_t stride; +} soft_Canvas; + +void soft_clear(soft_Canvas c, uint32_t color); +void soft_draw_rect(soft_Canvas c, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t color); + +// TODO(kard): prefix stripping + +#ifdef SOFTRENDER_IMPLEMENTATION + +void soft_clear(soft_Canvas c, uint32_t color) { + soft_draw_rect(c, 0, 0, c.width - 1, c.height - 1, color); +} + +// TODO(kard): probably bounds checking +// TODO(kard): alpha blending maybe +void soft_draw_rect(soft_Canvas c, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t color) { + uint32_t *row = c.pixels + (y0 * c.stride); + + for (uint32_t y = y0; y <= y1; y++, row += c.stride) { + for (uint32_t x = x0; x <= x1; x++) { + row[x] = color; + } + } +} + +#endif // SOFTRENDER_IMPLEMENTATION + -- cgit v1.3.1