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

Author Topic: Is there a way to initialize this struct at instantiation?  (Read 3195 times)

0 Members and 1 Guest are viewing this topic.

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Is there a way to initialize this struct at instantiation?
« 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)
« Last Edit: October 12, 2012, 10:15:33 pm by Raptor88 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is there a way to initialize this struct at instantiation?
« Reply #1 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);
Laurent Gomila - SFML developer

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Re: Is there a way to initialize this struct at instantiation?
« Reply #2 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)

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Is there a way to initialize this struct at instantiation?
« Reply #3 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


mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Is there a way to initialize this struct at instantiation?
« Reply #4 on: October 13, 2012, 11:10:58 pm »
You can use

sf::IntRect
 

instead of

sf::Rect<int>
 

 ;)
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Is there a way to initialize this struct at instantiation?
« Reply #5 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

 

anything