Skip to content

Commit a601d82

Browse files
draw cube util
1 parent c564e58 commit a601d82

File tree

3 files changed

+80
-67
lines changed

3 files changed

+80
-67
lines changed

iso_utils.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package maprenderer
22

3-
import "math"
3+
import (
4+
"image/color"
5+
"math"
6+
7+
"github.com/fogleman/gg"
8+
)
49

510
var sqrt3 = math.Sqrt(3)
611
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
4146

4247
return x_pos, y_pos
4348
}
49+
50+
func DrawCube(dc *gg.Context, c *color.RGBA, size float64, offset_x, offset_y float64) {
51+
size_x, size_y := GetIsoCubeSize(size)
52+
53+
// center position
54+
center_x := (size_x / 2) + offset_x
55+
center_y := (size_y / 2) + offset_y
56+
57+
// calculate ends
58+
end_x := offset_x + size_x
59+
end_y := offset_y + size_y
60+
61+
// proportional size
62+
sin30_proportional := sin30 * size
63+
64+
// right side
65+
dc.SetRGBA255(int(c.R), int(c.G), int(c.B), int(c.A))
66+
dc.MoveTo(center_x, center_y)
67+
dc.LineTo(end_x, center_y-sin30_proportional)
68+
dc.LineTo(end_x, end_y-sin30_proportional)
69+
dc.LineTo(center_x, end_y)
70+
dc.ClosePath()
71+
dc.Fill()
72+
73+
// left side
74+
dc.SetRGBA255(
75+
AdjustColorComponent(c.R, -20),
76+
AdjustColorComponent(c.G, -20),
77+
AdjustColorComponent(c.B, -20),
78+
int(c.A),
79+
)
80+
dc.MoveTo(center_x, center_y)
81+
dc.LineTo(center_x, end_y)
82+
dc.LineTo(offset_x, end_y-sin30_proportional)
83+
dc.LineTo(offset_x, center_y-sin30_proportional)
84+
dc.ClosePath()
85+
dc.Fill()
86+
87+
// top side
88+
dc.SetRGBA255(
89+
AdjustColorComponent(c.R, 20),
90+
AdjustColorComponent(c.G, 20),
91+
AdjustColorComponent(c.B, 20),
92+
int(c.A),
93+
)
94+
dc.MoveTo(center_x, center_y)
95+
dc.LineTo(offset_x, center_y-sin30_proportional)
96+
dc.LineTo(center_x, offset_y)
97+
dc.LineTo(end_x, center_y-sin30_proportional)
98+
dc.ClosePath()
99+
dc.Fill()
100+
}

isocache.go

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"image"
66
"image/color"
7-
"math"
87
"sync"
98

109
"github.com/fogleman/gg"
@@ -34,58 +33,9 @@ func (rc *IsoRenderCache) GetCachedIsoCubeImage(c *color.RGBA, size float64) ima
3433
rc.lock.RUnlock()
3534

