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