Browse Source

aa round rect done

cuu 6 years ago
parent
commit
2b89cd1a3e
4 changed files with 79 additions and 6 deletions
  1. 52 3
      gogame/draw/draw.go
  2. 21 0
      gogame/rect/rect.go
  3. BIN
      test
  4. 6 3
      test.go

+ 52 - 3
gogame/draw/draw.go

@@ -1,15 +1,64 @@
 package draw
 
 import (
-//	"fmt"
+	"fmt"
 //	"math"
 	"github.com/veandco/go-sdl2/sdl"
+	"github.com/veandco/go-sdl2/gfx"
+
 	"../color"
-	
+	"../rect"
 )
 
-func AARoundRect() {
+
+func _aa_render_region(image *sdl.Renderer, _rect *sdl.Rect, col color.Color, rad int) {
+	corners := rect.Inflate(_rect,-2*rad-1, -2*rad-1)
+	fmt.Println(_rect, corners)
+	topleft := []int{ int(corners.X),int(corners.Y)}
+	topright := []int{int(corners.X+corners.W-1), int(corners.Y)}
+	bottomleft := []int{int(corners.X),  int(corners.Y+corners.H-1)}
+	bottomright := []int{int(corners.X+corners.W -1), int(corners.Y+corners.H-1)}
+
+	attributes :=[][]int{topleft, topright, bottomleft, bottomright }
+
+	r,g,b,a := col.RGBA()
+	
+	image.SetDrawColor( uint8(r),uint8(g),uint8(b),uint8(a) )
+
+	
+	for i:=0; i< len(attributes);i++ {
+		x,y := attributes[i][0],attributes[i][1]
+		
+		gfx.AACircleRGBA(image,int32(x),int32(y),int32(rad),uint8(r),uint8(g),uint8(b),uint8(a))
+		gfx.FilledCircleRGBA(image,int32(x),int32(y), int32(rad),uint8(r),uint8(g),uint8(b),uint8(a))
+	}
+
+
+	r1 := rect.Inflate(_rect,-2*rad,0)
+	r2 := rect.Inflate(_rect,0,-2*rad)
 	
+	image.FillRect( &r1 ) // main body except four circles in corners
+	image.FillRect( &r2 ) // fix gap between circles of up and down vertical
+}
+
+//alpha of color should be 255 
+func AARoundRect(surf *sdl.Surface,_rect *sdl.Rect,col color.Color,rad,border int, inside color.Color) {
+
+	image,_ := sdl.CreateSoftwareRenderer(surf)
+
+	/*
+	image.SetDrawColor(233,100,200,0)
+	image.DrawLine(10,20,100,200)
+  */
+	
+//	image.Clear()
+	_aa_render_region(image,_rect,col,rad)
+	if border > 0 {
+		rect.InflateIp(_rect,-2*border,-2*border)
+		_aa_render_region(image,_rect,inside,rad)
+	}
+
+	//image.Present()
 }
 
 func Point(surf *sdl.Surface, c color.Color, x,y int) {

+ 21 - 0
gogame/rect/rect.go

@@ -7,3 +7,24 @@ import (
 func Rect(top ,left, width,height int) sdl.Rect {
 	return sdl.Rect{int32(top),int32(left),int32(width),int32(height)}
 }
+
+func InflateIp(rect *sdl.Rect,  x,y int) {
+
+	rect.X -= int32(x/2)
+	rect.Y -= int32(y/2)
+	rect.W += int32(x)
+	rect.H += int32(y)
+	
+}
+
+func Inflate(rect *sdl.Rect,  x,y int)  sdl.Rect {
+
+	r := sdl.Rect{0,0,0,0}
+	
+	r.X = rect.X - int32(x/2)
+	r.Y = rect.Y - int32(y/2)
+	r.W = rect.W + int32(x)
+	r.H = rect.H + int32(y)
+
+	return r
+}

BIN
test


+ 6 - 3
test.go

@@ -24,11 +24,11 @@ func run() int {
 	
 	surface.Fill(screen, color.Color{0,0,0,0} ) 
 
-	rect := rect.Rect(0,10, 12, 10)
+	rect1 := rect.Rect(0,10, 12, 10)
 	
 	//surface.FillRect(screen,&rect, 0xffff0000)
-	rect.X = 12
-	draw.Rect(screen,color.Color{129,235,234,0},&rect,1)
+	rect1.X = 12
+	draw.Rect(screen,color.Color{129,235,234,0},&rect1,1)
 
 	fmt.Println(screen.Pitch)
 	fmt.Println( screen.BytesPerPixel() )
@@ -41,6 +41,9 @@ func run() int {
 	
 //	draw.Line(screen,color.Color{255,44,255,0}, 0,100, 320,100,3)
 //	draw.Line(screen,color.Color{255,44,255,0}, 10, 0, 10,250,4)
+
+	rect2 := rect.Rect(3,120,200,30)
+	draw.AARoundRect(screen,&rect2,color.Color{0,213,222,255},10,0, color.Color{0,213,222,255})
 	
 	display.Flip()