Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double lerp(double y1,double y2,double mu){
- return(y1*(1-mu)+y2*mu);
- }
- sf::Color lerpColor(const sf::Color& from,const sf::Color& to,float pos, float size){
- sf::Color result;
- double mu = double(pos / size);
- result.r = (sf::Uint8)lerp(from.r, to.r,mu);
- result.g = (sf::Uint8)lerp(from.g, to.g,mu);
- result.b = (sf::Uint8)lerp(from.b, to.b,mu);
- result.a = (sf::Uint8)lerp(from.a, to.a,mu);
- return result;
- }
- sf::VertexArray createFilledRoundedRectangle(const sf::Vector2f& pos, const sf::Vector2f& size, float radius, const sf::Color& top, const sf::Color& bottom, int points){
- sf::VertexArray vertex(sf::TrianglesFan, (points*4)+6);
- // open shape at center
- vertex[0].position = sf::Vector2f(pos.x+(size.x/2),pos.y+(size.y/2));
- vertex[0].color = lerpColor(top,bottom,size.y/2,size.y);
- int indice = 1;
- // top left
- float angle = 180.0f;
- for(int i=0; i<points; ++i){
- float x = (pos.x + radius) + radius * cos(angle * 3.14159265359 / 180.f);
- float y = (pos.y + radius) + radius * sin(angle * 3.14159265359 / 180.f);
- angle += float(90.0f/points);
- vertex[indice].position = sf::Vector2f(x,y);
- vertex[indice].color = lerpColor(top,bottom,y-pos.y,size.y);
- indice++;
- }
- vertex[indice].position = sf::Vector2f(pos.x+radius,pos.y);
- vertex[indice].color = lerpColor(top,bottom,0,size.y);
- indice++;
- // top right
- angle = 270.0f;
- for(int i=0; i<points; ++i){
- float x = (pos.x + (size.x - radius)) + radius * cos(angle * 3.14159265359 / 180);
- float y = (pos.y + radius) + radius * sin(angle * 3.14159265359 / 180);
- angle += float(90/points);
- vertex[indice].position = sf::Vector2f(x,y);
- vertex[indice].color = lerpColor(top,bottom,y-pos.y,size.y);
- indice++;
- }
- vertex[indice].position = sf::Vector2f(pos.x+size.x,pos.y+radius);
- vertex[indice].color = lerpColor(top,bottom,radius,size.y);
- indice++;
- // bottom right
- angle = 0.0f;
- for(int i=0; i<points; ++i){
- float x = (pos.x + (size.x - radius)) + radius * cos(angle * 3.14159265359 / 180);
- float y = (pos.y + (size.y - radius)) + radius * sin(angle * 3.14159265359 / 180);
- angle += float(90/points);
- vertex[indice].position = sf::Vector2f(x,y);
- vertex[indice].color = lerpColor(top,bottom,y-pos.y,size.y);
- indice++;
- }
- vertex[indice].position = sf::Vector2f(pos.x+size.x-radius,pos.y+size.y);
- vertex[indice].color = lerpColor(top,bottom,size.y,size.y);
- indice++;
- // bottom left
- angle = 90.0f;
- for(int i=0; i<points; ++i){
- float x = (pos.x + radius) + radius * cos(angle * 3.14159265359 / 180);
- float y = (pos.y + (size.y - radius)) + radius * sin(angle * 3.14159265359 / 180);
- angle += float(90/points);
- vertex[indice].position = sf::Vector2f(x,y);
- vertex[indice].color = lerpColor(top,bottom,y-pos.y,size.y);
- indice++;
- }
- vertex[indice].position = sf::Vector2f(pos.x,pos.y+size.y-radius);
- vertex[indice].color = lerpColor(top,bottom,size.y-radius,size.y);
- indice++;
- // close shape
- vertex[indice].position = sf::Vector2f(pos.x,pos.y+radius);
- vertex[indice].color = lerpColor(top,bottom,radius,size.y);
- return vertex;
- }
- sf::VertexArray createHollowRoundedRectangle(const sf::Vector2f& pos, const sf::Vector2f& size, float radius, const sf::Color& color, int points){
- sf::VertexArray vertex(sf::LinesStrip, (points*4)+6);
- // open shape
- vertex[0].position = sf::Vector2f(pos.x,pos.y+radius);
- vertex[0].color = color;
- int indice = 1;
- // top left
- float angle = 180.0f;
- for(int i=0; i<points; ++i){
- float x = (pos.x+radius) + radius * cos(angle * 3.14159265359 / 180.f);
- float y = (pos.y+radius) + radius * sin(angle * 3.14159265359 / 180.f);
- angle += float(90.0f/points);
- vertex[indice].position = sf::Vector2f(x,y);
- vertex[indice].color = color;
- indice++;
- }
- vertex[indice].position = sf::Vector2f(pos.x+radius,pos.y);
- vertex[indice].color = color;
- indice++;
- // top right
- angle = 270.0f;
- for(int i=0; i<points; ++i){
- float x = (pos.x+(size.x - radius)) + radius * cos(angle * 3.14159265359 / 180);
- float y = (pos.y+radius) + radius * sin(angle * 3.14159265359 / 180);
- angle += float(90/points);
- vertex[indice].position = sf::Vector2f(x,y);
- vertex[indice].color = color;
- indice++;
- }
- vertex[indice].position = sf::Vector2f(pos.x+size.x,pos.y+radius);
- vertex[indice].color = color;
- indice++;
- // bottom right
- angle = 0.0f;
- for(int i=0; i<points; ++i){
- float x = (pos.x+(size.x - radius)) + radius * cos(angle * 3.14159265359 / 180);
- float y = (pos.y+(size.y - radius)) + radius * sin(angle * 3.14159265359 / 180);
- angle += float(90/points);
- vertex[indice].position = sf::Vector2f(x,y);
- vertex[indice].color = color;
- indice++;
- }
- vertex[indice].position = sf::Vector2f(pos.x+size.x-radius,pos.y+size.y);
- vertex[indice].color = color;
- indice++;
- // bottom left
- angle = 90.0f;
- for(int i=0; i<points; ++i){
- float x = (pos.x+radius) + radius * cos(angle * 3.14159265359 / 180);
- float y = (pos.y+(size.y - radius)) + radius * sin(angle * 3.14159265359 / 180);
- angle += float(90/points);
- vertex[indice].position = sf::Vector2f(x,y);
- vertex[indice].color = color;
- indice++;
- }
- vertex[indice].position = sf::Vector2f(pos.x,pos.y+size.y-radius);
- vertex[indice].color = color;
- indice++;
- // close shape
- vertex[indice].position = sf::Vector2f(pos.x,pos.y+radius);
- vertex[indice].color = color;
- return vertex;
- }
- sf::VertexArray createLine(const sf::Vector2f& from, const sf::Vector2f& to, const sf::Color& color){
- sf::VertexArray vertex(sf::Lines, 2);
- vertex[0].position = from;
- vertex[0].color = color;
- vertex[1].position = to;
- vertex[1].color = color;
- return vertex;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement