SFML community forums

Help => Graphics => Topic started by: firefly431 on July 16, 2013, 01:56:37 am

Title: sf::Sprite white rectangle
Post by: firefly431 on July 16, 2013, 01:56:37 am
Using the following code:
static void loadCourier(sf::Texture &courier, sf::Image &im)
{
    sf::Uint8 pxls[256 * 96 * 4];
    std::ifstream fl("Courier.pbm", std::ios_base::binary);
    if (!fl.good()) {
        std::runtime_error e("Could not load Courier.pbm");
        throw e;
    }
    fl.seekg(10, std::ios_base::beg);
    char c;
    unsigned int i = 0;
    while (fl.good()) {
        fl.get(c);
        pxls[i]=pxls[i+1]=pxls[i+2]=pxls[i+3]=((c&128)?255:0);
        pxls[i+4]=pxls[i+5]=pxls[i+6]=pxls[i+7]=((c&64)?255:0);
        pxls[i+8]=pxls[i+9]=pxls[i+10]=pxls[i+11]=((c&32)?255:0);
        pxls[i+12]=pxls[i+13]=pxls[i+14]=pxls[i+15]=((c&16)?255:0);
        pxls[i+16]=pxls[i+17]=pxls[i+18]=pxls[i+19]=((c&8)?255:0);
        pxls[i+20]=pxls[i+21]=pxls[i+22]=pxls[i+23]=((c&4)?255:0);
        pxls[i+24]=pxls[i+25]=pxls[i+26]=pxls[i+27]=((c&2)?255:0);
        pxls[i+28]=pxls[i+29]=pxls[i+30]=pxls[i+31]=((c&1)?255:0);
        i += 32;
    }
    im.create(256, 96, pxls);
    courier.loadFromImage(im);
    std::cout << "Loaded.\n";
}

Resources::Resources()
: courier(), textshader(), im()
{
    loadCourier(courier, im);
    // ...
}
// In .h file
class Resources
{
public:
    Resources();
    ~Resources();
    sf::Texture courier;
    sf::Shader textshader;
    sf::Image im;
    static Resources &get();
};
// ...
class CourierText : public sf::Drawable, public sf::Transformable
{
// ...
public:
    sf::Sprite s;
// ...
}
// In .cpp file
s.setTexture(Resources::get().courier, true);
states.blendMode = sf::BlendAlpha;
states.transform *= getTransform();
target.draw(s, states);
 
The sprite is a white rectangle.
Title: Re: sf::Sprite white rectangle
Post by: eXpl0it3r on July 16, 2013, 02:17:16 am
Why don't make a complete and minimal example, so we have a closed environment to figure out where things go wrong? And complete means, that one only has to copy & paste the code, compile and run it. And minimal means, you take out everything that isn't important to the problem, e.g. you can simply load a texture instead of generating an image etc. or you could just draw it, instead of applying a blend mode. That way you could essentially isolate the problem further and might be able to figure out on your own what the issue is. ;)
Title: Re: sf::Sprite white rectangle
Post by: firefly431 on July 16, 2013, 07:06:57 am
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    if (!sf::Shader::isAvailable()) {
        std::cerr << "Shaders are unavailable.\n";
        return 1;
    }
    sf::Image im;
    sf::Texture tex;
    sf::Sprite t;
    im.loadFromFile("out.png");
    tex.loadFromImage(im);
    t.setTexture(tex);
    sf::RenderWindow win(sf::VideoMode(640, 480), "Test Window");
    win.setVerticalSyncEnabled(true);
    while (win.isOpen()) {
        sf::Event ev;
        while (win.pollEvent(ev)) {
            if (ev.type == sf::Event::Closed) {
                win.close();
            }
        }
        win.clear(sf::Color::Red);
        win.draw(t);
        win.display();
    }
    return 0;
}
 
Title: AW: sf::Sprite white rectangle
Post by: eXpl0it3r on July 16, 2013, 09:33:54 am
So does that example work or not (since you posted an image)?

The code should essentially work, if it doesn't for you then you might want to check if the loading was successful and you could try reorder ghings, so the window is first initialized and then the texture.
Title: Re: sf::Sprite white rectangle
Post by: Laurent on July 16, 2013, 09:43:22 am
https://github.com/SFML/SFML/issues/211
Title: Re: sf::Sprite white rectangle
Post by: firefly431 on July 16, 2013, 07:44:06 pm
https://github.com/SFML/SFML/issues/211
Thanks; it worked.
(I moved the Shader::isAvailable() to just before the main loop)