I am new to sf::Transformable class and just recently tried it out. After experimenting it a bit, I am facing an issue. When sf::Transformable object is placed in a class, which itself is derived from sf::Transformable, then whenever transformation method is called, it only updates transformation of the class’ object, not the transformation of the objects present in the class. However when we draw the stuff, It automatically manages the transformations and temporarily, and correctly position the objects for drawing. This should be fine in most cases, but what if we need positions of the object present in the class rather than whole object’s position. Maybe the object is board, and all the sf::Transformable objects are boardplaces, in this case, while we don’t want to align everything manually each time, we can derive it from sf::Transformable class but, while we want it to align everything automatically, but it should actually update their positions, or other transformations, because maybe we need them. A little program to demonstrate the problem :
#include <SFML/Graphics.hpp>
#include <iostream>
std::ostream& operator << (std::ostream& os, const sf::Vector2f& vect) {
os << "(" << vect.x << ", " << vect.y << ")";
return os;
}
class TestClass : public sf::Transformable, public sf::Drawable {
public:
TestClass() {
shape.setSize(sf::Vector2f(100, 50));
shape.setPosition(0,0);
shape.setFillColor(sf::Color::Red);
}
void displayInfo() const {
std::cout << "nShape Position : " << shape.getPosition() << std::endl;
std::cout << "Shape Rotation : " << shape.getRotation() << std::endl;
std::cout << "Shape Origin : " << shape.getOrigin() << std::endl;
std::cout << "Position of Object : " << getPosition() << std::endl;
std::cout << "Rotation of Object : " << getRotation() << std::endl;
std::cout << "Origin of Object : " << getOrigin() << std::endl;
}
protected:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
states.transform *= getTransform();
target.draw(shape, states);
}
private:
sf::RectangleShape shape;
};
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "TextBox Test");
sf::Event event;
TestClass test;
sf::Clock clock;
double delta = 0;
double multiplier = 60;
float movement = 10.f;
float rotation = 2.f;
while (window.isOpen()) {
delta = clock.restart().asSeconds();
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) window.close();
else if (event.key.code == sf::Keyboard::I) {
test.displayInfo();
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
test.move(-movement * delta * multiplier, 0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
test.move(movement * delta * multiplier, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
test.move(0, -movement * delta * multiplier);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
test.move(0, movement * delta * multiplier);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
test.rotate(-rotation * delta * multiplier);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) {
test.rotate(rotation * delta * multiplier);
}
window.clear();
window.draw(test);
window.display();
}
return 0;
}
I’ve tried different approaches (as far as I can understand), but none of them seems to be working. I really tried to figure out how can I solve that problem but my little brain failed, so I think I need some professional’s help. I just want to know, that How can we update transformation of the transformable objects present in the class.
It_is_Unknown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.