mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2025-07-03 16:40:49 +02:00
Render blocks through a ring buffer which is a binary heap ordered by descending y coords. This establisches a stream of partial y ordered blocks to reduce the the over all overdraw. Speeds up rendering about 25%.
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
@ -24,6 +25,95 @@ type Renderer struct {
|
||||
Rejected int
|
||||
}
|
||||
|
||||
type YOrder struct {
|
||||
Renderer *Renderer
|
||||
blocks []*common.Block
|
||||
capacity int
|
||||
}
|
||||
|
||||
func NewYOrder(renderer *Renderer, capacity int) *YOrder {
|
||||
return &YOrder{
|
||||
Renderer: renderer,
|
||||
blocks: make([]*common.Block, 0, capacity),
|
||||
capacity: capacity}
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func copyData(data []byte) []byte {
|
||||
l := len(data)
|
||||
ndata := make([]byte, l, max(l, 8*1024))
|
||||
copy(ndata, data)
|
||||
return ndata
|
||||
}
|
||||
|
||||
func (yo *YOrder) RenderBlock(block *common.Block, nameIndex map[string]int32) (err error) {
|
||||
var nblock *common.Block
|
||||
if len(yo.blocks) == yo.capacity {
|
||||
oblock := yo.blocks[0]
|
||||
if oblock.Coord.Y < block.Coord.Y {
|
||||
// New one is above highest old. Directly render new.
|
||||
err = yo.Renderer.RenderBlock(block, nameIndex)
|
||||
return
|
||||
}
|
||||
// Render old one. Store copy of new in heap.
|
||||
heap.Pop(yo)
|
||||
err = yo.Renderer.RenderBlock(oblock, nameIndex)
|
||||
l := len(block.Data)
|
||||
if cap(oblock.Data) < l {
|
||||
oblock.Data = make([]byte, l, max(l, 8*1024))
|
||||
} else {
|
||||
oblock.Data = oblock.Data[0:l]
|
||||
}
|
||||
copy(oblock.Data, block.Data)
|
||||
oblock.Coord = block.Coord
|
||||
nblock = oblock
|
||||
} else {
|
||||
nblock = &common.Block{Coord: block.Coord, Data: copyData(block.Data)}
|
||||
}
|
||||
|
||||
heap.Push(yo, nblock)
|
||||
return
|
||||
}
|
||||
|
||||
func (yo *YOrder) Drain(nameIndex map[string]int32) (err error) {
|
||||
for len(yo.blocks) > 0 {
|
||||
if err = yo.Renderer.RenderBlock(heap.Pop(yo).(*common.Block), nameIndex); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (yo *YOrder) Len() int {
|
||||
return len(yo.blocks)
|
||||
}
|
||||
|
||||
func (yo *YOrder) Swap(i, j int) {
|
||||
yo.blocks[i], yo.blocks[j] = yo.blocks[j], yo.blocks[i]
|
||||
}
|
||||
|
||||
func (yo *YOrder) Less(i, j int) bool {
|
||||
// Reverse order intented.
|
||||
return yo.blocks[i].Coord.Y > yo.blocks[j].Coord.Y
|
||||
}
|
||||
|
||||
func (yo *YOrder) Push(x interface{}) {
|
||||
yo.blocks = append(yo.blocks, x.(*common.Block))
|
||||
}
|
||||
|
||||
func (yo *YOrder) Pop() (x interface{}) {
|
||||
l := len(yo.blocks)
|
||||
x = yo.blocks[l-1]
|
||||
yo.blocks = yo.blocks[0 : l-1]
|
||||
return x
|
||||
}
|
||||
|
||||
func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) {
|
||||
dim := width * height
|
||||
pixSize := dim * 16 * 16
|
||||
|
Reference in New Issue
Block a user