Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

sdl

surface

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);
like image 226
Ólafur Waage Avatar asked Dec 06 '22 04:12

Ólafur Waage


2 Answers

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

like image 69
oold Avatar answered Dec 07 '22 16:12

oold


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.

like image 31
Tamas Czinege Avatar answered Dec 07 '22 17:12

Tamas Czinege