gpt4 book ai didi

c++ - 从源 SDL_Texture 中提取 SDL_Texture

转载 作者:行者123 更新时间:2023-11-28 06:07:10 27 4
gpt4 key购买 nike

我有以下函数可以从一个更大的纹理中提取一个 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 之前添加:

SDL_SetRenderTarget(gRenderer, NULL);

关于c++ - 从源 SDL_Texture 中提取 SDL_Texture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com