2017-08-29 78 views
2

我想要做的事:SFML生成的紋理未按窗口

  • 繪製產生SF ::質感窗口

預期結果:

  • 窗口填充紋理(在示例代碼中:綠色背景)

的結果:

  • 空白(黑色)窗口

什麼我迄今所做的:

    從文件紋理
  • 試圖加載:工作
  • 嘗試將生成的紋理保存到文件中:作品
  • 嘗試生成較小的紋理,在Texture.getMaximumSize()的邊界內
  • 萎縮代碼降到了最低

我使用的工具:

  • OS(基於Ubuntu 16.04),Linux內核4.4
  • 編譯系統Kde的霓虹燈5.10:qmake的
  • sfml版本:2.3

示例代碼(完全剝離下來,在這種形式中無用,但它顯示了問題):

#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; 
} 

回答

0

你不設置每一個像素的alpha通道 (RGBA格式)。對於完全不透明的圖像,它需要爲255:

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

謝謝! alpha是第四個組件,所以'pixels-> at(i * 4 + 3)= 255;'是我需要的 – Riscky

+0

@Riscky對不起,我得到的訂單錯了 - 修正 – meowgoesthedog

0

我想你沒有設置紋理中所有的像素,但只有每第四,從2試試這個:

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; 
    }