3635
if img == nil {
37-
// create image
3836
size_x, size_y := GetIsoCubeSize(size)
39-
// round up
40-
size_x = math.Ceil(size_x)
41-
size_y = math.Ceil(size_y)
42-
43-
// center position
44-
center_x := size_x / 2
45-
center_y := size_y / 2
46-
47-
// proportional size
48-
sin30_proportional := sin30 * size
49-
50-
dc := gg.NewContext(int(math.Ceil(size_x)), int(math.Ceil(size_y)))
51-
52-
// right side
53-
dc.SetRGBA255(int(c.R), int(c.G), int(c.B), int(c.A))
54-
dc.MoveTo(center_x, center_y)
55-
dc.LineTo(size_x, center_y-sin30_proportional)
56-
dc.LineTo(size_x, size_y-sin30_proportional)
57-
dc.LineTo(center_x, size_y)
58-
dc.ClosePath()
59-
dc.Fill()
60-
61-
// left side
62-
dc.SetRGBA255(
63-
AdjustColorComponent(c.R, -20),
64-
AdjustColorComponent(c.G, -20),
65-
AdjustColorComponent(c.B, -20),
66-
int(c.A),
67-
)
68-
dc.MoveTo(center_x, center_y)
69-
dc.LineTo(center_x, size_y)
70-
dc.LineTo(0, size_y-sin30_proportional)
71-
dc.LineTo(0, center_y-sin30_proportional)
72-
dc.ClosePath()
73-
dc.Fill()
74-
75-
// top side
76-
dc.SetRGBA255(
77-
AdjustColorComponent(c.R, 20),
78-
AdjustColorComponent(c.G, 20),
79-
AdjustColorComponent(c.B, 20),
80-
int(c.A),
81-
)
82-
dc.MoveTo(center_x, center_y)
83-
dc.LineTo(0, center_y-sin30_proportional)
84-
dc.LineTo(center_x, 0)
85-
dc.LineTo(size_x, center_y-sin30_proportional)
86-
dc.ClosePath()
87-
dc.Fill()
88-
37+
dc := gg.NewContext(int(size_x), int(size_y))
38+
DrawCube(dc, c, size, 0, 0)
8939
img = dc.Image()
9040

9141
// cache for future use

isorenderer.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package maprenderer
33
import (
44
"image"
55
"image/color"
6-
"image/draw"
76
"sort"
7+
8+
"github.com/fogleman/gg"
89
)
910

1011
func NewIsoRenderer(cr ColorResolver, na NodeAccessor, cubesize int) (*IsoRenderer, error) {
@@ -78,29 +79,34 @@ func (r *IsoRenderer) Render(from, to *Pos) (image.Image, error) {
7879
return nodes[i].Order < nodes[j].Order
7980
})
8081

81-
// prepare image
82-
//dc := gg.NewContext(600, 600) //TODO
83-
8482
size := to.Subtract(from).Add(NewPos(1, 1, 1))
8583
size_x, size_y := GetIsometricImageSize(size, r.cubesize)
86-
img := image.NewRGBA(image.Rect(0, 0, size_x, size_y))
84+
85+
// prepare image
86+
dc := gg.NewContext(size_x, size_y)
8787

8888
for _, node := range nodes {
8989
rel_pos := node.Pos.Subtract(from)
9090
x, y := GetImagePos(rel_pos, size, size_x, size_y, r.cubesize)
9191

92-
cube_img := r.rc.GetCachedIsoCubeImage(node.RGBA, r.cubesize)
93-
p1 := image.Point{X: int(x), Y: int(y)}
94-
r := image.Rectangle{
95-
p1, p1.Add(cube_img.Bounds().Size()),
96-
}
92+
// uncached draw
93+
DrawCube(dc, node.RGBA, r.cubesize, x, y)
94+
95+
// cached draw
96+
/*
97+
cube_img := r.rc.GetCachedIsoCubeImage(node.RGBA, r.cubesize)
98+
p1 := image.Point{X: int(x), Y: int(y)}
99+
r := image.Rectangle{
100+
p1, p1.Add(cube_img.Bounds().Size()),
101+
}
97102
98-
// NOTE: the native "draw.Draw" function doesn't work with transparency
99-
draw.Draw(img, r, cube_img, image.Point{0, 0}, draw.Over)
100-
//dc.DrawImage(cube_img, int(math.Floor(x)), int(math.Floor(y)))
103+
// NOTE: the native "draw.Draw" function doesn't work with transparency
104+
draw.Draw(img, r, cube_img, image.Point{0, 0}, draw.Over)
105+
//dc.DrawImage(cube_img, int(math.Floor(x)), int(math.Floor(y)))
106+
*/
101107
}
102108

103-
return img, nil
109+
return dc.Image(), nil
104110
}
105111

106112
func (r *IsoRenderer) searchNode(pos, direction, base_pos *Pos, bounds [2]*Pos) (*IsometricNode, error) {

0 commit comments

Comments
 (0)