SFML community forums

Help => General => Topic started by: Bocaj on July 19, 2012, 06:22:36 am

Title: Undrawing Sprites and Shapes
Post by: Bocaj on July 19, 2012, 06:22:36 am
I know this sounds like a silly question, but how do you remove Sprites, shapes, and other drawable objects from the RenderWindow?
Title: Re: Undrawing Sprites and Shapes
Post by: unranked86 on July 19, 2012, 07:39:52 am
Don't draw them :)
Have a bool or something, and when you don't want your sprites, set it false (or true..)

Example:
// your main loop
// stuff here...handling input etc
if (drawSprites == true) // if this is false, then obviously your program will 'skip' this part
{
   // draw your stuff here
}

Or something like this.
Title: Re: Undrawing Sprites and Shapes
Post by: eXpl0it3r on July 19, 2012, 09:35:25 am
Additionally you have to make sure to clear the window otherwise you'll always see the old drawings.
window.clear();
Title: Re: Undrawing Sprites and Shapes
Post by: Bocaj on July 19, 2012, 04:00:08 pm
But is it possible to remove only one or two drawable object instead of all?  Thank you for your responses.
Title: Re: Undrawing Sprites and Shapes
Post by: slotdev on July 19, 2012, 04:05:19 pm
Well, you really need a list of objects that you want to draw; a "render list". Then add objects to the list when you want them drawn, and erase from the list when you want them invisible.

Then, every frame, iterate over the list and draw the objects.

Simple :)
Title: Re: Undrawing Sprites and Shapes
Post by: Bocaj on July 19, 2012, 04:49:14 pm
Got it.  By the way, does anyone have source code for a simple game?  It would help to be able to see what an SFML game should look like.
Title: Re: Undrawing Sprites and Shapes
Post by: eXpl0it3r on July 19, 2012, 04:51:37 pm
Of course you can also just draw a black rectangle shape over the sprite to 'remove' it, but thos is really not a good thing to do, sibce you should call clear() ever frame iteration, because you don't fully 'own' the video memory that's used for showing stuff on tze screen, but you have to share it with other applications. So if you don't clear it you'll get some strange noise.

There's a complete pong gane being shipped with the SDK.
Title: Re: Undrawing Sprites and Shapes
Post by: mutonizer on July 19, 2012, 05:37:41 pm
Got it.  By the way, does anyone have source code for a simple game?  It would help to be able to see what an SFML game should look like.

Aside from the source in the SDK, I advise browsing some source repository like Github or others, sometimes you can find interesting stuff that expands the way you think and code, give you guidelines on which you can later build upon.

Here's one for example: https://github.com/krofna/Dreaming-Warrior (https://github.com/krofna/Dreaming-Warrior)
It's not code perfect or whatever, but the structure, and how it flows might help. There are plenty others around.


Nice blog btw Exploiter.
Title: Re: Undrawing Sprites and Shapes
Post by: eXpl0it3r on July 19, 2012, 05:44:21 pm
You can alos browse through the subforum 'Projects' which has some interesting games incl. sourcecode.

Nice blog btw Exploiter.
Hehe thanks. :)
Will have to update some stuff, but I'm a bit busy with studying. ;)