Advertisement
Guest User

004 - Handmade Hero

a guest
Aug 17th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.03 KB | Source Code | 0 0
  1. #include <Windows.h>
  2. #include <stdint.h>
  3.  
  4. //Defining different dimensions of variables.
  5. typedef uint8_t uint8;
  6. typedef uint16_t uint16;
  7. typedef uint32_t uint32;
  8. typedef uint64_t uint64;
  9.  
  10. typedef int8_t int8;
  11. typedef int16_t int16;
  12. typedef int32_t int32;
  13. typedef int64_t int64;
  14.  
  15. #define internal static
  16. #define local_variable static
  17. #define global_variable static
  18.  
  19.  
  20.  
  21. global_variable bool isRunning; //TODO: This is global for now, it will change.
  22. global_variable BITMAPINFO BitmapInfo;
  23. global_variable void *BitmapMemory;
  24. global_variable int BitmapHeight;
  25. global_variable int BitmapWidth;
  26. global_variable int BytesPerPixels = 4;
  27.  
  28. internal void WeirdGradientRenderer(int XOffset, int YOffset)
  29. {
  30.    
  31.     int Width = BitmapWidth;
  32.     int Pitch = Width*BytesPerPixels;
  33.     //int Pitch = BitmapWidth*BytesPerPixels;
  34.     uint8 *Row = (uint8 *)BitmapMemory;
  35.     for(int Y = 0; Y < BitmapHeight; Y++) //We navigate through the Bitmap's Height
  36.     {
  37.         uint8 *Pixel = (uint8 *)Row;
  38.         for(int X = 0; X < BitmapWidth; X++) //WE navigate through the Bitmap's Width
  39.         {
  40.            
  41.             *Pixel = (uint8)(X + XOffset);
  42.             Pixel++;
  43.  
  44.             *Pixel = (uint8)(Y + YOffset);
  45.             Pixel++;
  46.  
  47.             *Pixel = 0;
  48.             Pixel++;
  49.  
  50.             *Pixel = 0;
  51.             Pixel++;
  52.  
  53.         }
  54.         Row += Pitch; //I dont really fully understand this Pitch and Row stuff.
  55.     }
  56.  
  57. }
  58.  
  59.  
  60. internal void Win32ResizeDIBSection(int Height, int Width) //ResizeDEVICEINDEPENDENTBITMAPSection
  61. {
  62.  
  63.     if(BitmapMemory)
  64.     {
  65.         VirtualFree(BitmapMemory, 0, MEM_RELEASE);
  66.     }
  67.  
  68.     BitmapHeight = Height;
  69.     BitmapWidth = Width;
  70.    
  71.     BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
  72.     BitmapInfo.bmiHeader.biWidth = BitmapWidth;
  73.     BitmapInfo.bmiHeader.biHeight = -BitmapHeight;
  74.     BitmapInfo.bmiHeader.biPlanes = 1;
  75.     BitmapInfo.bmiHeader.biBitCount = 32;
  76.     BitmapInfo.bmiHeader.biCompression = BI_RGB;
  77.  
  78.    
  79.     int BitmapMemorySize = (BitmapWidth * BitmapHeight) * BytesPerPixels;
  80.     BitmapMemory = VirtualAlloc(0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE);
  81.  
  82.    
  83.     WeirdGradientRenderer(0, 0);
  84.    
  85. };    
  86.  
  87. internal void Win32UpdateWindow(HDC DeviceContext, RECT *WindowRect, int Width, int Height, int X, int Y)
  88. {
  89.     int WindowWidth = WindowRect->right - WindowRect->left;
  90.     int WindowHeight = WindowRect->bottom - WindowRect->top;
  91.         StretchDIBits(DeviceContext,
  92.                        
  93.                       0, 0, BitmapWidth, BitmapHeight,
  94.                       0, 0, WindowWidth, WindowHeight,
  95.                       BitmapMemory,
  96.                       &BitmapInfo,
  97.                       DIB_RGB_COLORS,
  98.                       SRCCOPY
  99. );
  100.  
  101. };
  102.  
  103.  
  104. //The windowProcedure is created by us, there's a default one aswell on windows (used in this function in the default case)
  105. LRESULT CALLBACK Win32WindowProcedure(
  106.   HWND hWindow, //Window handler, passed in by the WindowClass
  107.   UINT Message, //Messages, passed in by the WindowClass
  108.   WPARAM wParameter,
  109.   LPARAM lParameter
  110. )
  111. {
  112.     LRESULT Result = 0;
  113.     switch(Message) //Switch used to manage different messages
  114.     {
  115.         case(WM_ACTIVATEAPP):
  116.         {
  117.             OutputDebugStringA("WM_ACTIVATEAPP\n");
  118.         } break;
  119.  
  120.         case(WM_CLOSE):
  121.         {
  122.             isRunning = false; //TODO: handle this as a message to the user?
  123.             OutputDebugStringA("WM_CLOSE\n");
  124.         } break;
  125.  
  126.         case(WM_SIZE):
  127.         {
  128.             RECT ClientRectangle;
  129.             GetClientRect(hWindow, &ClientRectangle);
  130.             int Width = ClientRectangle.right - ClientRectangle.left;
  131.             int Height = ClientRectangle.bottom - ClientRectangle.top;
  132.             Win32ResizeDIBSection(Width, Height);
  133.             OutputDebugStringA("WM_SIZE\n");
  134.         } break;
  135.  
  136.         case(WM_DESTROY):
  137.         {
  138.             isRunning = false; //TODO: handle this as an error - recreate window?
  139.             OutputDebugStringA("WM_DESTROY\n");      
  140.         } break;
  141.  
  142.         case(WM_PAINT):
  143.         {
  144.             PAINTSTRUCT Paint;
  145.             HDC DeviceContext = BeginPaint(hWindow, &Paint);
  146.            
  147.             int Width = Paint.rcPaint.right - Paint.rcPaint.left;
  148.             int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
  149.             int X = Paint.rcPaint.left;
  150.             int Y = Paint.rcPaint.top;
  151.  
  152.             RECT ClientRectangle;
  153.             GetClientRect(hWindow, &ClientRectangle);
  154.             Win32UpdateWindow(DeviceContext, &ClientRectangle, Width, Height, X, Y);
  155.  
  156.             EndPaint(hWindow, &Paint);
  157.                              
  158.  
  159.         } break;
  160.  
  161.         default: //Default case, used DefWindowProc to manage the rest of the messages
  162.         {
  163.             //OutputDebugStringA("DEFAULT");
  164.             Result = DefWindowProc(hWindow, Message, wParameter, lParameter);
  165.         } break;
  166.     }
  167.       return(Result);
  168. }
  169.  
  170.  
  171. int WINAPI WinMain(HINSTANCE hInstance,
  172.                    HINSTANCE hPrevInstance,
  173.                    PSTR lpCmdLine,
  174.                    int nCmdShow)
  175. {
  176.  
  177.     //LPCSTR m_title = "HandMade Hero";
  178.     //LPCSTR m_content = "Handmade Hero Content";
  179.    
  180.        
  181.  
  182.         WNDCLASSEX WindowClass = {};
  183.  
  184.         WindowClass.cbSize = sizeof(WNDCLASSEX);
  185.         WindowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
  186.         WindowClass.lpfnWndProc = Win32WindowProcedure; //WindowProcedure function, created by the programmer and passed into the window which sends the parameters to the function and then it processes them giving something in return(result).
  187.         WindowClass.hInstance = hInstance; //Instance passed in by the main function, otherwise we can use the function GetModuleHandle
  188.         WindowClass.hIcon = NULL;
  189.         WindowClass.lpszClassName = "HandMade Hero"; //Name of the window class
  190.         WindowClass.hIconSm = NULL;
  191.  
  192.         if(RegisterClassEx(&WindowClass))
  193.         {
  194.             isRunning = true;
  195.             HWND WindowHandler = CreateWindowEx(0,
  196.                                                 WindowClass.lpszClassName,
  197.                                                 "Handmade Hero",
  198.                                                 WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  199.                                                 CW_USEDEFAULT,
  200.                                                 CW_USEDEFAULT,
  201.                                                 CW_USEDEFAULT,
  202.                                                 CW_USEDEFAULT,
  203.                                                 0,
  204.                                                 0,
  205.                                                 hInstance,
  206.                                                 0 );
  207.  
  208.                 if(WindowHandler)
  209.                 {
  210.                         MSG Message; //Declaring a Message variable
  211.                         while(isRunning) //Infinite loop, will loop forever until we get something that is not a positive result
  212.                         {
  213.                          bool MessageResult = GetMessage(&Message, 0, 0, 0); //Using GetMessage to get the Windows Message and inserting it into &Message
  214.                          if (MessageResult > 0) //If the function returns 1 then it went ok and we can proceed
  215.                          {
  216.                             TranslateMessage(&Message); //Translates the message for a better output
  217.                             DispatchMessage(&Message); //Outputs the message to us (console)
  218.  
  219.                          }
  220.                          else //The function returns -1 or 0 we exit the loop because something went wrong
  221.                          {
  222.                              break;
  223.                          }
  224.                         }
  225.                 }
  226.                 else
  227.                 {
  228.                     //TODO: Logging
  229.                 }
  230.  
  231.         }
  232.         else
  233.         {
  234.  
  235.             //TODO: Logging
  236.         }
  237.  
  238.  
  239.        
  240.  
  241.  
  242.  
  243.  
  244.        
  245.    
  246.  
  247.     //MessageBoxA(0, "Handmade Hero Content", "Handmade Hero", MB_OK | MB_ICONINFORMATION);
  248.  
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement