main.cpp
上传用户:cxh8989
上传日期:2021-01-22
资源大小:2544k
文件大小:4k
源码类别:

射击游戏

开发平台:

Visual C++

  1. #include "main.h"
  2. #include "Camera.h"
  3. #include "Quake3Bsp.h"
  4. #include "Frustum.h"
  5. #pragma comment(lib, "opengl32.lib")
  6. #pragma comment(lib, "glu32.lib")
  7. #pragma comment(lib, "glaux.lib")
  8. #pragma comment(lib, "jpeg.lib")
  9. #pragma comment(lib, "winmm.lib")
  10. CVector3 g_vVelocity = CVector3(0, 0, 0);
  11. CCamera g_Camera;
  12. bool  g_bFullScreen = true;
  13. HWND  g_hWnd;
  14. RECT  g_rRect;
  15. HDC   g_hDC;
  16. HGLRC g_hRC;
  17. HINSTANCE g_hInstance;
  18. UINT g_Texture[MAX_TEXTURES] = {0};
  19. bool g_StopUpdate = false;
  20. CQuake3BSP g_Level;
  21. bool g_RenderMode = true;
  22. bool g_bTextures  = true;
  23. /** 多重纹理指针 */
  24. PFNGLACTIVETEXTUREARBPROC glActiveTextureARB  = NULL;
  25. PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = NULL;
  26. bool g_bLightmaps = true;
  27. float g_Gamma = 10;
  28. CFrustum g_Frustum;
  29. int g_VisibleFaces = 0;
  30. /** 初始化 */
  31. void Init(HWND hWnd)
  32. {
  33. g_hWnd = hWnd;
  34. GetClientRect(g_hWnd, &g_rRect);
  35. InitializeOpenGL(g_rRect.right, g_rRect.bottom);
  36. /** 读入配置文件 */
  37. ifstream fin("Config.ini");
  38. string strLevel = "";
  39. string strTemp = "";
  40. if(fin.fail())
  41. {
  42. MessageBox(g_hWnd, "Could not find Config.ini file!", "Error", MB_OK);
  43. PostQuitMessage(0);
  44. return;
  45. }
  46. fin >> strLevel >> strLevel;
  47. fin >> strTemp  >> g_Gamma;
  48. glActiveTextureARB  = (PFNGLACTIVETEXTUREARBPROC)  wglGetProcAddress("glActiveTextureARB");
  49.     glClientActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)   wglGetProcAddress("glClientActiveTextureARB");
  50. if(!glActiveTextureARB || !glClientActiveTextureARB)
  51. {
  52. MessageBox(g_hWnd, "Your video card doesn't support multitexturing", "Error", MB_OK);
  53. PostQuitMessage(0);
  54. }
  55. fin.close();
  56. /** 载入BSP文件 */
  57. bool bResult = g_Level.LoadBSP(strLevel.c_str());
  58. if(bResult == false)
  59. {
  60. PostQuitMessage(0);
  61. return;
  62. }
  63. g_Camera.PositionCamera( -555, 150, 55, -555, 150, 155, 0, 1, 0);
  64. glEnable(GL_DEPTH_TEST);
  65. glEnable(GL_TEXTURE_2D);
  66. glCullFace(GL_FRONT);
  67.   glEnable(GL_CULL_FACE);
  68. }
  69. WPARAM MainLoop()
  70. {
  71. MSG msg;
  72. while(1)
  73. {
  74. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
  75.         { 
  76. if(msg.message == WM_QUIT)
  77. break;
  78.             TranslateMessage(&msg);
  79.             DispatchMessage(&msg);
  80.         }
  81. else
  82. g_Camera.Update();
  83. RenderScene();
  84.         } 
  85. }
  86. DeInit();
  87. return(msg.wParam);
  88. }
  89. void RenderScene() 
  90. {
  91. int i = 0;
  92. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  93. glLoadIdentity();
  94. g_Camera.Look();
  95. g_Frustum.CalculateFrustum();
  96. static CVector3 s_pos;
  97. s_pos = g_Camera.Position();
  98. if (g_StopUpdate)
  99. {
  100. g_Level.RenderLevel(s_pos);
  101. }
  102. else
  103. {
  104. g_Level.RenderLevel(g_Camera.Position());
  105. }
  106. SwapBuffers(g_hDC);
  107. }
  108. LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam)
  109. {
  110.     LONG    lRet = 0; 
  111.     PAINTSTRUCT    ps;
  112.     switch (uMsg)
  113.     case WM_SIZE:
  114. if(!g_bFullScreen)
  115. {
  116. SizeOpenGLScreen(LOWORD(lParam),HIWORD(lParam));
  117. GetClientRect(hWnd, &g_rRect);
  118. }
  119.         break; 
  120.  
  121. case WM_PAINT:
  122. BeginPaint(hWnd, &ps);
  123. EndPaint(hWnd, &ps);
  124. break;
  125. case WM_KEYDOWN:
  126. switch(wParam) {
  127. case VK_ESCAPE:
  128. PostQuitMessage(0);
  129. break;
  130. case VK_F1:
  131. g_RenderMode = !g_RenderMode;
  132. if(g_RenderMode) 
  133. {
  134. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  135. }
  136. else 
  137. {
  138. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  139. }
  140. break;
  141. case VK_F2:
  142. g_StopUpdate = !g_StopUpdate;
  143. break;
  144. case VK_SPACE:
  145. if(g_Level.IsOnGround())
  146. g_vVelocity.y = kJumpAcceleration;
  147. break;
  148. }
  149. break;
  150.     case WM_CLOSE:
  151.         PostQuitMessage(0);
  152.         break; 
  153.      
  154.     default:
  155.         lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); 
  156.         break; 
  157.     } 
  158.  
  159.     return lRet;
  160. }