From 8ec1bdfadf56ac681c37e97b619328da96205bf0 Mon Sep 17 00:00:00 2001
From: faiface <faiface@ksp.sk>
Date: Wed, 11 Jan 2017 23:17:42 +0100
Subject: [PATCH] remove TrianglesContainer + add Append to Triangles

---
 graphics.go  |  9 ---------
 interface.go |  5 +++++
 window.go    | 53 +++++++++++++++++++++++++++++++++-------------------
 3 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/graphics.go b/graphics.go
index 9e7de07..6dc1108 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 0c1e2e8..9dc400e 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 28434bb..9d2dfad 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()))
-- 
GitLab