1
General / Sprites turn into white boxes when deleting objects
« on: August 21, 2022, 11:59:50 pm »
New to C++, I'm creating a program that spawns "platforms" which move up and get deleted once they hit the screen's top.
I read the White Box problem and used new for the textures, but my sprites still turn white whenever an object from the vector is erased. However, they seem to regain their textures right after, which hasn't happened to me before.
I read the White Box problem and used new for the textures, but my sprites still turn white whenever an object from the vector is erased. However, they seem to regain their textures right after, which hasn't happened to me before.
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <vector>
class Platform
{
private:
sf::Sprite m_sprite;
sf::Texture* m_texture;
public:
Platform() : m_texture{ new sf::Texture }
{
if (!m_texture->loadFromFile("textures/platform.png"))
{
throw "image not found";
}
m_sprite.setTexture(*m_texture);
m_sprite.setPosition(100, 1080);
}
Platform(const Platform& copy) : m_sprite{ copy.m_sprite }, m_texture{ new sf::Texture }
{
if (!m_texture->loadFromFile("textures/platform.png"))
{
throw "image not found";
}
m_sprite.setTexture(*m_texture);
}
sf::Sprite getSprite() { return m_sprite; }
void moveUp() { m_sprite.move(0, -10.f); }
~Platform() { delete m_texture; }
};
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "SFML works!", sf::Style::Fullscreen);
window.setFramerateLimit(60);
std::vector<Platform> platforms;
int timer{};
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == sf::Event::Closed)
{
window.close();
}
}
//Spawn platforms every second
timer++;
if (timer >= 60)
{
platforms.push_back(Platform{});
timer = 0;
}
//Erase platforms that go off screen
for (auto i{ platforms.begin() }; i < platforms.end(); )
{
i->moveUp();
if (i->getSprite().getPosition().y < 0)
{
i = platforms.erase(i);
}
else
{
i++;
}
}
window.clear();
//Draw
for (auto i{ platforms.begin() }; i < platforms.end(); )
{
window.draw(i->getSprite());
i++;
}
window.display();
}
return 0;
}
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <vector>
class Platform
{
private:
sf::Sprite m_sprite;
sf::Texture* m_texture;
public:
Platform() : m_texture{ new sf::Texture }
{
if (!m_texture->loadFromFile("textures/platform.png"))
{
throw "image not found";
}
m_sprite.setTexture(*m_texture);
m_sprite.setPosition(100, 1080);
}
Platform(const Platform& copy) : m_sprite{ copy.m_sprite }, m_texture{ new sf::Texture }
{
if (!m_texture->loadFromFile("textures/platform.png"))
{
throw "image not found";
}
m_sprite.setTexture(*m_texture);
}
sf::Sprite getSprite() { return m_sprite; }
void moveUp() { m_sprite.move(0, -10.f); }
~Platform() { delete m_texture; }
};
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "SFML works!", sf::Style::Fullscreen);
window.setFramerateLimit(60);
std::vector<Platform> platforms;
int timer{};
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == sf::Event::Closed)
{
window.close();
}
}
//Spawn platforms every second
timer++;
if (timer >= 60)
{
platforms.push_back(Platform{});
timer = 0;
}
//Erase platforms that go off screen
for (auto i{ platforms.begin() }; i < platforms.end(); )
{
i->moveUp();
if (i->getSprite().getPosition().y < 0)
{
i = platforms.erase(i);
}
else
{
i++;
}
}
window.clear();
//Draw
for (auto i{ platforms.begin() }; i < platforms.end(); )
{
window.draw(i->getSprite());
i++;
}
window.display();
}
return 0;
}