SFML community forums

Help => Graphics => Topic started by: maehze on November 15, 2012, 09:47:40 pm

Title: White sprite
Post by: maehze on November 15, 2012, 09:47:40 pm
Problem: When mCity->getEntities is > 1 or mWindow->draw() is called twice the background is just white.
What the code does: It renders a grid once into gridSprite. Then every time gridSprite is drawed, after that some circles are drawed on this.
Code:
void CityGridViewer::paintCity()
{  
    // 1. Render grid if necessary
    static bool targetReady=false;
    static sf::Texture gridTex;
    static sf::Sprite gridSprite;

    if (!targetReady)
    {
        sf::RectangleShape cell;
        cell.setSize(sf::Vector2f(5,5));
        cell.setOutlineColor(sf::Color::Black);
        cell.setOutlineThickness(1);
        City::GridCellType const*const grid = mCity->getCityGrid(); // it returns a copy of the grid
        for(unsigned int x=0; x < mCity->getGridSize().x; ++x)
            for(unsigned int y=0; y < mCity->getGridSize().y; ++y)
            {
                cell.setFillColor(sf::Color::Red);
                cell.setPosition(x*scale, y*scale);
                mWindow->draw(cell);
            }

        sf::Image gridImage = mWindow->capture();
        gridTex.loadFromImage(gridImage);
        gridSprite.setTexture(gridTex);
        targetReady=true;
    }
    mWindow->draw( gridSprite );

    // 2. Render entities
    sf::CircleShape entity;
    entity.setFillColor(sf::Color::Red);
    entity.setRadius( 5 );
    for (auto& e : mCity->getEntities())
    {
        entity.setPosition( e.position.x, e.position.y );
        mWindow->draw(entity);
    }
}

Does anyone has an idea?
Additionally, i'm using win7 x64 and compile with msvc11 x86. I tested it on a x64 linux with gcc4.7 x64 and it worked...
Title: Re: White sprite
Post by: masskiller on November 15, 2012, 10:03:07 pm
Quote
 for(unsigned int y=0; y < mCity->getGridSize().y; ++y)
            {
                cell.setFillColor(sf::Color::Red); break;
                cell.setPosition(x*scale, y*scale);
                mWindow->draw(cell);
            }

Shouldn't that break be conditional?

Title: Re: White sprite
Post by: maehze on November 15, 2012, 11:14:07 pm
Quote
 for(unsigned int y=0; y < mCity->getGridSize().y; ++y)
            {
                cell.setFillColor(sf::Color::Red); break;
                cell.setPosition(x*scale, y*scale);
                mWindow->draw(cell);
            }

Shouldn't that break be conditional?

Well... it should not be there ;-)
I cleaned up the code before i posted it here and forgot this litte "break".
Original code:

                switch(grid[x + mCity->getGridSize().x * y])
                {
                    case(City::GridCellType::Free): cell.setFillColor(sf::Color::White); break;
                    case(City::GridCellType::PublicPlace): cell.setFillColor(sf::Color::Green); break;
                    case(City::GridCellType::PoliceStation): cell.setFillColor(sf::Color::Blue); break;
                    case(City::GridCellType::Building): cell.setFillColor(sf::Color::Black); break;
                    default: cell.setFillColor(sf::Color::Red); break;
                }
Title: Re: White sprite
Post by: eigenbom on November 16, 2012, 12:34:07 am
i think its because you need to call display() before capture(), BUT, you should use a rendertexture instead of capture() to do this sort of thing.

Quote
Image sf::RenderWindow::capture() const
Copy the current contents of the window to an image.

This is a slow operation, whose main purpose is to make screenshots of the application. If you want to update an image with the contents of the window and then use it for drawing, you should rather use a sf::Texture and its update(Window&) function. You can also draw things directly to a texture with the sf::RenderTexture class.

from http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderWindow.php#a9bd8655d0bac83145bfc329ea7a6d538 (http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderWindow.php#a9bd8655d0bac83145bfc329ea7a6d538)
Title: Re: White sprite
Post by: maehze on November 16, 2012, 07:52:13 pm
[...], BUT, you should use a rendertexture instead of capture() to do this sort of thing. [...]
I use the current version of SFML (1.6). sf::RenderTexture::capture() does only exist in 2.0.
Calling "display()" before "capture()" did not help, but now the background is black instead of white.

I can see the right background for a very short moment on startup.
Title: Re: White sprite
Post by: eigenbom on November 16, 2012, 11:22:08 pm
hmm, well upgrading to 2.0 may be the first step towards resolving this :)
Title: Re: White sprite
Post by: maehze on November 17, 2012, 08:55:15 pm
hmm, well upgrading to 2.0 may be the first step towards resolving this :)

I want to use a stable version of SFML.
Title: Re: White sprite
Post by: victorlevasseur on November 17, 2012, 08:56:05 pm
SFML 2.0 RC is less buggy and more stable than 1.6.
Title: Re: White sprite
Post by: eigenbom on November 17, 2012, 11:11:06 pm
SFML 2.0 RC is less buggy and more stable than 1.6.

yeah and unfortunately this isn't posted on the front of the website, leading a lot of people to use 1.6.
Title: Re: White sprite
Post by: Nexus on November 19, 2012, 10:24:55 am
That's true, probably the version 2.0 RC should appear before 1.6 on the download page. Same for tutorials and documentation ;)
Title: Re: White sprite
Post by: masskiller on November 19, 2012, 09:40:17 pm
I started with 1.6 due to that, I guess this should be brought properly in the website forum.
Title: Re: White sprite
Post by: Laurent on November 19, 2012, 10:43:09 pm
I hope to be able to release SFML 2 and the new website soon enough. The current website is like SFML 1.6: still the latest official "release", but totally unmaintained ;D
Title: Re: White sprite
Post by: eXpl0it3r on November 20, 2012, 12:58:28 am
I hope to be able to release SFML 2 and the new website soon enough. The current website is like SFML 1.6: still the latest official "release", but totally unmaintained ;D
I'll believe it only when it actually has happened and even then I'll verify it at least one hundred times. ;D
Title: Re: White sprite
Post by: FRex on November 20, 2012, 01:25:46 am
Laurent: So it's safe to assume 2.0 is safe, stable and efficient and should be used over 1.6?
Quote
I'll verify it at least one hundred times. ;D
In before release postponed for another 3 years..
Title: Re: White sprite
Post by: Laurent on November 20, 2012, 08:01:51 am
Quote
Laurent: So it's safe to assume 2.0 is safe, stable and efficient and should be used over 1.6?
Sure!