0

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
  • I cant remember what was my error, but this type of error has hapened to me when i destroyed the renderer before destroying the texture it is atached to. Doesnt seem to be the case here, but i dont see anything wrong just looking at your code. Have you tryed having it as a SDL_TEXTUREACCESS_STATIC and comenting render() and the SDL_SetRenderTarget's just to see if that is the problem. – jofra Sep 01 '14 at 10:22
  • OK I think checking when the renderer is destroyed solved it. updateLogic() destroyed the proxy renderer when it set quit = true (which was a mistake on my part) so the texture was being destroyed after the renderer. The only reason I didn't get a segfault from deleting the renderer again aftr the texture is because I put null checks everywhere when stuff is deleted. I never knew that you had to destroy the renderer after destroying textures created with it. But I guess I know now. Thanks for the help! – NuclearInternets Sep 01 '14 at 13:56
  • Glad i helped. Should i put is as an answer since it solved your problem? (So other people could see it as a question answered) – jofra Sep 01 '14 at 18:13
  • Yeah, just put it as an answer since it pretty much resolved the question. – NuclearInternets Sep 02 '14 at 22:55
  • Ok, you just need to accept it now. ;) – jofra Sep 03 '14 at 11:25

1 Answers1

2

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

jofra
  • 617
  • 6
  • 13