Avoid copying for performance

This commit is contained in:
Hajime Hoshi 2015-01-03 03:29:04 +09:00
parent 447bda7d68
commit 1117862d19
5 changed files with 17 additions and 10 deletions

View File

@ -41,7 +41,7 @@ func textWidth(str string) int {
}
func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) error {
parts := []ebiten.ImagePart{}
parts := make([]ebiten.ImagePart, 0, len(str))
locationX, locationY := 0, 0
for _, c := range str {

View File

@ -144,7 +144,16 @@ const fieldBlockNumX = 10
const fieldBlockNumY = 20
func drawBlocks(r *ebiten.Image, blocks [][]BlockType, x, y int, clr ebiten.ColorM) error {
parts := []ebiten.ImagePart{}
l := 0
for _, blockCol := range blocks {
for _, block := range blockCol {
if block == BlockTypeNone {
continue
}
l++
}
}
parts := make([]ebiten.ImagePart, 0, l)
for i, blockCol := range blocks {
for j, block := range blockCol {
if block == BlockTypeNone {

View File

@ -63,11 +63,9 @@ func (i *innerImage) drawImage(c *opengl.Context, img *innerImage, options *Draw
},
}
}
geo := options.GeoM
clr := options.ColorM
w, h := img.size()
quads := &textureQuads{parts, w, h}
return i.framebuffer.DrawTexture(c, img.texture, quads, &geo, &clr)
return i.framebuffer.DrawTexture(c, img.texture, quads, &options.GeoM, &options.ColorM)
}
func u(x float64, width int) float32 {

View File

@ -20,13 +20,13 @@ import (
"github.com/hajimehoshi/ebiten/internal/opengl"
)
func orthoProjectionMatrix(left, right, bottom, top int) [4][4]float64 {
func orthoProjectionMatrix(left, right, bottom, top int) *[4][4]float64 {
e11 := float64(2) / float64(right-left)
e22 := float64(2) / float64(top-bottom)
e14 := -1 * float64(right+left) / float64(right-left)
e24 := -1 * float64(top+bottom) / float64(top-bottom)
return [4][4]float64{
return &[4][4]float64{
{e11, 0, 0, e14},
{0, e22, 0, e24},
{0, 0, 1, 0},
@ -77,7 +77,7 @@ func (f *Framebuffer) setAsViewport(c *opengl.Context) error {
return c.SetViewport(f.native, width, height)
}
func (f *Framebuffer) projectionMatrix() [4][4]float64 {
func (f *Framebuffer) projectionMatrix() *[4][4]float64 {
width := internal.NextPowerOf2Int(f.width)
height := internal.NextPowerOf2Int(f.height)
m := orthoProjectionMatrix(0, width, 0, height)

View File

@ -18,7 +18,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/opengl"
)
func glMatrix(m [4][4]float64) [16]float32 {
func glMatrix(m *[4][4]float64) [16]float32 {
result := [16]float32{}
for j := 0; j < 4; j++ {
for i := 0; i < 4; i++ {
@ -43,7 +43,7 @@ var initialized = false
// TODO: Use unsafe.SizeOf?
const float32Size = 4
func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error {
func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error {
// TODO: Check len(quads) and gl.MAX_ELEMENTS_INDICES?
const stride = 4 * 4
if !initialized {