Guest User

DisplayCode

a guest
Oct 14th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | Software | 0 0
  1. #include <windows.h>
  2.  
  3. #define local_function static
  4. #define local_persist static
  5. #define global_variable static
  6.  
  7. global_variable bool running = true;
  8. global_variable BITMAPINFO BitmapInfo;
  9. global_variable void *BitmapMemory;
  10. global_variable HBITMAP BitmapHandle;
  11. global_variable HDC BitmapDeviceContext;
  12.  
  13. local_function void ResizeDIBSection(int width, int height) {
  14.  
  15.     if (BitmapHandle) {
  16.         DeleteObject(BitmapHandle);
  17.     }
  18.  
  19.     if (!BitmapDeviceContext) {
  20.         BitmapDeviceContext = CreateCompatibleDC(0);
  21.     }
  22.  
  23.     BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
  24.     BitmapInfo.bmiHeader.biWidth = width;
  25.     BitmapInfo.bmiHeader.biHeight = height;
  26.     BitmapInfo.bmiHeader.biPlanes = 1;
  27.     BitmapInfo.bmiHeader.biBitCount = 32;
  28.     BitmapInfo.bmiHeader.biCompression = BI_RGB;
  29.  
  30.  
  31.     BitmapHandle = CreateDIBSection(
  32.         BitmapDeviceContext,
  33.         &BitmapInfo,
  34.         DIB_RGB_COLORS,
  35.         &BitmapMemory,
  36.         0, 0);
  37. };
  38.  
  39. local_function void UpdateMyWindow(HDC DeviceContext, int X, int Y, int Width, int Height) {
  40.     StretchDIBits(
  41.         DeviceContext,
  42.          X, Y, Width, Height,
  43.          X, Y, Width, Height,
  44.          &BitmapMemory,
  45.          &BitmapInfo,
  46.          DIB_RGB_COLORS,
  47.          SRCCOPY
  48.     );
  49. }
  50.  
  51.  
  52. LRESULT Wndproc(
  53.     HWND Window,
  54.     UINT Message,
  55.     WPARAM unnamedParam3,
  56.     LPARAM unnamedParam4
  57. )
  58. {
  59.  
  60.     switch (Message) {
  61.         case WM_DESTROY:
  62.             running = false;
  63.             break;
  64.  
  65.         case WM_CLOSE:
  66.             running = false;
  67.             break;
  68.  
  69.         case WM_SIZE: {
  70.             RECT clientRect;
  71.             GetClientRect(Window, &clientRect);
  72.             int Width = clientRect.right - clientRect.left;
  73.             int Height = clientRect.bottom - clientRect.top;
  74.             ResizeDIBSection(Width, Height);
  75.         }
  76.             break;
  77.            
  78.  
  79.         case WM_PAINT:
  80.         {
  81.             PAINTSTRUCT Paint;
  82.             HDC DeviceContext = BeginPaint(Window, &Paint);
  83.  
  84.             int X = Paint.rcPaint.left;
  85.             int Y = Paint.rcPaint.top;
  86.             int Width = Paint.rcPaint.right - Paint.rcPaint.left;
  87.             int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
  88.  
  89.             UpdateMyWindow(DeviceContext, X, Y, Width, Height);
  90.             EndPaint(Window, &Paint);
  91.  
  92.         };
  93.         break;
  94.          
  95.  
  96.        
  97.         default:
  98.             return DefWindowProcA(Window, Message, unnamedParam3, unnamedParam4);
  99.             break;
  100.     }
  101. }
  102.  
  103. int WINAPI WinMain(
  104.     HINSTANCE Instance,
  105.     HINSTANCE PrevInstance,
  106.     LPSTR lpCmdLine,
  107.     int nCmdShow
  108. ) {
  109.  
  110.     WNDCLASSEXA windowClass = {};
  111.     windowClass.cbSize = sizeof(WNDCLASSEXA);
  112.     windowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
  113.     windowClass.lpfnWndProc = Wndproc;
  114.     windowClass.hInstance = Instance;
  115.     windowClass.lpszClassName = "Game";
  116.  
  117.  
  118.     if (RegisterClassExA(&windowClass)) {
  119.         HWND windowHandle = CreateWindowExA(
  120.             0,
  121.             windowClass.lpszClassName,
  122.             "My Window",
  123.             WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  124.             CW_USEDEFAULT,
  125.             CW_USEDEFAULT,
  126.             CW_USEDEFAULT,
  127.             CW_USEDEFAULT,
  128.             0,
  129.             0,
  130.             Instance,
  131.             0
  132.         );
  133.         if (windowHandle) {
  134.             MSG Message;
  135.             while (running) {
  136.                 BOOL messageResult = GetMessage(&Message, 0, 0, 0);
  137.                 if (messageResult > 0) {
  138.                     TranslateMessage(&Message);
  139.                     DispatchMessageA(&Message);
  140.                 }
  141.                 else {
  142.                     //In here the Data saving and everything else is handled.
  143.                     break;
  144.                 }
  145.             }
  146.              
  147.         }
  148.         else {
  149.             MessageBoxA(0, "windowHandle failure", "Bruh", 0);
  150.             //Add error handling;
  151.         }
  152.     }
  153.     else {
  154.         //Error Handling
  155.         MessageBoxA(0, "RegisterClass failure", "Bruh", 0);
  156.     }
  157.        
  158. };
  159.  
Advertisement
Add Comment
Please, Sign In to add comment