-
-
Save dezren39/5c09a0c4003e5941fd797eaa97ec26c2 to your computer and use it in GitHub Desktop.
Minimal code example for creating a window to plot pixels to
This file contains hidden or 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
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment