我有以下功能,从用它们平铺的更大纹理中提取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的新手 .
1 回答
在裁剪纹理后,需要将渲染目标设置回默认值,否则它将继续在纹理上绘制 . 在
cropTexture
中的return
之前添加: