The number of asteroids increases with every level you progress in the game. In some scenarios, it can cause performance problems, because data of all the asteroids are processed during update and render operations that are performed very often. In this part, you will make some minor modifications that can significantly increase the overall performance, what will be especially important during the higher levels of the game.
At the beginning, you will limit the number of operations performed in the Update
method of the GameRenderer
class. You can see in the following part of the code that the Update
method is called for each asteroid created in the game world, even those which have been already passed by the rocket:
for_each(m_asteroids.begin(), m_asteroids.end(), [&](unique_ptr<Asteroid>& a) { a->Update(&m_viewMatrix, timeTotal); });
You should correct it, by replacing the preceding part with:
float rocketZ = m_rocket.GetZ(); float asteroidZ = 0.0f; for_each(m_asteroids.begin(), m_asteroids.end(), [&](unique_ptr<Asteroid>& a) { asteroidZ = a->GetZ(); if (asteroidZ < rocketZ && abs(asteroidZ - rocketZ) <= SA3D_ASTEROIDS_DISTANCE_FILTER) { a->Update(&m_viewMatrix, timeTotal); } });
The current version updates data of only those asteroids that are placed before the rocket and for which a distance between an asteroid and the planet is no longer than a constant value named SA3D_ASTEROIDS_DISTANCE_FILTER
. Its default value can be set to 50.0f
.
At the end, you extract the logic of updating the planet and asteroids (starting from calling the Update
method on the Planet
instance), as an additional method called UpdatePlanetAndAsteroids
, which takes one parameter (total elapsed time) and does not return any value. You should declare it in the header file and call at the end of the Update
method.
Also in the Render
method of the GameRenderer
class, some unnecessary operations are performed. Their elimination is really important because your game uses very basic approach of rendering many objects from the game world. However, such an approach is chosen to keep the simplicity of the content that should be suitable for beginners in the area of 3D games development. You can improve performance of the Render
method by rendering only a subset of asteroids. First of all, you remove the following part of code:
for (UINT i = 0; i < m_asteroids.size(); i++) { m_asteroids[i]->Render(m_d3dContext, m_constantBuffer); }
You should replace it with the improved version:
float rocketZ = m_rocket.GetZ(); float asteroidZ = 0.0f; for_each(m_asteroids.begin(), m_asteroids.end(), [&](unique_ptr<Asteroid>& a) { asteroidZ = a->GetZ(); if (asteroidZ < rocketZ && abs(asteroidZ - rocketZ) <= SA3D_ASTEROIDS_DISTANCE_FILTER) { a->Render(m_d3dContext, m_constantBuffer); } });
This method renders only those asteroids that are placed before the rocket and for which the distance between a particular asteroid and the planet is not greater than the constant value defined earlier.
To improve the code readiness, you extract a part of the code that renders the planet and asteroids (starting with setting values of the stride
and offset
local variables) as an additional method called RenderPlanetAndAsteroids
. It does not take parameters nor returns any value. Of course, you should not forget to declare it in the header file and call at the end of the Render
method.
3.21.104.183