diff --git a/gogame/draw/draw.go b/gogame/draw/draw.go index d4b1514..c5cc592 100644 --- a/gogame/draw/draw.go +++ b/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) { diff --git a/gogame/rect/rect.go b/gogame/rect/rect.go index 5b7c02b..51fb6b8 100644 --- a/gogame/rect/rect.go +++ b/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 +} diff --git a/test b/test index 5184a7c..4dd86d8 100755 Binary files a/test and b/test differ diff --git a/test.go b/test.go index c758c0f..4c9535d 100644 --- a/test.go +++ b/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()