Advertisement
Guest User

Untitled

a guest
Mar 8th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1.  
  2. double lerp(double y1,double y2,double mu){
  3.    return(y1*(1-mu)+y2*mu);
  4. }
  5.  
  6. sf::Color lerpColor(const sf::Color& from,const sf::Color& to,float pos, float size){
  7.     sf::Color result;
  8.     double mu = double(pos / size);
  9.     result.r   = (sf::Uint8)lerp(from.r, to.r,mu);
  10.     result.g   = (sf::Uint8)lerp(from.g, to.g,mu);
  11.     result.b   = (sf::Uint8)lerp(from.b, to.b,mu);
  12.     result.a   = (sf::Uint8)lerp(from.a, to.a,mu);
  13.     return result;
  14. }
  15.  
  16.  
  17. sf::VertexArray createFilledRoundedRectangle(const sf::Vector2f& pos, const sf::Vector2f& size, float radius, const sf::Color& top, const sf::Color& bottom, int points){
  18.  
  19.     sf::VertexArray vertex(sf::TrianglesFan, (points*4)+6);
  20.  
  21.     // open shape at center
  22.     vertex[0].position = sf::Vector2f(pos.x+(size.x/2),pos.y+(size.y/2));
  23.     vertex[0].color    = lerpColor(top,bottom,size.y/2,size.y);
  24.    
  25.     int indice = 1;
  26.    
  27.     // top left
  28.     float angle = 180.0f;
  29.     for(int i=0; i<points; ++i){
  30.        
  31.         float x = (pos.x + radius) + radius * cos(angle * 3.14159265359 / 180.f);
  32.         float y = (pos.y + radius) + radius * sin(angle * 3.14159265359 / 180.f);
  33.         angle += float(90.0f/points);
  34.         vertex[indice].position = sf::Vector2f(x,y);
  35.         vertex[indice].color    = lerpColor(top,bottom,y-pos.y,size.y);
  36.    
  37.         indice++;  
  38.     }
  39.  
  40.     vertex[indice].position = sf::Vector2f(pos.x+radius,pos.y);
  41.     vertex[indice].color    = lerpColor(top,bottom,0,size.y);
  42.     indice++;
  43.    
  44.     // top right
  45.     angle = 270.0f;
  46.     for(int i=0; i<points; ++i){
  47.         float x = (pos.x + (size.x - radius)) + radius * cos(angle * 3.14159265359 / 180);
  48.         float y = (pos.y + radius) + radius            * sin(angle * 3.14159265359 / 180);
  49.         angle += float(90/points);
  50.         vertex[indice].position = sf::Vector2f(x,y);
  51.         vertex[indice].color    = lerpColor(top,bottom,y-pos.y,size.y);
  52.         indice++;      
  53.     }
  54.    
  55.     vertex[indice].position = sf::Vector2f(pos.x+size.x,pos.y+radius);
  56.     vertex[indice].color    = lerpColor(top,bottom,radius,size.y);
  57.     indice++;
  58.    
  59.    
  60.     // bottom right
  61.     angle = 0.0f;
  62.     for(int i=0; i<points; ++i){
  63.         float x = (pos.x + (size.x - radius)) + radius * cos(angle * 3.14159265359 / 180);
  64.         float y = (pos.y + (size.y - radius)) + radius * sin(angle * 3.14159265359 / 180);
  65.         angle += float(90/points);
  66.         vertex[indice].position = sf::Vector2f(x,y);
  67.         vertex[indice].color    = lerpColor(top,bottom,y-pos.y,size.y);
  68.         indice++;      
  69.     }
  70.    
  71.     vertex[indice].position = sf::Vector2f(pos.x+size.x-radius,pos.y+size.y);
  72.     vertex[indice].color    = lerpColor(top,bottom,size.y,size.y);
  73.     indice++;
  74.    
  75.     // bottom left
  76.     angle = 90.0f;
  77.     for(int i=0; i<points; ++i){
  78.         float x = (pos.x + radius) + radius              * cos(angle * 3.14159265359 / 180);
  79.         float y = (pos.y + (size.y - radius)) + radius   * sin(angle * 3.14159265359 / 180);
  80.         angle += float(90/points);
  81.         vertex[indice].position = sf::Vector2f(x,y);
  82.         vertex[indice].color    = lerpColor(top,bottom,y-pos.y,size.y);
  83.         indice++;      
  84.     }
  85.    
  86.     vertex[indice].position = sf::Vector2f(pos.x,pos.y+size.y-radius);
  87.     vertex[indice].color    = lerpColor(top,bottom,size.y-radius,size.y);
  88.     indice++;
  89.    
  90.     // close shape
  91.     vertex[indice].position = sf::Vector2f(pos.x,pos.y+radius);
  92.     vertex[indice].color    = lerpColor(top,bottom,radius,size.y);
  93.     return vertex;
  94. }
  95.  
  96.  
  97.  
  98. sf::VertexArray createHollowRoundedRectangle(const sf::Vector2f& pos, const sf::Vector2f& size, float radius, const sf::Color& color, int points){
  99.  
  100.     sf::VertexArray vertex(sf::LinesStrip, (points*4)+6);
  101.    
  102.    
  103.     // open shape
  104.     vertex[0].position = sf::Vector2f(pos.x,pos.y+radius);
  105.     vertex[0].color    = color;
  106.    
  107.     int indice = 1;
  108.    
  109.     // top left
  110.     float angle = 180.0f;
  111.     for(int i=0; i<points; ++i){
  112.        
  113.         float x = (pos.x+radius) + radius * cos(angle * 3.14159265359 / 180.f);
  114.         float y = (pos.y+radius) + radius * sin(angle * 3.14159265359 / 180.f);
  115.         angle += float(90.0f/points);
  116.         vertex[indice].position = sf::Vector2f(x,y);
  117.         vertex[indice].color    = color;
  118.    
  119.         indice++;  
  120.     }
  121.  
  122.     vertex[indice].position = sf::Vector2f(pos.x+radius,pos.y);
  123.     vertex[indice].color    = color;
  124.     indice++;
  125.    
  126.     // top right
  127.     angle = 270.0f;
  128.     for(int i=0; i<points; ++i){
  129.         float x = (pos.x+(size.x - radius)) + radius * cos(angle * 3.14159265359 / 180);
  130.         float y = (pos.y+radius) + radius            * sin(angle * 3.14159265359 / 180);
  131.         angle += float(90/points);
  132.         vertex[indice].position = sf::Vector2f(x,y);
  133.         vertex[indice].color    = color;
  134.         indice++;      
  135.     }
  136.    
  137.     vertex[indice].position = sf::Vector2f(pos.x+size.x,pos.y+radius);
  138.     vertex[indice].color    = color;
  139.     indice++;
  140.    
  141.    
  142.     // bottom right
  143.     angle = 0.0f;
  144.     for(int i=0; i<points; ++i){
  145.         float x = (pos.x+(size.x - radius)) + radius * cos(angle * 3.14159265359 / 180);
  146.         float y = (pos.y+(size.y - radius)) + radius * sin(angle * 3.14159265359 / 180);
  147.         angle += float(90/points);
  148.         vertex[indice].position = sf::Vector2f(x,y);
  149.         vertex[indice].color    = color;
  150.         indice++;      
  151.     }
  152.    
  153.     vertex[indice].position = sf::Vector2f(pos.x+size.x-radius,pos.y+size.y);
  154.     vertex[indice].color    = color;
  155.     indice++;
  156.    
  157.     // bottom left
  158.     angle = 90.0f;
  159.     for(int i=0; i<points; ++i){
  160.         float x = (pos.x+radius) + radius            * cos(angle * 3.14159265359 / 180);
  161.         float y = (pos.y+(size.y - radius)) + radius * sin(angle * 3.14159265359 / 180);
  162.         angle += float(90/points);
  163.         vertex[indice].position = sf::Vector2f(x,y);
  164.         vertex[indice].color    = color;
  165.         indice++;      
  166.     }
  167.    
  168.     vertex[indice].position = sf::Vector2f(pos.x,pos.y+size.y-radius);
  169.     vertex[indice].color    = color;
  170.     indice++;
  171.    
  172.     // close shape
  173.     vertex[indice].position = sf::Vector2f(pos.x,pos.y+radius);
  174.     vertex[indice].color    = color;
  175.     return vertex;
  176. }
  177.  
  178.  
  179. sf::VertexArray createLine(const sf::Vector2f& from, const sf::Vector2f& to, const sf::Color& color){
  180.     sf::VertexArray vertex(sf::Lines, 2);
  181.    
  182.     vertex[0].position  = from;
  183.     vertex[0].color     = color;
  184.    
  185.     vertex[1].position  = to;
  186.     vertex[1].color     = color;
  187.    
  188.     return vertex;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement