Guest User

Untitled

a guest
Oct 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. //-----------------------------------------------------------------
  2. // Game Engine Object
  3. // C++ Source - GameEngine.cpp
  4. //-----------------------------------------------------------------
  5.  
  6. //-----------------------------------------------------------------
  7. // Include Files
  8. //-----------------------------------------------------------------
  9. #include "GameEngine.h"
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <string>
  13. #include <cstdlib>
  14. #include <ctime>
  15. #include <sstream>
  16. using namespace std;
  17.  
  18. //-----------------------------------------------------------------
  19. // Static Variable Initialization
  20. //-----------------------------------------------------------------
  21. GameEngine *GameEngine::m_pGameEngine = NULL;
  22.  
  23. //-----------------------------------------------------------------
  24. // Windows Functions
  25. //-----------------------------------------------------------------
  26. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27. PSTR szCmdLine, int iCmdShow)
  28. {
  29. MSG msg;
  30. static int iTickTrigger = 0;
  31. int iTickCount;
  32.  
  33. if (GameInitialize(hInstance))
  34. {
  35. // Initialize the game engine
  36. if (!GameEngine::GetEngine()->Initialize(iCmdShow))
  37. return FALSE;
  38.  
  39. // Enter the main message loop
  40. while (TRUE)
  41. {
  42. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  43. {
  44. // Process the message
  45. if (msg.message == WM_QUIT)
  46. break;
  47. TranslateMessage(&msg);
  48. DispatchMessage(&msg);
  49. }
  50. else
  51. {
  52. // Make sure the game engine isn't sleeping
  53. if (!GameEngine::GetEngine()->GetSleep())
  54. {
  55. // Check the tick count to see if a game cycle has elapsed
  56. iTickCount = GetTickCount();
  57. if (iTickCount > iTickTrigger)
  58. {
  59. iTickTrigger = iTickCount +
  60. GameEngine::GetEngine()->GetFrameDelay();
  61. GameCycle();
  62. }
  63. }
  64. }
  65. }
  66. return (int)msg.wParam;
  67. }
  68.  
  69. // End the game
  70. GameEnd();
  71.  
  72. return TRUE;
  73. }
  74.  
  75. LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
  76. {
  77. // Route all Windows messages to the game engine
  78. return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
  79. }
  80.  
  81. //-----------------------------------------------------------------
  82. // GameEngine Constructor(s)/Destructor
  83. //-----------------------------------------------------------------
  84. GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass,
  85. LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight)
  86. {
  87. // Set the member variables for the game engine
  88. m_pGameEngine = this;
  89. m_hInstance = hInstance;
  90. m_hWindow = NULL;
  91. if (lstrlen(szWindowClass) > 0)
  92. lstrcpy(m_szWindowClass, szWindowClass);
  93. if (lstrlen(szTitle) > 0)
  94. lstrcpy(m_szTitle, szTitle);
  95. m_wIcon = wIcon;
  96. m_wSmallIcon = wSmallIcon;
  97. m_iWidth = iWidth;
  98. m_iHeight = iHeight;
  99. m_iFrameDelay = 50; // 20 FPS default
  100. m_bSleep = TRUE;
  101. }
  102.  
  103. GameEngine::~GameEngine()
  104. {
  105. }
  106.  
  107. //-----------------------------------------------------------------
  108. // Game Engine General Methods
  109. //-----------------------------------------------------------------
  110. BOOL GameEngine::Initialize(int iCmdShow)
  111. {
  112. WNDCLASSEX wndclass;
  113.  
  114. // Create the window class for the main window
  115. wndclass.cbSize = sizeof(wndclass);
  116. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  117. wndclass.lpfnWndProc = WndProc;
  118. wndclass.cbClsExtra = 0;
  119. wndclass.cbWndExtra = 0;
  120. wndclass.hInstance = m_hInstance;
  121. wndclass.hIcon = LoadIcon(m_hInstance,
  122. MAKEINTRESOURCE(GetIcon()));
  123. wndclass.hIconSm = LoadIcon(m_hInstance,
  124. MAKEINTRESOURCE(GetSmallIcon()));
  125. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  126. wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  127. wndclass.lpszMenuName = NULL;
  128. wndclass.lpszClassName = m_szWindowClass;
  129.  
  130. // Register the window class
  131. if (!RegisterClassEx(&wndclass))
  132. return FALSE;
  133.  
  134. // Calculate the window size and position based upon the game size
  135. int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2,
  136. iWindowHeight = m_iHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 +
  137. GetSystemMetrics(SM_CYCAPTION);
  138. if (wndclass.lpszMenuName != NULL)
  139. iWindowHeight += GetSystemMetrics(SM_CYMENU);
  140. int iXWindowPos = (GetSystemMetrics(SM_CXSCREEN) - iWindowWidth) / 2,
  141. iYWindowPos = (GetSystemMetrics(SM_CYSCREEN) - iWindowHeight) / 2;
  142.  
  143. // Create the window
  144. m_hWindow = CreateWindow(m_szWindowClass, m_szTitle, WS_POPUPWINDOW |
  145. WS_CAPTION | WS_MINIMIZEBOX, iXWindowPos, iYWindowPos, iWindowWidth,
  146. iWindowHeight, NULL, NULL, m_hInstance, NULL);
  147. if (!m_hWindow)
  148. return FALSE;
  149.  
  150. // Show and update the window
  151. ShowWindow(m_hWindow, iCmdShow);
  152. UpdateWindow(m_hWindow);
  153.  
  154. return TRUE;
  155. }
  156.  
  157. LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
  158. {
  159. // Route Windows messages to game engine member functions
  160. switch (msg)
  161. {
  162. case WM_CREATE:
  163. // Set the game window and start the game
  164. SetWindow(hWindow);
  165. GameStart(hWindow);
  166. return 0;
  167.  
  168. case WM_ACTIVATE:
  169. // Activate/deactivate the game and update the Sleep status
  170. if (wParam != WA_INACTIVE)
  171. {
  172. GameActivate(hWindow);
  173. SetSleep(FALSE);
  174. }
  175. else
  176. {
  177. GameDeactivate(hWindow);
  178. SetSleep(TRUE);
  179. }
  180. return 0;
  181.  
  182. case WM_PAINT:
  183. HDC hDC;
  184. PAINTSTRUCT ps;
  185. hDC = BeginPaint(hWindow, &ps);
  186.  
  187. // Paint the game
  188. GamePaint(hDC);
  189.  
  190. EndPaint(hWindow, &ps);
  191. return 0;
  192.  
  193. case WM_DESTROY:
  194. // End the game and exit the application
  195. GameEnd();
  196. PostQuitMessage(0);
  197. return 0;
  198. }
  199. return DefWindowProc(hWindow, msg, wParam, lParam);
  200. }
Add Comment
Please, Sign In to add comment