[英]How to use SDL functions like SDL_DestroyTexture() properly?
[英]SDL - invalid texture error on SDL_DestroyTexture()
我正在用C ++中的SDL制作一個小的“復古風格” 2D平台游戲。 我認為,將游戲保持在低分辨率的最佳方法是,允許具有不同尺寸的顯示器的人拉伸游戲窗口以適合其設置,同時將所有內容渲染為低分辨率紋理,然后將該紋理渲染為整個窗口(由用戶設置窗口大小/分辨率)。
當我運行此設置時,游戲將完全正常運行並呈現良好的效果(在全屏和窗口模式下)。 但是,當我使用SDL_DestroyTexture()釋放低分辨率渲染目標紋理時,控制台會吐出“錯誤:無效的紋理”。 我已經確認這是使用調試器發生錯誤的地方。 以下是創建,使用和破壞紋理的相關代碼。 為什么我可以正常使用該紋理時突然變得無效?
// 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
當我在破壞渲染器的紋理之前破壞了渲染器時,這種錯誤就發生了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.