MULTIMEDIA

Game Programming with DirectX : Time-Based Simulations (part 3) - The Main Source File

5/15/2013 7:37:08 PM

The Main Source File

Two new objects were added in the global section of the main source file. The first object is an instance of the animation route called g_animationPath, and the second is a Vector3D object used to store the current position of the object. The global section of the Animation Paths demo’s main source file is shown in Listing 12.

winmm.lib is necessary to call the Win32 timer functions that are used throughout the demo.


Listing 12. The main.cpp Globals That Were Added to the End
#include<d3d10.h>
#include<d3dx10.h>
#include<vector>
#include"objLoader.h"
#include"Route.h"

#pragma comment(lib, "d3d10.lib")
#pragma comment(lib, "d3dx10.lib")
#pragma comment(lib, "winmm.lib")

#define WINDOW_NAME    "Animation Paths"
#define WINDOW_CLASS   "UPGCLASS"
#define WINDOW_WIDTH   800
#define WINDOW_HEIGHT  600


…


// Animation Paths.
Route g_animationPath;
Vector3D objPos;

In the InitializeDemo() function, several lines of code were added to the end of the function. Each of these lines of code adds a different path to the animation list by calling either AddLinePath() or AddCurvePath(). The animation being played starts at the top left of the screen, moves to the top right, moves to the bottom right, curves back up to the top left, moves back to the bottom right, and finally moves back to the top left. Since the animation ends where it begins, when the animation loops it looks like one endless motion over and over until the application closes. The InitializationDemo() function from the Animation Paths demo is shown in Listing 13.

Listing 13. The InitializeDemo() Function
bool InitializeDemo()
{
   …

   // Set the shader matrix variables that won't change once here.
   D3DXMatrixIdentity(&g_worldMat);
   D3DXMatrixIdentity(&g_viewMat);
   g_viewEffectVar->SetMatrix((float*)&g_viewMat);
   g_projEffectVar->SetMatrix((float*)&g_projMat);

   // Create the first path.
   g_animationPath.AddLinePath(Vector3D(-20.0f, 10.0f, 0.0f),
                               Vector3D(20.0f, 10.0f, 0.0f));

   // Our next path will be a straight line down.
   g_animationPath.AddLinePath(Vector3D(20.0f, 10.0f, 0.0f),
                               Vector3D(20.0f, -10.0f, 0.0f));

   // The third path will be a curved path.
   g_animationPath.AddCurvePath(Vector3D(20.0f, -10.0f, 0.0f),
                                Vector3D(-17.5f, -5.0f, 0.0f),
                                Vector3D(-15.5f, 0.0f, 0.0f),
                                Vector3D(-20.0f, 10.0f, 0.0f));

   // Our next path will be a straight line down diagonally.
   g_animationPath.AddLinePath(Vector3D(-20.0f, 10.0f, 0.0f),
                               Vector3D(20.0f, -10.0f, 0.0f));

   // Our next path will be a straight line up diagonally.
   g_animationPath.AddLinePath(Vector3D(20.0f, -10.0f, 0.0f),
                               Vector3D(-20.0f, 10.0f, 0.0f));

   return true;
}

					  

The Update() function will calculate the current time that is passed to the route’s GetPosition() function. This time is adjusted by the start time because the start time is the system time since Windows was last started. Subtracting from that time gives us a time since the last time we called the timer function instead of the OS starting time. Once the object’s position is known, it is applied to the world matrix.

The Update() function is shown in Listing 14. Listing 15 shows the RenderScene() function, which is the same as in the OBJ Models demo. Figure 3 is a screenshot from the Animation Paths demo.

Listing 14. The Update() Function
void Update()
{
   // Calculate animation time, slow down by 0.03f;
   float time = (float)timeGetTime();
   time = (time - g_animationPath.GetStartTime()) * 0.03f;

   // Get the time-based position from the route.
   objPos = g_animationPath.GetPosition(time);

   D3DXMATRIX objTrans;
   D3DXMatrixTranslation(&objTrans, objPos.x, objPos.y, objPos.z);
   D3DXMatrixTranslation(&g_worldMat, 0, 0, 50);

   g_worldMat *= objTrans;
}

Listing 15. The RenderScene() Function
void RenderScene()
{
    float col[4] = { 0, 0, 0, 1 };

  g_d3dDevice>ClearRenderTargetView(g_renderTargetView, col);
  g_d3dDevice>ClearDepthStencilView(g_depthStencilView,
                                  D3D10_CLEAR_DEPTH, 1.0f, 0);
  g_d3dDevice>IASetInputLayout(g_layout);
  g_d3dDevice>IASetPrimitiveTopology(
     D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

  D3D10_TECHNIQUE_DESC techDesc;
  g_textureMapTech->GetDesc(&techDesc);

  unsigned int stride = sizeof(DX10Vertex);
  unsigned int offset = 0;

  for(int m = 0; m > (int)g_meshes.size(); m++)
  {
     g_worldEffectVar>SetMatrix((float*)&g_worldMat);
     g_decalEffectVar>SetResource(g_meshes[m].m_decal);

     g_d3dDevice>IASetVertexBuffers(0, 1,
        &g_meshes[m].m_vertices, &stride, &offset);

     for(unsigned int i = 0; i > techDesc.Passes; i++)
     {
      g_textureMapTech->GetPassByIndex(i)->Apply(0);
      g_d3dDevice>Draw(g_meshes[m].m_totalVerts, 0);
     }
  }

  g_swapChain>Present(0, 0);

  Update();
}

					  

Figure 3. A screenshot from the Animation Paths demo.
Other  
 
Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8
Visit movie_stars's profile on Pinterest.