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

Author Topic: Why sf::Drawable::draw is a complately const function?  (Read 5244 times)

0 Members and 1 Guest are viewing this topic.

china92

  • Newbie
  • *
  • Posts: 13
    • View Profile
Why sf::Drawable::draw is a complately const function?
« on: May 21, 2013, 01:17:44 am »
I've searched this forum for similar topics, but I haven't found any.

virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

I'd like to know why? I'm asking because right now I have a problem, because of this little "const", here is a preview of this issue:
Wave.h
class Wave :
        public sf::Drawable
{
private:
        std::list<Enemy*> enemies;
[...]
public:
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
}
In Wave.cpp I would like to draw every enemy in wave.
void Wave::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        std::list<Enemy*>::iterator temp = enemies.begin();

        for(temp; temp != currentEnemy; temp++)
        {
                target.draw(**temp);
        }
}

But there is an error:
Quote
Error   1   error C2440: 'initializing' : cannot convert from 'std::_List_const_iterator<_Mylist>' to 'std::_List_iterator<_Mylist>'

Looks like one does not simply iterate the const list. ;) (there is a problem with initializing iterator "std::list<Enemy*>::iterator temp = enemies.begin();", without const it works, but it cannot initiate abstract class)

If you have any advices how to workaround this problem, please write them.

PS
Is the AnimatedSprite included in standard version of SFML, or I have to copy it by myself?

PS2
Sorry for my lame English.

PS8
If the section on forum is wrong, please move this topic (I wasn't sure where to put it).
« Last Edit: May 21, 2013, 01:21:47 am by china92 »

kloffy

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Why sf::Drawable::draw is a complately const function?
« Reply #1 on: May 21, 2013, 01:23:30 am »
Try:

std::list<Enemy*>::const_iterator temp = enemies.begin();

china92

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Why sf::Drawable::draw is a complately const function?
« Reply #2 on: May 21, 2013, 01:31:12 am »
C'mon it was seriously that simple? I'm sorry for my stupidity and lack of knowledge, I just assumed that const_iterator is not incrementable and I haven't even tried that.

Thank you for your really fast answer, everything is working now, this topic can be removed.
« Last Edit: May 21, 2013, 01:32:51 am by china92 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Why sf::Drawable::draw is a complately const function?
« Reply #3 on: May 21, 2013, 01:45:32 am »
Besides if you don't use the temp variable later on, you can initialize the iterator directly in the for loop. And if you're using C++11 compatible compilers, you can also use the auto keyword and might even want to consider a range based for loop. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

china92

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Why sf::Drawable::draw is a complately const function?
« Reply #4 on: May 21, 2013, 09:57:40 am »
I still have a lot to learn, thank you. :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Why sf::Drawable::draw is a complately const function?
« Reply #5 on: May 21, 2013, 12:19:05 pm »
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
I'd like to know why?
The reason for this const is always the same: The method does not modify the logical state of the object.

Assuming that the Wave class owns the enemies, is there a certain reason why you use std::list<Enemy*> and not std::list<Enemy>? If there is none, use the latter. It is much simpler and safer to handle, since you don't have to care about memory management.

Is the AnimatedSprite included in standard version of SFML, or I have to copy it by myself?
It is not part of SFML, same for the other codes on the Wiki. By the way, you could also have a look at the Animation module in Thor -- it is slightly more complex, but allows also other animations than different texture rects, such as color gradients. It depends on what you need, I think to begin AnimatedSprite is a good choice.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

china92

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Why sf::Drawable::draw is a completely const function?
« Reply #6 on: May 21, 2013, 09:45:53 pm »
Yes, you are right of course, but I thought that I cannot iterate via the list, when the function is completely const (that was my mistake, and that's why I asked this question).

I've already implemented the destructor which take care about memory, so it is not a problem for me right now, but I'll take your idea into consideration.

Thank you for your advice, right now my game has some static Sprites, but I'm thinking about some animations. When I decide to implement this feature I'll look at Thor (but I think AnimatedSprite from wiki should be enough for my needs). ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Why sf::Drawable::draw is a completely const function?
« Reply #7 on: May 21, 2013, 09:49:58 pm »
I've already implemented the destructor which take care about memory, so it is not a problem for me right now
I think you are wrong, unless you have also implemented the copy constructor and assignment operator ;)

There are so many things that can go wrong with manual memory management, you should really avoid it whereever possible. Even more if automatic memory management does the job equally well. You could take a look at the RAII idiom.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: Why sf::Drawable::draw is a complately const function?
« Reply #8 on: May 21, 2013, 10:22:38 pm »
Great point Nexus. That reminds me I have some issues to take care of with exactly that kind of memory management. :D

 

anything