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

Author Topic: workaround for Drawable::FlipX, Drawable::FlipY  (Read 2388 times)

0 Members and 1 Guest are viewing this topic.

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
workaround for Drawable::FlipX, Drawable::FlipY
« on: October 15, 2010, 10:34:51 am »
If someone needs it, here's my workaround to make drawables being flippable. I did it months ago.. I should have kept copies of the matrices, but it was just a quick workaround to test some stuff.
With that I was able to flip shapes, text, and my own drawable classes, like drawable trees.


Code: [Select]
#ifndef INVERSIBLE_DRAWABLE_HPP
#define INVERSIBLE_DRAWABLE_HPP

#include <SFML/Graphics.hpp>


namespace gm2{
// Note: T inherits from sf::Drawable
template<class T>
class InversibleDrawable: public T{
 public:


     // default constructor
     InversibleDrawable():
         T(), myFlipX(false), myFlipY(false)
     {
         // Nothing to do
     }

     void flipX(bool flipX){
         myFlipX = flipX;
     }

     void flipY(bool flipY){
         myFlipY = flipY;
     }

//TODO:
//        sf::Vector2f TransformToLocal(const sf::Vector2f& point) const;
//        sf::Vector2f TransformToGlobal(const sf::Vector2f& point) const;

private:

     // render method
     void Render(sf::RenderTarget& target, sf::Renderer& renderer) const{

         if (myFlipX || myFlipY){

             sf::Matrix3 tmp;
             float rot = T::GetRotation();
             sf::Vector2f origin = T::GetOrigin();

             // Revert origin and rotation
             tmp.Transformation( origin, sf::Vector2f(0,0), rot, sf::Vector2f(1,1));
             tmp = tmp.GetInverse();
             renderer.ApplyModelView(tmp);

             // Apply flip
             tmp.Transformation( sf::Vector2f(0,0), sf::Vector2f(0,0), 0, sf::Vector2f(myFlipX?-1:1,myFlipY?-1:1));
             renderer.ApplyModelView(tmp);

             // Apply origin and rotation on the f
             tmp.Transformation( origin, sf::Vector2f(0,0), rot, sf::Vector2f(1,1));
             renderer.ApplyModelView(tmp);
         }

         T::Render(target,renderer);
     }
       
private:

     // our flip flags
     bool myFlipX, myFlipY;


};

typedef InversibleDrawable<sf::Sprite> InversibleSprite;
typedef InversibleDrawable<sf::Shape> InversibleShape;
typedef InversibleDrawable<sf::Text> InversibleText;

}  // namespace gm2

#endif // INVERSIBLE_DRAWABLE_HPP
Pluma - Plug-in Management Framework

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
workaround for Drawable::FlipX, Drawable::FlipY
« Reply #1 on: October 15, 2010, 10:51:04 am »
Don't forget to say that this is for SFML 2 only ;)
Laurent Gomila - SFML developer