具有特定颜色的像素不在 SFML 中放置的位置
pixel with specyfic color is not in the position it was put in 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版本是Windows 7上的SFML-2.4.2 gcc-4.9.2-tdm-32位
我试图用一个例子来回答这个问题。我认为您可以在此基础上构建解决方案。我将图像加载到精灵的纹理中,然后读取特定的像素并更改其颜色。我不明白的是,你写了"终于把像素放了别的东西"。显然我无法回答这个问题。
当你执行这个函数时,你会在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();
}
}
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- SFML 鼠标获取位置无法正常工作
- SFML 将形状绘制到屏幕上的多个位置
- SFML参考窗口在绝对位置绘制
- 具有特定颜色的像素不在 SFML 中放置的位置
- 如何获得精灵的实际位置并将其打印在C 和SFML中
- SFML鼠标位置和碰撞从文件加载
- 访问冲突写入位置0x00000000.C++/SFML游戏
- SFML 碰撞检测,C++ -> 重置玩家位置
- 如何在 SFML 中访问形状位置
- 使用SFML RenderWindow时,内存位置的std::length_error
- 将聚焦的精灵逐渐移动到目标位置(C++和SFML)
- SFML中移动位置的居中视图
- 鼠标位置并单击 sfml 中的检测
- C++ SFML 精灵位置在功能中时不会更新
- C++SFML 1.6精灵位置与鼠标
- 如何将鼠标位置从SFML转换为OpenGL坐标