c++ - SDL 从纹理中读取像素

标签 c++ opengl sdl pixel sdl-2

我想使用 SDL2 库从纹理中读取像素。 我知道无法直接从纹理中读取像素,但您可以使用以下函数:

int SDL_RenderReadPixels(SDL_Renderer*   renderer,
                     const SDL_Rect* rect,
                     Uint32          format,
                     void*           pixels,
                     int             pitch)

基本上,我想在我的应用程序中做的是从窗口屏幕读取单个像素并检测它是否是透明像素。 我该如何使用该功能? 什么是 pitch 属性,如何定义 SDL_Rect 以及如何获得具有正确格式的像素? 我正在检测鼠标左键单击事件并将 x 和 y 坐标传递给一个方法,如您所见:

void detectElementOnXandY(SDL_Renderer* renderer, int x, int y){

 // x represents the coordinate x.
 // y represents the coordinate y.

 // Here i have to call the function
 int result = SDL_RenderReadPixels(renderer, rect, format, pixels, pitch);
}

最佳答案

间距似乎是扫描线的字节长度。所以取决于你的纹理格式:1 * bytesPerPixel。 (大概每个像素 4 个字节) 矩形应该类似于 (x,y,x+1,y+1) 并且格式应该类似于 SDL_PIXELFORMAT_RGBA8888 或您的特定纹理的等效格式。 您可能需要反转 y 坐标,具体取决于 SDL 使用的是 OpenGL(从下到上)约定还是 Windows(从上到下)约定。

关于c++ - SDL 从纹理中读取像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19116766/

相关文章:

c++ - C++中的正则表达式问题

c++ - 为什么这个简单的 C++ 类层次结构无法表现出多态行为?

c - 如何在 openGL 中加载 png 图像并使用鼠标移动它?

c++ - CLIon - 使用 SDL_TTF 的 undefined reference

c++ - spirit boost : how to collect "anything from line till particular word" into one variable?

C++ QT 桌面工具栏

c++ - 自定义 opengl 压缩纹理

c++ - 在基于着色器的 opengl 中绘制圆柱体

c++ - SDL 纹理数组?

c++ - 在 Visual Studio 2010 中使用 SDL_image,出现错误 "application was unable to start correctly 0xc000007b"