Advertisement

SDL --- how to clear the current image ?

Started by March 22, 2011 05:08 AM
4 comments, last by jbadams 13 years, 8 months ago
hi, everybody, i meet a problem during my development using SDL and I am also a newbie. the problem is :

firstly, I draw a chess game board on screen using SDL_LoadBMP
then, I draw a chess piece on this board also using SDL_LoadBMP,
after that, I wanna make the current chess piece disappear but keep the board, then I can draw a piece on the other place on the board.

but I found that I can't delete the chess at the same time keep the board on screen, if I use SDL_FreeSurface, both the piece and board disappear.

So, would you help me out? thanks a lot.
To make the piece disappear, you draw the board on top of it. You can draw the whole board or just the part of the board underneath the piece, it's up to you. SDL_FreeSurface doesn't affect what already on the screen. It removes the surface from memory.

EDIT: And you don't want to load the surfaces with SDL_LoadBMP each time you draw them because that will be terrible slow. Just load them once before you need them and use SDL_FreeSurface when you no longer need the surface.
Advertisement
thank you so much!

actually i only draw the chess board one time at the beginning of the game, but you know, i have to save the board and display it again after even just move one piece.

so, there is no function in SDL to delete one image already on screen and don't influence others, right?

thanks again!


To make the piece disappear, you draw the board on top of it. You can draw the whole board or just the part of the board underneath the piece, it's up to you. SDL_FreeSurface doesn't affect what already on the screen. It removes the surface from memory.

EDIT: And you don't want to load the surfaces with SDL_LoadBMP each time you draw them because that will be terrible slow. Just load them once before you need them and use SDL_FreeSurface when you no longer need the surface.
Just draw the entire board on top of what's already there and redraw all the pieces; it'll happen so fast you won't even notice. (In fact, it'll happen so fast you could do it dozens if not hundreds of times a frame on any modern PC.)
I recommend you to download SDL_image library, so you could load multiple image formats, like PNG for example, it really easy to learn it ;)

Deltron Zero and Automator.


I recommend you to download SDL_image library, so you could load multiple image formats, like PNG for example, it really easy to learn it ;)

While that isn't a bad suggestion per se, it doesn't really relate to the question at hand.


As already mentioned, the solution is to simply redraw the board and any pieces currently in play.

Also already mentioned, if you wanted to save on redrawing the whole thing you could only redraw the portions that have changed. On any reasonably modern PC this should be unnecessary however, and would only really serve to make your drawing calls more complex.

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement