如何清除 SDL_Surface 并将其替换为另一个 SDL_Surface?
一段时间以来一直试图在网上找到这个。
我有一个包含一些内容的 SDL_Surface(其中一个是文本,另一个是精灵的一部分)。在游戏循环中,我将数据很好地显示在屏幕上。但随后它再次循环,并且不会替换旧数据,而只是覆盖它。所以在文本的情况下,它变得一团糟。
我已经尝试过 SDL_FreeSurface 但它不起作用,有人知道另一种方法吗?
fpsStream.str("");
fpsStream << fps.get_ticks();
fpsString = fpsStream.str();
game.fpsSurface = TTF_RenderText_Solid(game.fpsFont, fpsString.c_str(), textColor);
game.BlitSurface(0, 0, game.fpsSurface, game.screen);
Been trying to find this online for a while now.
I have a SDL_Surface with some content (in one it's text, in another is a part of a sprite). Inside the game loop I get the data onto the screen fine. But then it loops again and it doesn't replace the old data but just writes over it. So in the case of the text, it becomes a mess.
I've tried SDL_FreeSurface and it didn't work, anyone know another way?
fpsStream.str("");
fpsStream << fps.get_ticks();
fpsString = fpsStream.str();
game.fpsSurface = TTF_RenderText_Solid(game.fpsFont, fpsString.c_str(), textColor);
game.BlitSurface(0, 0, game.fpsSurface, game.screen);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试类似的方法:
SDL_FillRect(屏幕, NULL, 0x000000);
在循环的开始处。
Try something like:
SDL_FillRect(screen, NULL, 0x000000);
at the beginning of your loop.
我通常做的是绘制到辅助表面(即,不是屏幕的内存表面),然后在准备好复制到屏幕时绘制
SDL_BlitSurface
。然后,您可以在下一次迭代中清除整个辅助缓冲区(使用SDL_FillRect
),并重新绘制所有内容或仅绘制一部分(如果您不想丢失整个表面而只更改了一个矩形)。这样,您还可以获得双缓冲并避免闪烁。另外,不要忘记 blitting 后的
SDL_UpdateRects
。What I do usually is drawing to a secondary surface (that is, an in-memory surface that's not the screen) and then
SDL_BlitSurface
when it's ready to be copied to the screen. You can then clear the whole secondary buffer (withSDL_FillRect
) in the next iteration and redraw everything or just a part of if you don't want to lose the whole surface and only changed a rectangle.This way, you also get doublebuffering and avoid flickering. Also don't forget
SDL_UpdateRects
after blitting.如果您正在绘制具有透明度的内容(例如来自 SDL_ttf 的内容),则文本之间的透明区域不会改变,这意味着之前的写入将保留。这通常不是问题,因为程序的通常行为是清除帧缓冲区并每帧重绘整个场景一次。在过去,只重画屏幕的“脏”部分是很常见的,但现在已经不那么常见了。
If you are drawing something with transparency (eg. stuff from SDL_ttf) then the transparent areas between the text won't be altered, meaning previous writes will remain. This isn't usually a problem because the usual behaviour is for the program to clear the frame buffer and redraw the entire scene once per frame. In the old days it was common to only redraw the 'dirty' parts of the screen but that is not so common now.