After reading
http://sfml-dev.org/documentation/2.0/classsf_1_1Texture.php usage examples and
http://en.sfml-dev.org/forums/index.php?topic=11900.msg82631You should really not use new[] and delete[]. Take std::vector instead.
I am trying to display an image from a set of pixel data from an old file format (width*height values, ranging from 0-255, mapped to a 256-color palette, the details aren't relevant).
My code won't compile if I attempt to use an std::vector.
As in, this compiles
mTexture.create(width, height);
mSprite.setTexture(mTexture, true);
sf::Uint8* pixels = new sf::Uint8[width * height * 4];
for (int i = 0; i < width*height; i+=4)
{
//TODO: Change to the palette mapping...
pixels[i] = colors[i];
pixels[i+1] = colors[i];
pixels[i+2] = colors[i];
pixels[i+3] = 255;
}
mTexture.update(pixels);
delete[] pixels;
but it doesn't if I try to change it to an std::vector...
mTexture.create(width, height);
mSprite.setTexture(mTexture, true);
std::vector<sf::Uint8> pixels(width * height * 4);
for (int i = 0; i < width*height; i+=4)
{
//TODO: Change to the palette mapping...
pixels[i] = colors[i];
pixels[i+1] = colors[i];
pixels[i+2] = colors[i];
pixels[i+3] = 255;
}
mTexture.update(pixels);
What should I be doing instead to avoid using new and delete[]? It looks like the usage example in the docs is using a dynamically allocated array
sf::Uint8* pixels = ...; // get a fresh chunk of pixels