Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Undrawing Sprites and Shapes  (Read 3059 times)

0 Members and 1 Guest are viewing this topic.

Bocaj

  • Newbie
  • *
  • Posts: 32
    • View Profile
Undrawing Sprites and Shapes
« 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?

unranked86

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Undrawing Sprites and Shapes
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Undrawing Sprites and Shapes
« Reply #2 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();
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Bocaj

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Undrawing Sprites and Shapes
« Reply #3 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.

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Re: Undrawing Sprites and Shapes
« Reply #4 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 :)
SFML 2.1

Bocaj

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Undrawing Sprites and Shapes
« Reply #5 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Undrawing Sprites and Shapes
« Reply #6 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mutonizer

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Undrawing Sprites and Shapes
« Reply #7 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
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.
« Last Edit: July 19, 2012, 05:43:33 pm by mutonizer »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Undrawing Sprites and Shapes
« Reply #8 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/