2017-10-15 88 views
0

的位置。遺憾的是像素讀取不起作用:像素與specyfic顏色不是我想要加載圖像到窗口的可能性,然後閱讀特定的像素,改變其顏色,最後把像素別的東西就被戴上SFML

#include <iostream> 
using namespace std; 

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

inline bool isOnScreen(const sf::RenderWindow& screen, int x, int y) 
{ 
    return 0 <= x && x < screen.getSize().x && 0 <=y && y < screen.getSize().y; 
} 

void setPixelColor(sf::RenderWindow& screen, int x, int y, const sf::Color& color) 
{ 
    if (isOnScreen(screen, x, y)) 
    { 
     sf::Vertex vertex(sf::Vector2f(x, y), color); 
     screen.draw(&vertex, 1, sf::Points); 
    } 
    else 
    { 
     cerr << "point " << x << ", " << y << " out of screen, which is: " << screen.getSize().x << ", " << screen.getSize().y << "!\n"; 
    } 
} 

sf::Color getPixelColor(sf::RenderWindow& screen, int x, int y) 
{ 
    if (isOnScreen(screen, x, y)) 
    { 
     return screen.capture().getPixel(x, y); 
    } 
    cerr << "Out of stage: " << x << ", " << y << endl; 
    return sf::Color::Transparent; 
} 

void printColor(const sf::Color& color) 
{ 
    cout << "Color: " << (int)color.r << ", " << (int)color.g << ", " << (int)color.b << '\n'; 
} 

int main() 
{ 
    sf::RenderWindow screen(sf::VideoMode(900, 600), "window"); 

    sf::Color myColor{255, 128, 0}; 

    int x=10, y=10; 
    setPixelColor(screen, x, y, myColor); 

    auto readColor = getPixelColor(screen, x, y); // it is 0, 0, 0 
    printColor(readColor); 

    screen.display(); 

    while (screen.isOpen()) 
    { 
     sf::Event event; 
     while (screen.pollEvent(event)) 
     { 
      if (sf::Event::Closed == event.type) 
      { 
        screen.close(); 
        break; 
      } 
     } 
    } 
} 

我不知道爲什麼來自同一個地方讀取的顏色,我已經把它的值爲0,0,0。 奇怪的是,有效顏色在位置靠下的位置,我已經把它:

readColor = getPixelColor(screen, x, y-1); // it is valid colour 
    printColor(readColor); 

它可以是某種骯髒的解決方案的,但有時我需要閱讀從位置y = 0像素,所以在這種情況下我的黑客不工作。

我現在SFML版本是SFML-2.4.2 GCC-4.9.2-TDM-32位在Windows 7

回答

1

我試圖回答用一個例子的問題。我認爲你可以在此基礎上構建解決方案。我將圖像加載到sprite的紋理中,然後讀取特定的像素並更改其顏色。我不明白的是,你寫了「最後把像素放在其他地方」。顯然我無法回答這個問題。

當你執行這個功能,你會看到在10一個微小的綠色像素,10希望這有助於。

#include <iostream> 
using namespace std; 

#include <SFML/Graphics.hpp> 

sf::Color setPixelColor(sf::Texture& texture, int x, int y, sf::Color color){ 
    // Get texture image 
    sf::Image image = texture.copyToImage(); 

    // Optional show case: read size of image (not used in this function) 
    int width = image.getSize().x; 
    int height = image.getSize().y; 

    // Get color of specific position 
    sf::Color imagecolor = image.getPixel(x, y); 

    // Just an example: if alpha is above 128, make it green 
    if (imagecolor.a > 128){ 
    imagecolor.g = 255; 
    } 

    // Set pixel to color 
    image.setPixel(x, y, color); 

    // Update texture 
    texture.loadFromImage(image); 
} 

int main(){ 
    sf::RenderWindow screen(sf::VideoMode(900, 600), "window"); 

    sf::Sprite background; 
    sf::Texture texture; 

    if (!texture.loadFromFile("background.png")){ 
    cout << "Error loading texture" << endl; 
    } 
    background.setTexture(texture); 

    while (screen.isOpen()){ 
    sf::Event event; 
    while (screen.pollEvent(event)){ 
     if (sf::Event::Closed == event.type){ 
     screen.close(); 
     break; 
     } 
    } 

    setPixelColor(texture, 10, 10, sf::Color::Green); 

    screen.clear(); 
    screen.draw(background); 
    screen.display(); 
    } 
} 
相關問題