Ok, I'm going now for the array approach as the array is "more stable" than a vector. One small question: How to unload for example image0.png from memory or succesfully replace it by a 1x1 pixel png, so the memory is "freed". I tried out some attempts, but without success.
#include <SFML/Graphics.hpp>
#include <iostream>
#include <array>
int main()
{
sf::RenderWindow mMainWindow(sf::VideoMode(1000,500), "Map", sf::Style::Close);
sf::View view(sf::Vector2f(500, 300), sf::Vector2f(1000, 500));
mMainWindow.setFramerateLimit(60);
mMainWindow.setKeyRepeatEnabled(false);
sf::Image ImageName;
sf::Texture TextureName;
sf::Sprite SpriteName;
std::array<sf::Image, 4> ImageArray = { {ImageName, ImageName, ImageName, ImageName} };
std::array<std::string, 4> FileNameArray = { {"map0.png", "map1.png", "map2.png", "map3.png"} };
std::array<sf::Texture, 4> TextureArray = { {TextureName, TextureName, TextureName, TextureName} };
std::array<sf::Sprite, 4> SpriteArray = { {SpriteName, SpriteName, SpriteName, SpriteName} };
std::array<sf::Vector2i, 4> TexturePositionArray = { {sf::Vector2i(0,0), sf::Vector2i(500,0), sf::Vector2i(1000,0), sf::Vector2i(1500,0)} };
for(int i=0; i<4; i++)
{
(ImageArray[i]).loadFromFile((FileNameArray[i]));
(TextureArray[i]).loadFromImage((ImageArray[i]));
(SpriteArray[i]).setPosition(TexturePositionArray[i].x,TexturePositionArray[i].y);
(SpriteArray[i]).setTexture((TextureArray[i]));
}
while (mMainWindow.isOpen())
{
sf::Event event;
sf::Vector2i mousePos;
bool leftclicked = false;
while (mMainWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
mMainWindow.close();
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
{
leftclicked = true;
mousePos = sf::Vector2i(event.mouseButton.x, event.mouseButton.y);
break;
}
}
}
if(leftclicked)
{
//1. attempt:
//for (int i = 0; i < 4; i++)
//{
// ImageArray[i].loadFromFile("white.png") ; //Replacement by white texture of the same size: texture must have same size du to possible erros with collision detection... (not implemented here)
// TextureArray[i].loadFromImage(ImageArray[i]); // Memory usage goes up significantly
// SpriteArray[i].setTexture((TextureArray[i]));
//}
//2. attempt:
//ImageArray[0] = sf::Image(); //Image still visible, memory usage unchanged......
//3. attempt:
//ImageArray[0].loadFromFile(""); //Image still visible, memory usage unchanged......
//4. attempt:
ImageArray[0].loadFromFile("white.png"); // .....Memory usage goes up significantly
TextureArray[0].update(ImageArray[0].getPixelsPtr());
}
mMainWindow.clear();
mMainWindow.setView(view);
for(int i=0; i<4; i++)
{
mMainWindow.draw(SpriteArray[i]);
}
mMainWindow.display();
}
return 0;
}