我试着用SDL_RenderCopy()
来做这件事,但我只得到了一个黑匣子。这是我的密码。
static SDL_Texture* GetAreaTextrue(SDL_Rect rect, SDL_Renderer* renderer, SDL_Texture* source)
{
SDL_Texture* result = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
SDL_SetRenderTarget(renderer, result);
SDL_RenderCopy(renderer, source, &rect, NULL);
SDL_RenderPresent(renderer);
return result;
}
正确的操作是什么?
发布于 2016-08-12 13:39:42
编辑:
您在这里要做的是将纹理的一部分呈现到屏幕上。
有一种方法可以通过使用RenderCopy来实现这一点,但是通过这样做,您只需从纹理中“获取”您想要的部分,并将其“拍打”到屏幕上。
根据我的理解,您想要的是获取纹理的一部分,并将其保存到另一个纹理变量中,然后再将其呈现到屏幕上。
这个问题的第一个解决方案如下:
// load your image in a SDL_Texture variable (myTexture for example)
// if the image is (256,128) and you want to render only the first half
// you need a rectangle of the same size as that part of the image
SDL_Rect imgPartRect;
imgPartRect.x = 0;
imgPartRect.y = 0;
imgPartRect.w = 32;
imgPartRect.h = 32;
// and the program loop should have a draw block looking like this:
SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0x00, 0xFF );
SDL_RenderClear( renderer );
SDL_RenderCopy( renderer, myTexture, &imgPartRect, NULL );
SDL_RenderPresent( renderer );
您尝试使用的方法有一个中间纹理,您可以在上面渲染,然后再将该纹理呈现到屏幕上。这里的问题是,您将渲染器设置为在您刚刚创建的纹理上绘制,但您从未将渲染器重置为使用默认目标(屏幕)。
正如您在SDL文档这里中看到的那样,第二个参数接收希望renerer绘制的纹理,如果要将其重置为默认目标(屏幕),则为NULL。
int SDL_SetRenderTarget(SDL_Renderer*渲染器,SDL_Texture*纹理) 其中:
呈现上下文
必须使用SDL_TEXTUREACCESS_TARGET标志创建的目标纹理,或默认呈现目标的NULL。
让我们使用与前面相同的示例:
// first off let's build the function that you speak of
SDL_Texture* GetAreaTextrue(SDL_Rect rect, SDL_Renderer* renderer, SDL_Texture* source)
{
SDL_Texture* result = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
SDL_SetRenderTarget(renderer, result);
SDL_RenderCopy(renderer, source, &rect, NULL);
// the folowing line should reset the target to default(the screen)
SDL_SetRenderTarget(renderer, NULL);
// I also removed the RenderPresent funcion as it is not needed here
return result;
}
// load your image in a SDL_Texture variable (myTexture for example)
// if the image is (256,128) and you want to render only the first half
// you need a rectangle of the same size as that part of the image
SDL_Rect imgPartRect;
imgPartRect.x = 0;
imgPartRect.y = 0;
imgPartRect.w = 32;
imgPartRect.h = 32;
// now we use the function from above to build another texture
SDL_Texture* myTexturePart = GetAreaTextrue( imgPartRect, renderer, myTexture );
// and the program loop should have a draw block looking like this:
SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0x00, 0xFF );
SDL_RenderClear( renderer );
// here we render the whole newly created texture as it contains only a part of myTexture
SDL_RenderCopy( renderer, myTexturePart, NULL, NULL );
SDL_RenderPresent( renderer );
我不知道你想做什么,但我强烈推荐第一种方法。
https://stackoverflow.com/questions/38686927
复制相似问题