我有以下功能,从更大的纹理贴图中提取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_walls2是正确的:这个数组位置的纹理在我的程序中呈现得很好。
cropTexture有什么问题?我在C++和SDL2都是新手。
发布于 2015-08-23 17:25:04
您需要在裁剪文本之后将呈现目标设置为默认,否则它将继续绘制纹理。在return in cropTexture之前添加以下内容
SDL_SetRenderTarget(gRenderer, NULL);https://stackoverflow.com/questions/32168684
复制相似问题