SFML community forums

Help => General => Topic started by: Raptor88 on October 12, 2012, 10:10:24 pm

Title: Is there a way to initialize this struct at instantiation?
Post by: Raptor88 on October 12, 2012, 10:10:24 pm
I currently initialize this struct as follows:

        struct MenuItem
        {
                sf::Rect<int> rect;
                eButtonAction action;
        };

        //Setup PLAY (RTPlay) button clickable area
        MenuItem playButton;
        playButton.rect.left = 110;
        playButton.rect.top = 265;
        playButton.rect.width = 200;
        playButton.rect.height = 50;
        playButton.action = RT_PLAY;

        //Setup QUIT button clickable area
        MenuItem exitButton;
        exitButton.rect.left = 700;
        exitButton.rect.top = 535;
        exitButton.rect.width = 200;
        exitButton.rect.height = 50;
        exitButton.action = QUIT;
       
        _menuItems.push_back (playButton);
        _menuItems.push_back (exitButton);
 

Is there a way to initialize the structs similar to this:

        struct MenuItem
        {
                sf::Rect<int> rect;
                eButtonAction action;
        };

        MenuItem playButton =
        {110, 265, 200, 50, RT_PLAY};

        MenuItem exitButton =
        {700, 535, 200, 50, QUIT};
 

The second method doesn't work as shown since the struct contains "sf::Rect<int> rect;" and my second example is using integer values.  Any way to initialize using the short form?

(Only 2 buttons shown here but my craps program will contain many menu buttons and the crap table will contain a large amount of clickable areas so I'm looking for a concise way to initialize the structs.)

Thanks,
Raptor88 (First C++ and SFML program)
Title: Re: Is there a way to initialize this struct at instantiation?
Post by: Laurent on October 12, 2012, 10:25:28 pm
Either

MenuItem playButton =
{sf::Rect<int>(110, 265, 200, 50), RT_PLAY};

Or

struct MenuItem
{
    MenuItem(int l, int t, int w, int h, eButtonAction a) : rect(l, t, w, h), action(a) {}
    sf::Rect<int> rect;
    eButtonAction action;
};

MenuItem playButton(110, 265, 200, 50, RT_PLAY);
Title: Re: Is there a way to initialize this struct at instantiation?
Post by: Contadotempo on October 12, 2012, 10:27:42 pm
EDIT: Whoops, Laurent got it first.

You could make your own constructor. Something like:
MenuItem(int left, int top, int width, int height, eButtonAction myaction)
{
        rect.left = left;
        rect.top = top;
        rect.width = width;
        rect.height = height;
        action = myaction
}

Or more efficiently, with a constructor using an initialization list and a sf::Rect<int> as a parameter:
MenuItem(const sf::Rect<int>& myrect, eButtonAction myaction):
rect(myrect),
action(myaction)
{

}

Then you could create an object like this:
MenuItem playButton(110, 265, 200, 50, RT_PLAY)
or with the second constructor:
MenuItem playButton(sf::Rect<int>(110, 265, 200, 50), RT_PLAY)
Title: Re: Is there a way to initialize this struct at instantiation?
Post by: Raptor88 on October 13, 2012, 12:06:12 am
Either

MenuItem playButton =
{sf::Rect<int>(110, 265, 200, 50), RT_PLAY};

OR ...snip...

Hi Laurent and Contadotempo,

I'll go with the example I quoted from Laurent (and Contadotempo's second example) since I understand it.  I tried it in my code and it works great.  But I'll dig into both of your other examples to learn how they work.

Thanks so much to you both for your quick help!
Raptor

Title: Re: Is there a way to initialize this struct at instantiation?
Post by: mateandmetal on October 13, 2012, 11:10:58 pm
You can use

sf::IntRect
 

instead of

sf::Rect<int>
 

 ;)
Title: Re: Is there a way to initialize this struct at instantiation?
Post by: Raptor88 on October 14, 2012, 02:08:52 am
You can use

sf::IntRect
 

instead of

sf::Rect<int>
 

 ;)

Yup, already changed to IntRect in my code while entering the many button coordinates.  Easier to type  ;)

Thanks,
Raptor