From a601d82511441d23d47449f64953babe6a7ffcf7 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Sun, 5 Feb 2023 09:54:43 +0100 Subject: [PATCH] draw cube util --- iso_utils.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++- isocache.go | 54 ++------------------------------------------- isorenderer.go | 34 +++++++++++++++++------------ 3 files changed, 80 insertions(+), 67 deletions(-) diff --git a/iso_utils.go b/iso_utils.go index 4074a2d..59145df 100644 --- a/iso_utils.go +++ b/iso_utils.go @@ -1,6 +1,11 @@ package maprenderer -import "math" +import ( + "image/color" + "math" + + "github.com/fogleman/gg" +) var sqrt3 = math.Sqrt(3) var sin30 = math.Sin(30 * math.Pi / 180) @@ -41,3 +46,55 @@ func GetImagePos(rel_pos, size *Pos, size_x, size_y int, cubesize float64) (floa return x_pos, y_pos } + +func DrawCube(dc *gg.Context, c *color.RGBA, size float64, offset_x, offset_y float64) { + size_x, size_y := GetIsoCubeSize(size) + + // center position + center_x := (size_x / 2) + offset_x + center_y := (size_y / 2) + offset_y + + // calculate ends + end_x := offset_x + size_x + end_y := offset_y + size_y + + // proportional size + sin30_proportional := sin30 * size + + // right side + dc.SetRGBA255(int(c.R), int(c.G), int(c.B), int(c.A)) + dc.MoveTo(center_x, center_y) + dc.LineTo(end_x, center_y-sin30_proportional) + dc.LineTo(end_x, end_y-sin30_proportional) + dc.LineTo(center_x, end_y) + dc.ClosePath() + dc.Fill() + + // left side + dc.SetRGBA255( + AdjustColorComponent(c.R, -20), + AdjustColorComponent(c.G, -20), + AdjustColorComponent(c.B, -20), + int(c.A), + ) + dc.MoveTo(center_x, center_y) + dc.LineTo(center_x, end_y) + dc.LineTo(offset_x, end_y-sin30_proportional) + dc.LineTo(offset_x, center_y-sin30_proportional) + dc.ClosePath() + dc.Fill() + + // top side + dc.SetRGBA255( + AdjustColorComponent(c.R, 20), + AdjustColorComponent(c.G, 20), + AdjustColorComponent(c.B, 20), + int(c.A), + ) + dc.MoveTo(center_x, center_y) + dc.LineTo(offset_x, center_y-sin30_proportional) + dc.LineTo(center_x, offset_y) + dc.LineTo(end_x, center_y-sin30_proportional) + dc.ClosePath() + dc.Fill() +} diff --git a/isocache.go b/isocache.go index 4f0e69b..e80f75c 100644 --- a/isocache.go +++ b/isocache.go @@ -4,7 +4,6 @@ import ( "fmt" "image" "image/color" - "math" "sync" "github.com/fogleman/gg" @@ -34,58 +33,9 @@ func (rc *IsoRenderCache) GetCachedIsoCubeImage(c *color.RGBA, size float64) ima rc.lock.RUnlock() if img == nil { - // create image size_x, size_y := GetIsoCubeSize(size) - // round up - size_x = math.Ceil(size_x) - size_y = math.Ceil(size_y) - - // center position - center_x := size_x / 2 - center_y := size_y / 2 - - // proportional size - sin30_proportional := sin30 * size - - dc := gg.NewContext(int(math.Ceil(size_x)), int(math.Ceil(size_y))) - - // right side - dc.SetRGBA255(int(c.R), int(c.G), int(c.B), int(c.A)) - dc.MoveTo(center_x, center_y) - dc.LineTo(size_x, center_y-sin30_proportional) - dc.LineTo(size_x, size_y-sin30_proportional) - dc.LineTo(center_x, size_y) - dc.ClosePath() - dc.Fill() - - // left side - dc.SetRGBA255( - AdjustColorComponent(c.R, -20), - AdjustColorComponent(c.G, -20), - AdjustColorComponent(c.B, -20), - int(c.A), - ) - dc.MoveTo(center_x, center_y) - dc.LineTo(center_x, size_y) - dc.LineTo(0, size_y-sin30_proportional) - dc.LineTo(0, center_y-sin30_proportional) - dc.ClosePath() - dc.Fill() - - // top side - dc.SetRGBA255( - AdjustColorComponent(c.R, 20), - AdjustColorComponent(c.G, 20), - AdjustColorComponent(c.B, 20), - int(c.A), - ) - dc.MoveTo(center_x, center_y) - dc.LineTo(0, center_y-sin30_proportional) - dc.LineTo(center_x, 0) - dc.LineTo(size_x, center_y-sin30_proportional) - dc.ClosePath() - dc.Fill() - + dc := gg.NewContext(int(size_x), int(size_y)) + DrawCube(dc, c, size, 0, 0) img = dc.Image() // cache for future use diff --git a/isorenderer.go b/isorenderer.go index 733b2b5..815308e 100644 --- a/isorenderer.go +++ b/isorenderer.go @@ -3,8 +3,9 @@ package maprenderer import ( "image" "image/color" - "image/draw" "sort" + + "github.com/fogleman/gg" ) func NewIsoRenderer(cr ColorResolver, na NodeAccessor, cubesize int) (*IsoRenderer, error) { @@ -78,29 +79,34 @@ func (r *IsoRenderer) Render(from, to *Pos) (image.Image, error) { return nodes[i].Order < nodes[j].Order }) - // prepare image - //dc := gg.NewContext(600, 600) //TODO - size := to.Subtract(from).Add(NewPos(1, 1, 1)) size_x, size_y := GetIsometricImageSize(size, r.cubesize) - img := image.NewRGBA(image.Rect(0, 0, size_x, size_y)) + + // prepare image + dc := gg.NewContext(size_x, size_y) for _, node := range nodes { rel_pos := node.Pos.Subtract(from) x, y := GetImagePos(rel_pos, size, size_x, size_y, r.cubesize) - cube_img := r.rc.GetCachedIsoCubeImage(node.RGBA, r.cubesize) - p1 := image.Point{X: int(x), Y: int(y)} - r := image.Rectangle{ - p1, p1.Add(cube_img.Bounds().Size()), - } + // uncached draw + DrawCube(dc, node.RGBA, r.cubesize, x, y) + + // cached draw + /* + cube_img := r.rc.GetCachedIsoCubeImage(node.RGBA, r.cubesize) + p1 := image.Point{X: int(x), Y: int(y)} + r := image.Rectangle{ + p1, p1.Add(cube_img.Bounds().Size()), + } - // NOTE: the native "draw.Draw" function doesn't work with transparency - draw.Draw(img, r, cube_img, image.Point{0, 0}, draw.Over) - //dc.DrawImage(cube_img, int(math.Floor(x)), int(math.Floor(y))) + // NOTE: the native "draw.Draw" function doesn't work with transparency + draw.Draw(img, r, cube_img, image.Point{0, 0}, draw.Over) + //dc.DrawImage(cube_img, int(math.Floor(x)), int(math.Floor(y))) + */ } - return img, nil + return dc.Image(), nil } func (r *IsoRenderer) searchNode(pos, direction, base_pos *Pos, bounds [2]*Pos) (*IsometricNode, error) {