为什么像素不改变颜色

Why pixel is not changing color

本文关键字:变颜色 改变 像素 为什么      更新时间:2023-10-16

我使用SFML库在C 上(C 的零经验(进行伪涂料。我做了一个单独的功能来编辑图像,"工具0"基本上是铅笔。当我尝试将其传递给我的函数时,它行不通,但是当我这样做而不将其传递给绘制图像功能时,它正常起作用。我在提出的代码上删除了额外的零件

我试图将鼠标位置的x和y作为单独的args传递(不使用向量(,但这无济于事

void drawOnImage(sf::Color color, int tool, sf::Texture canvas, sf::Image image, sf::Vector2i vector);
int main()
{
    sf::Image image;
    image.create(windowWidth, windowHeight, sf::Color::White);
    sf::Texture canvas;
    sf::Color currentColor = sf::Color::Black;  // Color
    int currentTool = 0; // instrument that is currently chosen
    sf::Sprite sprite;
    int mousedown = 0;
    mainWindow.setFramerateLimit(60);
    canvas.loadFromImage(image);
    sprite.setTexture(canvas);
while (mainWindow.isOpen())
    {
        canvas.loadFromImage(image);
        sf::Event event;
        while (mainWindow.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                mainWindow.close();
            else if ((event.type == sf::Event::MouseMoved) && (mousedown == 1)) 
            {
                canvas.update(mainWindow);
                drawOnImage(currentColor, currentTool, canvas, image, sf::Mouse::getPosition(mainWindow));
            }
            else if (event.type == sf::Event::MouseButtonPressed)
            {
                mousedown = 1;
                drawOnImage(currentColor, currentTool, canvas, image, sf::Mouse::getPosition(mainWindow));
                //image.setPixel(sf::Mouse::getPosition(mainWindow).x, //IT WORKS but drawOnImage does not 
 sf::Mouse::getPosition(mainWindow).y, sf::Color::Black);
}
void drawOnImage(sf::Color color, int tool, sf::Texture canvas, sf::Image image,sf::Vector2i vector) {
    if (tool == 0) {
        image.setPixel(vector.x,vector.y, sf::Color::Black);
    }
}

我敢肯定,我根本不知道一些C 功能,例如传递对象的地址而不是名称,但是我自己无法弄清楚

修改图像后,您需要再次将其加载到纹理上,因此您需要再次调用此行:

canvas.loadFromImage(image);