Ólafur Waage
Ólafur Waage

Reputation: 70001

How can I clear a SDL_Surface to be replaced with another one?

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);

Upvotes: 4

Views: 17585

Answers (4)

kamalesh biswas
kamalesh biswas

Reputation: 1013

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 );
            }

Upvotes: 0

Kylotan
Kylotan

Reputation: 18449

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.

Upvotes: 3

oold
oold

Reputation: 326

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

Upvotes: 16

Tamas Czinege
Tamas Czinege

Reputation: 121314

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.

Upvotes: 3

Related Questions