Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created December 25, 2023 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skeeto/0548e854379dab34f81782d04bfc3f1b to your computer and use it in GitHub Desktop.
Save skeeto/0548e854379dab34f81782d04bfc3f1b to your computer and use it in GitHub Desktop.
// $ cc -mwindows stretchdibits.c
#include <windows.h>
enum {
WIDTH = 1280,
HEIGHT = 720,
};
static unsigned char *pixels;
int blit(HDC dc)
{
BITMAPINFO info = {
.bmiHeader = {
.biSize = sizeof(info.bmiHeader),
.biWidth = WIDTH,
.biHeight = -HEIGHT,
.biPlanes = 1,
.biBitCount = 32,
.biCompression = BI_RGB,
},
};
int r = StretchDIBits(
dc,
0, 0, WIDTH, HEIGHT,
0, 0, WIDTH, HEIGHT,
pixels,
&info,
DIB_RGB_COLORS,
SRCCOPY
);
return r;
}
LRESULT CALLBACK mainLoop(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
HDC dc = GetDC(hwnd);
blit(dc);
ReleaseDC(hwnd, dc);
break;
}
return DefWindowProcW(hwnd, msg, wparam, lparam);
}
HWND createWindow(void)
{
wchar_t *className = L"buggy.example.window";
WNDCLASSEXW wndclass = {
.cbSize = sizeof(wndclass),
.lpfnWndProc = mainLoop,
.lpszClassName = className,
};
RegisterClassExW(&wndclass);
HWND w = CreateWindowExW(
0, className, L"title",
WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
WIDTH, HEIGHT,
0, 0, 0, 0
);
return w;
}
int main(void)
{
// Place it at the same address as Go
pixels = VirtualAlloc(
(void *)0xc000100000,
WIDTH*HEIGHT*4,
MEM_RESERVE|MEM_COMMIT,
PAGE_READWRITE
);
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
pixels[y*WIDTH*4 + x*4 + 0] = 0x00;
pixels[y*WIDTH*4 + x*4 + 1] = 0x00;
pixels[y*WIDTH*4 + x*4 + 2] = 0xff;
pixels[y*WIDTH*4 + x*4 + 3] = 0xff;
}
}
createWindow();
for (MSG msg; GetMessageW(&msg, 0, 0, 0);) {
if (msg.message == WM_QUIT) {
break;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment