Hello,
while working on my game I ran into a really strange issue: I'm using a standard map container to dynamically store 80x80 large blocks of the level. Another map container contains a texture-sprite pair which is pre-rendered when the associated block is loaded.
This has worked fine so far, but now I'm having problems with lighting. Light is calculated and drawn when a block loads: a 'global' RenderTexture is cleared, the lighting sprites are drawn onto it, and then it is copied into a Texture in a Sprite/Texture std-map. However, the RenderTexture seems to modify the Texture the lighting sprites use: they are turned into garbage, or parts of the actual level, but most of them do not display at all and the RenderTexture remains black. I wrote a minimal example program that reproduces the error, it uses the same method that I use in my game, just simplified:
#include <SFML/Graphics.hpp>
#include <map>
#include <utility>
int main() {
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");
sf::Texture Tex;
if (!Tex.LoadFromFile("test.png")) // this can be any image file smaller than 100x100.
return EXIT_FAILURE;
sf::Sprite TestSpr(Tex);
sf::RenderTexture RTex;
RTex.Create(100,100);
std::map<std::pair<int,int>,std::pair<sf::Texture,sf::Sprite> > DrawMap; // 2d-matrix of drawn sprites.
App.SetFramerateLimit(70);
for (int x=0; x<8; x++)
for (int y=0; y<6; y++) {
const std::pair<int,int> cur = std::make_pair(x,y); // current entry in map.
// Tex.LoadFromFile("test.png");
RTex.Clear(); // clear rendertexture.
RTex.Draw(TestSpr); // draw test sprite.
RTex.Display(); // update texture.
DrawMap[cur].first.LoadFromImage(RTex.GetTexture().CopyToImage()); // copy rendertexture to texture in drawmap.
DrawMap[cur].second.SetTexture(DrawMap[cur].first); // define sprite.
DrawMap[cur].second.SetPosition(x*100,y*100); // position sprite.
}
while (App.IsOpened()) {
sf::Event Event;
while (App.PollEvent(Event)) {
if (Event.Type == sf::Event::Closed) App.Close();
}
App.Clear();
for (int x=0; x<8; x++)
for (int y=0; y<6; y++)
App.Draw(DrawMap[std::make_pair(x,y)].second); // render map.
App.Display();
}
return EXIT_SUCCESS;
}
Change "test.png" to any image smaller than 100x100 px. The result I am getting is that the texture gets smaller and smaller every time it is drawn. However, if you uncomment the line "Tex.LoadFromFile("test.png");", the image is drawn correctly every time. For my experiments, I used an 80x80 PNG image with an alpha channel, but it seems to happen with most files.
I am using CrunchBang Linux, the latest version of SFML2 (updated an hour ago), Code::Blocks w/ GCC 4.6, my video card is NVIDIA GeForce GTX 560Ti with driver version 280.13.
Any help with this strange issue would be greatly appreciated.