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

游戏引擎

开发平台:

Visual C++

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