Skip to content

Instantly share code, notes, and snippets.

@mattiasgustavsson
Created March 5, 2024 19:18
Show Gist options
  • Select an option

  • Save mattiasgustavsson/f98fe37c3ea360b8ff200c22314489e3 to your computer and use it in GitHub Desktop.

Select an option

Save mattiasgustavsson/f98fe37c3ea360b8ff200c22314489e3 to your computer and use it in GitHub Desktop.
Minimal code example for creating a window to plot pixels to
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <windows.h>
#pragma comment( lib, "user32.lib" )
#pragma comment( lib, "gdi32.lib" )
#define SCRW 640
#define SCRH 480
uint32_t scrbuf[ SCRW * SCRH ];
LRESULT window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) {
if( msg == WM_CLOSE ) {
DestroyWindow( hwnd );
} else if( msg == WM_DESTROY ) {
PostQuitMessage( 0 );
}
return DefWindowProc( hwnd, msg, wparam, lparam );
};
int main( int argc, char* argv[] ) {
// register window class
WNDCLASS wc = { CS_CLASSDC, window_proc };
wc.lpszClassName = TEXT( "MyClassName" );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
RegisterClass( &wc );
// create window
HWND hwnd = CreateWindow( wc.lpszClassName, TEXT( "Window Title" ),
WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, SCRW, SCRH, NULL, NULL, NULL, NULL );
// make sure we update at least every 16ms
SetTimer( NULL, 0, 16, NULL );
bool exit = false;
while( !exit ) {
// windows message handling
MSG msg;
while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) {
if( msg.message == WM_QUIT ) exit = true;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
// display our screenbuffer
BITMAPINFOHEADER bmi = { sizeof( bmi ), SCRW, -SCRH, 1, 32 };
HDC dc = GetDC( hwnd );
SetDIBitsToDevice( dc, 0, 0, SCRW, SCRH, 0, 0, 0u, SCRH, scrbuf,
(BITMAPINFO*)&bmi, DIB_RGB_COLORS );
ReleaseDC( hwnd, dc );
// plot random pixels in our buffer
int x = rand() % SCRW;
int y = rand() % SCRH;
uint32_t c = ( rand() << 15 ) | rand(); // random rgb color
scrbuf[ x + y * SCRW ] = c;
}
return 0;
}
@mrange
Copy link
Copy Markdown

mrange commented Mar 6, 2024

Very cool!

@DeepDarkDweller
Copy link
Copy Markdown

This is cool! I'm gonna play around with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment