Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #define internal static
- #define local_persist static
- #define global_variable static
- // TODO global for now
- global_variable bool Running;
- global_variable BITMAPINFO BitmapInfo;
- global_variable void *BitmapMemory;
- global_variable int BitmapWidth;
- global_variable int BitmapHeight;
- internal void
- Win32ResizeDIBSection(int Width, int Height){
- if (BitmapMemory){
- VirtualFree(BitmapMemory, 0, MEM_RELEASE);
- }
- // see global above; BITMAPINFO BitmapInfo;
- BitmapWidth = Width;
- BitmapHeight = Height;
- BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
- BitmapInfo.bmiHeader.biWidth = BitmapWidth;
- BitmapInfo.bmiHeader.biHeight = -BitmapHeight; //biHeight starts from bottom, so Height must be flipped
- BitmapInfo.bmiHeader.biPlanes = 1;
- BitmapInfo.bmiHeader.biBitCount = 32;
- BitmapInfo.bmiHeader.biCompression = BI_RGB;
- int bytesPerPixel = 4;
- int BitmapMemorySize = bytesPerPixel * (BitmapWidth*BitmapHeight);
- BitmapMemory = VirtualAlloc(0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE);
- }
- internal void
- Win32UpdateWindow(HDC DeviceContext, RECT * WindowRect, int X, int Y, int Width, int Height){
- int WindowWidth = WindowRect->right - WindowRect->left;
- int WindowHeight = WindowRect->bottom - WindowRect->top;
- StretchDIBits(DeviceContext,
- 0, 0,BitmapWidth, BitmapHeight,
- 0, 0, WindowWidth, WindowHeight,
- BitmapMemory,
- &BitmapInfo,
- DIB_RGB_COLORS, SRCCOPY);
- }
- LRESULT CALLBACK
- Win32MainWindowCallback(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam){
- LRESULT Result = 0;
- switch (Message){
- case WM_SIZE:
- {
- RECT ClientRect;
- GetClientRect(Window, &ClientRect);
- int Width = ClientRect.right - ClientRect.left;
- int Height = ClientRect.bottom - ClientRect.top;
- Win32ResizeDIBSection(Width, Height);
- OutputDebugStringA("WM_SIZE \n");
- }break;
- case WM_DESTROY:
- {
- Running = false;
- OutputDebugStringA("WM_DESTROY \n");
- }break;
- case WM_CLOSE:
- {
- Running = false;
- }break;
- case WM_ACTIVATEAPP:
- {
- OutputDebugStringA("WM_ACTIVATE \n");
- }break;
- case WM_PAINT:
- {
- PAINTSTRUCT Paint;
- HDC DeviceContext = BeginPaint(Window, &Paint);
- int X = Paint.rcPaint.left;
- int Y = Paint.rcPaint.top;
- int Width = Paint.rcPaint.right - Paint.rcPaint.left;
- int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
- RECT ClientRect;
- GetClientRect(Window, &ClientRect);
- Win32UpdateWindow(DeviceContext, &ClientRect, X, Y, Width, Height);
- local_persist DWORD Operation = WHITENESS;
- PatBlt(DeviceContext, X, Y, Width, Height, Operation);
- if (Operation == WHITENESS)
- {
- Operation = BLACKNESS;
- }
- else
- {
- Operation = WHITENESS;
- }
- EndPaint(Window, &Paint);
- }break;
- default:
- {
- OutputDebugStringA("Default \n");
- Result = DefWindowProc(Window, Message, WParam, LParam);
- }break;
- }
- return(Result);
- }
- int CALLBACK
- Win32WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode){
- WNDCLASS WindowClass = {};
- WindowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
- WindowClass.lpfnWndProc = Win32MainWindowCallback;
- // WindowClass.cbClsExtra = ;
- // WindowClass.cbWndExtra = ;
- WindowClass.hInstance = Instance;
- // WindowClass.hIcon;
- // WindowClass.hCursor;
- // WindowClass.hbrBackground;
- // WindowClass.lpszMenuName;
- WindowClass.lpszClassName = (LPCWSTR)L"HandmadeHeroWindowClass";
- if (RegisterClass(&WindowClass)){
- HWND WindowHandle = CreateWindowEx(
- 0,
- WindowClass.lpszClassName,
- (LPCWSTR)L"Handmade Hero",
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 0,
- 0,
- Instance,
- 0);
- if (WindowHandle){
- while(Running){
- MSG Message;
- BOOL MessageResult = GetMessage(&Message, 0, 0, 0);
- if (MessageResult > 0){
- TranslateMessage(&Message);
- DispatchMessage(&Message);
- }
- else{
- break;
- }
- }
- }
- else{
- }
- }
- else{
- }
- MessageBox(0, (LPCWSTR)L"This is Handmade Hero", (LPCWSTR)L"Handmade Hero", MB_OK | MB_ICONINFORMATION);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement