SDL - invalid texture error on SDL_DestroyTexture(

2019-09-06 04:18发布

I'm making a small "retro-style" 2D platformer game with SDL in C++. I figured that the best way to keep the game at a low resolution, while allowing people with different size monitors to stretch the game window to fit their setup, would be to render everything to a low-res texture and then render that texture to the whole window (with the window size/resolution set by the user).

When I run this setup, the game works exactly as it should and renders fine (in both fullscreen and windowed modes). However, when I use SDL_DestroyTexture() to free my low-res render target texture, the console spits out "ERROR: Invalid texture". I have confirmed that this is where the error occurs using a debugger. Below is the relevant code which creates, uses, and destroys the texture. Why is the texture suddenly invalid when I can use it normally otherwise?

// SDL is initialized

// "proxy" is the texture used for render-to-texture
// it is set to the "logical" low resolution (lxres, lyres) (usually 320x240)
// renderer is an SDL_Renderer* that initializes with no problems
SDL_Texture* proxy = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
                         SDL_TEXTUREACCESS_TARGET, lxres, lyres);

SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);

// game runs fine
while (!quit) {
    SDL_SetRenderTarget(renderer, proxy);
    render();
    SDL_SetRenderTarget(renderer, nullptr);
    // stretch the low resolution texture onto the at-least-as-high resolution
    // renderer (usually 640x480)
    SDL_RenderCopy(renderer, proxy, nullptr, nullptr);
    SDL_RenderPresent(renderer);
    SDL_RenderClear(renderer);
    updateLogic();
}

// Time to quit
SDL_SetRenderTarget(renderer, nullptr);
if (proxy != nullptr)
    SDL_DestroyTexture(proxy);    // "ERROR: Invalid texture"

// Clean up other resources

// close SDL

标签: c++ sdl-2
1条回答
放我归山
2楼-- · 2019-09-06 04:43

This type of error has hapened to me when i destroyed the renderer before destroying the texture it is atached to.

查看更多
登录 后发表回答