【问题标题】:Extract an SDL_Texture from a source SDL_Texture从源 SDL_Texture 中提取 SDL_Texture
【发布时间】:2015-08-23 16:05:54
【问题描述】:

我有以下函数,可以从与其平铺的更大纹理中提取 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 的新手。

【问题讨论】:

    标签: c++ sdl-2


    【解决方案1】:

    裁剪纹理后,您需要将渲染目标设置回默认值,否则它将继续在纹理上绘制。在cropTexture 中的return 之前添加这个:

    SDL_SetRenderTarget(gRenderer, NULL);
    

    【讨论】:

    • 愚蠢的是我没有想到这一点。这是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多