From b1062a80bfb5e9096b73caaca6c08b4690badf9a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 14 Dec 2013 05:34:27 +0900 Subject: [PATCH] Refactoring --- example/main.go | 8 ++++---- graphics/opengl/shader/draw_texture.go | 15 +++++++++------ graphics/opengl/shader/program.go | 18 ++++++++---------- graphics/opengl/shader/shader.go | 16 ++++++++-------- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/example/main.go b/example/main.go index 067ab8ab3..ef59847a9 100644 --- a/example/main.go +++ b/example/main.go @@ -96,19 +96,19 @@ func main() { } }() - frameTime := time.Duration(int64(time.Second) / 120) - tick := time.Tick(frameTime) for { ui.PollEvents() select { default: drawing <- graphics.NewLazyCanvas() canvas := <-drawing + window.Draw(func(actualCanvas graphics.Canvas) { canvas.Flush(actualCanvas) }) - // To avoid a busy loop, take a rest after drawing. - <-tick + after := time.After(time.Duration(int64(time.Second) / 120)) + ui.PollEvents() + <-after case <-quit: return } diff --git a/graphics/opengl/shader/draw_texture.go b/graphics/opengl/shader/draw_texture.go index c16e1a0a1..37f8fe50b 100644 --- a/graphics/opengl/shader/draw_texture.go +++ b/graphics/opengl/shader/draw_texture.go @@ -9,14 +9,17 @@ import ( "github.com/hajimehoshi/go-ebiten/graphics/matrix" "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" + "sync" "unsafe" ) +var once sync.Once + func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gtexture.Quad, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - if !initialized { + once.Do(func() { initialize() - } + }) if len(quads) == 0 { return @@ -28,14 +31,14 @@ func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gt defer C.glBindTexture(C.GL_TEXTURE_2D, 0) vertexAttrLocation := getAttributeLocation(shaderProgram, "vertex") - textureAttrLocation := getAttributeLocation(shaderProgram, "texture") + texCoordAttrLocation := getAttributeLocation(shaderProgram, "tex_coord") C.glEnableClientState(C.GL_VERTEX_ARRAY) C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY) C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation)) - C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation)) + C.glEnableVertexAttribArray(C.GLuint(texCoordAttrLocation)) defer func() { - C.glDisableVertexAttribArray(C.GLuint(textureAttrLocation)) + C.glDisableVertexAttribArray(C.GLuint(texCoordAttrLocation)) C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation)) C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY) C.glDisableClientState(C.GL_VERTEX_ARRAY) @@ -75,7 +78,7 @@ func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gt C.glVertexAttribPointer(C.GLuint(vertexAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE, 0, unsafe.Pointer(&vertices[0])) - C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2, + C.glVertexAttribPointer(C.GLuint(texCoordAttrLocation), 2, C.GL_FLOAT, C.GL_FALSE, 0, unsafe.Pointer(&texCoords[0])) C.glDrawElements(C.GL_TRIANGLES, C.GLsizei(len(indicies)), diff --git a/graphics/opengl/shader/program.go b/graphics/opengl/shader/program.go index c7a81533f..2890752cc 100644 --- a/graphics/opengl/shader/program.go +++ b/graphics/opengl/shader/program.go @@ -48,8 +48,6 @@ func (p *program) create() { } } -var initialized = false - func initialize() { for _, shader := range shaders { shader.compile() @@ -63,24 +61,24 @@ func initialize() { for _, program := range programs { program.create() } - - initialized = true } +type qualifierVariableType int + const ( - qualifierVariableTypeAttribute = iota + qualifierVariableTypeAttribute qualifierVariableType = iota qualifierVariableTypeUniform ) var ( - shaderLocationCache = map[int]map[string]C.GLint{ + shaderLocationCache = map[qualifierVariableType]map[string]C.GLint{ qualifierVariableTypeAttribute: map[string]C.GLint{}, qualifierVariableTypeUniform: map[string]C.GLint{}, } ) -func getLocation(program C.GLuint, name string, qualifierVariableType int) C.GLint { - if location, ok := shaderLocationCache[qualifierVariableType][name]; ok { +func getLocation(program C.GLuint, name string, qvType qualifierVariableType) C.GLint { + if location, ok := shaderLocationCache[qvType][name]; ok { return location } @@ -89,7 +87,7 @@ func getLocation(program C.GLuint, name string, qualifierVariableType int) C.GLi location := C.GLint(-1) - switch qualifierVariableType { + switch qvType { case qualifierVariableTypeAttribute: location = C.glGetAttribLocation(program, (*C.GLchar)(locationName)) case qualifierVariableTypeUniform: @@ -100,7 +98,7 @@ func getLocation(program C.GLuint, name string, qualifierVariableType int) C.GLi if location == -1 { panic("glGetUniformLocation failed") } - shaderLocationCache[qualifierVariableType][name] = location + shaderLocationCache[qvType][name] = location return location } diff --git a/graphics/opengl/shader/shader.go b/graphics/opengl/shader/shader.go index f51cd4d90..b477e6d76 100644 --- a/graphics/opengl/shader/shader.go +++ b/graphics/opengl/shader/shader.go @@ -29,14 +29,14 @@ var shaders = map[shaderId]*shader{ shaderVertex: &shader{ shaderType: C.GL_VERTEX_SHADER, source: ` -attribute vec2 vertex; -attribute vec2 texture; uniform mat4 projection_matrix; uniform mat4 modelview_matrix; -varying vec2 tex_coord; +attribute vec2 vertex; +attribute vec2 tex_coord; +varying vec2 vertex_out_tex_coord; void main(void) { - tex_coord = texture; + vertex_out_tex_coord = tex_coord; gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1); } `, @@ -45,10 +45,10 @@ void main(void) { shaderType: C.GL_FRAGMENT_SHADER, source: ` uniform sampler2D texture; -varying vec2 tex_coord; +varying vec2 vertex_out_tex_coord; void main(void) { - gl_FragColor = texture2D(texture, tex_coord); + gl_FragColor = texture2D(texture, vertex_out_tex_coord); } `, }, @@ -58,10 +58,10 @@ void main(void) { uniform sampler2D texture; uniform mat4 color_matrix; uniform vec4 color_matrix_translation; -varying vec2 tex_coord; +varying vec2 vertex_out_tex_coord; void main(void) { - vec4 color = texture2D(texture, tex_coord); + vec4 color = texture2D(texture, vertex_out_tex_coord); gl_FragColor = (color_matrix * color) + color_matrix_translation; } `,