SDL - SDL_DestroyTexture()上无效的纹理错误 [英] SDL - invalid texture error on SDL_DestroyTexture()
问题描述
我在C ++中使用SDL制作了一个小型的复古风格2D平台游戏。我认为,保持游戏在低分辨率,同时允许人们使用不同大小的显示器来拉伸游戏窗口以适应自己的设置,最好的方法是将所有的东西渲染为低分辨率纹理,然后渲染纹理到整个窗口(用户设置的窗口大小/分辨率)。
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).
当我运行这个设置,游戏的工作原理和渲染精细和窗口模式)。然而,当我使用SDL_DestroyTexture()释放我的低分辨率渲染目标纹理,控制台吐出错误:无效的纹理。我已经确认这是使用调试器发生错误的地方。下面是创建,使用和破坏纹理的相关代码。为什么纹理突然失效,否则我可以正常使用它?
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
推荐答案
这种类型的错误在我破坏渲染器之前就已经毁了它的纹理。
This type of error has hapened to me when i destroyed the renderer before destroying the texture it is atached to.
这篇关于SDL - SDL_DestroyTexture()上无效的纹理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!