www.pudn.com > ATimeToKill.rar > engine.cpp
#define WIN32_LEAN_AND_MEAN #define WIN32_EXTRA_LEAN #include#include "engine.h" #include "HiResTimer.h" #include "camera.h" #include "world.h" void CEngine::CheckInput(float deltaTime) { static float buttonDelta = 0.0f; int mouseDeltaX, mouseDeltaY; // changes in the mouse position // decrease amount of time until next possible recognized button pressing buttonDelta -= deltaTime; if (buttonDelta < 0.0f) buttonDelta = 0.0f; // update devices inputSystem->Update(); // retrieve the latest mouse movements inputSystem->GetMouseMovement(mouseDeltaX, mouseDeltaY); OnMouseMove(mouseDeltaX, mouseDeltaY); if (inputSystem->KeyDown(DIK_UP)) OnKeyDown(VK_UP); if (inputSystem->KeyDown(DIK_DOWN)) OnKeyDown(VK_DOWN); if (inputSystem->KeyDown(DIK_LEFT)) OnKeyDown(VK_LEFT); if (inputSystem->KeyDown(DIK_RIGHT)) OnKeyDown(VK_RIGHT); if (inputSystem->KeyDown(DIK_ADD)) OnKeyDown(VK_ADD); if (inputSystem->KeyDown(DIK_SUBTRACT)) OnKeyDown(VK_SUBTRACT); if (inputSystem->KeyDown(DIK_ESCAPE)) OnKeyDown(VK_ESCAPE); if (inputSystem->ButtonDown(0)) { if (buttonDelta == 0.0f) { OnMouseDownL(0,0); buttonDelta = 0.5f; } } } void CEngine::GameCycle(float deltaTime) { CCamera *camera = OnGetCamera(); // get the camera CWorld *world = OnGetWorld(); // get the world if (useDInput) CheckInput(deltaTime); // setup opengl for frame (clear, identity) OnPrepare(); // prepare objects and perform collisions world->Prepare(); // move/orient camera camera->Animate(deltaTime); // move/orient objects world->Animate(deltaTime); // draw objects world->Draw(camera); // swap buffers SwapBuffers(hDC); } // EnterMessageLoop() // desc: the Windows message loop LRESULT CEngine::EnterMessageLoop() { // Message loop MSG msg; timer = new CHiResTimer; timer->Init(); for (;;) { GameCycle(timer->GetElapsedSeconds(1)); SetWindowText(hWnd, "A Time To Kill"); while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) { // we always update if there are any events, even if we're paused if (!GetMessage (&msg, NULL, 0, 0)) { delete timer; return msg.wParam; } TranslateMessage (&msg); DispatchMessage (&msg); } } delete timer; return msg.wParam; }