diff --git a/graphics.go b/graphics.go index 9e7de07c2d3ea87533671d13fbf9fd1ae0e25111..6dc110886666f634d6d37b1add224b758029284b 100644 --- a/graphics.go +++ b/graphics.go @@ -5,15 +5,6 @@ import ( "image/color" ) -// TrianglesContainer is an extension of Triangles that can accumulate Triangles inside of it using -// the Append method. -type TrianglesContainer interface { - Triangles - - // Append adds supplied Triangles to the end of the TrianglesContainer. - Append(Triangles) -} - // TrianglesData specifies a list of Triangles vertices with three common properties: Position, // Color and Texture. type TrianglesData []struct { diff --git a/interface.go b/interface.go index 0c1e2e846251e0b69c610b1e64e274b18058085d..9dc400ec530fea2ae3b9746ac2c52fe500bf6f2d 100644 --- a/interface.go +++ b/interface.go @@ -44,6 +44,11 @@ type Triangles interface { // If these Triangles and supplied Triangles have different lengths, these Triangles should // be resized. Update(Triangles) + + // Append adds supplied Triangles to the end of these Triangles. + // + // Behavior regarding unsupported properties should be same as with Update. + Append(Triangles) } // Drawer is something that can be drawn onto any Target. diff --git a/window.go b/window.go index 28434bbb40dcfccac9e8f5a3a65ea56ac2c2c8a2..9d2dfad6aa2ff451ee374496b858571040b1b398 100644 --- a/window.go +++ b/window.go @@ -395,23 +395,9 @@ func (wt *windowTriangles) Draw() { }) } -func (wt *windowTriangles) Update(t Triangles) { - if t.Len() > wt.Len() { - newData := make([]pixelgl.VertexData, t.Len()-wt.Len()) - // default values - for i := range newData { - newData[i] = make(pixelgl.VertexData) - newData[i][colorVec4] = mgl32.Vec4{1, 1, 1, 1} - newData[i][textureVec2] = mgl32.Vec2{-1, -1} - } - wt.data = append(wt.data, newData...) - } - if t.Len() < wt.Len() { - wt.data = wt.data[:t.Len()] - } - +func (wt *windowTriangles) updateData(offset int, t Triangles) { if t, ok := t.(TrianglesPosition); ok { - for i := range wt.data { + for i := offset; i < offset+t.Len(); i++ { pos := t.Position(i) wt.data[i][positionVec2] = mgl32.Vec2{ float32(pos.X()), @@ -420,7 +406,7 @@ func (wt *windowTriangles) Update(t Triangles) { } } if t, ok := t.(TrianglesColor); ok { - for i := range wt.data { + for i := offset; i < offset+t.Len(); i++ { col := NRGBAModel.Convert(t.Color(i)).(NRGBA) wt.data[i][colorVec4] = mgl32.Vec4{ float32(col.R), @@ -431,7 +417,7 @@ func (wt *windowTriangles) Update(t Triangles) { } } if t, ok := t.(TrianglesTexture); ok { - for i := range wt.data { + for i := offset; i < offset+t.Len(); i++ { tex := t.Texture(i) wt.data[i][textureVec2] = mgl32.Vec2{ float32(tex.X()), @@ -439,8 +425,25 @@ func (wt *windowTriangles) Update(t Triangles) { } } } +} + +func (wt *windowTriangles) resize(len int) { + if len > wt.Len() { + newData := make([]pixelgl.VertexData, len-wt.Len()) + // default values + for i := range newData { + newData[i] = make(pixelgl.VertexData) + newData[i][colorVec4] = mgl32.Vec4{1, 1, 1, 1} + newData[i][textureVec2] = mgl32.Vec2{-1, -1} + } + wt.data = append(wt.data, newData...) + } + if len < wt.Len() { + wt.data = wt.data[:len] + } +} - // submit data to vertex slice +func (wt *windowTriangles) submitData() { data := wt.data // avoid race condition pixelgl.DoNoBlock(func() { wt.vs.Begin() @@ -455,6 +458,18 @@ func (wt *windowTriangles) Update(t Triangles) { }) } +func (wt *windowTriangles) Update(t Triangles) { + wt.resize(t.Len()) + wt.updateData(0, t) + wt.submitData() +} + +func (wt *windowTriangles) Append(t Triangles) { + wt.resize(wt.Len() + t.Len()) + wt.updateData(wt.Len()-t.Len(), t) + wt.submitData() +} + func (wt *windowTriangles) Position(i int) Vec { v := wt.data[i][positionVec2].(mgl32.Vec2) return V(float64(v.X()), float64(v.Y()))