DirectX 9 Timing

This is a discussion on DirectX 9 Timing within the Game Programming forums, part of the General Programming Boards category; My work-in-progress Direct3D engine now has the option for each rendered object to have a velocity vector. I calculate the ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    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?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with my DirectX program
    By fighter92 in forum Game Programming
    Replies: 1
    Last Post: 01-23-2007, 05:28 PM
  2. Isometric Tile Engine using DirectX
    By Wraithan in forum Game Programming
    Replies: 3
    Last Post: 07-17-2006, 12:16 PM
  3. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 06:16 PM
  4. DirectX - Starting Guide?
    By Zeusbwr in forum Game Programming
    Replies: 13
    Last Post: 11-24-2004, 11:49 PM
  5. DirectX 8 with Borland
    By Tommaso in forum Game Programming
    Replies: 11
    Last Post: 08-12-2003, 09:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21