remove TrianglesContainer + add Append to Triangles

This commit is contained in:
faiface 2017-01-11 23:17:42 +01:00
parent b4c035e2ff
commit 8ec1bdfadf
3 changed files with 39 additions and 28 deletions

View File

@ -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 {

View File

@ -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.

View File

@ -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) {
}
}
}
}
// submit data to vertex slice
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]
}
}
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()))