简体   繁体   中英

Why cant I draw to a texture in SDL2?

So my main goal is to read the pixel data of a given texture, so to test this, i've created a new texture with target access then drawn it red.

The problem is that when I access the pixel data, SDL_RenderReadPixels() says the pitch is 0, and all the pixel data is 0. Also when I try to draw the new texture, it comes out black.

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;
}

For some reason you assume that SDL_RenderReadPixels allocates pixels array and returns resulting pitch; it does neither. Calling side passes pixels array big enough to hold data, and its correct pitch, eg:

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

In C/C++, functions does not modify input parameters directly, unless parameter is a reference type (in C++), or value is modified indirectly through pointer, so if function takes int - it is clearly input parameter, not output. Eg take a look at SDL_CreateWindowAndRenderer , which outputs window and renderer pointers and hence its parameter types are pointer-to-pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM