Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3.  
  4. sf::RenderWindow window;
  5. constexpr int WIDTH  = 1280,
  6.               HEIGHT = 720;
  7.  
  8. void checkForClose();
  9.  
  10. class Draggable_Sprite
  11. {
  12.     public:
  13.         void setSprite ( sf::RectangleShape& sprite )
  14.         {
  15.             m_sprite = &sprite;
  16.             offset = getOffsetToMouse();
  17.         }
  18.  
  19.         void update()
  20.         {
  21.             if ( !m_sprite ) return;
  22.  
  23.             if ( !sf::Mouse::isButtonPressed( sf::Mouse::Left ) )
  24.             {
  25.                 m_sprite = nullptr;
  26.                 return;
  27.             }
  28.  
  29.             if ( m_sprite )
  30.             {
  31.  
  32.                 float yPos = sf::Mouse::getPosition( window ).y - offset.y;
  33.                 float xPos = sf::Mouse::getPosition( window ).x - offset.x;
  34.  
  35.                 m_sprite->setPosition( { xPos, yPos } );
  36.  
  37.             }
  38.  
  39.         }
  40.  
  41.         bool hasSprite()
  42.         {
  43.             return m_sprite;
  44.         }
  45.  
  46.     private:
  47.         sf::Vector2i getOffsetToMouse (  )
  48.         {
  49.             float mouseX = sf::Mouse::getPosition( window ).x;
  50.             float mouseY = sf::Mouse::getPosition( window ).y;
  51.  
  52.             float spriteX = m_sprite->getPosition().x;
  53.             float spriteY = m_sprite->getPosition().y;
  54.  
  55.             return {    (int)mouseX - spriteX,
  56.                         (int)mouseY - spriteY };
  57.         }
  58.  
  59.  
  60.         sf::RectangleShape* m_sprite    = nullptr;
  61.         sf::Vector2i offset             = { 0, 0 };
  62. };
  63.  
  64. bool touchingMouse ( const sf::RectangleShape& shape )
  65. {
  66.     float height = shape.getLocalBounds().height;
  67.     float width  = shape.getLocalBounds().width;
  68.  
  69.     float mouseX = sf::Mouse::getPosition( window ).x;
  70.     float mouseY = sf::Mouse::getPosition( window ).y;
  71.  
  72.     float spriteX = shape.getPosition().x;
  73.     float spriteY = shape.getPosition().y;
  74.  
  75.     return  (   mouseX > spriteX &&
  76.                 mouseX < spriteX + width &&
  77.                 mouseY > spriteY &&
  78.                 mouseY < spriteY + height );
  79. }
  80.  
  81. int main()
  82. {
  83.     window.create( sf::VideoMode ( WIDTH, HEIGHT ), "Drag the square!", sf::Style::Close );
  84.  
  85.     window.setFramerateLimit( 60 );
  86.  
  87.     sf::RectangleShape shape;
  88.     shape.setSize( { 50, 50 } );
  89.     shape.setPosition( { 242, 124 } );
  90.  
  91.     Draggable_Sprite sprite;
  92.  
  93.     std::cout << sizeof ( window ) << std::endl;
  94.  
  95.  
  96.     while ( window.isOpen() )
  97.     {
  98.         window.clear();
  99.  
  100.         if ( touchingMouse( shape ) && sf::Mouse::isButtonPressed( sf::Mouse::Left ) && !sprite.hasSprite() )
  101.         {
  102.             sprite.setSprite( shape );
  103.             continue;
  104.         }
  105.  
  106.         sprite.update();
  107.  
  108.  
  109.  
  110.         window.draw( shape );
  111.  
  112.         window.display();
  113.  
  114.         checkForClose();
  115.     }
  116. }
  117.  
  118. void checkForClose()
  119. {
  120.     sf::Event e;
  121.     while ( window.pollEvent( e ) )
  122.     {
  123.         if ( e.type == sf::Event::Closed )
  124.         {
  125.             window.close();
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement