Obviously something is wrong in my more complex source code since this example works well.
#include <SFML/Graphics.hpp>
using namespace std;
int main(int argc, char** argv)
{
sf::RenderWindow App;
sf::VideoMode VidMode;
sf::Event Event;
VidMode = sf::VideoMode::GetDesktopMode();
VidMode.Width = 1024;
VidMode.Height = 768;
sf::Color color = sf::Color::Blue;
App.Create(VidMode, "Test shape", sf::Style::Close);
sf::Shape triangle;
triangle.AddPoint(12.f, 0.f, color, color);
triangle.AddPoint(0.f, 24.f, color, color);
triangle.AddPoint(24.f, 24.f, color, color);
triangle.EnableFill(false);
triangle.EnableOutline(true);
triangle.SetOutlineWidth(2.f);
triangle.SetCenter(12.f, 12.f);
triangle.Move(200.f, 200.f);
while(App.IsOpened())
{
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
}
if(App.GetInput().IsKeyDown(sf::Key::Left))
triangle.Rotate(180.f*App.GetFrameTime());
if(App.GetInput().IsKeyDown(sf::Key::Right))
triangle.Rotate(-180.f*App.GetFrameTime());
triangle.Move(20.f*App.GetFrameTime(), 20.f*App.GetFrameTime());
App.Clear();
App.Draw(triangle);
App.Display();
}
return 0;
}
In my source code I have a class called CPlayer which inherits from sf::Drawable. It is structured like this:
class CPlayer : public sf::Drawable
{
public:
CPlayer(sf::Color color);
private:
// Irrelevant variables and functions removed
sf::Shape shape;
virtual void Render(sf::RenderTarget& Target) const;
};
CPlayer::CPlayer(sf::Color color)
{
///Triangle
shape.AddPoint(12.f, 0.f, color, color);
shape.AddPoint(0.f, 24.f, color, color);
shape.AddPoint(24.f, 24.f, color, color);
shape.EnableFill(false);
shape.EnableOutline(true);
shape.SetOutlineWidth(2.f);
///Set center to center of shape
shape.SetCenter(12.f, 12.f);
///Move the shape to a more centerified position
shape.Move(200.f, 200.f);
}
void CPlayer::Render(sf::RenderTarget& Target) const
{
Target.Draw(shape);
}
In CGame, players are added to a vector by using
players.push_back(new CPlayer(sf::Color::Blue));
and the game-constructor looks like this:
CGame::CGame(sf::RenderWindow& _App) : App(_App), input(App.GetInput())
{
this->add_player(sf::Color::Blue);
}
void CGame::add_player(sf::Color col)
{
players.push_back(new CPlayer(col));
}
and here's the game-loop
int CGame::Run()
{
while(App.IsOpened())
{
while(App.GetEvent(event))
{
if(event.Type == sf::Event::Closed)
App.Close();
}
if(input.IsKeyDown(sf::Key::Left))
players[0]->Rotate(180.f*App.GetFrameTime());
if(input.IsKeyDown(sf::Key::Right))
players[0]->Rotate(-180.f*App.GetFrameTime());
///Testing some movement
players[0]->Move(20.f*App.GetFrameTime(), 20.f*App.GetFrameTime());
///Drawing
App.Clear();
for(unsigned int i=0 ; i<players.size() ; i++)
App.Draw(*players[i]);
App.Display();
}
return -1;
}
With this code, the triangle is spawned in the top left corner, its center is set to the center of the triangle, then it's moved away to 200, 200.
When pressing left- or right-key it's rotating around 12, 12.
Observe though that my shape is moving by 20 pixels per second diagonally, and so is the spot of rotation.
I'm sorry if the source code is a bit messy! :oops: