SFML community forums

Help => Graphics => Topic started by: MDK on April 18, 2017, 09:43:38 pm

Title: Why can't i initialize IntRect in specific way, but e.g. i can Sprite?
Post by: MDK on April 18, 2017, 09:43:38 pm
After many battles with IntRect i successfully initialized it like this:
player.cpp
Player::Player(VideoMode VMo){
    playerRectSourceSprite.left = 0;
    playerRectSourceSprite.top = 0;
    playerRectSourceSprite.width = 60;
    playerRectSourceSprite.height = 60;
}
 
Why i couldn't do this like this:
playerRectSourceSprite(0, 0, 60, 60);
 
or like this 
IntRect playerRectSourceSprite(0, 0, 60, 60); // I know this one is definitely wrong but i tried this as well
 
Of course i could initialize it this way:
playerRectSourceSprite = {0, 0, 60, 60}; // but then i receive warnings and i read this isn't best way of initializing
 
but in the same moment i can initialize Sprite position in the same constructor in a way i can't IntRect:
playerS.setPosition(50, 450);
 
Is there any reason behind it, or maybe there is different way to initialize IntRect collectively? Or, maybe, i am doing something incorrectly?
Title: Re: Why can't i initialize IntRect in specific way, but e.g. i can Sprite?
Post by: Turbine on April 19, 2017, 03:50:06 am
I agree SFML's constructors can be a real pain. Just like when you want to copy one sort of vector to an SFML vector. They should just make it a template as the compiler will figure whether x and y exists. Not to mention data conversions.

Anyway, there's always initialiser lists if you want to construct one with the least amount of syntax.
Title: Re: Why can't i initialize IntRect in specific way, but e.g. i can Sprite?
Post by: Laurent on April 19, 2017, 06:29:27 am
This is not a SFML problem, and it has nothing to do with the constructors of SFML classes. You just lack some basic understanding of C++ itself.

Either you use the initialization list of the constructor to construct your rectangle with arguments directly:
Player::Player(VideoMode VMo) :
playerRectSourceSprite(0, 0, 60, 60)
{
}

... or you assign a new IntRect after its construction:
Player::Player(VideoMode VMo)
{
    playerRectSourceSprite = sf::IntRect(0, 0, 60, 60); // or using C++11 uniform brace initialization
}

Quote
but then i receive warnings
Which ones?
Title: Re: Why can't i initialize IntRect in specific way, but e.g. i can Sprite?
Post by: MDK on April 19, 2017, 01:53:20 pm
Thank you Laurent. This is exactly what i needed.
As for those warnings: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|
I know i shouldn't bother with them, but i wanted my code better, and now i have.
Title: Re: Why can't i initialize IntRect in specific way, but e.g. i can Sprite?
Post by: Laurent on April 19, 2017, 02:04:40 pm
Quote
As for those warnings: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|
gcc doesn't enable C++11 (and beyond) by default, so you must add the "-std=c++11" compiler flag if you use C++11 syntax.