gpt4 book ai didi

c++ - 为什么我不能绘制到 SDL2 中的纹理?

转载 作者:行者123 更新时间:2023-11-28 01:40:48 25 4
gpt4 key购买 nike

所以我的主要目标是读取给定纹理的像素数据,因此为了测试这一点,我创建了一个具有目标访问权限的新纹理,然后将其绘制为红色。

问题是当我访问像素数据时,SDL_RenderReadPixels() 表示间距为 0,所有像素数据均为 0。此外,当我尝试绘制新纹理时,它显示为黑色。

SDL_Texture* tempChar = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 3, 5);

if (SDL_SetRenderTarget(renderer, tempChar) != 0) {
cout << "SDL_SetRenderTarget() Error! " << SDL_GetError() << endl;
} else {
cout << "SDL_SetRenderTarget() success" << endl;
}

if (SDL_RenderClear(renderer) != 0) {
cout << "SDL_RenderClear() Error! " << SDL_GetError() << endl;
} else {
cout << "SDL_RenderClear() success" << endl;
}

if (SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255) != 0) {
cout << "SDL_SetRenderDrawColor() Error! " << SDL_GetError() << endl;
} else {
cout << "SDL_SetRenderDrawColor() success" << endl;
}

if (SDL_RenderFillRect(renderer, NULL) != 0) {
cout << "SDL_RenderClear() Error! " << SDL_GetError() << endl;
} else {
cout << "SDL_RenderClear() success" << endl;
}

int w;
int h;
Uint32 format;
int access;

if (SDL_QueryTexture(tempChar, &format, &access, &w, &h) != 0) {
cout << "SDL_QueryTexture() ERROR : " << SDL_GetError() << endl;
} else {
cout << "SDL_QueryTexture() no error" << endl;
}

cout << "width is : " << w << " height is : " << h << endl;

if (access == SDL_TEXTUREACCESS_TARGET) {
cout << "TARGET" << endl;
}

if (format == SDL_PIXELFORMAT_RGBA8888) {
cout << "format is : SDL_PIXELFORMAT_RGBA8888" << endl;
}

void* readPixels = NULL;
int pitch;

if (SDL_RenderReadPixels(renderer, NULL, 0, readPixels, pitch) != 0) {//12 pitch, 3x4bytes
//an error occurred
cout << "SDL_RenderReadPixels() Error, text probably wont work... :-/\n" << SDL_GetError() << endl;
} else {
cout << "SDL_RenderReadPixels() success" << endl;
}

cout << "pitch returned : " << pitch << endl;

menuMouse = tempChar;

for (int i = 0; i < 5; i++) {
cout << "i is : " << i << endl;
char* rowStart = ((char*) readPixels) + i * pitch;
cout << "pix data is : " << int(rowStart) << endl;
}

最佳答案

出于某种原因,您假设 SDL_RenderReadPixels 分配像素数组并返回结果间距;它都没有。调用方传递足够大的像素数组来保存数据及其正确的间距,例如:

Uint32 pixels[3*5];
int pitch = sizeof(Uint32)*3;

在 C/C++ 中,函数不会直接修改输入参数,除非参数是引用类型(在 C++ 中),或者值是通过指针间接修改的,所以如果函数采用 int - 它是明明是输入参数,不是输出。例如。看看 SDL_CreateWindowAndRenderer,它输出窗口和渲染器指针,因此它的参数类型是指针到指针。

关于c++ - 为什么我不能绘制到 SDL2 中的纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47250980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com