How can I remove SDL_Surface to replace with another?

Now trying to find this on the Internet for a while.

I have SDL_Surface with some content (in one of its text, in another - part of the sprite). Inside the game loop, I get the data on the screen in order. But then it loops again and does not replace the old data, but simply writes over it. So in the case of text, it becomes a mess.

I tried SDL_FreeSurface and it did not work, does 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);
+4
source share
4 answers

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

+10
source

- (, SDL_ttf), , , . , , . "" , .

+3

( , ), SDL_BlitSurface, . ( SDL_FillRect) , .

This way you also get double buffering and avoid flickering. Also don't forget SDL_UpdateRectsafter blitting.

+2
source
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 );
            }
0
source

Source: https://habr.com/ru/post/1720741/


All Articles