Directx11 tutorial (9) adds a timerclass class

Source: Internet
Author: User

Based on the code in the previous tutorial, we added a timerclass class. The function of this class is very simple, that is, it can calculate the time difference between two adjacent frames. This time difference can be used to achieve smooth animation, so that the animation will not be different from the FPs of different machines, so that the animation effect becomes faster or slower.

We mainly use the queryperformancecounter function to query the counter value of the timer.

If there is a timer in the hardware, it will start the timer, and then it will continuously get the value of the timer, such a timer accuracy, just as accurate as the hardware clock crystal.

The timerclass. H code is as follows:

# Pragma once
# Include <windows. h>

// Mainly used to calculate the time between two adjacent frames
// It can be used to achieve smooth FPS-independent animation.
Class timerclass
{
Public:
Timerclass (void );
Timerclass (const timerclass &);
~ Timerclass (void );
Bool initialize ();
Void frame ();

Float gettime ();

PRIVATE:
Int64 m_frequency;
Float m_ticksperms;
Int64 m_starttime;
Float m_frametime;

};

The timerclass. CPP Code is as follows:

# Include "timerclass. H"

Timerclass: timerclass (void)
{
}

Timerclass: timerclass (const timerclass & other)
{
}

Timerclass ::~ Timerclass (void)
{
}

Bool timerclass: Initialize ()
{
// Check whether the system supports high-precision timer.
Queryperformancefrequency (large_integer *) & m_frequency );
If (m_frequency = 0)
{
Return false;
}

// Obtain the number of counters per millisecond.
M_ticksperms = (float) (m_frequency/1000 );

Queryperformancecounter (large_integer *) & m_starttime );

Return true;
}

// Each rendering frame is called to calculate the time between frames.
Void timerclass: frame ()
{
Int64 currenttime;
Float timedifference;

Queryperformancecounter (large_integer *) & currenttime );

Timedifference = (float) (currenttime-m_starttime );

M_frametime = timedifference/m_ticksperms;

M_starttime = currenttime;

Return;
}

Float timerclass: gettime ()
{
Return m_frametime;
}

We add some code in systemclass. H, including timerclass. h.

...

# Include "graphicsclass. H"
# Include "timerclass. H"

Const float Pi = 3.14159265358979323f;

Class systemclass
{
...

Graphicsclass * m_graphics;

// Timer class
Timerclass * m_timer;
};

 

The systemclass. CPP Code is as follows. I only pasted the function that modified the code.

# Include "systemclass. H"

Systemclass: systemclass (void)
{
M_input = 0;
M_graphics = 0;

M_timer = 0;

}

// Call window initialization functions and other class initialization functions
Bool systemclass: Initialize ()
{
...
// Initialize the graphic object
Result = m_graphics-> initialize (screenwidth, screenheight, m_hwnd );
If (! Result)
{
Return false;
}
// Create a timer object.
M_timer = new timerclass;
If (! M_timer)
{
Return false;
}

// Initialize the timer object
Result = m_timer-> initialize ();
If (! Result)
{
MessageBox (m_hwnd, l "cocould not initialize the timer object.", l "error", mb_ OK );
Return false;
}

Return true;
}

Bool systemclass: frame ()
{
...

M_timer-> frame ();

You can set the camera Rotation Angle Based on the time, which basically ensures that the rotation will not be different from the FPS.

// Animation, rotating camera
M_graphics-& gt; m_camera-& gt; Roll (m_timer-& gt; gettime ()/1000 );

// Execute the frame rendering function.
Result = m_graphics-> frame ();
If (! Result)
{
Return false;
}
Return true;
}

After the program is executed, you will find a rotating color cube. You can also use Q, W, E, A, S, D, Z, X, C to control the camera and change the observation direction.

The diagram of the program is the same as that in the previous tutorial.

Complete code can be found:

Project File mytutoriald3d11_8

Download Code:

Http://files.cnblogs.com/mikewolf2002/myTutorialD3D11.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.