Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdint.h>
- //Defining different dimensions of variables.
- typedef uint8_t uint8;
- typedef uint16_t uint16;
- typedef uint32_t uint32;
- typedef uint64_t uint64;
- typedef int8_t int8;
- typedef int16_t int16;
- typedef int32_t int32;
- typedef int64_t int64;
- #define internal static
- #define local_variable static
- #define global_variable static
- global_variable bool isRunning; //TODO: This is global for now, it will change.
- global_variable BITMAPINFO BitmapInfo;
- global_variable void *BitmapMemory;
- global_variable int BitmapHeight;
- global_variable int BitmapWidth;
- global_variable int BytesPerPixels = 4;
- internal void WeirdGradientRenderer(int XOffset, int YOffset)
- {
- int Width = BitmapWidth;
- int Pitch = Width*BytesPerPixels;
- //int Pitch = BitmapWidth*BytesPerPixels;
- uint8 *Row = (uint8 *)BitmapMemory;
- for(int Y = 0; Y < BitmapHeight; Y++) //We navigate through the Bitmap's Height
- {
- uint8 *Pixel = (uint8 *)Row;
- for(int X = 0; X < BitmapWidth; X++) //WE navigate through the Bitmap's Width
- {
- *Pixel = (uint8)(X + XOffset);
- Pixel++;
- *Pixel = (uint8)(Y + YOffset);
- Pixel++;
- *Pixel = 0;
- Pixel++;
- *Pixel = 0;
- Pixel++;
- }
- Row += Pitch; //I dont really fully understand this Pitch and Row stuff.
- }
- }
- internal void Win32ResizeDIBSection(int Height, int Width) //ResizeDEVICEINDEPENDENTBITMAPSection
- {
- if(BitmapMemory)
- {
- VirtualFree(BitmapMemory, 0, MEM_RELEASE);
- }
- BitmapHeight = Height;
- BitmapWidth = Width;
- BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
- BitmapInfo.bmiHeader.biWidth = BitmapWidth;
- BitmapInfo.bmiHeader.biHeight = -BitmapHeight;
- BitmapInfo.bmiHeader.biPlanes = 1;
- BitmapInfo.bmiHeader.biBitCount = 32;
- BitmapInfo.bmiHeader.biCompression = BI_RGB;
- int BitmapMemorySize = (BitmapWidth * BitmapHeight) * BytesPerPixels;
- BitmapMemory = VirtualAlloc(0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE);
- WeirdGradientRenderer(0, 0);
- };
- internal void Win32UpdateWindow(HDC DeviceContext, RECT *WindowRect, int Width, int Height, int X, int Y)
- {
- 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
- );
- };
- //The windowProcedure is created by us, there's a default one aswell on windows (used in this function in the default case)
- LRESULT CALLBACK Win32WindowProcedure(
- HWND hWindow, //Window handler, passed in by the WindowClass
- UINT Message, //Messages, passed in by the WindowClass
- WPARAM wParameter,
- LPARAM lParameter
- )
- {
- LRESULT Result = 0;
- switch(Message) //Switch used to manage different messages
- {
- case(WM_ACTIVATEAPP):
- {
- OutputDebugStringA("WM_ACTIVATEAPP\n");
- } break;
- case(WM_CLOSE):
- {
- isRunning = false; //TODO: handle this as a message to the user?
- OutputDebugStringA("WM_CLOSE\n");
- } break;
- case(WM_SIZE):
- {
- RECT ClientRectangle;
- GetClientRect(hWindow, &ClientRectangle);
- int Width = ClientRectangle.right - ClientRectangle.left;
- int Height = ClientRectangle.bottom - ClientRectangle.top;
- Win32ResizeDIBSection(Width, Height);
- OutputDebugStringA("WM_SIZE\n");
- } break;
- case(WM_DESTROY):
- {
- isRunning = false; //TODO: handle this as an error - recreate window?
- OutputDebugStringA("WM_DESTROY\n");
- } break;
- case(WM_PAINT):
- {
- PAINTSTRUCT Paint;
- HDC DeviceContext = BeginPaint(hWindow, &Paint);
- int Width = Paint.rcPaint.right - Paint.rcPaint.left;
- int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
- int X = Paint.rcPaint.left;
- int Y = Paint.rcPaint.top;
- RECT ClientRectangle;
- GetClientRect(hWindow, &ClientRectangle);
- Win32UpdateWindow(DeviceContext, &ClientRectangle, Width, Height, X, Y);
- EndPaint(hWindow, &Paint);
- } break;
- default: //Default case, used DefWindowProc to manage the rest of the messages
- {
- //OutputDebugStringA("DEFAULT");
- Result = DefWindowProc(hWindow, Message, wParameter, lParameter);
- } break;
- }
- return(Result);
- }
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- PSTR lpCmdLine,
- int nCmdShow)
- {
- //LPCSTR m_title = "HandMade Hero";
- //LPCSTR m_content = "Handmade Hero Content";
- WNDCLASSEX WindowClass = {};
- WindowClass.cbSize = sizeof(WNDCLASSEX);
- WindowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
- 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).
- WindowClass.hInstance = hInstance; //Instance passed in by the main function, otherwise we can use the function GetModuleHandle
- WindowClass.hIcon = NULL;
- WindowClass.lpszClassName = "HandMade Hero"; //Name of the window class
- WindowClass.hIconSm = NULL;
- if(RegisterClassEx(&WindowClass))
- {
- isRunning = true;
- HWND WindowHandler = CreateWindowEx(0,
- WindowClass.lpszClassName,
- "Handmade Hero",
- WS_OVERLAPPEDWINDOW|WS_VISIBLE,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 0,
- 0,
- hInstance,
- 0 );
- if(WindowHandler)
- {
- MSG Message; //Declaring a Message variable
- while(isRunning) //Infinite loop, will loop forever until we get something that is not a positive result
- {
- bool MessageResult = GetMessage(&Message, 0, 0, 0); //Using GetMessage to get the Windows Message and inserting it into &Message
- if (MessageResult > 0) //If the function returns 1 then it went ok and we can proceed
- {
- TranslateMessage(&Message); //Translates the message for a better output
- DispatchMessage(&Message); //Outputs the message to us (console)
- }
- else //The function returns -1 or 0 we exit the loop because something went wrong
- {
- break;
- }
- }
- }
- else
- {
- //TODO: Logging
- }
- }
- else
- {
- //TODO: Logging
- }
- //MessageBoxA(0, "Handmade Hero Content", "Handmade Hero", MB_OK | MB_ICONINFORMATION);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement