SFML community forums

Help => General => Topic started by: idontmakesense on July 19, 2013, 02:37:26 pm

Title: Simultaneous input
Post by: idontmakesense on July 19, 2013, 02:37:26 pm
How do I go about moving and turning the rectangle at the same time. He is my code..

#include<SFML/Graphics.hpp>
#include<iostream>
#include<cmath>

#define PI 3.14159265

using namespace std;

void displace(sf::RectangleShape *shape, float angle)
{

}

int main()
{
        const int xRes= 800;
        const int yRes= 600;
       
        sf::RenderWindow window(sf::VideoMode(xRes, yRes), "Window");
       
        float rectLength= 20, rectBreadth= 10;
        sf::RectangleShape rectangle(sf::Vector2f(rectLength, rectBreadth));
       
        float xCod= 100, yCod= 100;
        rectangle.setPosition(xCod, yCod);
        rectangle.setFillColor(sf::Color::Black);
        rectangle.setOrigin(rectLength/2, rectBreadth/2);
       
        float direction, speed= 0.5;
       
        while(window.isOpen())
        {
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type== sf::Event::Closed || (event.type== sf::Event::KeyReleased) && (event.key.code== sf::Keyboard::Escape))
                                window.close();
                }
               
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        direction= rectangle.getRotation();
                        rectangle.move(speed*cos(direction*PI/180), speed*sin(direction*PI/180));
                }
               
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        direction= rectangle.getRotation();
                        rectangle.move(-speed*cos(direction*PI/180), -speed*sin(direction*PI/180));
                }
               
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        rectangle.rotate(-0.5);
                        direction= rectangle.getRotation();
                }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        rectangle.rotate(0.5);
                        direction= rectangle.getRotation();
                }
               
                window.clear(sf::Color::White);
                window.draw(rectangle);
                window.display();
        }
        return 0;
}
Title: Re: Simultaneous input
Post by: Foaly on July 19, 2013, 02:50:57 pm
Just leave out the else 's before the if statements and then you can move and rotate at the same time
Title: Re: Simultaneous input
Post by: idontmakesense on July 19, 2013, 03:16:38 pm
It took me while to realize the difference. xD Really dumb of me.

Thanks a lot.