My brain makes things explode.
C++/D3D Pong Maintenance: The Game Loop/Frame Delta
Tutorial Description
While running the game/engine that we’ve been developing throughout this tutorial series, you may have noticed that the engine kind of runs… like crap. So, before moving onto the next tutorial in the series (the code for which is completed), which will cover keyboard and mouse input, I feel that it’s somewhat important to fix this. It’s just going to be some small updates to CGameEngine.cpp, so it won’t be hard.
Code Listing 1: CGameEngine.cpp -> CGameEngine::CGameEengine(void)
First, we’re going to correct an error with our game timer, which we never actually created an instance of in code – causing the GetRunTime feature to fail due to a member variable not being initialized (CGameTimer::_start). Replace the constructor for CGameEngine with the following, in CGameEngine.cpp
CGameEngine::CGameEngine(void)
: _windowHandle(NULL), _windowTitle(L"D3DPong"),
_screenWidth(640), _screenHeight(480), _colorDepth(32),
_gameRunning(true), _gameTimer(new CGameTimer())
{
}
Code Listing 2: CGameEngine.cpp -> CGameEngine::Update(void)
Our new Update method is much smaller than it was before. We’re still using fast-as-possible rendering and updating, so this isn’t going to fix the minor graphics tearing that you’ve no doubt noticed before this, but it is going to make things run a little better.
void CGameEngine::Update(void)
{
float delta = this->_gameTimer->GetRunTime();
GameUpdate(delta);
this->_gameTimer->Reset();
this->BeginRender();
GameRender(delta);
this->EndRender();
}
Nicer, cleaner, shorter. This was caused by a bit of an inconsistency and laziness on my part, but things should run a bit smoother now. In the next tutorial, we’re going to be adding input, and will put the beginning pieces of our game into effect. I’ll cover enabling VSync at a later point, because I’m too lazy to deal with it right now. It’ll probably be in another “Maintenance” mini-tutorial.
Expect Tutorial 4 soon!
| Print article | This entry was posted by Samantha on March 25, 2010 at 6:23 PM, and is filed under C++, Development, Languages, Pong (C++/DirectX), Series, Technology, Tutorials. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 4 months ago
Hey dude, im from Brazil and im glad i found your blog.
Thanks for the info, looking forward for the next tutorial.
about 5 months ago
Cheers for this update :)