Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SFML2] RenderTexture messes with Texture it should draw  (Read 1695 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 36
    • View Profile
[SFML2] RenderTexture messes with Texture it should draw
« on: September 09, 2011, 07:27:29 pm »

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:

Code: [Select]
#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;

    std::map<std::pair<int,int>,std::pair<sf::Texture,sf::Sprite> > DrawMap;   // 2d-matrix of drawn sprites.

    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();

        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.
    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.


  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SFML2] RenderTexture messes with Texture it should draw
« Reply #1 on: September 09, 2011, 08:24:19 pm »
It's a bug in the internal cache of SFML, comment line 236 of src/SFML/Graphics/Renderer.cpp to get rid of it. This will be fixed soon with the new graphics API :)
Laurent Gomila - SFML developer


  • Newbie
  • *
  • Posts: 36
    • View Profile
[SFML2] RenderTexture messes with Texture it should draw
« Reply #2 on: September 09, 2011, 08:35:24 pm »
It works perfectly now, thank you very much! :D