简体   繁体   English

从源SDL_Texture提取SDL_Texture

[英]Extract an SDL_Texture from a source SDL_Texture

I have the following function that extracts a 64x64 pixel texture from a bigger texture tiled with them: 我具有以下函数,该函数从与它们平铺的较大纹理中提取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;
}

Now, when I call the function like this: 现在,当我这样调用函数时:

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);

then txt_walls[2] results in a black texture (or at least, uninitialized). 然后txt_walls[2]会产生黑色纹理(或至少未初始化)。

When I add one meaningless line of code to that: 当我向其中添加无意义的代码行时:

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

then txt_walls[2] is correct: the texture at this array position renders just fine in my program. 那么txt_walls [2]是正确的:此数组位置处的纹理在我的程序中渲染得很好。

What is wrong with cropTexture ? cropTexture什么问题? I'm new in both C++ and SDL2. 我是C ++和SDL2的新手。

You need to set the render target back to default after you have cropped the texure, otherwise it will continue to draw on the texture. 裁剪纹理后,需要将渲染目标设置回默认值,否则它将继续在纹理上绘制。 Add this before the return in cropTexture : cropTexture return之前添加以下cropTexture

SDL_SetRenderTarget(gRenderer, NULL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM