1

在过去的几个月里,我一直在晚上和周末在 SDL 从事一个项目。我目前正在尝试使菜单系统正常工作。目前,我正在使用SDL_TTF. 至于我的问题,当我尝试将一些纹理绘制到另一个纹理时,我看到了一些奇怪的行为。

奇怪的是,当我绘制它时,在使用创建的目标纹理上SDL_TEXTUREACCESS_TARGET(就像它在文档中所说的那样)什么都不绘制,但不返回任何错误。但是,如果我使用SDL_TEXTUREACCESS_STATICor SDL_TEXTUREACCESS_STREAM,由于 access 属性,它会在我设置渲染目标时返回错误,但绘制得很好。在进行了一些挖掘之后,我听说了一些关于英特尔驱动程序中的错误的消息(我使用的是带有英特尔显卡的 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;

总和返回到另一个进行绘图的函数。

提前致谢!

更新 所以我发现它只在我的访问设置不正确时才绘制的原因是,由于该函数返回一个错误值,渲染目标从未设置为纹理,所以它只是在屏幕上绘制. 我还通过编写函数 AuditTexture 检查了所有纹理,该函数检查纹理的纹理格式是否受渲染器支持,打印访问属性的字符串描述,并打印尺寸。我现在知道它们所有的纹理格式都受支持,两条线是静态的,canvas 和 sum 是渲染目标,它们的尺寸都不为零。

4

1 回答 1

1

事实证明,我已经将渲染目标设置为复合纹理,然后调用我的函数,在绘制文本之前将渲染目标设置为文本纹理。然后当我从函数返回时,渲染目标仍然是文本纹理而不是复合纹理,所以我基本上将文本绘制在自身上而不是在背景上绘制。

对智者的一句话:永远不要假设某些事情会为您处理好,尤其是在 c 或 c++ 中。

于 2015-09-29T03:05:38.283 回答