I looked at your code a little.
In Entity.cpp - If you use something more than once, I think it's better to store it ( getGlobalBounds could do something extra each call )
auto globalBounds = entSprite.getGlobalBounds();
entSprite.setOrigin(globalBounds.width / 2, globalBounds.height / 2);
In textureManager.cpp
sf::Texture* textureManager::addTexture(const std::string &filepath, const std::string &name)
{
sf::Texture* newTexture = getTexture(name);
if( newTexture )
return newTexture;
newTexture = new sf::Texture;
if(newTexture->loadFromFile(filepath)) // otherwise, load a new texture from desired path
{
s_allTextures[name] = std::make_shared<sf::Texture>(*newTexture); // and then add it to the map
return newTexture;
}
std::cout << "Could not load " << filepath << std::endl; // if it can't load, filepath didn't work
return nullptr;
}
sf::Texture* textureManager::getTexture(const std::string &name)
{
auto ret = m_allTextures.find(name);
if( ret != m_allTextures.end() )
{
return ret->second;
}
return nullptr;
}
I didn't test it. I wrote getTexture more clear. Same with creating new Texture
Old
sf::Texture *newTexture = new sf::Texture;
newTexture = getTexture(name);
if (newTexture) // check if getTexture doesnt return nullptr
{
return newTexture; // if it does, return it
}
newTexture = new sf::Texture;
if (newTexture->loadFromFile(filepath)) // otherwise, load a new texture from desired path
{
allTextures[name] = std::make_shared<sf::Texture>(*newTexture); // and then add it to the map
return allTextures[name].get();
}
std::cout << "Could not load " << filepath << std::endl; // if it can't load, filepath didn't work
return nullptr;
Here - sf::Texture *newTexture = new sf::Texture; - you created a new texture and than ?
BUM - newTexture = getTexture(name); - you request a pointer to texture (old texture wasn't released)
and 7 lines under BAM - newTexture = new sf::Texture; again new texture ( this time is needed ). So you had mem leak.
In collision.cpp
sf::Vector2f clsn::handleCollision(sf::FloatRect &AABBFirst, sf::FloatRect &AABBSecond) // A is object colliding. I.E, player and the wall
{
sf::Vector2f ret(0, 0);
sf::Vector2f centerA(AABBFirst.left + (AABBFirst.width / 2), AABBFirst.top + (AABBFirst.height / 2));
sf::Vector2f centerB(AABBSecond.left + (AABBSecond.width / 2), AABBSecond.top + (AABBSecond.height / 2));
sf::Vector2f distance(centerA - centerB);
sf::Vector2f minDistance((AABBFirst.width / 2) + (AABBSecond.width / 2), (AABBFirst.height / 2) + (AABBSecond.height / 2));
if (abs(distance.x) < minDistance.x && abs(distance.y) < minDistance.y)
{
ret = sf::Vector2f(distance.x > 0 ? minDistance.x - distance.x : -minDistance.x - distance.x,
distance.y > 0 ? minDistance.y - distance.y : -minDistance.y - distance.y);
}
return ret;
}
You could have just 1 return.
I don't have time for checking whole code. Keep going.