COCOS2DX WIN32 version CPU usage 25% improvement policy

Source: Internet
Author: User

I guess it may be that Sleep (0) is used in the main loop, and the specific code is found. It is located in cocos2dx \ platform \ win32 \ CCApplication. cpp, and its length is roughly as follows:

1 while( 1 ) {
2 if(With messages ){
3 if(Time to) Update timing, call the main cyclic function;
4 else Sleep(0);
5 }
6 // Other code that jumps out of the loop
7 }

 

That is to say, in addition to executing mainLoop, this loop spends a lot of time checking messages and Sleep (0.

 

In addition, I also found a strange phenomenon (why is it unknown for the time being), that is:

The AppDelegate. cpp file of the HelloCPP project contains a line of code:

 

// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);

  

 

If the above 60 is changed to a large value, the frame speed will never change if it is changed to 60. But if it is changed to less than 60, it can work.

 

As a result, the idea of solving CPU usage began with the idea of "can reduce the cycle precision.

It is known that, under normal circumstances, executing Sleep (1) will Sleep for about 1/50 seconds. This time is not accurate or accurate, and it does not seem to be able to meet the smoothness requirement of 60 fps. However, if the frame speed of a game does not need to be so high, such as 30 fps ?? This solution is very feasible.

 

After testing, change Sleep (0) to Sleep (1), and then change 60 in the code above to 25. The effect is remarkable. But another problem arises: if each game has a lot of loop tasks and a long time, the game will be slowed down.

 

In the original engine, the synchronization time code is as follows:

 

QueryPerformanceCounter(&nNow);
if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart) {
nLast.QuadPart = nNow.QuadPart;

  

Because every time nNow time is recorded in nLast and compared with the set interval, the time difference is usually greater than the set interval, if it is in an inaccurate Sleep (1) when the burden on each cycle is large, the actual time consumed by each frame will exceed the set interval, this slows down the speed of the game (if the game is timing by frame ).

To solve this problem, I use time alignment. Actually, I changed the update nLast expression:

 

nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart %m_nAnimationInterval.QuadPart);

  

In this way, the total consumption time of each frame is quite constant.

 

The above problem is not perfect. How can I keep 60 fps to 0% cpu usage? The solution I want to consider is to modify the precision of Sleep (1.

Find the information and find Winmm. the lib library contains timeBeginPeriod (1); timeEndPeriod (1); the function can be used for this purpose, so that the precision of Sleep (1) is increased to 1 millisecond level, so you can change it:

1. Add a reference to the Winmm. lib library. Here I add the # pragma comment (lib, "Winmm. lib") Statement in the CCApplication. cpp header.

2. Put timeBeginPeriod (1); timeEndPeriod (1); statement before and after the while (1) code segment.

This is done.

 

After testing, the frame speed is set to less than 59 fps, And the cpu usage can be 0 (i7 2600 k ). If the value is set to 60, the cpu usage will fluctuate cyclically. Set it to 60 +, and the cpu will be 100%.

However, even if this problem has come to an end for the time being, it is better to limit the program to 50 fps first, it is smooth, no problem, and it is also convenient to calculate...

Related Article

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.