Files
handmade_hero/code/win32_handmade.cpp
2026-01-25 15:05:05 +02:00

130 lines
3.8 KiB
C++

#include <stdio.h>
#include <windows.h>
#include <wingdi.h>
#define internal static
#define local_persist static
#define global_variable static
// NOTE: this is a global for now
global_variable bool running;
global_variable BITMAPINFO bitmapInfo;
global_variable void *bitmapMemory;
global_variable HBITMAP bitmapHandle;
global_variable HDC bitmapDeviceContext;
internal void Win32ResizeDIBSection(int width, int height) {
// TODO: bulletproof this.
// perhaps don't free first, free after, then free first if that fails
if (bitmapHandle) {
DeleteObject(bitmapHandle);
}
if (!bitmapDeviceContext) {
// TODO: should we recreate these under certain special circumstances?
bitmapDeviceContext = CreateCompatibleDC(0);
}
bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo.bmiHeader);
bitmapInfo.bmiHeader.biWidth = width;
bitmapInfo.bmiHeader.biHeight = height;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 24;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapHandle = CreateDIBSection(bitmapDeviceContext, &bitmapInfo,
DIB_RGB_COLORS, &bitmapMemory, 0, 0);
}
internal void Win32UpdateWindow(HDC deviceContext, int x, int y, int width,
int height) {
StretchDIBits(deviceContext, x, y, width, height, x, y, width, height,
bitmapMemory, &bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
}
LRESULT CALLBACK Win32MainWindowCallback(HWND window, UINT message,
WPARAM wParam, LPARAM lParam) {
LRESULT res = 0;
switch (message) {
case WM_SIZE: {
RECT rect;
GetClientRect(window, &rect);
int width = rect.right - rect.left;
int height = rect.top - rect.bottom;
Win32ResizeDIBSection(width, height);
printf("WM_SIZE\n");
} break;
case WM_DESTROY: {
running = false;
// TODO: handle this as an error - recreate window?
printf("WM_DESTROY\n");
} break;
case WM_CLOSE: {
// TODO: handle this as a message to the user?
running = false;
printf("WM_CLOSE\n");
} break;
case WM_PAINT: {
PAINTSTRUCT paint;
HDC deviceContext = BeginPaint(window, &paint);
int height = paint.rcPaint.bottom - paint.rcPaint.top;
int width = paint.rcPaint.right - paint.rcPaint.left;
int x = paint.rcPaint.left;
int y = paint.rcPaint.top;
Win32UpdateWindow(deviceContext, x, y, width, height);
EndPaint(window, &paint);
} break;
case WM_ACTIVATEAPP: {
printf("WM_ACTIVATEAPP\n");
} break;
default: {
// printf("WM_ACTIVATEAPP\n");
res = DefWindowProc(window, message, wParam, lParam);
} break;
}
return res;
}
#undef RegisterClass
#undef CreateWindowEx
#undef GetMessage
#undef DispatchMessage
int CALLBACK WinMain(HINSTANCE instance, HINSTANCE prevInstance,
LPSTR commandLine, int showCode) {
WNDCLASS WindowClass = {0};
WindowClass.lpfnWndProc = Win32MainWindowCallback;
// WindowClass.cbClsExtra = ;
// WindowClass.cbWndExtra = ;
WindowClass.hInstance = instance;
// WindowClass.hIcon = ;
// WindowClass.hCursor = ;
// WindowClass.lpszMenuName = ;
WindowClass.lpszClassName = "HandmadeHeroWindowClass";
if (RegisterClassA(&WindowClass)) {
HWND windowHandle = CreateWindowExA(
0, WindowClass.lpszClassName, "Handmade Hero",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, instance, 0);
if (windowHandle) {
running = true;
MSG message;
while (running) {
BOOL msgResult = GetMessageA(&message, 0, 0, 0);
if (msgResult > 0) {
TranslateMessage(&message);
DispatchMessageA(&message);
} else {
break;
}
}
} else {
// TODO: log?
}
} else {
// TODO: log?
}
return (0);
}