如何在 C++ 中进行像素操作? - Linux

How to do pixel manipulations in C++? - Linux

我的目标:

我想像位图图片一样独立操作创建window的像素颜色。 我更喜欢 以 Ubuntu -> Linux 为目标的本地库。它可能正在使用如下函数:

SetPixel(int x,int y,int Color) {

  //Some code that set the color of one pixel by his coordinates x and y

  return 0;//May return an error
}

它会根据他的位置window设置一个像素的颜色

问题:

我已经尝试了很多教程 (使用 Graphics.h 或 Windows.h) ,但是 none在我的电脑上工作我可能做错了什么但是我发现的并不明确。无论如何,如果没有说明,我不知道如何下载库。我认为它不适用于 Linux.

SDL2 不是 Linux 的原生版本。顺便说一句

如果需要任何说明,请添加评论或建议修改

你应该学习SDL2。它非常容易学习,我建议您阅读这些文章,它们是很棒且完整的教程。

http://lazyfoo.net/tutorials/SDL/index.php

你特别需要的在这一个里有: http://lazyfoo.net/tutorials/SDL/08_geometry_rendering/index.php

只为大家展示功能:

SDL_RenderSetDrawColor( myRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawPoint( myRenderer, cordX, cordY);

因此您可以独立选择颜色和更改像素

PS:在ubuntu上有效,文章会教你如何配置你需要的一切。 PS2:需要帮助联系我,我的OS也是ubuntu。

对我来说,简单的解决方案类似于下面的代码

#include <X11/Xlib>
bool SetPixel(uint16_t X,uint16_t Y,uint32_t Color){
    XSetForeground(_Display,_GraphicCTX,Color);
    XDrawPoint(_Display,_Window,_GraphicCTX,X,Y);
    return true;
}

bool Setup(){ //Use it first to make every graphical manipulations working :)
    Display=XOpenDisplay(0);//Create a display
    Window=XCreateSimpleWindow(Display,DefaultRootWindow(Display),0,0,480,360,0,0,0);   //Create a Window
    XMapWindow(Display,Window);//Make the Window visible
    GraphicCTX=XCreateGC(Display,Window,0,NIL);//Create a Graphics Context
    //v Wait for a MapNotify XEvent for next commands
    XSelectInput(Display,Window,StructureNotifyMask);
    while(1){
        XEvent E;
        XNextEvent(Display,&E);
        if(E.type==MapNotify)break;
    }
    return true;
}

我使用 X11,因为它是 Linux 的原生图形协议。

事实上,我制作了一个库,用 class 使一切变得更简单。

设置函数必须运行在任何图形操作之前,否则它不会做任何事情!

这可行,但对于高级开发,您需要使用图像等等!有一个很厉害manual for Xlib