Thanks for the reply.
I finally have what I really wanted. I finally made this code.
Actually, What I want is this, just to make clear:
When the user press left, using sprite. Rotate Sprite such that the sprite facing left.
Take note that when the user press left, the Sprite will not rotate left based on its new orientation.
like:
<-- user press left:
<0 // sprite facing west
<-- user press left again:
<0 // sprite still facing west
unlike before I have this:
<-- user press left:
<0 // sprite facing west
<-- user press left again:
0 // sprite now faces south, since south is the left of the new orientation!
What i did is that, I get the actual rotation( sprite.getRotation() SFML 2.0 ) of the newly rotated sprite and subtract that to my desired angle,
Now I got my desired result.
#include<SFML/Graphics.hpp>
#include<SFML/Window.hpp>
#include<iostream>
using std::cout;
using std::endl;
int main()
{
sf::RenderWindow window( sf::VideoMode( 800 , 640 , 32 ) , "Hello World!" );
sf::Event event;
sf::Texture texture;
texture.loadFromFile("images/Trooper.png");
sf::Sprite sprite;
sprite.setTexture( texture );
sprite.setPosition( 400 , 320 );
sprite.setOrigin( sprite.getLocalBounds().height / 2 , sprite.getLocalBounds().width / 2 );
sprite.setRotation( 90 );
float angle;
while( window.isOpen() )
{
window.waitEvent( event );
switch( event.type )
{
case sf::Event::Closed:
window.close();
case sf::Event::KeyPressed:
{
switch( event.key.code )
{
case sf::Keyboard::Escape:
window.close();
case sf::Keyboard::Left:
angle = 270.0f - sprite.getRotation();
cout << "angle : " << sprite.getRotation() << endl;
sprite.rotate( angle );
break;
case sf::Keyboard::Right:
angle = 90.0f - sprite.getRotation();
cout << "angle : " << angle << endl;
sprite.rotate( angle );
break;
case sf::Keyboard::Up:
angle = 360.0f - sprite.getRotation();
cout << "angle : " << sprite.getRotation() << endl;
sprite.rotate( angle );
break;
case sf::Keyboard::Down:
angle = 180.0f - sprite.getRotation();
cout << "angle : " << angle << endl;
sprite.rotate( angle );
break;
}
}
}
window.clear( sf::Color ( 0 , 0 , 0 ) );
window.draw( sprite );
window.display();
}
return 0;
}
I hope this question helps other people who want a zelda/final fantasy sprite orientation although I know the fact this might not be that so trivial. I got so much time wasted searching over the web but all I see is rotating sprite base on mouse direction! That is not what I want.
So I hope if someone is searching for this specific sprite orientation behavior based on strict key press: left, right , up down, or a , s ,w ,d.
This is how I solved it.