[英]Extract an SDL_Texture from a source SDL_Texture
我具有以下函數,該函數從與它們平鋪的較大紋理中提取64x64像素紋理:
SDL_Texture* cropTexture (SDL_Texture* src, int x, int y)
{
SDL_Texture* dst = SDL_CreateTexture(gRenderer, gSurface->format->format, SDL_TEXTUREACCESS_TARGET, 64, 64);
SDL_Rect rect = {64 * x, 64 * y, 64, 64};
SDL_SetRenderTarget(gRenderer, dst);
SDL_RenderCopy(gRenderer, src, &rect, NULL);
return dst;
}
現在,當我這樣調用函數時:
SDL_Texture* txt_walls [3];
txt_walls[0] = cropTexture (allWalls, 0, 0);
txt_walls[1] = cropTexture (allWalls, 0, 1);
txt_walls[2] = cropTexture (allWalls, 4, 3);
然后txt_walls[2]
會產生黑色紋理(或至少未初始化)。
當我向其中添加無意義的代碼行時:
SDL_Texture* txt_walls [3];
txt_walls[0] = cropTexture (allWalls, 0, 0);
txt_walls[1] = cropTexture (allWalls, 0, 1);
txt_walls[2] = cropTexture (allWalls, 4, 3);
cropTexture (allWalls, 1, 1); // whatever, ignore the returned object
那么txt_walls [2]是正確的:此數組位置處的紋理在我的程序中渲染得很好。
cropTexture
什么問題? 我是C ++和SDL2的新手。
裁剪紋理后,需要將渲染目標設置回默認值,否則它將繼續在紋理上繪制。 在cropTexture
return
之前添加以下cropTexture
:
SDL_SetRenderTarget(gRenderer, NULL);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.