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

Author Topic: Why can't i initialize IntRect in specific way, but e.g. i can Sprite?  (Read 3364 times)

0 Members and 1 Guest are viewing this topic.

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
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?
« Last Edit: April 18, 2017, 09:45:30 pm by MDK »

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
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?
« Last Edit: April 19, 2017, 06:30:58 am by Laurent »
Laurent Gomila - SFML developer

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
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.
« Last Edit: April 19, 2017, 02:01:13 pm by MDK »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
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.
Laurent Gomila - SFML developer