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

Author Topic: How do you draw a pointer to sf::Drawable?  (Read 3037 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
How do you draw a pointer to sf::Drawable?
« on: August 05, 2017, 10:24:04 pm »
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?
« Last Edit: August 05, 2017, 10:26:36 pm by NGM88 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11028
    • View Profile
    • development blog
    • Email
Re: How do you draw a pointer to sf::Drawable?
« Reply #1 on: August 05, 2017, 10:55:26 pm »
I tried pointer->draw but it didn't seem to work.
That's not a problem description. What does not work? How do you know it doesn't work? What's the error message?

The documentation shows how to use sf::Drawable. If you mean when sprItem is a pointer, then you'll simply have to dereference the pointer (basic C++ pointer knowledge) to get the value the pointer points to: *sprItem ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: How do you draw a pointer to sf::Drawable?
« Reply #2 on: August 05, 2017, 11:07:34 pm »
error message: cannot convert from 'Item *const ' to 'const sf::Drawable'

looking into how to dereference.

sorry, I don't have any formal programming training, missing some basics. life's hard when you're self taught  :'(

this is the exact line where I get the error:

target.draw(Head, states);

where "Head" is an Item* (class Item : public sf::Drawable, public sf::Transformable)
« Last Edit: August 05, 2017, 11:10:07 pm by NGM88 »

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: How do you draw a pointer to sf::Drawable?
« Reply #3 on: August 05, 2017, 11:15:01 pm »
OK problem solved. Thanks for telling me where to look eXpl0it3r