如何清除 SDL_Surface 并将其替换为另一个 SDL_Surface?

发布于 2024-08-08 10:58:50 字数 441 浏览 7 评论 0原文

一段时间以来一直试图在网上找到这个。

我有一个包含一些内容的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦屿孤独相伴 2024-08-15 10:58:50

尝试类似的方法:
SDL_FillRect(屏幕, NULL, 0x000000);
在循环的开始处。

Try something like:
SDL_FillRect(screen, NULL, 0x000000);
at the beginning of your loop.

星星的軌跡 2024-08-15 10:58:50

我通常做的是绘制到辅助表面(即,不是屏幕的内存表面),然后在准备好复制到屏幕时绘制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 (with SDL_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.

£烟消云散 2024-08-15 10:58:50

如果您正在绘制具有透明度的内容(例如来自 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.

我要还你自由 2024-08-15 10:58:50
while( !quit )
            {
                while( SDL_PollEvent( &e ) != 0)
                {
                    if( e.type == SDL_QUIT )
                    {
                        quit = true;
                    }
                    else if( e.type == SDL_KEYDOWN)
                    {
                        **SDL_FillRect(screenSurface, NULL, 0x000000);**
                        switch(e.key.keysym.sym)
                        {
                            case SDLK_w:
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_UP ];
                                break;

                            case SDLK_d: 
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_RIGHT ];
                                break;

                            case SDLK_s: 
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_DOWN ];
                                break;

                            case SDLK_a: 
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_LEFT ];
                                break;

                            default: 
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
                                break;
                        }
                    }
                }

                SDL_BlitSurface(CurrentSurface, NULL, screenSurface, NULL);

                SDL_UpdateWindowSurface( window );
            }
while( !quit )
            {
                while( SDL_PollEvent( &e ) != 0)
                {
                    if( e.type == SDL_QUIT )
                    {
                        quit = true;
                    }
                    else if( e.type == SDL_KEYDOWN)
                    {
                        **SDL_FillRect(screenSurface, NULL, 0x000000);**
                        switch(e.key.keysym.sym)
                        {
                            case SDLK_w:
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_UP ];
                                break;

                            case SDLK_d: 
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_RIGHT ];
                                break;

                            case SDLK_s: 
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_DOWN ];
                                break;

                            case SDLK_a: 
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_LEFT ];
                                break;

                            default: 
                                CurrentSurface = ImageSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
                                break;
                        }
                    }
                }

                SDL_BlitSurface(CurrentSurface, NULL, screenSurface, NULL);

                SDL_UpdateWindowSurface( window );
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文