GameApp.cpp
上传用户:ghyvgy
上传日期:2009-05-26
资源大小:547k
文件大小:9k
源码类别:

其他游戏

开发平台:

Python

  1. /*
  2. s_p_oneil@hotmail.com
  3. Copyright (c) 2000, Sean O'Neil
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice,
  8.   this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10.   this list of conditions and the following disclaimer in the documentation
  11.   and/or other materials provided with the distribution.
  12. * Neither the name of this project nor the names of its contributors
  13.   may be used to endorse or promote products derived from this software
  14.   without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "Master.h"
  28. #include "GameApp.h"
  29. CWinApp *CWinApp::m_pMainApp;
  30. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char *pszCmdLine, int nShowCmd)
  31. {
  32. CGameApp app(hInstance, hPrevInstance, pszCmdLine, nShowCmd);
  33. if(app.InitInstance())
  34. app.Run();
  35. return app.ExitInstance();
  36. }
  37. void CDisplayMode::LoadDisplaySettings()
  38. {
  39. m_bFullScreen = (GetApp()->GetProfileInt(DISPLAY_SECTION, "FullScreen", 0) != 0);
  40. m_nWidth = GetApp()->GetProfileInt(DISPLAY_SECTION, "Width", 640);
  41. m_nHeight = GetApp()->GetProfileInt(DISPLAY_SECTION, "Height", 480);
  42. m_nBitsPerPixel = m_bFullScreen ? GetApp()->GetProfileInt(DISPLAY_SECTION, "BitsPerPixel", 32) : 0;
  43. m_nFrequency = m_bFullScreen ? GetApp()->GetProfileInt(DISPLAY_SECTION, "Frequency", 0) : 0;
  44. }
  45. void CDisplayMode::SaveDisplaySettings()
  46. {
  47. GetApp()->WriteProfileInt(DISPLAY_SECTION, "FullScreen", m_bFullScreen);
  48. GetApp()->WriteProfileInt(DISPLAY_SECTION, "Width", m_nWidth);
  49. GetApp()->WriteProfileInt(DISPLAY_SECTION, "Height", m_nHeight);
  50. GetApp()->WriteProfileInt(DISPLAY_SECTION, "BitsPerPixel", m_nBitsPerPixel);
  51. GetApp()->WriteProfileInt(DISPLAY_SECTION, "Frequency", m_nFrequency);
  52. }
  53. bool CGameApp::InitInstance()
  54. {
  55. // Register the window class and create the window
  56. WNDCLASS wc = {CS_OWNDC | CS_VREDRAW | CS_HREDRAW, (WNDPROC)WindowProc, 0, 0, m_hInstance, LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_APP)), LoadCursor((HINSTANCE)NULL, IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH), MAKEINTRESOURCE(IDM_APP), m_szAppName};
  57. if(!RegisterClass(&wc))
  58. {
  59. MessageBox("Unable to register window class, aborting.");
  60. return false;
  61. }
  62. // Load the last saved display mode and use it to initialize the window
  63. LoadDisplaySettings();
  64. return InitMode(IsFullScreen(), GetWidth(), GetHeight(), GetBitsPerPixel(), GetFrequency());
  65. }
  66. bool CGameApp::InitMode(bool bFullScreen, int nWidth, int nHeight, int nBitsPerPixel, int nFrequency)
  67. {
  68. if(m_hWnd)
  69. {
  70. ResetDisplayMode();
  71. DestroyWindow();
  72. m_hWnd = NULL;
  73. }
  74. SetFullScreen(bFullScreen);
  75. SetWidth(nWidth);
  76. SetHeight(nHeight);
  77. SetBitsPerPixel(nBitsPerPixel);
  78. SetFrequency(nFrequency);
  79. DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | (IsFullScreen() ? WS_POPUP : WS_OVERLAPPEDWINDOW);
  80. #ifdef _DEBUG
  81. DWORD dwExStyle = 0;
  82. #else
  83. DWORD dwExStyle = (IsFullScreen() ? WS_EX_TOPMOST : 0);
  84. #endif
  85. CRect rect(0, 0, GetWidth(), GetHeight());
  86. if(!CreateEx(m_hInstance, m_szAppName, m_szAppName, dwExStyle, dwStyle, &rect))
  87. {
  88. MessageBox("Unable to create application window, aborting.");
  89. return false;
  90. }
  91. if(!IsFullScreen())
  92. {
  93. CalcWindowRect(&rect);
  94. MoveWindow(0, 0, rect.Width(), rect.Height(), false);
  95. }
  96. ShowWindow(m_nShowCmd);
  97. UpdateWindow();
  98. return true;
  99. }
  100. int CGameApp::ExitInstance()
  101. {
  102. UnregisterClass(m_szAppName, m_hInstance);
  103. SaveDisplaySettings();
  104. ResetDisplayMode();
  105. return 0;
  106. }
  107. bool CGameApp::OnIdle()
  108. {
  109. if(!m_bActive)
  110. return false;
  111. int nTimer = timeGetTime();
  112. m_pGameEngine->RenderFrame(nTimer-m_nTimer);
  113. SwapBuffers(m_hDC);
  114. m_nTimer = nTimer;
  115. return true;
  116. }
  117. void CGameApp::Pause()
  118. {
  119. if(m_bActive)
  120. {
  121. #ifndef _DEBUG
  122. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
  123. #endif
  124. m_pGameEngine->Pause();
  125. m_bActive = false;
  126. if(IsFullScreen())
  127. {
  128. ShowWindow(SW_MINIMIZE);
  129. ResetDisplayMode();
  130. }
  131. }
  132. }
  133. void CGameApp::Restore()
  134. {
  135. if(!m_bActive)
  136. {
  137. #ifndef _DEBUG
  138. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
  139. #endif
  140. m_bActive = true;
  141. if(IsFullScreen())
  142. {
  143. ShowWindow(SW_SHOWNORMAL);
  144. SetDisplayMode();
  145. }
  146. m_nTimer = timeGetTime();
  147. m_pGameEngine->Restore();
  148. }
  149. }
  150. int CGameApp::OnCreate(HWND hWnd) 
  151. {
  152. PIXELFORMATDESCRIPTOR pfdDesc;
  153. memset((char *)&pfdDesc, 0, sizeof(PIXELFORMATDESCRIPTOR));
  154. pfdDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  155. pfdDesc.nVersion = 1;
  156. pfdDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  157. pfdDesc.iPixelType = PFD_TYPE_RGBA;
  158. pfdDesc.iLayerType = PFD_MAIN_PLANE;
  159. pfdDesc.cColorBits = 32;
  160. pfdDesc.cAlphaBits = 8;
  161. pfdDesc.cDepthBits = 32;
  162. pfdDesc.cStencilBits = 32;
  163. m_hWnd = hWnd;
  164. m_hDC = ::GetDC(m_hWnd);
  165. int nPixelIndex = ChoosePixelFormat(m_hDC, &pfdDesc);
  166. if(!SetPixelFormat(m_hDC, nPixelIndex, &pfdDesc))
  167. {
  168. MessageBox("Error finding a suitable pixel format.");
  169. return -1;
  170. }
  171. DescribePixelFormat(m_hDC, GetPixelFormat(m_hDC), sizeof(PIXELFORMATDESCRIPTOR), &pfdDesc);
  172. m_hGLRC = wglCreateContext(m_hDC);
  173. if(!m_hGLRC || !wglMakeCurrent(m_hDC, m_hGLRC))
  174. {
  175. MessageBox("Error creating OpenGL rendering context.");
  176. return -1;
  177. }
  178. m_pGameEngine = new CGameEngine;
  179. return 0;
  180. }
  181. void CGameApp::OnDestroy()
  182. {
  183. if(m_pGameEngine)
  184. {
  185. delete m_pGameEngine;
  186. m_pGameEngine = NULL;
  187. }
  188. if(wglGetCurrentContext())
  189. wglMakeCurrent(NULL, NULL);
  190. if(m_hGLRC)
  191. {
  192. wglDeleteContext(m_hGLRC);
  193. m_hGLRC = NULL;
  194. }
  195. if(m_hDC)
  196. {
  197. ::ReleaseDC(m_hWnd, m_hDC);
  198. m_hDC = NULL;
  199. }
  200. }
  201. void CGameApp::OnSize(int nType, int nWidth, int nHeight)
  202. {
  203. if(!nHeight || !nWidth)
  204. return;
  205. glViewport(0, 0, nWidth, nHeight);
  206. glMatrixMode(GL_PROJECTION);
  207. glLoadIdentity();
  208. gluPerspective(45.0, (double)nWidth / (double)nHeight, MIN_DISTANCE, MAX_DISTANCE*1.05);
  209. glMatrixMode(GL_MODELVIEW);
  210. glLoadIdentity();
  211. if(!IsFullScreen())
  212. {
  213. CRect rect;
  214. GetClientRect(&rect);
  215. SetWidth(rect.Width());
  216. SetHeight(rect.Height());
  217. }
  218. }
  219. bool CGameApp::OnCommand(WPARAM wParam, LPARAM lParam)
  220. {
  221. switch(LOWORD(wParam))
  222. {
  223. case ID_FILE_EXIT:
  224. PostMessage(WM_CLOSE);
  225. return true;
  226. case ID_OPTIONS_PLANET:
  227. MessageBox("This feature has not been implemented yet.");
  228. return true;
  229. case ID_HELP_KEYCOMMANDS:
  230. {
  231. CDialog dlg(IDD_KEY_COMMANDS, m_hInstance, m_hWnd);
  232. dlg.DoModal();
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. LRESULT CALLBACK CGameApp::WindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  239. {
  240. CRect rect;
  241. switch(nMsg)
  242. {
  243. case WM_CREATE:
  244. return GameApp()->OnCreate(hWnd);
  245. case WM_DESTROY:
  246. GameApp()->OnDestroy();
  247. break;
  248. case WM_CLOSE:
  249. GameApp()->DestroyWindow();
  250. PostQuitMessage(0);
  251. return 0;
  252. case WM_COMMAND:
  253. if(GameApp()->OnCommand(wParam, lParam))
  254. return 0;
  255. break;
  256. case WM_SIZE:
  257. GameApp()->OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
  258. break;
  259. case WM_ACTIVATE:
  260. if(wParam && !GameApp()->IsActive())
  261. GameApp()->Restore();
  262. else if(!wParam && GameApp()->IsActive())
  263. GameApp()->Pause();
  264. break;
  265. case WM_GETMINMAXINFO:
  266. rect = CRect(0, 0, 320, 240);
  267. CWnd(hWnd).CalcWindowRect(&rect);
  268. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = rect.Width();
  269. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = rect.Height();
  270. return 0;
  271. case WM_SYSCOMMAND:
  272. // Prevent system commands (like closing, moving, sizing, screensaver, power management, etc) when active
  273. //if(GameApp()->m_bActive)
  274. // return 0;
  275. break;
  276. case WM_POWERBROADCAST:
  277. // Prevent power suspend when active
  278. if(GameApp()->m_bActive && wParam == PBT_APMQUERYSUSPEND)
  279. return BROADCAST_QUERY_DENY;
  280. break;
  281. case WM_SYSKEYDOWN:
  282. if(wParam == VK_MENU)
  283. return 0;
  284. break;
  285. case WM_KEYDOWN:
  286. if(wParam == VK_ESCAPE)
  287. {
  288. if(GameApp()->IsActive())
  289. GameApp()->Pause();
  290. else
  291. GameApp()->Restore();
  292. }
  293. break;
  294. /* Disable full-screen for now
  295. case WM_CHAR:
  296. switch(wParam)
  297. {
  298. case 'f':
  299. GameApp()->InitMode(!GameApp()->IsFullScreen(), 640, 480, 32);
  300. break;
  301. }
  302. break;
  303. */
  304. }
  305. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  306. }