Riscky
Riscky

Reputation: 23

sfml generated texture is not drawn to window

What I'm trying to do:

Expected result:

The result:

What I've done so far:

The tools I use:

Example code (completely stripped down, and useless in this form, but it displays the problem):

#include <iostream> 
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
    // width and height of the window
    // this will not exceed max size of texture
    uint width = 128;
    uint height = 128;

    // create new window
    sf::RenderWindow * window = new sf::RenderWindow(sf::VideoMode(height,width), "Mandelbrot");

    // create pixel array
    vector<sf::Uint8> * pixels = new vector<sf::Uint8>(height * width * 4);

    // fill array with collors;
    for (uint i = 0; i < height*width; i++)
    {
        pixels->at(i*4 + 2) = 255;
    }

    // create texture
    sf::Texture * texture = new sf::Texture();
    texture->create(width,height);
    texture->update(pixels->data());

    // create sprite to hold the texture
    sf::Sprite * sprite = new sf::Sprite();
    sprite->setTexture(*texture);
    sprite->setTextureRect(sf::IntRect(0, 0, width, height));

    // draw the sprite
    window->draw(*sprite);

    // draw buffer to screen
    window->display();

    // close window on system close action
    sf::Event event;
    while (window->waitEvent(event))
    {
        if (event.type == sf::Event::Closed)
        {
            // close window
            window->close();
            return 0;
        }
    }

return 1;
}

Upvotes: 2

Views: 454

Answers (2)

meowgoesthedog
meowgoesthedog

Reputation: 15035

You are not setting the alpha channel of every pixel (RGBA format). It needs to be 255 for a fully opaque image:

for (uint i = 0; i < height*width; i++)
{
    pixels->at(i*4 + 3) = 255; // alpha
    pixels->at(i*4 + 2) = 255; // blue
}

Upvotes: 1

Martin Sand
Martin Sand

Reputation: 518

I think you did not set all the pixels for the texture but only every fourth, starting with 2. Try this:

for (uint i = 0; i < height*width; i++)
    {
        pixels.at(i*4 + 0) = 255;
        pixels.at(i*4 + 1) = 221;
        pixels.at(i*4 + 2) = 255;
        pixels.at(i*4 + 3) = 255;
    }

Upvotes: 0

Related Questions