为什么我无法在SDL2中绘制纹理
Why am I unable to draw to a texture in SDL2?
在过去的几个月里,我一直在夜间和周末用SDL做一个项目。我目前正试图得到一个菜单系统的工作。目前,我正在使用SDL_TTF
绘制文本。至于我的问题,当我试图将一些纹理绘制到另一个纹理时,我看到了一些奇怪的行为。
奇怪的是,当我画它时,在用SDL_TEXTUREACCESS_TARGET
创建的目标纹理上(就像它在文档中说的那样)什么也没画,但没有返回错误。但是,如果我使用SDL_TEXTUREACCESS_STATIC
或SDL_TEXTUREACCESS_STREAM
,由于访问属性,当我设置渲染目标时,它会返回一个错误,但绘制得很好。在做了一些调查之后,我听说了一些关于英特尔驱动程序的错误(我使用的是带有英特尔显卡的Macbook),所以我想知道这是不是我搞砸了,以及我该如何修复它。另外,如果这不是我的错,我仍然想知道发生了什么,它在不同的平台上是否会有不同的表现,以及我该如何解决它。
这是我的代码,删除不必要的部分和:
我创建渲染器:
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
之后,当我在画布上渲染时:
TTF_Font *fnt = loadFont(fontName.c_str(), fontSize);
在这里,我解析出一些属性,并使用TTF_SetFontStyle()和文本颜色的clr设置它们。
SDL_Texture *canvas;
canvas = SDL_CreateTexture(rendy, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, contentRect.w, contentRect.h)
SDL_SetRenderTarget(rendy, canvas);
int pos = 0;
for (list<string>::iterator itr = lines.begin(); itr != lines.end(); itr++){
SDL_Surface *temp;
temp = TTF_RenderText_Blended(fnt, src.c_str(), clr);
SDL_Texture *line;
line = SDL_CreateTextureFromSurface(rendy, temp);
int w,h;
SDL_QueryTexture(line, NULL, NULL, &w, &h);
SDL_Rect destR;
//Assume that we're left justified
destR.x = 0;
destR.y = pos;
destR.w = w;
destR.h = h;
SDL_RenderCopy(rendy, line, NULL, &destR);
SDL_DestroyTexture(line);
SDL_FreeSurface(temp);
pos += TTF_FontLineSkip(fnt);
}
//Clean up
SDL_SetRenderTarget(rendy, NULL);
canvas返回给调用函数,这样它就可以被缓存,直到这个文本框被修改。该函数的工作原理是为整个框创建纹理,在其上绘制背景纹理,然后在其上绘制此图像,并保持整体
代码是这样的:
(用来绘制背景的东西,渲染效果很好)
SDL_Texture *sum = SDL_CreateTexture(rendy, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, globalRect.w, globalRect.h);
SDL_SetTextureBlendMode(sum, SDL_BLENDMODE_BLEND);
SDL_SetRenderTarget(rendy, sum);
string lPad = getAttribute(EL_LEFT_PADDING);
string tPad = getAttribute(EL_TOP_PADDING);
int paddingL = strtol(lPad.c_str(), NULL, 10);
int paddingT = strtol(tPad.c_str(), NULL, 10);
SDL_Rect destR;
SDL_Rect srcR;
srcR.x = 0;
srcR.y = 0;
srcR.w = globalRect.w; //globalRect is the size size of the whole button
srcR.h = globalRect.h;
destR.x = 0;
destR.y = 0;
destR.w = globalRect.w;
destR.h = globalRect.h;
SDL_RenderCopy(rendy, bgTexture, NULL, &destR);
int maxX = contentRect.w;
fgTexture = getFGImage(rendy); //The call to the previous part
int w, h;
SDL_QueryTexture(fgTexture, NULL, NULL, &w, &h);
int width, height;
getTextSize(&width, &height, maxX);
srcR.x = 0;
srcR.y = 0;
srcR.w = width;
srcR.h = height;
destR.x = paddingL;
destR.y = paddingT;
destR.w = globalRect.w;
destR.h = globalRect.h;
SDL_RenderCopy(rendy, fgTexture, NULL, &destR);
SDL_DestroyTexture(fgTexture);
SDL_DestroyTexture(bgTexture);
return sum;
Sum返回给另一个绘图的函数。
提前感谢!
更新所以我已经弄清楚它只在我有不正确的访问设置时绘制的原因是,因为函数返回一个错误值,渲染目标从未设置为纹理,所以它只是在屏幕上绘制。我还通过编写AuditTexture函数检查了我所有的纹理,该函数检查纹理格式是否被渲染器支持,打印访问属性的字符串描述,并打印尺寸。我现在知道它们所有的纹理格式都是支持的,这两条线是静态的,canvas和sum是渲染目标,它们都没有维度为零。
事实证明,我已经将渲染目标设置为复合纹理,然后调用我的函数,该函数在绘制文本之前将渲染目标设置为文本纹理。然后当我从函数返回时,渲染目标仍然是文本纹理而不是复合纹理,所以我基本上是在文本本身上绘制而不是在背景上绘制。
给你一句忠告:永远不要以为有什么东西是为你准备好的,尤其是在c或c++中。
- OpenGL ES 2.0将纹理绘制到屏幕上是行不通的
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- 基本 GLSL:两个片段着色器彼此相邻,但第一个(绘制纹理)被覆盖
- 如何绘制到目标纹理大于屏幕分辨率的帧缓冲区
- 在使用PI上的OpenGL ES绘制之前,如何旋转纹理
- 在版本 440 核心中绘制具有纹理的立方体的顶点和片段着色器应该是什么
- 为什么我不能在SDL2中绘制纹理
- OpenGL 纹理无法绘制
- 在OpenGL中绘制多个2D纹理
- 使用纹理绘制多个对象不起作用
- 如何使用SKIA SKBITMAP在EGL纹理上绘制文本
- 如果我使用碎片着色器绘制纹理,如何绘制红线
- 在Cocos2dx中绘制手指运动的纹理
- OpenCL-绘制到OpenGL纹理崩溃
- 使用自定义着色器在全屏四边形上从 QGLFramebufferObject 绘制纹理
- 如何在C++中绘制纹理/位图的区域
- 为什么我无法在SDL2中绘制纹理
- 在屏幕上绘制纹理
- 绘制纹理四边形看起来是扭曲的
- OpenGL绘制纹理