mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
sprites: Speed up: use float64 as often as possible
This commit is contained in:
parent
3200ae58a4
commit
25cf2dab22
4
image.go
4
image.go
@ -108,9 +108,9 @@ func (l rectVertexQuads) Len() int {
|
||||
return l.Rects.Len()
|
||||
}
|
||||
|
||||
func (l rectVertexQuads) Vertex(i int) (x0, y0, x1, y1 float32) {
|
||||
func (l rectVertexQuads) Vertex(i int) (x0, y0, x1, y1 float64) {
|
||||
ix0, iy0, ix1, iy1 := l.Rects.Points(i)
|
||||
return float32(ix0), float32(iy0), float32(ix1), float32(iy1)
|
||||
return float64(ix0), float64(iy0), float64(ix1), float64(iy1)
|
||||
}
|
||||
|
||||
// DrawRects draws rectangles on the image.
|
||||
|
@ -66,12 +66,12 @@ func (w *wholeImage) Src(i int) (x0, y0, x1, y1 int) {
|
||||
return 0, 0, w.width, w.height
|
||||
}
|
||||
|
||||
func u(x float32, width int) float32 {
|
||||
return x / float32(internal.NextPowerOf2Int(width))
|
||||
func u(x float64, width int) float64 {
|
||||
return x / float64(internal.NextPowerOf2Int(width))
|
||||
}
|
||||
|
||||
func v(y float32, height int) float32 {
|
||||
return y / float32(internal.NextPowerOf2Int(height))
|
||||
func v(y float64, height int) float64 {
|
||||
return y / float64(internal.NextPowerOf2Int(height))
|
||||
}
|
||||
|
||||
type textureQuads struct {
|
||||
@ -84,13 +84,13 @@ func (t *textureQuads) Len() int {
|
||||
return t.parts.Len()
|
||||
}
|
||||
|
||||
func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float32) {
|
||||
func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float64) {
|
||||
ix0, iy0, ix1, iy1 := t.parts.Dst(i)
|
||||
return float32(ix0), float32(iy0), float32(ix1), float32(iy1)
|
||||
return float64(ix0), float64(iy0), float64(ix1), float64(iy1)
|
||||
}
|
||||
|
||||
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float32) {
|
||||
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float64) {
|
||||
x0, y0, x1, y1 := t.parts.Src(i)
|
||||
w, h := t.width, t.height
|
||||
return u(float32(x0), w), v(float32(y0), h), u(float32(x1), w), v(float32(y1), h)
|
||||
return u(float64(x0), w), v(float64(y0), h), u(float64(x1), w), v(float64(y1), h)
|
||||
}
|
||||
|
@ -94,8 +94,8 @@ type Matrix interface {
|
||||
|
||||
type TextureQuads interface {
|
||||
Len() int
|
||||
Vertex(i int) (x0, y0, x1, y1 float32)
|
||||
Texture(i int) (u0, v0, u1, v1 float32)
|
||||
Vertex(i int) (x0, y0, x1, y1 float64)
|
||||
Texture(i int) (u0, v0, u1, v1 float64)
|
||||
}
|
||||
|
||||
func (f *Framebuffer) Fill(c *opengl.Context, r, g, b, a float64) error {
|
||||
@ -115,7 +115,7 @@ func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQu
|
||||
|
||||
type VertexQuads interface {
|
||||
Len() int
|
||||
Vertex(i int) (x0, y0, x1, y1 float32)
|
||||
Vertex(i int) (x0, y0, x1, y1 float64)
|
||||
}
|
||||
|
||||
func (f *Framebuffer) DrawRects(c *opengl.Context, r, g, b, a float64, quads VertexQuads) error {
|
||||
|
@ -35,19 +35,19 @@ type Matrix interface {
|
||||
|
||||
type TextureQuads interface {
|
||||
Len() int
|
||||
Vertex(i int) (x0, y0, x1, y1 float32)
|
||||
Texture(i int) (u0, v0, u1, v1 float32)
|
||||
Vertex(i int) (x0, y0, x1, y1 float64)
|
||||
Texture(i int) (u0, v0, u1, v1 float64)
|
||||
}
|
||||
|
||||
// TODO: better name?
|
||||
const stride = 4 * 4
|
||||
|
||||
var initialized = false
|
||||
|
||||
func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error {
|
||||
// TODO: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far.
|
||||
// Let's use them to compare to len(quads) in the future.
|
||||
|
||||
// TODO: Unify stride or other consts
|
||||
const stride = 4 * 4
|
||||
|
||||
if !initialized {
|
||||
if err := initialize(c); err != nil {
|
||||
return err
|
||||
@ -73,10 +73,10 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
|
||||
continue
|
||||
}
|
||||
vertices = append(vertices,
|
||||
x0, y0, u0, v0,
|
||||
x1, y0, u1, v0,
|
||||
x0, y1, u0, v1,
|
||||
x1, y1, u1, v1,
|
||||
float32(x0), float32(y0), float32(u0), float32(v0),
|
||||
float32(x1), float32(y0), float32(u1), float32(v0),
|
||||
float32(x0), float32(y1), float32(u0), float32(v1),
|
||||
float32(x1), float32(y1), float32(u1), float32(v1),
|
||||
)
|
||||
}
|
||||
if len(vertices) == 0 {
|
||||
@ -89,7 +89,7 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
|
||||
|
||||
type VertexQuads interface {
|
||||
Len() int
|
||||
Vertex(i int) (x0, y0, x1, y1 float32)
|
||||
Vertex(i int) (x0, y0, x1, y1 float64)
|
||||
}
|
||||
|
||||
func max(a, b float32) float32 {
|
||||
@ -99,9 +99,9 @@ func max(a, b float32) float32 {
|
||||
return a
|
||||
}
|
||||
|
||||
func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, r, g, b, a float64, quads VertexQuads) error {
|
||||
const stride = 4 * 4
|
||||
var vertices = make([]float32, 0, stride*quadsMaxNum)
|
||||
|
||||
func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, r, g, b, a float64, quads VertexQuads) error {
|
||||
if !initialized {
|
||||
if err := initialize(c); err != nil {
|
||||
return err
|
||||
@ -116,17 +116,17 @@ func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, r, g, b, a fl
|
||||
f := useProgramRect(c, glMatrix(projectionMatrix), r, g, b, a)
|
||||
defer f.FinishProgram()
|
||||
|
||||
vertices := make([]float32, 0, stride*quads.Len())
|
||||
vertices := vertices[0:0]
|
||||
for i := 0; i < quads.Len(); i++ {
|
||||
x0, y0, x1, y1 := quads.Vertex(i)
|
||||
if x0 == x1 || y0 == y1 {
|
||||
continue
|
||||
}
|
||||
vertices = append(vertices,
|
||||
x0, y0, 0, 0,
|
||||
x1, y0, 1, 0,
|
||||
x0, y1, 0, 1,
|
||||
x1, y1, 1, 1,
|
||||
float32(x0), float32(y0), 0, 0,
|
||||
float32(x1), float32(y0), 1, 0,
|
||||
float32(x0), float32(y1), 0, 1,
|
||||
float32(x1), float32(y1), 1, 1,
|
||||
)
|
||||
}
|
||||
if len(vertices) == 0 {
|
||||
|
Loading…
Reference in New Issue
Block a user