From f762154b0553ccf413294ed5a126dfa4c8775436 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Sun, 14 Sep 2014 14:57:49 +0200 Subject: [PATCH] Made tile renderer resetable to reduce the memory for render more than one tile. --- cmd/tilemapper/main.go | 3 ++- common/renderer.go | 34 ++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/cmd/tilemapper/main.go b/cmd/tilemapper/main.go index 227a807..fd977fa 100644 --- a/cmd/tilemapper/main.go +++ b/cmd/tilemapper/main.go @@ -70,7 +70,8 @@ func main() { q1x, q1y, q1z := int16(x), int16(y), int16(z) q2x, q2y, q2z := q1x+int16(width)-1, q1y+int16(depth)-1, q1z+int16(height)-1 - renderer := common.NewRenderer(q1x, q1z, width, height) + renderer := common.NewRenderer(width, height) + renderer.SetPos(q1x, q1z) yOrder := common.NewYOrder(renderer, 512) numBlocks := 0 diff --git a/common/renderer.go b/common/renderer.go index c60473e..34e9a68 100644 --- a/common/renderer.go +++ b/common/renderer.go @@ -40,6 +40,10 @@ func NewYOrder(renderer *Renderer, capacity int) *YOrder { capacity: capacity} } +func (yo *YOrder) Reset() { + yo.blocks = yo.blocks[0:0] +} + func max(a, b int) int { if a > b { return a @@ -116,33 +120,39 @@ func (yo *YOrder) Pop() (x interface{}) { return x } -func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) { +func NewRenderer(width, height int) (renderer *Renderer) { dim := width * height pixSize := dim * 16 * 16 yBuffer := make([]int32, pixSize) cBuffer := make([]int32, pixSize) yMin := make([]int32, dim) - for i := 0; i < pixSize; i++ { - yBuffer[i] = math.MinInt32 - cBuffer[i] = -1 - } - - for i := 0; i < dim; i++ { - yMin[i] = math.MinInt32 - } - renderer = &Renderer{ width: width, height: height, - xOfs: xOfs, - zOfs: zOfs, yBuffer: yBuffer, cBuffer: cBuffer, yMin: yMin} + + renderer.Reset() return } +func (r *Renderer) SetPos(xOfs, zOfs int16) { + r.xOfs = xOfs + r.zOfs = zOfs +} + +func (r *Renderer) Reset() { + for i, n := 0, len(r.yBuffer); i < n; i++ { + r.yBuffer[i] = math.MinInt32 + r.cBuffer[i] = -1 + } + for i, n := 0, len(r.yMin); i < n; i++ { + r.yMin[i] = math.MinInt32 + } +} + func (r *Renderer) IsFilled() bool { for _, y := range r.yMin { if y == math.MinInt32 {