Jump to content

Draw rounded rectangle with X


Josh
 Share

Recommended Posts

 void Window::DrawRect(int x, int y, int width, int height, bool outline, int radius)

{

if (width<=0 || height<=0) return;

if (outline)

{

if (radius==0i)

{

//Outline

XDrawRectangle(display, drawable, gc, x,y,width,height);

}

else

{

//Draw corners

XArc arcs[4];

arcs[0].x = x;

arcs[0].y = y;

arcs[0].width = radius*2;

arcs[0].height = radius*2;

arcs[0].angle1 = 90*64;

arcs[0].angle2 = 90*64;

arcs[1].x = x+width-radius*2;

arcs[1].y = y;

arcs[1].width = radius*2;

arcs[1].height = radius*2;

arcs[1].angle1 = 0*64;

arcs[1].angle2 = 90*64;

arcs[2].x = x+width-radius*2;

arcs[2].y = y+height-radius*2;

arcs[2].width = radius*2;

arcs[2].height = radius*2;

arcs[2].angle1 = 270*64;

arcs[2].angle2 = 90*64;

arcs[3].x = x;

arcs[3].y = y+height-radius*2;

arcs[3].width = radius*2;

arcs[3].height = radius*2;

arcs[3].angle1 = 180*64;

arcs[3].angle2 = 90*64;

XDrawArcs(display,drawable,gc,arcs,4);

 

//Draw edges

XSegment segments[4];

segments[0].x1 = x+radius;

segments[0].y1 = y;

segments[0].x2 = x+width-radius;

segments[0].y2 = y;

segments[1].x1 = x+radius;

segments[1].y1 = y+height;

segments[1].x2 = x+width-radius;

segments[1].y2 = y+height;

segments[2].x1 = x;

segments[2].y1 = y+radius;

segments[2].x2 = x;

segments[2].y2 = y+height-radius;

segments[3].x1 = x+width;

segments[3].y1 = y+radius;

segments[3].x2 = x+width;

segments[3].y2 = y+height-radius;

XDrawSegments(display,drawable,gc,segments,4);

}

}

else

{

if (radius==0i)

{

//Solid

XFillRectangle(display, drawable, gc, x,y,width,height);

}

else

{

//Draw corners

XArc arcs[4];

arcs[0].x = x;

arcs[0].y = y;

arcs[0].width = radius*2;

arcs[0].height = radius*2;

arcs[0].angle1 = 90*64;

arcs[0].angle2 = 90*64;

arcs[1].x = x+width-radius*2;

arcs[1].y = y;

arcs[1].width = radius*2;

arcs[1].height = radius*2;

arcs[1].angle1 = 0*64;

arcs[1].angle2 = 90*64;

arcs[2].x = x+width-radius*2;

arcs[2].y = y+height-radius*2;

arcs[2].width = radius*2;

arcs[2].height = radius*2;

arcs[2].angle1 = 270*64;

arcs[2].angle2 = 90*64;

arcs[3].x = x;

arcs[3].y = y+height-radius*2;

arcs[3].width = radius*2;

arcs[3].height = radius*2;

arcs[3].angle1 = 180*64;

arcs[3].angle2 = 90*64;

XFillArcs(display,drawable,gc,arcs,4);

 

//Draw inside

XRectangle rects[2];

rects[0].x = x + radius;

rects[0].y = y;

rects[0].width = width - radius * 2;

rects[0].height = height;

rects[1].x = x;

rects[1].y = y + radius;

rects[1].width = width;

rects[1].height = height - radius * 2;

XFillRectangles(display,drawable,gc,rects,2);

}

}

}

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Here is the GLSL equivalent (just for fun)

 

#version 400

uniform bool isbackbuffer;
uniform vec2 buffersize;

out vec4 fragData0;

float RoundRect(in float radius, in vec2 fragCoord)
{
   vec2 halfSize=vec2(buffersize/2.0);
   vec2 distFromCenter=fragCoord.xy - halfSize;
   return clamp(length(max(abs(distFromCenter) - (halfSize - radius), vec2(0.0))) - radius, 0.0, 1.0);

}

void main(void)
{
   vec2 tcoord = vec2(gl_FragCoord.xy);
   if (isbackbuffer) tcoord.y = buffersize.y - tcoord.y;

   float pct = RoundRect(100.,tcoord);    
   fragData0 = mix(vec4(1.0), vec4(0.0), pct);
}

  • Upvote 1

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...