SFML community forums
Help => Graphics => Topic started by: e_barroga on April 16, 2009, 11:16:10 pm
-
I've created a shape as follows:
sf::Shape Ball = sf::Shape::Circle( 400, 300, BallRadius, sf::Color( 255, 255, 255 ) ); //Ball
When I check the Y coordinate (writing to console) I get: -7
Shouldn't it be 300?
-
It should be zero. The fact that you put the points of your shape around (300, 400) doesn't move its origin from (0, 0). It's just not a good idea because you'll always have this offset.
-
It should be zero. The fact that you put the points of your shape around (300, 400) doesn't move its origin from (0, 0). It's just not a good idea because you'll always have this offset.
Oh, okay. I think I get it. This makes perfect sense, thank you. The actual reason it is -7 is because I move the shape -7.
Thanks for the explanation.
-
Now, I'm getting the following error after trying to write the shape as a class:
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|6|error: `sf::Shape::Rectangle' cannot appear in a constant-expression|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|6|error: a call to a constructor cannot appear in a constant-expression|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|6|error: a function call cannot appear in a constant-expression|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|6|error: invalid in-class initialization of static data member of non-integral type `const sf::Shape'|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h||In constructor `Paddle::Paddle(float, float)':|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|11|error: `self' was not declared in this scope|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|11|warning: unused variable 'self'|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h||In member function `void Paddle::Move(float, float)':|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|15|error: `self' was not declared in this scope|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|15|warning: unused variable 'self'|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h||In member function `void Paddle::SetX(float)':|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|19|error: `self' was not declared in this scope|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|19|warning: unused variable 'self'|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h||In member function `void Paddle::SetY(float)':|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|23|error: `self' was not declared in this scope|
C:\Users\BarrogaTravels\Desktop\Pong\Engine.h|23|warning: unused variable 'self'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp||In function `int main()':|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|45|error: 'class Paddle' has no member named 'GetPosition'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|46|error: 'class Paddle' has no member named 'GetPosition'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|48|error: 'class Paddle' has no member named 'GetPosition'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|49|error: 'class Paddle' has no member named 'GetPosition'|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|66|error: expected `)' before "App"|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|67|error: no matching function for call to `sf::RenderWindow::Draw(Paddle&)'|
C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\SFML\Graphics\RenderTarget.hpp|67|note: candidates are: virtual void sf::RenderTarget::Draw(const sf::Drawable&)|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|68|error: no matching function for call to `sf::RenderWindow::Draw(Paddle&)'|
C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\SFML\Graphics\RenderTarget.hpp|67|note: candidates are: virtual void sf::RenderTarget::Draw(const sf::Drawable&)|
C:\Users\BarrogaTravels\Desktop\Pong\main.cpp|52|warning: unused variable 'Factor'|
||=== Build finished: 15 errors, 5 warnings ===|
Disregard the "GetPosition" error.
class Paddle {
private:
sf::Shape self = sf::Shape::Rectangle( 0, 10, 100, sf::Color( 255, 255, 255 ) );
public:
Paddle();
Paddle( float X, float Y ) {
self.Move( X, Y );
}
void Move( float X, float Y ) {
self.Move( X, Y );
}
void SetX( float X ) {
self.SetX( X );
}
void SetY( float Y ) {
self.SetY( Y );
}
};
-
You should probably have a look at a good C++ book or at least a tutorial. ;)
You cannot initialize class members inside the class definition. That's the constructor's task.
-
I've moved the initialization to the constructor, but now I'm not quite sure how to write this:
float GetX() {
return ( self.GetPosition().x );
}
...considering it isn't returning a float type. I checked the source and it seems I should be using const Vector2f but it doesn't work.
-
What doesn't work? Could you post the surrounding and relevant code, including the used types and invoked functions?
The error might be helpful, too.
-
I'm sorry.... I rechecked the code more indepth and I wrote a class member when it should have been a function member:
Paddle1.GetY
//Fixed
Paddle1.GetY()
class Paddle {
private:
sf::Shape self;
public:
Paddle() {
self = sf::Shape::Rectangle( 0.f, 0.f, 10.f, 100.f, sf::Color( 255, 255, 255 ) );
}
Paddle( float X, float Y ) {
self.Move( X, Y );
}
void Move( float X, float Y ) {
self.Move( X, Y );
}
void SetX( float X ) {
self.SetX( X );
}
void SetY( float Y ) {
self.SetY( Y );
}
float GetX() {
return ( self.GetPosition().x );
}
float GetY() {
return ( self.GetPosition().y );
}
sf::Shape Draw() {
return ( self );
}
};
float lengthdir_x( float Length, float Direction ) {
return ( std::cos( -Direction*( 3.14159265/180 ) ) * Length );
}
float lengthdir_y( float Length, float Direction ) {
return ( std::sin( -Direction*( 3.14159265/180 ) ) * Length );
}
-
Okay... Now everything works?
By the way, I have some advice for you. First, you should use the constructor's initializer list:
Paddle() : self(sf::Shape::Rectangle( 0.f, 0.f, 10.f, 100.f, sf::Color( 255, 255, 255 ) )
{
}
Second, there is mostly no reason not to declare getter functions const:
float GetX() const
{
return self.GetPosition().x;
}
-
Second, there is mostly no reason not to declare getter functions const:
float GetX() const
{
return self.GetPosition().x;
}
Yes there is. If the reference to the object is constant then only the constant function works. The const keyword promises that the function does not change the internal state of the object.
-
Yes there is. If the reference to the object is constant then only the constant function works. The const keyword promises that the function does not change the internal state of the object.
I wrote "no reason not to declare getter functions const". Sorry if that was misleading... ;)
-
Damn it! I thought I looked smart <_<