As we know, the virtual draw function draws stuff with this code
target.draw(sprItem, states);
Is it possible to draw a pointer to an sf::Drawable class? I tried pointer->draw but it didn't seem to work.
Here's a minimalistic example that shows why I need to do this:
class Item : public sf::Drawable, public sf::Transformable
{ };
class Helmet : public Item
{
// has texture and sprite
virtual void draw(sf::RenderTarget& target, sf::RenderStates states)const
{
states.transform *= getTransform();
states.texture = &texItem;
target.draw(sprItem, states);
}
};
class Player
{
Item* pHeadSlot;
Helmet objHelmet;
// let's say I click on the helmet in my inventory, it's now being dragged by the mouse
Item* pDrag;
pDrag = &objHelmet;
// let's equip the helmet by clicking on the helmet slot while dragging it
pHeadSlot = pDrag;
// now I want to draw the player with the helmet on (paperdoll)
virtual void draw(sf::RenderTarget& target, sf::RenderStates states)const
{
states.transform *= getTransform();
states.texture = &texEntity;
target.draw(sprEntity, states);
if (Head != nullptr) { target.draw(pHeadSlot, states); } // doesn't work because it's a pointer
}
};
I had to create a separate sprite for the helmet instead (and all other equipment slots).
I would prefer to draw the pointer instead if it's possible.
Also, as a side question, is it wise to be using raw pointers in this code?