Advertisement
DoubleW

Handmade Hero day 4 linker error

Jul 13th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.19 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. #define internal static
  4. #define local_persist static
  5. #define global_variable static
  6.  
  7. // TODO global for now
  8. global_variable bool Running;
  9.  
  10.  
  11. global_variable BITMAPINFO BitmapInfo;
  12. global_variable void *BitmapMemory;
  13. global_variable int BitmapWidth;
  14. global_variable int BitmapHeight;
  15.  
  16. internal void
  17. Win32ResizeDIBSection(int Width, int Height){
  18.  
  19.     if (BitmapMemory){
  20.         VirtualFree(BitmapMemory, 0, MEM_RELEASE);
  21.     }
  22.    
  23.     // see global above; BITMAPINFO BitmapInfo;
  24.  
  25.     BitmapWidth = Width;
  26.     BitmapHeight = Height;
  27.  
  28.     BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
  29.     BitmapInfo.bmiHeader.biWidth = BitmapWidth;
  30.     BitmapInfo.bmiHeader.biHeight = -BitmapHeight; //biHeight starts from bottom, so Height must be flipped
  31.     BitmapInfo.bmiHeader.biPlanes = 1;
  32.     BitmapInfo.bmiHeader.biBitCount = 32;
  33.     BitmapInfo.bmiHeader.biCompression = BI_RGB;
  34.  
  35.     int bytesPerPixel = 4;
  36.     int BitmapMemorySize = bytesPerPixel * (BitmapWidth*BitmapHeight);
  37.     BitmapMemory = VirtualAlloc(0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE);
  38. }
  39.  
  40. internal void
  41. Win32UpdateWindow(HDC DeviceContext, RECT * WindowRect, int X, int Y, int Width, int Height){
  42.    
  43.     int WindowWidth = WindowRect->right - WindowRect->left;
  44.     int WindowHeight = WindowRect->bottom - WindowRect->top;
  45.  
  46.     StretchDIBits(DeviceContext,
  47.         0, 0,BitmapWidth, BitmapHeight,
  48.         0, 0, WindowWidth, WindowHeight,
  49.         BitmapMemory,
  50.         &BitmapInfo,
  51.         DIB_RGB_COLORS, SRCCOPY);
  52. }
  53.  
  54. LRESULT CALLBACK
  55. Win32MainWindowCallback(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam){
  56.    
  57.     LRESULT Result = 0;
  58.    
  59.     switch (Message){
  60.         case WM_SIZE:
  61.         {
  62.             RECT ClientRect;
  63.             GetClientRect(Window, &ClientRect);
  64.             int Width = ClientRect.right - ClientRect.left;
  65.             int Height = ClientRect.bottom - ClientRect.top;
  66.             Win32ResizeDIBSection(Width, Height);
  67.             OutputDebugStringA("WM_SIZE \n");
  68.         }break;
  69.         case WM_DESTROY:
  70.         {
  71.             Running = false;
  72.             OutputDebugStringA("WM_DESTROY \n");
  73.         }break;
  74.         case WM_CLOSE:
  75.         {
  76.             Running = false;
  77.         }break;
  78.         case WM_ACTIVATEAPP:
  79.         {
  80.             OutputDebugStringA("WM_ACTIVATE \n");
  81.         }break;
  82.         case WM_PAINT:
  83.         {
  84.  
  85.             PAINTSTRUCT Paint;
  86.             HDC DeviceContext = BeginPaint(Window, &Paint);
  87.             int X = Paint.rcPaint.left;
  88.             int Y = Paint.rcPaint.top;
  89.             int Width = Paint.rcPaint.right - Paint.rcPaint.left;
  90.             int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
  91.            
  92.             RECT ClientRect;
  93.             GetClientRect(Window, &ClientRect);
  94.  
  95.             Win32UpdateWindow(DeviceContext, &ClientRect, X, Y, Width, Height);
  96.             local_persist DWORD Operation = WHITENESS;
  97.             PatBlt(DeviceContext, X, Y, Width, Height, Operation);
  98.             if (Operation == WHITENESS)
  99.             {
  100.                 Operation = BLACKNESS;
  101.             }
  102.             else
  103.             {
  104.                 Operation = WHITENESS;
  105.             }
  106.  
  107.             EndPaint(Window, &Paint);
  108.         }break;
  109.         default:
  110.         {
  111.             OutputDebugStringA("Default \n");
  112.             Result = DefWindowProc(Window, Message, WParam, LParam);
  113.         }break;
  114.     }
  115.     return(Result);
  116. }
  117.  
  118. int CALLBACK
  119. Win32WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode){
  120.  
  121.     WNDCLASS WindowClass = {};
  122.         WindowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
  123.         WindowClass.lpfnWndProc = Win32MainWindowCallback;
  124. //      WindowClass.cbClsExtra = ;
  125. //      WindowClass.cbWndExtra = ;
  126.         WindowClass.hInstance = Instance;
  127. //      WindowClass.hIcon;
  128. //      WindowClass.hCursor;
  129. //      WindowClass.hbrBackground;
  130. //      WindowClass.lpszMenuName;
  131.         WindowClass.lpszClassName = (LPCWSTR)L"HandmadeHeroWindowClass";
  132.    
  133.         if (RegisterClass(&WindowClass)){
  134.             HWND WindowHandle = CreateWindowEx(
  135.                 0,
  136.                 WindowClass.lpszClassName,
  137.                 (LPCWSTR)L"Handmade Hero",
  138.                 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  139.                 CW_USEDEFAULT,
  140.                 CW_USEDEFAULT,
  141.                 CW_USEDEFAULT,
  142.                 CW_USEDEFAULT,
  143.                 0,
  144.                 0,
  145.                 Instance,
  146.                 0);
  147.             if (WindowHandle){
  148.                     while(Running){
  149.                         MSG Message;
  150.                         BOOL MessageResult = GetMessage(&Message, 0, 0, 0);
  151.                         if (MessageResult > 0){
  152.                             TranslateMessage(&Message);
  153.                             DispatchMessage(&Message);
  154.                         }
  155.                         else{
  156.                             break;
  157.                         }
  158.                        
  159.                     }
  160.                    
  161.             }
  162.             else{
  163.            
  164.             }
  165.         }
  166.         else{
  167.         }
  168.  
  169.     MessageBox(0, (LPCWSTR)L"This is Handmade Hero", (LPCWSTR)L"Handmade Hero", MB_OK | MB_ICONINFORMATION);
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement