diff --git a/main.go b/main.go index c0b0b17..6411863 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "flag" "fmt" - "image/png" + "time" "mapserver/colormapping" "mapserver/coords" "mapserver/db" @@ -12,8 +12,6 @@ import ( "mapserver/params" "mapserver/worldconfig" "os" - "time" - "github.com/sirupsen/logrus" ) @@ -21,27 +19,6 @@ const ( Version = "2.0-DEV" ) -type JobData struct { - pos1, pos2 coords.MapBlockCoords - x, z int -} - -func worker(r *mapblockrenderer.MapBlockRenderer, jobs <-chan JobData) { - for d := range jobs { - img, _ := r.Render(d.pos1, d.pos2) - - if img != nil { - f, _ := os.Create(fmt.Sprintf("output/image_%d_%d.png", d.x, d.z)) - start := time.Now() - png.Encode(f, img) - f.Close() - t := time.Now() - elapsed := t.Sub(start) - logrus.WithFields(logrus.Fields{"elapsed": elapsed}).Debug("Encoding completed") - } - } -} - func main() { logrus.SetLevel(logrus.InfoLevel) @@ -85,25 +62,53 @@ func main() { r := mapblockrenderer.NewMapBlockRenderer(cache, c) os.Mkdir("output", 0755) - jobs := make(chan JobData, 100) - go worker(&r, jobs) - go worker(&r, jobs) - go worker(&r, jobs) - go worker(&r, jobs) + results := make(chan mapblockrenderer.JobResult, 100) + jobs := make(chan mapblockrenderer.JobData, 100) - from := -500 - to := 500 + for i := 0; i<3; i++ { + go mapblockrenderer.Worker(&r, jobs, results) + } + + os.Mkdir("output", 0755) + + go func() { + for result := range results { + if result.Data.Len() == 0 { + continue + } + + tc := coords.GetTileCoordsFromMapBlock(result.Job.Pos1) + f, _ := os.Create(fmt.Sprintf("output/image_%d_%d.png", tc.X, tc.Y)) + result.Data.WriteTo(f) + f.Close() + } + }() + + from := coords.MinCoord + to := coords.MaxCoord logrus.WithFields(logrus.Fields{"from": from, "to": to}).Info("Starting rendering") + start := time.Now() + complete_count := (to - from) * (to - from) + current_count := 0 + for x := from; x < to; x++ { for z := from; z < to; z++ { pos1 := coords.NewMapBlockCoords(x, 10, z) pos2 := coords.NewMapBlockCoords(x, -1, z) - jobs <- JobData{pos1: pos1, pos2: pos2, x: x, z: z} + jobs <- mapblockrenderer.JobData{Pos1: pos1, Pos2: pos2} + current_count++ + + if time.Now().Sub(start).Seconds() > 2 { + start = time.Now() + progress := float64(current_count) / float64(complete_count) * 100 + logrus.WithFields(logrus.Fields{"x": x, "z": z, "progress%": progress}).Info("Render progress") + } } } close(jobs) + defer close(results) } diff --git a/mapblockrenderer/renderer_test.go b/mapblockrenderer/renderer_test.go index b35eafa..2326fdb 100644 --- a/mapblockrenderer/renderer_test.go +++ b/mapblockrenderer/renderer_test.go @@ -55,7 +55,9 @@ func TestSimpleRender(t *testing.T) { if result.Data.Len() == 0 { continue } - f, _ := os.Create(fmt.Sprintf("../output/image_%d_%d.png", result.Job.X, result.Job.Z)) + + tc := coords.GetTileCoordsFromMapBlock(result.Job.Pos1) + f, _ := os.Create(fmt.Sprintf("../output/image_%d_%d.png", tc.X, tc.Y)) result.Data.WriteTo(f) f.Close() } @@ -69,11 +71,11 @@ func TestSimpleRender(t *testing.T) { pos1 := coords.NewMapBlockCoords(x, 10, z) pos2 := coords.NewMapBlockCoords(x, -1, z) - jobs <- JobData{Pos1: pos1, Pos2: pos2, X: x, Z: z} + jobs <- JobData{Pos1: pos1, Pos2: pos2} } } close(jobs) - close(results) + defer close(results) } diff --git a/mapblockrenderer/worker.go b/mapblockrenderer/worker.go index c68d3b5..8059786 100644 --- a/mapblockrenderer/worker.go +++ b/mapblockrenderer/worker.go @@ -9,7 +9,6 @@ import ( type JobData struct { Pos1, Pos2 coords.MapBlockCoords - X, Z int } type JobResult struct {