diff --git a/internal/graphics/internal/shader/drawtexture.go b/internal/graphics/internal/shader/drawtexture.go index 1d229faa6..1fb5c348c 100644 --- a/internal/graphics/internal/shader/drawtexture.go +++ b/internal/graphics/internal/shader/drawtexture.go @@ -15,7 +15,6 @@ package shader import ( - "github.com/go-gl/gl" "github.com/hajimehoshi/ebiten/internal/opengl" ) @@ -42,7 +41,6 @@ type TextureQuads interface { var initialized = false // TODO: Use unsafe.SizeOf? -const uint16Size = 2 const float32Size = 4 func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error { @@ -62,8 +60,8 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4] // TODO: Check performance program := useProgramColorMatrix(glMatrix(projectionMatrix), geo, color) - gl.ActiveTexture(gl.TEXTURE0) - gl.Texture(texture).Bind(gl.TEXTURE_2D) + // TODO: Do we have to call gl.ActiveTexture(gl.TEXTURE0)? + texture.Bind() vertexAttrLocation := program.GetAttributeLocation("vertex") texCoordAttrLocation := program.GetAttributeLocation("tex_coord") @@ -89,9 +87,9 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix [4] x1, y1, u1, v1, ) } - gl.BufferSubData(gl.ARRAY_BUFFER, 0, float32Size*len(vertices), vertices) - gl.DrawElements(gl.TRIANGLES, 6*quads.Len(), gl.UNSIGNED_SHORT, uintptr(0)) + c.BufferSubData(c.ArrayBuffer, vertices) + c.DrawElements(6 * quads.Len()) - gl.Flush() + c.Flush() return nil } diff --git a/internal/graphics/internal/shader/program.go b/internal/graphics/internal/shader/program.go index 402c5231f..1221780ae 100644 --- a/internal/graphics/internal/shader/program.go +++ b/internal/graphics/internal/shader/program.go @@ -22,6 +22,7 @@ var programColorMatrix opengl.Program func initialize(c *opengl.Context) error { const size = 10000 + const uint16Size = 2 var err error shaders[shaderVertex].native, err = c.NewShader(c.VertexShader, shaders[shaderVertex].source) @@ -41,6 +42,9 @@ func initialize(c *opengl.Context) error { shaders[shaderColorMatrix].native, } programColorMatrix, err = c.NewProgram(shaders) + if err != nil { + return err + } const stride = 4 * 4 s := float32Size * stride * size @@ -57,7 +61,7 @@ func initialize(c *opengl.Context) error { } c.NewBuffer(c.ElementArrayBuffer, uint16Size*len(indices), indices, c.StaticDraw) - return err + return nil } var lastProgram opengl.Program = 0 diff --git a/internal/opengl/context.go b/internal/opengl/context.go index 0468e2abd..cd9e92296 100644 --- a/internal/opengl/context.go +++ b/internal/opengl/context.go @@ -39,6 +39,10 @@ func (t Texture) Pixels(width, height int) ([]uint8, error) { return pixels, nil } +func (t Texture) Bind() { + gl.Texture(t).Bind(gl.TEXTURE_2D) +} + func (t Texture) Delete() { gl.Texture(t).Delete() } @@ -220,3 +224,16 @@ func (c *Context) NewBuffer(bufferType BufferType, size int, ptr interface{}, bu gl.GenBuffer().Bind(gl.GLenum(bufferType)) gl.BufferData(gl.GLenum(bufferType), size, ptr, gl.GLenum(bufferUsageType)) } + +func (c *Context) BufferSubData(bufferType BufferType, data []float32) { + const float32Size = 4 + gl.BufferSubData(gl.GLenum(bufferType), 0, float32Size*len(data), data) +} + +func (c *Context) DrawElements(len int) { + gl.DrawElements(gl.TRIANGLES, len, gl.UNSIGNED_SHORT, uintptr(0)) +} + +func (c *Context) Flush() { + gl.Flush() +}