ENGINE.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 2k
Category:

OpenCV

Development Platform:

Visual C++

  1. #define WIN32_LEAN_AND_MEAN
  2. #define WIN32_EXTRA_LEAN
  3. #include <stdlib.h>
  4. #include "engine.h"
  5. #include "HiResTimer.h"
  6. #include "camera.h"
  7. #include "world.h"
  8. void CEngine::CheckInput(float deltaTime)
  9. {
  10. static float buttonDelta = 0.0f;
  11. int mouseDeltaX, mouseDeltaY; // changes in the mouse position
  12. // decrease amount of time until next possible recognized button pressing
  13. buttonDelta -= deltaTime;
  14. if (buttonDelta < 0.0f)
  15. buttonDelta = 0.0f;
  16. // update devices
  17. inputSystem->Update();
  18. // retrieve the latest mouse movements
  19. inputSystem->GetMouseMovement(mouseDeltaX, mouseDeltaY);
  20. OnMouseMove(mouseDeltaX, mouseDeltaY);
  21. if (inputSystem->KeyDown(DIK_W))
  22. OnKeyDown(VK_UP);
  23. if (inputSystem->KeyDown(DIK_S))
  24. OnKeyDown(VK_DOWN);
  25. if (inputSystem->KeyDown(DIK_A))
  26. OnKeyDown(VK_LEFT);
  27. if (inputSystem->KeyDown(DIK_D))
  28. OnKeyDown(VK_RIGHT);
  29. if (inputSystem->KeyDown(DIK_ADD))
  30. OnKeyDown(VK_ADD);
  31. if (inputSystem->KeyDown(DIK_SUBTRACT))
  32. OnKeyDown(VK_SUBTRACT);
  33. if (inputSystem->KeyDown(DIK_ESCAPE))
  34. OnKeyDown(VK_ESCAPE);
  35. if (inputSystem->ButtonDown(0))
  36. {
  37. if (buttonDelta == 0.0f)
  38. {
  39. OnMouseDownL(0,0);
  40. buttonDelta = 0.5f;
  41. }
  42. }
  43. }
  44. void CEngine::GameCycle(float deltaTime)
  45. {
  46. CCamera *camera = OnGetCamera(); // get the camera
  47. CWorld *world = OnGetWorld(); // get the world
  48. if (useDInput)
  49. CheckInput(deltaTime);
  50. // setup opengl for frame (clear, identity)
  51. OnPrepare();
  52. // prepare objects and perform collisions
  53. world->Prepare();
  54. // move/orient camera
  55. camera->Animate(deltaTime);
  56. // move/orient objects
  57. world->Animate(deltaTime);
  58. // draw objects
  59. world->Draw(camera);
  60. // swap buffers
  61. SwapBuffers(hDC);
  62. }
  63. // EnterMessageLoop()
  64. // desc: the Windows message loop
  65. LRESULT CEngine::EnterMessageLoop()
  66. {
  67. // Message loop
  68. MSG msg;
  69. timer = new CHiResTimer;
  70. timer->Init();
  71. for (;;)
  72. {
  73. GameCycle(timer->GetElapsedSeconds(1));
  74. SetWindowText(hWnd, "恐怖之战");
  75. while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
  76. {
  77. // we always update if there are any events, even if we're paused
  78. if (!GetMessage (&msg, NULL, 0, 0))
  79. {
  80. delete timer;
  81. return msg.wParam;
  82. }
  83. TranslateMessage (&msg);
  84.      DispatchMessage (&msg);
  85. }
  86. }
  87. delete timer;
  88. return msg.wParam;
  89. }