3dExplorer.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:11k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // 3dExplorer.cpp : Defines the entry point for the application.
  2. ///////////////////////////////////////////////
  3. #include "stdafx.h"
  4. #include "CGL.h"
  5. #include "Input.h"
  6. #include "texmanager.h"
  7. #include "stdio.h"
  8. #include "FPSCounter.h"
  9. #include "Audio.h"
  10. #include "MediaPlayer.h"
  11. #include "AudioManager.h"
  12. #include "GameSetting.h"
  13. #include "menu.h"
  14. #include "tempmenu.h"
  15. #include "imgtext.h"
  16. #include "Scrmasker.h"
  17. #include "mission.h"
  18. ///////////////////////////////////////////////
  19. HWND    hWnd=NULL;
  20. HINSTANCE    hInstance;
  21. CGL            cGL;
  22. CInput         cInput;
  23. CImgText       cText;
  24. CGameSetting   cGameSetting;
  25. CAudio         cAudio;
  26. CMediaPlayer   cMusic;
  27. CAudioManager  cAudioManager;
  28. CFPSCounter    cFPS;
  29. CMenu          cMenu;
  30. CTempMenu      cTempMenu;
  31. CScrMasker     cScrMasker;
  32. CTexManager    cTexManager;
  33. CMission       cMission;
  34. int            oldGameState=GAME_MAIN_MENU;
  35. BOOL CreateGLWindow(int width, int height, int bits);
  36. void KillGLWindow() ;
  37. ///////////////////////////////////////////////
  38. bool InitEngine()
  39. {
  40. ///////check audio
  41.   cGameSetting.LoadSetting();
  42. cAudioManager.InitCheck(); 
  43.     ///////////////////menu
  44. if(!cMenu.LoadMenu())
  45. {
  46. MessageBox(0, "menu error", "Error", MB_OK | MB_ICONERROR);
  47. return FALSE;
  48. }
  49. ///////////////////////////
  50.     oldGameState=cGameSetting.m_iGameState;
  51. ////////////////////////////
  52. return true;
  53. }
  54. GLvoid PrepareRun()
  55. {
  56.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
  57. glLoadIdentity();
  58. glColor3f(1,1,1);
  59. ////////////////////////////////////
  60. //////////// for test //////////////
  61.     if(cInput.m_keys['L'] && cGameSetting.m_iGameState==GAME_MISSION)
  62. {
  63.         cInput.m_keys['L']=false;
  64. int mode[4];
  65. glGetIntegerv(GL_POLYGON_MODE,mode);
  66. if( mode[0] == GL_FILL)
  67.     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  68. else
  69.             glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  70. }
  71. ///////////// end test //////////////
  72. ///////////////////////////////////
  73. if(oldGameState!=cGameSetting.m_iGameState)
  74. {
  75. if(oldGameState==GAME_MAIN_MENU)
  76. {
  77. if(cGameSetting.m_iGameState==GAME_MISSION)
  78. {
  79. cMenu.DeleteMenu();
  80.                 if(cGameSetting.m_iScrWidth !=800)
  81. {
  82.                     KillGLWindow();
  83.     CreateGLWindow(cGameSetting.m_iScrWidth,cGameSetting.m_iScrHeight,cGameSetting.m_iColorDepth);
  84. }
  85. cGL.SetGLMissionState();
  86. cMission.LoadMission();
  87. }
  88. }
  89. else if(oldGameState==GAME_MISSION)
  90. {
  91. if(cGameSetting.m_iGameState==GAME_TEMP_MENU)
  92. {
  93. cMission.PauseMission();
  94. cGL.SetGLMenuState();
  95. cTempMenu.LoadMenu();
  96. }
  97. if(cGameSetting.m_iGameState==GAME_MAIN_MENU)
  98. {
  99. cMission.DeleteMission();
  100.                 if(cGameSetting.m_iScrWidth !=800)
  101. {
  102.                     KillGLWindow();
  103.     CreateGLWindow(800,600,16);
  104. }
  105. cGL.SetGLMenuState();
  106. cMenu.LoadMenu();
  107. }
  108. }
  109. else if(oldGameState==GAME_TEMP_MENU)
  110. {
  111. if(cGameSetting.m_iGameState==GAME_MAIN_MENU)
  112. {
  113. cMission.DeleteMission();
  114. cTempMenu.DeleteMenu();
  115.                 if(cGameSetting.m_iScrWidth !=800)
  116. {
  117.                     KillGLWindow();
  118.     CreateGLWindow(800,600,16);
  119. }
  120. cGL.SetGLMenuState();
  121. cMenu.LoadMenu();
  122. }
  123. if(cGameSetting.m_iGameState==GAME_MISSION)
  124. {
  125.     cTempMenu.DeleteMenu();
  126. cGL.SetGLMissionState();
  127. cMission.ResumeMission();
  128. }
  129. }
  130. oldGameState=cGameSetting.m_iGameState;
  131. }
  132. }
  133. LRESULT CALLBACK WndProc( HWND hWnd,
  134. UINT message,
  135. WPARAM wParam,
  136. LPARAM lParam)
  137. {
  138. // Used later on to get the size of the window
  139. // Tells Windows we want to check the message
  140. switch (message)
  141. {
  142. case WM_CLOSE: // Windows being closed
  143. cGL.DestroyGL(); // Shutown GL
  144. ShowCursor(TRUE); // Show mouse
  145. PostQuitMessage(0); // Quit the program
  146. break;
  147.      case WM_KILLFOCUS:
  148. ShowCursor(true);
  149. if(cGameSetting.m_iGameState==GAME_MISSION)cMission.PauseMission();
  150. break;
  151. case WM_SETFOCUS:
  152. ShowCursor(false);
  153. if(cGameSetting.m_iGameState==GAME_MISSION)cMission.ResumeMission();
  154. break;
  155. case WM_KEYDOWN: // Key Being Held Down
  156. cInput.GetInput(wParam,true); // Make That Keys Cell True
  157. break;
  158. case WM_KEYUP: // Key Is Released
  159. cInput.GetInput(wParam,false); // Make That Keys Cell False
  160. break;
  161. case WM_LBUTTONDOWN: // Left mouse button is pressed
  162. cInput.m_keys[MOUSE_0]=true;
  163. break;
  164. case WM_LBUTTONUP: // Left mouse button is pressed
  165. cInput.m_keys[MOUSE_0]=false;
  166. break;
  167. case WM_RBUTTONDOWN: // Right mouse button is pressed
  168. cInput.m_keys[MOUSE_1]=true;
  169. break;
  170. case WM_RBUTTONUP: // Right mouse button is pressed
  171. cInput.m_keys[MOUSE_1]=false;
  172. break;
  173. case WM_MOUSEMOVE:
  174. cInput.m_mousePosX = LOWORD(lParam); // Position of the cursor 
  175. cInput.m_mousePosY = HIWORD(lParam);
  176. break;
  177. case WM_SIZE: // Resizing the screen
  178. cGL.Resize(LOWORD(lParam),HIWORD(lParam));
  179. break;
  180. default:
  181. // Pass windows messages
  182. return (DefWindowProc(hWnd, message, wParam, lParam));
  183. }
  184. return (0);
  185. }
  186. void KillGLWindow()
  187. {
  188. ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
  189. cGL.DestroyGL();
  190. if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
  191. {
  192. MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  193. hWnd=NULL; // Set hWnd To NULL
  194. }
  195. if (!UnregisterClass("3dExplorer",hInstance)) // Are We Able To Unregister Class
  196. {
  197. MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  198. hInstance=NULL; // Set hInstance To NULL
  199. }
  200. ShowCursor(TRUE); // Show Mouse Pointer
  201. }
  202. BOOL CreateGLWindow(int width, int height, int bits)
  203. {
  204. WNDCLASS wc; // Windows Class Structure
  205. DWORD dwExStyle; // Window Extended Style
  206. DWORD dwStyle; // Window Style
  207. RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
  208. WindowRect.left=(long)0; // Set Left Value To 0
  209. WindowRect.right=(long)width; // Set Right Value To Requested Width
  210. WindowRect.top=(long)0; // Set Top Value To 0
  211. WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
  212. hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
  213. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
  214. wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
  215. wc.cbClsExtra = 0; // No Extra Window Data
  216. wc.cbWndExtra = 0; // No Extra Window Data
  217. wc.hInstance = hInstance; // Set The Instance
  218. wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
  219. wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
  220. wc.hbrBackground = NULL; // No Background Required For GL
  221. wc.lpszMenuName = NULL; // We Don't Want A Menu
  222. wc.lpszClassName = "3dExplorer"; // Set The Class Name
  223. if (!RegisterClass(&wc)) // Attempt To Register The Window Class
  224. {
  225. MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  226. return FALSE; // Return FALSE
  227. }
  228. ////////////////////////////////////////////////////////////////////////////////
  229. ///////// Change Resoultion
  230. DEVMODE dmScreenSettings; // Device Mode
  231. memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
  232. dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
  233. dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
  234. dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
  235. dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
  236. dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  237. // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
  238. if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
  239. {
  240. MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
  241. return FALSE; // Return FALSE
  242. }
  243. dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
  244. dwStyle=WS_POPUP; // Windows Style
  245. ShowCursor(FALSE); // Hide Mouse Pointer
  246. AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
  247. // Create The Window
  248. if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
  249. "3dExplorer", // Class Name
  250. "3dExplorer", // Window Title
  251. dwStyle | // Defined Window Style
  252. WS_CLIPSIBLINGS | // Required Window Style
  253. WS_CLIPCHILDREN, // Required Window Style
  254. 0, 0, // Window Position
  255. WindowRect.right-WindowRect.left, // Calculate Window Width
  256. WindowRect.bottom-WindowRect.top, // Calculate Window Height
  257. NULL, // No Parent Window
  258. NULL, // No Menu
  259. hInstance, // Instance
  260. NULL))) // Dont Pass Anything To WM_CREATE
  261. {
  262. KillGLWindow(); // Reset The Display
  263. MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  264. return FALSE; // Return FALSE
  265. }
  266. cGL.InitGL(hWnd,bits);
  267.     
  268. ShowWindow(hWnd,SW_SHOW); // Show The Window
  269. SetForegroundWindow(hWnd); // Slightly Higher Priority
  270. SetFocus(hWnd); // Sets Keyboard Focus To The Window
  271. cGL.Resize(width, height); // Set Up Our Perspective GL Screen
  272.  
  273. ShowCursor(FALSE);
  274.     return true;
  275. }
  276. int APIENTRY WinMain(HINSTANCE hInstance,
  277.                      HINSTANCE hPrevInstance,
  278.                      LPSTR     lpCmdLine,
  279.                      int       nCmdShow)
  280. {
  281.   MSG msg; // Windows message structure
  282.     
  283. CreateGLWindow(800,600,16);
  284. cGL.SetGLMenuState();
  285.     
  286.     if(!InitEngine())
  287. {
  288. MessageBox(0, "InitEngine error", "Error", MB_OK | MB_ICONERROR);
  289. return FALSE;
  290. }
  291. // cScrMasker.FadeIn();
  292. while (1)
  293. {
  294. // Process All Messages
  295. while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
  296. {
  297. if (GetMessage(&msg, NULL, 0, 0))
  298. {
  299. TranslateMessage(&msg);
  300. DispatchMessage(&msg);
  301. }
  302. else
  303. {
  304. return TRUE;
  305. }
  306. }
  307. PrepareRun();  
  308. cMenu.RenderMenu();  
  309. cTempMenu.RenderMenu();
  310. cMission.RunMission();
  311.      cFPS.ShowFPS(700,10);
  312. cScrMasker.BrightnessCtrl();
  313. cGL.SwapBuffers();
  314. if (cGameSetting.m_iGameState==GAME_DEAD)
  315. SendMessage(hWnd, WM_CLOSE, 0, 0);
  316. }
  317. KillGLWindow() ;
  318. return 0;
  319. }