mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-12 22:17:26 +01:00
graphics: Move makeing vertices from drawTexture to textureQuads to avoid copying
This commit is contained in:
parent
6d2148f3ea
commit
1e9fb05161
@ -15,9 +15,11 @@
|
|||||||
package ebiten
|
package ebiten
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Deprecated (as of 1.1.0-alpha): Use ImageParts instead.
|
// Deprecated (as of 1.1.0-alpha): Use ImageParts instead.
|
||||||
@ -85,12 +87,33 @@ func (t *textureQuads) Len() int {
|
|||||||
return t.parts.Len()
|
return t.parts.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 int) {
|
func (t *textureQuads) SetVertices(vertices []int16) error {
|
||||||
return t.parts.Dst(i)
|
l := t.Len()
|
||||||
}
|
if len(vertices) < l*16 {
|
||||||
|
return fmt.Errorf("grphics: vertices size must be greater than %d but %d", l*16, len(vertices))
|
||||||
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 int) {
|
}
|
||||||
x0, y0, x1, y1 := t.parts.Src(i)
|
p := t.parts
|
||||||
w, h := t.width, t.height
|
w, h := t.width, t.height
|
||||||
return u(x0, w), v(y0, h), u(x1, w), v(y1, h)
|
for i := 0; i < l; i++ {
|
||||||
|
x0, y0, x1, y1 := p.Dst(i)
|
||||||
|
sx0, sy0, sx1, sy1 := p.Src(i)
|
||||||
|
u0, v0, u1, v1 := u(sx0, w), v(sy0, h), u(sx1, w), v(sy1, h)
|
||||||
|
vertices[16*i] = int16(x0)
|
||||||
|
vertices[16*i+1] = int16(y0)
|
||||||
|
vertices[16*i+2] = int16(u0)
|
||||||
|
vertices[16*i+3] = int16(v0)
|
||||||
|
vertices[16*i+4] = int16(x1)
|
||||||
|
vertices[16*i+5] = int16(y0)
|
||||||
|
vertices[16*i+6] = int16(u1)
|
||||||
|
vertices[16*i+7] = int16(v0)
|
||||||
|
vertices[16*i+8] = int16(x0)
|
||||||
|
vertices[16*i+9] = int16(y1)
|
||||||
|
vertices[16*i+10] = int16(u0)
|
||||||
|
vertices[16*i+11] = int16(v1)
|
||||||
|
vertices[16*i+12] = int16(x1)
|
||||||
|
vertices[16*i+13] = int16(y1)
|
||||||
|
vertices[16*i+14] = int16(u1)
|
||||||
|
vertices[16*i+15] = int16(v1)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -48,11 +48,10 @@ func drawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
|
|||||||
shadersInitialized = true
|
shadersInitialized = true
|
||||||
}
|
}
|
||||||
|
|
||||||
l := quads.Len()
|
if quads.Len() == 0 {
|
||||||
if l == 0 {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if quadsMaxNum < l {
|
if quadsMaxNum < quads.Len() {
|
||||||
return errors.New(fmt.Sprintf("len(quads) must be equal to or less than %d", quadsMaxNum))
|
return errors.New(fmt.Sprintf("len(quads) must be equal to or less than %d", quadsMaxNum))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,26 +65,8 @@ func drawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
|
|||||||
}
|
}
|
||||||
p.begin()
|
p.begin()
|
||||||
defer p.end()
|
defer p.end()
|
||||||
|
if err := quads.SetVertices(vertices); err != nil {
|
||||||
for i := 0; i < l; i++ {
|
return err
|
||||||
x0, y0, x1, y1 := quads.Vertex(i)
|
|
||||||
u0, v0, u1, v1 := quads.Texture(i)
|
|
||||||
vertices[16*i] = int16(x0)
|
|
||||||
vertices[16*i+1] = int16(y0)
|
|
||||||
vertices[16*i+2] = int16(u0)
|
|
||||||
vertices[16*i+3] = int16(v0)
|
|
||||||
vertices[16*i+4] = int16(x1)
|
|
||||||
vertices[16*i+5] = int16(y0)
|
|
||||||
vertices[16*i+6] = int16(u1)
|
|
||||||
vertices[16*i+7] = int16(v0)
|
|
||||||
vertices[16*i+8] = int16(x0)
|
|
||||||
vertices[16*i+9] = int16(y1)
|
|
||||||
vertices[16*i+10] = int16(u0)
|
|
||||||
vertices[16*i+11] = int16(v1)
|
|
||||||
vertices[16*i+12] = int16(x1)
|
|
||||||
vertices[16*i+13] = int16(y1)
|
|
||||||
vertices[16*i+14] = int16(u1)
|
|
||||||
vertices[16*i+15] = int16(v1)
|
|
||||||
}
|
}
|
||||||
c.BufferSubData(c.ArrayBuffer, vertices[:16*quads.Len()])
|
c.BufferSubData(c.ArrayBuffer, vertices[:16*quads.Len()])
|
||||||
c.DrawElements(c.Triangles, 6*quads.Len())
|
c.DrawElements(c.Triangles, 6*quads.Len())
|
||||||
|
@ -22,8 +22,7 @@ import (
|
|||||||
|
|
||||||
type TextureQuads interface {
|
type TextureQuads interface {
|
||||||
Len() int
|
Len() int
|
||||||
Vertex(i int) (x0, y0, x1, y1 int)
|
SetVertices(vertices []int16) error
|
||||||
Texture(i int) (u0, v0, u1, v1 int)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Lines interface {
|
type Lines interface {
|
||||||
|
Loading…
Reference in New Issue
Block a user