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

Author Topic: Help with vector of Drawables  (Read 2453 times)

0 Members and 1 Guest are viewing this topic.

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Help with vector of Drawables
« on: November 04, 2018, 04:07:17 am »
I'm trying to make a vector of all drawables so that I can make a simple render routine that just loops through all drawable objects and draws them. Here are some of the relevant bits of my code:

Declaring the vector:
std::vector<sf::Drawable*> drawables;

Looping through the enemies and adding them to the vector:
for (enemy_main::Enemy& enemy : enemies)
{
  drawables.emplace_back(enemy.getSprite());
  drawables.emplace_back(enemy.getRedRectnagle());
  drawables.emplace_back(enemy.getHealthRectangle());
}

In my Enemy class, here are the accessors you see in the code above:
sf::Sprite& getSprite() { return sprite; }
sf::RectangleShape& getHealthRectangle() { return healthRectangle; }
sf::RectangleShape& getRedRectnagle() { return redRectangle; }

And finally the drawing:
for (auto const& drawable : drawables)
{
  myWindow.draw(*drawable);
}

What I'm getting when I try to build are the following errors:
Error   C2440   'initializing': cannot convert from 'sf::Sprite' to 'sf::Drawable *'    TestSFML        c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xmemory0        918

Error   C2440   'initializing': cannot convert from 'sf::Sprite' to 'sf::Drawable *'    TestSFML        c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector  1035   

Error   C2440   'initializing': cannot convert from 'sf::RectangleShape' to 'sf::Drawable *'    TestSFML        c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xmemory0        918

The errors are pointing to libraries that I didn't obviously write, but the error isn't in those libraries. That much is obvious to me. What's not obvious to me is what's wrong with my code where

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Help with vector of Drawables
« Reply #1 on: November 04, 2018, 04:49:50 am »
You need to read up about (basic) differences between references and pointers and how to convert between them.

You could also make your Drawable references/pointers const if it's just for drawing and not for any modification (this is just nice and clean practice, it's unrelated to your problem here).
Back to C++ gamedev with SFML in May 2023

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Help with vector of Drawables
« Reply #2 on: November 04, 2018, 05:29:31 am »
You need to read up about (basic) differences between references and pointers and how to convert between them.

You could also make your Drawable references/pointers const if it's just for drawing and not for any modification (this is just nice and clean practice, it's unrelated to your problem here).

You are absolutely right. Thank you for pointing it out to me and consequently solving my issue.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Help with vector of Drawables
« Reply #3 on: November 04, 2018, 03:06:23 pm »
What about making Enemy a Drawable itself, and write window.draw(enemy), rather than adding all those accessors? That would make it much more encapsulated, readable and maintainable.
Laurent Gomila - SFML developer

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Help with vector of Drawables
« Reply #4 on: November 04, 2018, 05:25:26 pm »
What about making Enemy a Drawable itself, and write window.draw(enemy), rather than adding all those accessors? That would make it much more encapsulated, readable and maintainable.

Excellent suggestion. Now I can:

Add an enemy directly to the drawables vector:
for (enemy_main::Enemy& enemy : enemies)
{
  drawables.emplace_back(&enemy);
}

Draw all of my drawable objects:
for (sf::Drawable* const drawable : drawables)
{
  myWindow.draw(*drawable);
}

 

anything