From f6547e1d69ed20afd9757e6e0db432eea3ea664e Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 11 Jan 2019 11:44:17 +0100 Subject: [PATCH] renderer --- colormapping/colormapping.go | 26 ++++++-------------- colormapping/colormapping_test.go | 2 +- mapblockrenderer/renderer.go | 17 +++++++++++-- mapblockrenderer/renderer_test.go | 41 +++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 mapblockrenderer/renderer_test.go diff --git a/colormapping/colormapping.go b/colormapping/colormapping.go index 56b6986..817dfcd 100644 --- a/colormapping/colormapping.go +++ b/colormapping/colormapping.go @@ -8,17 +8,14 @@ import ( "strings" "strconv" "github.com/sirupsen/logrus" + "image/color" ) -type Color struct { - R,G,B,A int -} - type ColorMapping struct { - colors map[string]*Color + colors map[string]*color.RGBA } -func (m *ColorMapping) GetColor(name string) *Color { +func (m *ColorMapping) GetColor(name string) *color.RGBA { return m.colors[name] } @@ -37,7 +34,6 @@ func (m *ColorMapping) LoadBytes(buffer []byte) error { continue } - c := Color{} parts := strings.Fields(txt) if len(parts) < 4 { @@ -60,22 +56,14 @@ func (m *ColorMapping) LoadBytes(buffer []byte) error { return err } - c.R = int(r) - c.G = int(g) - c.B = int(b) + c := color.RGBA{uint8(r),uint8(g),uint8(b), 0xFF} + m.colors[parts[0]] = &c } if len(parts) >= 5 { //with alpha - a, err := strconv.ParseInt(parts[4], 10, 32) - if err != nil { - return err - } - - c.A = int(a) } - m.colors[parts[0]] = &c } return nil @@ -96,6 +84,6 @@ func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) error { return m.LoadBytes(buffer) } -func CreateColorMapping() *ColorMapping { - return &ColorMapping{colors: make(map[string]*Color)} +func NewColorMapping() *ColorMapping { + return &ColorMapping{colors: make(map[string]*color.RGBA)} } diff --git a/colormapping/colormapping_test.go b/colormapping/colormapping_test.go index 4140bc7..a82222f 100644 --- a/colormapping/colormapping_test.go +++ b/colormapping/colormapping_test.go @@ -5,7 +5,7 @@ import ( ) func TestNewMapping(t *testing.T) { - m := CreateColorMapping() + m := NewColorMapping() err := m.LoadVFSColors(false, "/colors.txt") if err != nil { t.Fatal(err) diff --git a/mapblockrenderer/renderer.go b/mapblockrenderer/renderer.go index bd53497..1e30971 100644 --- a/mapblockrenderer/renderer.go +++ b/mapblockrenderer/renderer.go @@ -4,6 +4,8 @@ import ( "mapserver/colormapping" "mapserver/coords" "mapserver/mapblockaccessor" + "image" + "image/color" ) type MapBlockRenderer struct { @@ -15,6 +17,17 @@ func NewMapBlockRenderer(accessor *mapblockaccessor.MapBlockAccessor, colors *co return MapBlockRenderer{accessor: accessor, colors: colors} } -func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) { - //TODO +const ( + IMG_SCALE = 16 + IMG_SIZE = IMG_SCALE * 16 +) + +func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) *image.RGBA { + upLeft := image.Point{0, 0} + lowRight := image.Point{IMG_SIZE, IMG_SIZE} + + img := image.NewRGBA(image.Rectangle{upLeft, lowRight}) + cyan := color.RGBA{100, 200, 200, 0xff} + img.Set(10, 10, cyan) + return img } diff --git a/mapblockrenderer/renderer_test.go b/mapblockrenderer/renderer_test.go new file mode 100644 index 0000000..f2a505e --- /dev/null +++ b/mapblockrenderer/renderer_test.go @@ -0,0 +1,41 @@ +package mapblockrenderer + +import ( + "os" + "io/ioutil" + "mapserver/coords" + "testing" + "mapserver/testutils" + "mapserver/db" + "mapserver/colormapping" + "mapserver/mapblockaccessor" + "image/png" +) + +func TestSimpleRender(t *testing.T) { + tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite") + if err != nil { + panic(err) + } + defer os.Remove(tmpfile.Name()) + testutils.CreateTestDatabase(tmpfile.Name()) + + a, err := db.NewSqliteAccessor(tmpfile.Name()) + if err != nil { + panic(err) + } + + err = a.Migrate() + if err != nil { + panic(err) + } + + cache := mapblockaccessor.NewMapBlockAccessor(a) + c := colormapping.NewColorMapping() + + r := NewMapBlockRenderer(cache, c) + img := r.Render(coords.NewMapBlockCoords(0, 10, 0), coords.NewMapBlockCoords(0, -1, 0)) + + f, _ := os.Create("image.png") + png.Encode(f, img) +}