I wanted to use my own coordinate system in a view, so I have the following code so far
#include <SFML/Graphics.hpp>
int main (int argc, const char * argv[])
{
const float w = 640.0f;
const float h = 480.0f;
sf::RenderWindow window(sf::VideoMode(w, h), "Coordinates Test");
sf::View view(sf::Vector2f(0, 0), sf::Vector2f(20.0f * w / h, 20.0f));
window.SetView(view);
sf::Shape box = sf::Shape::Rectangle(0, 0, 1, 1, sf::Color::Red);
box.SetOrigin(0.5, 0.5);
box.SetPosition(5, 5);
while (window.IsOpened()) {
sf::Event e;
while (window.PollEvent(e)) {
if (e.Type == sf::Event::Closed)
window.Close();
}
window.Clear(sf::Color::White);
window.Draw(box);
window.Display();
}
return EXIT_SUCCESS;
}
This gives me the coordinate system I want, except upside down. I can not find a way to flip the y-axis. Is there a way to do this that I am just missing, or can this only be done be physically changing sfml?
To set up the view's projection matrix, the sf::View class uses the following function
inline Matrix3 Matrix3::Projection(const Vector2f& center, const Vector2f& size, float rotation)
{
// Rotation components
float angle = rotation * 3.141592654f / 180.f;
float cosine = static_cast<float>(std::cos(angle));
float sine = static_cast<float>(std::sin(angle));
float tx = -center.x * cosine - center.y * sine + center.x;
float ty = center.x * sine - center.y * cosine + center.y;
// Projection components
float a = 2.f / size.x;
float b = -2.f / size.y;
float c = -a * center.x;
float d = -b * center.y;
// Rebuild the projection matrix
return Matrix3( a * cosine, a * sine, a * tx + c,
-b * sine, b * cosine, b * ty + d,
0.f, 0.f, 1.f);
}
It seems that this is where the mirroring ability is missing from. Is this standard?
Thanks for any advice.