Guest User

win32_ProjectSeijun

a guest
Feb 6th, 2020
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdint.h>
  3.  
  4. #define internal static
  5. #define local_persist static
  6. #define global_variable static
  7.  
  8. typedef int8_t int8;
  9. typedef int16_t int16;
  10. typedef int32_t int32;
  11. typedef int64_t int64;
  12.  
  13. typedef uint8_t uint8;
  14. typedef uint16_t uint16;
  15. typedef uint32_t uint32;
  16. typedef uint64_t uint64;
  17.  
  18.  
  19. global_variable bool Running;
  20.  
  21. global_variable BITMAPINFO BitmapInfo;
  22. global_variable void *BitmapMemory;
  23. global_variable int BitmapWidth;
  24. global_variable int BitmapHeight;
  25. global_variable int BytesPerPixel = 4;
  26.  
  27. internal void RenderWeirdGradient(int XOffset, int YOffset)
  28. {
  29. int Width = BitmapWidth;
  30. int Height = BitmapHeight;
  31.  
  32. int Pitch = Width*BytesPerPixel;
  33. uint8 *Row = (uint8 *)BitmapMemory;
  34. for(int Y = 0; Y < Height; ++Y)
  35. {
  36. uint32 *Pixel = (uint32 *)Row;
  37. for(int X = 0; X < Width; ++X)
  38. {
  39. uint8 Blue= (X + XOffset);
  40. uint8 Green = (Y + YOffset);
  41.  
  42. *Pixel++ = ((Green << 8) | Blue);
  43. }
  44.  
  45. Row += Pitch;
  46. }
  47. }
  48.  
  49. internal void
  50. Win32ResizeDIBSection(int Width, int Height)
  51. {
  52. if(BitmapMemory)
  53. {
  54. VirtualFree(BitmapMemory, 0, MEM_RELEASE);
  55. }
  56.  
  57. BitmapWidth = Width;
  58. BitmapHeight = Height;
  59.  
  60. BITMAPINFO BitmapInfo;
  61. BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
  62. BitmapInfo.bmiHeader.biWidth = BitmapWidth;
  63. BitmapInfo.bmiHeader.biHeight = -BitmapHeight;
  64. BitmapInfo.bmiHeader.biPlanes = 1;
  65. BitmapInfo.bmiHeader.biBitCount = 32;
  66. BitmapInfo.bmiHeader.biCompression = BI_RGB;
  67.  
  68. int BitmapMemorySize = (BitmapWidth*BitmapHeight)*BytesPerPixel;
  69. BitmapMemory = VirtualAlloc(0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE);
  70.  
  71. RenderWeirdGradient(0, 0);
  72.  
  73. }
  74.  
  75. internal void
  76. Win32UpdateWindow(HDC DeviceContext, RECT *ClientRect, int X, int Y, int Width, int Height)
  77. {
  78. int WindowWidth = ClientRect->right - ClientRect->left;
  79. int WindowHeight = ClientRect->bottom - ClientRect->top;
  80.  
  81. StretchDIBits(
  82. DeviceContext,
  83. 0, 0, BitmapWidth, BitmapHeight,
  84. 0, 0, WindowWidth, WindowHeight,
  85. BitmapMemory,
  86. &BitmapInfo,
  87. DIB_RGB_COLORS, SRCCOPY);
  88. }
  89.  
  90.  
  91. LRESULT CALLBACK Win32MainWindowCallback(
  92. HWND Window,
  93. UINT Message,
  94. WPARAM WParam,
  95. LPARAM LParam)
  96. {
  97. LRESULT Result = 0;
  98.  
  99. switch(Message)
  100. {
  101. case WM_SIZE:
  102. {
  103. RECT ClientRect;
  104. GetClientRect(Window, &ClientRect);
  105. int Height = ClientRect.bottom - ClientRect.top;
  106. int Width = ClientRect.right - ClientRect.left;
  107. Win32ResizeDIBSection(Width, Height);
  108. } break;
  109.  
  110. case WM_DESTROY:
  111. {
  112. Running = false;
  113. } break;
  114.  
  115. case WM_CLOSE:
  116. {
  117. Running = false;
  118. } break;
  119.  
  120. case WM_ACTIVATEAPP:
  121. {
  122. OutputDebugStringA("WM_ACTIVATEAPP\n");
  123. } break;
  124.  
  125. case WM_PAINT:
  126. {
  127. PAINTSTRUCT Paint;
  128. HDC DeviceContext = BeginPaint(Window, &Paint);
  129. int Y = Paint.rcPaint.left;
  130. int X = Paint.rcPaint.top;
  131. int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
  132. int Width = Paint.rcPaint.right - Paint.rcPaint.left;
  133.  
  134. RECT ClientRect;
  135. GetClientRect(Window, &ClientRect);
  136.  
  137. Win32UpdateWindow(DeviceContext, &ClientRect, X, Y, Width, Height);
  138. EndPaint(Window, &Paint);
  139. }break;
  140.  
  141. default:
  142. {
  143. Result = DefWindowProc(Window, Message, WParam, LParam);
  144. } break;
  145. }
  146.  
  147. return(Result);
  148. }
  149.  
  150. int CALLBACK
  151. WinMain(
  152. HINSTANCE Instance,
  153. HINSTANCE PrevInstance,
  154. LPSTR CommandLine,
  155. int ShowCode)
  156.  
  157. {
  158. WNDCLASS WindowClass = {};
  159. WindowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
  160. WindowClass.lpfnWndProc = Win32MainWindowCallback;
  161. WindowClass.hInstance = Instance;
  162. //WindowClass.hIcon;
  163. WindowClass.lpszClassName = "ProjectSeijunWindowClass";
  164.  
  165. if(RegisterClass(&WindowClass))
  166. {
  167. HWND Window =
  168. CreateWindowEx(
  169. 0,
  170. "ProjectSeijunWindowClass",
  171. "Project Seijun: Insurgence of Forgotten Fairies",
  172. WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  173. CW_USEDEFAULT,
  174. CW_USEDEFAULT,
  175. CW_USEDEFAULT,
  176. CW_USEDEFAULT,
  177. 0,
  178. 0,
  179. Instance,
  180. 0);
  181.  
  182. if(Window)
  183. {
  184. Running = true;
  185. MSG Message;
  186. while(Running)
  187. {
  188.  
  189. MSG Message;
  190. while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE))
  191. {
  192. if(Message.message == WM_QUIT)
  193. {
  194. Running = false;
  195. }
  196.  
  197. TranslateMessage(&Message);
  198. DispatchMessage(&Message);
  199. }
  200.  
  201. RenderWeirdGradient(0, 0);
  202.  
  203. HDC DeviceContext = GetDC(Window);
  204. RECT ClientRect;
  205. GetClientRect(Window, &ClientRect);
  206. int WindowWidth = ClientRect.right - ClientRect.left;
  207. int WindowHeight = ClientRect.bottom - ClientRect.top;
  208. Win32UpdateWindow(DeviceContext, &ClientRect, 0, 0, WindowWidth, WindowHeight);
  209. ReleaseDC(Window, DeviceContext);
  210. }
  211. }
  212. else
  213. {
  214. /* Logging */
  215. }
  216.  
  217.  
  218. }
  219. else
  220. {
  221. /* Logging */
  222. }
  223.  
  224. return(0);
  225. }
Advertisement
Add Comment
Please, Sign In to add comment