Center? Upper-left corner? I was wondering because I'm trying to get sf::Rect and sf::Shape to play nice, meaning they would both move at the same time. So I've implemented a class like this:
Shape.h
/*
* Shape.h
*
* Created on: Aug 3, 2009
* Author: Jordy
*/
#ifndef SHAPE_H_
#define SHAPE_H_
#include <algorithm>
#include <SFML/Graphics/Shape.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp>
namespace Solids
{
class Shape : public sf::Shape, public sf::Rect<float>
{
public:
Shape();
virtual ~Shape();
virtual void Move(float OffsetX, float OffsetY);
virtual void Move(const sf::Vector2f &Offset);
virtual void SetPosition(float OffsetX, float OffsetY);
virtual void SetPosition(const sf::Vector2f &Offset);
void SetSpeed(float NewSpeedX, float NewSpeedY);
void SetSpeedX(float NewSpeedX);
void SetSpeedY(float NewSpeedY);
void Update(float FrameTime=1);
static Shape Rectangle(float X, float Y, float Width, float Height);
static Shape Triangle(float P1X, float P1Y, float P2X, float P2Y, float P3X, float P3Y);
private:
float SpeedX, SpeedY;
};
}
#endif /* SHAPE_H_ */
Shape.cpp
/*
* Shape.cpp
*
* Created on: Aug 3, 2009
* Author: Jordy
*/
#include "Shape.h"
namespace Solids
{
Shape::Shape()
{
}
Shape::~Shape()
{
}
void Shape::Move(float OffsetX, float OffsetY)
{
sf::Shape::Move(OffsetX, OffsetY);
Offset(OffsetX, OffsetY);
}
void Shape::Move(const sf::Vector2f &Offset)
{
Move(Offset.x, Offset.y);
}
void Shape::SetSpeed(float NewSpeedX, float NewSpeedY)
{
SpeedX = NewSpeedX;
SpeedY = NewSpeedY;
}
void Shape::SetSpeedX(float NewSpeedX)
{
SpeedX = NewSpeedX;
}
void Shape::SetSpeedY(float NewSpeedY)
{
SpeedY = NewSpeedY;
}
void Shape::Update(float FrameTime)
{
Move(SpeedX * FrameTime, SpeedY * FrameTime);
}
Shape Shape::Rectangle(float X, float Y, float Width, float Height)
{
Shape Rect;
Rect.Left = X;
Rect.Top = Y;
Rect.Right = Rect.Left + Width;
Rect.Bottom = Rect.Top + Height;
Rect.AddPoint(Rect.Left, Rect.Top);
Rect.AddPoint(Rect.Right, Rect.Top);
Rect.AddPoint(Rect.Right, Rect.Bottom);
Rect.AddPoint(Rect.Left, Rect.Bottom);
return Rect;
}
Shape Shape::Triangle(float P1X, float P1Y, float P2X, float P2Y, float P3X, float P3Y)
{
Shape Tri;
// Find boundaries of enclosing sf::Rect
Tri.Left = std::min(std::min(P1X, P2X), P3X);
Tri.Right = std::max(std::max(P1X, P2X), P3X);
Tri.Top = std::min(std::min(P1Y, P2Y), P3Y);
Tri.Bottom = std::max(std::max(P1Y, P2Y), P3Y);
Tri.AddPoint(P1X, P1Y);
Tri.AddPoint(P2X, P2Y);
Tri.AddPoint(P3X, P3Y);
return Tri;
}
}
As you can see, I've implemented a Shape::Move method, but not the Shape::SetPosition method. This is because I'm not sure whether I should be doing this:
void Shape::SetPosition(float PosX, float PosY)
{
sf::Shape::SetPosition(PosX, PosY);
float Width = GetWidth();
float Height = GetHeight();
Left = PosX;
Top = PosY;
Right = Left + Width;
Bottom = Top + Height;
}
or this:
void Shape::SetPosition(float PosX, float PosY)
{
sf::Shape::SetPosition(PosX, PosY);
float Width = GetWidth();
float Height = GetHeight();
Left = PosX - Width / 2;
Top = PosY - Height / 2;
Right = Left + Width;
Bottom = Top + Height;
}
Note that there may be other errors in my code. I haven't tested it thoroughly yet.
Thanks,
Jordy