Refactoring

This commit is contained in:
Hajime Hoshi 2013-12-14 05:34:27 +09:00
parent 5aa0b5f5c2
commit b1062a80bf
4 changed files with 29 additions and 28 deletions

View File

@ -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
}

View File

@ -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)),

View File

@ -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
}

View File

@ -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;
}
`,