So this is the last blog post for this project. We have managed to complete most of the feature we wanted to have for the beta so this week have mostly been about cleaning up the code and trying to eliminate memory leaks. This makes it harder for me to find an artifact to write about in this weeks blog post.
The new particle system implemented in our game last week resulted in some major memory leaks. I mentioned smart pointers last week, and I solved this memory leak using smart pointers. We used unique_ptr in our project. They keep track of the memory they point to and delete themselves when they have completed their task. The advantage for us using this method is that it’s more simple for us to use, and make the code shorter. One disadvantage, and a reason that we maybe should have not have used this method in this project, is that we should practice deleting pointers in a traditional way to get a greater understanding on how they work. We don’t have enough time to change back, and they work as we want them to so we won’t change them back.
Another problem I had with the smart pointer, or at least I think it was because of it, was that I had to write some collision checking in another way. Using a for loop crashed the game when collision between some objects happened. So instead I had to use “auto, while”. I don’t know why this works while the for loop doesn’t but at least it works now.
Here is an example of the collision checking:
auto enemybulletIter = m_enemyBullet.begin();
while (enemybulletIter != m_enemyBullet.end())
{
bool enemyDied = false;
auto bulletsIter = m_bullets.begin();
while (bulletsIter != m_bullets.end())
{
if (enemybulletIter->get()->getSprite()->getGlobalBounds().intersects(bulletsIter->get()->getSprite()->getGlobalBounds()) &&
enemybulletIter->get()->IsVisible() && bulletsIter->get()->IsVisible())
{
enemyDied = true;
enemybulletIter = m_enemyBullet.erase(enemybulletIter);
bulletsIter = m_bullets.erase(bulletsIter);
m_PlayerScore += MAX_SCORE;
++m_bulletCount;
break;
}
else
{
++bulletsIter;
}
}
if (!enemyDied)
++enemybulletIter;
}
It checks if two sprites intersects with each other and if that is the case remove both of them from the smart pointer “list”. This does almost the same thing as a for loop would do, but works better in this instance, and also looks more advanced for better or worse. We also have a nice debug screen activated by pressing F3. With that we can see if the objects in game are really removed when they should be.
A picture showing the debug info:
I have also reduced the size of Enter and Update in the GameState.cpp. In the Enter I created functions where I moved parts of the Enter, to give the code a cleaner look. Now I call on these functions in enter to load input, sound, textures and text. For the Update I moved some parts of the code to already existing update functions and created some new ones. With the load background Love crated earlier we have now moved about 300 lines of code out of the Enter, and around 100 for the Update. This won’t make any difference in the game play but makes the code cleaner and easier to read.
After this was done I went through the whole code to restructure some parts of the code, removing unnecessary white space and remove some code we did not use anymore, and removed some code that were commented out and only made the code harder to read.
This blog post was a little messy because of the lack of a good topic to talk about. With this being my last blog post I just want to mention that I thing I have learned a lot during this project, and while these couple of weeks have had their ups and down it has been a fun experience!