【发布时间】:2019-12-24 03:21:13
【问题描述】:
我打开了一个窗口,它在屏幕上显示两个矩形,然后使用 SDL_TTF 在屏幕上显示鼠标位置。
我很难理解的一点是,为什么在渲染文本之后,它之前的两个矩形没有显示出来。
我正在使用 SDL_RenderFillRect 在屏幕上绘制两个矩形
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, rect1);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, rect2);
渲染文本的代码是
// define string with mouse x, y coords
sprintf(someString, "x: %d, y: %d", mouse.x, mouse.y);
SDL_Point textPos = {10, 10};
WriteText(renderer, font, someString, textPos, (SDL_Color){255, 255, 255, 255});
SDL_Surface *fontSurface = TTF_RenderText_Blended(font, someString, COLOR_BLACK); // create font surface
SDL_Texture *fontTexture = SDL_CreateTextureFromSurface(renderer, fontSurface); // create the texture
// get clip width and height from fontsurface clip rect
SDL_Rect *fontRect = &fontSurface->clip_rect;
fontRect->x = pos.x;
fontRect->y = pos.y;
SDL_RenderCopy(renderer, fontTexture, NULL, fontRect); // copy text to the renderer
// delete surface and texture
SDL_FreeSurface(fontSurface);
SDL_DestroyTexture(fontTexture);
它显示窗口左上角的鼠标位置。但是,这会使窗口的其余部分变为空白。
为了防止这种情况发生,我必须在调用 SDL_RendererCopy 之后在屏幕上绘制一些东西(奇怪的是在调用 SDL_DestroyTexture 之前)例如绘制单点
...
SDL_RenderCopy(renderer, fontTexture, NULL, fontRect); // copy text to the renderer
// why is this needed??
SDL_RenderDrawPoint(renderer, 0, 0);
// delete surface and texture
SDL_FreeSurface(fontSurface);
SDL_DestroyTexture(fontTexture); // have to draw a point before this
...
然后显示在文本之前呈现的两个矩形
如果我在调用 SDL_RenderCopy 时将 dstRect 设置为 NULL,那么文本会跨越整个窗口,但我可以在文本下方看到之前呈现的内容。
为什么我必须在调用 SDL_RenderCopy 以阻止之前渲染的内容不显示后绘制一个点?
注意:链接到完整的源代码https://pastebin.com/tRSFT0PV
【问题讨论】:
-
我在 linux 中编译了源代码,我更改了字体(我没有 Arial),我看到评论
SDL_RenderDrawPoint没有区别,它总是按预期工作,文本坐标鼠标 + 2 个矩形( SDL 版本 2.0.9) -
看起来像hg.libsdl.org/SDL/rev/5b0c4bfbd083 中引入的错误(这是对另一个问题的修复)。我想应该向 SDL bugzilla 报告一个错误(如果您不能或不想报告,请在评论中告诉我)。作为一种解决方法,您可以使用另一个 SDL 渲染器实现(例如 opengl)。
-
有趣的是,我在 Manjaro Linux VM 上使用 gcc 编译了这段代码,它运行得很好,不需要
SDL_RenderDrawPoint。当使用配置为使用 mingw32-gcc.exe 编译的 Visual Studio Code 在 Win10 上编译时,我遇到了这个错误。两者都使用 SDL 版本 2.0.10,应该修复链接到的错误 keltar? -
@DanM 不,它在 2.0.10 中(并且仅使用 direct3d 渲染器),但不在例如2.0.9。 bugzilla.libsdl.org/show_bug.cgi?id=4768