Created
December 25, 2023 20:15
-
-
Save skeeto/0548e854379dab34f81782d04bfc3f1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ 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