Skip to content

Commit

Permalink
draw cube util
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Feb 5, 2023
1 parent c564e58 commit a601d82
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 67 deletions.
59 changes: 58 additions & 1 deletion iso_utils.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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()
}
54 changes: 2 additions & 52 deletions isocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"image"
"image/color"
"math"
"sync"

"github.com/fogleman/gg"
Expand Down Expand Up @@ -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
Expand Down
34 changes: 20 additions & 14 deletions isorenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a601d82

Please sign in to comment.