我正在尝试学习SDL,我有一个应用程序,我想绘制应用程序的绘制周期的纹理,然后我只需要在刷新周期中将纹理复制到屏幕。我的班级有两种方法可以做到这一点:
Tester::CreateTexture(SDL_Renderer* pRenderer, XPoint<int>* pResolution)
{
mResolution.x = pResolution->x;
mResolution.y = pResolution->y;
std::cout << "Renderer: " << pRenderer << std::endl;
SDL_ClearError();
mpTexture = SDL_CreateTexture(pRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, mResolution.x, mResolution.y);
std::cout << "BeamformImage: " << mpTexture << " : " << mResolution.x << ", " << mResolution.y << std::endl;
int foo = SDL_SetRenderTarget(pRenderer, mpTexture);
std::cout << "Set Renderer: Cn: " << foo << " : " << SDL_GetError() << std::endl;
int bar = SDL_SetRenderTarget(pRenderer, NULL);
std::cout << "Set Renderer: Tn: " << bar << " : " << SDL_GetError() << std::endl;
}
此方法创建纹理并将其另存为实例变量。我添加了代码来设置渲染器的目标以查看它是否有效且没有错误,并且两个调用都按预期返回0。
然后我有一个想要写入纹理的方法:
void Tester::WriteTexture(SDL_Renderer* pRenderer)
{
SDL_ClearError();
std::cout << "Renderer: " << pRenderer << " : " << mpTexture << std::endl;
int foo = SDL_SetRenderTarget(pRenderer, mpTexture);
std::cout << "Render Target: " << foo << " : " << SDL_GetError() << std::endl;
// Code to write to the renderer
}
渲染器在两种方法中始终相同,并且纹理相同。
当调用Write Texture时,foo为-1,错误为无效纹理。
有没有办法找出纹理的问题是什么?问题是我在两次调用之间以某种方式破坏了渲染器吗?
在每个方法的底部添加一个,以查看渲染器是否以某种方式更改并且不允许渲染到纹理
SDL_RendererInfo info;
SDL_GetRendererInfo(pRenderer, &info);
if (info.flags && SDL_RENDERER_TARGETTEXTURE > 0)
std::cout << "*Can Target Texture: " << std::endl;
else
std::cout << "*Can't Target Texture: " << std::endl;
在这两个地方似乎都允许渲染纹理。