-
DirectX 9 Timing
My work-in-progress Direct3D engine now has the option for each rendered object to have a velocity vector. I calculate the change in position right before rendering, and the timer stops and starts before and after the calculation:
Code:
//Calc new object positions
Timer.Stop();
NewPositions();
Timer.Start();
//Render objects
vector<CRenderObject *>::iterator i;
for (i=Objects.begin();i!=Objects.end();i++)
{
Device->SetRenderState(D3DRS_FILLMODE,FillMode);
(*i)->Render(mWorld);
}
Code:
void DrawClass::NewPositions(void)
{
vector<CRenderObject *>::iterator i;
float Elapsed=(float)Timer.Elapsed();
for (i=Objects.begin();i!=Objects.end();i++)
{
(*i)->x+=Elapsed*(*i)->vx;
(*i)->y+=Elapsed*(*i)->vy;
(*i)->z+=Elapsed*(*i)->vz;
}
}
However, it does not work. Upon execution, each object travels only for a short period of time, eg less than one second, and then stops. If the window is manipulated, ie, moved or resized, the object moves for a little bit more. I guess that implies that the problem is related to the message loop. Here it is:
Code:
while (msg.message!=WM_QUIT)
{
dc->Render();
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
As far as I can tell, it should be continually rendering, and thus updating the object positions. Is there anything that appears incorrectly coded?
-
The message loop I usually use is like this:
Code:
while(true)
{
if(PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// Do all your updates / rendering here
}
}
I didn't look too much at your timing stuff. If this doesn't fix it then come back and post again and I'll have a look. Also, look at the STL function for_each( ) it's pretty handy. You'll need to #include <algorithm> to use it.