mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 03:02:49 +01:00
Refactoring
This commit is contained in:
parent
5aa0b5f5c2
commit
b1062a80bf
@ -96,19 +96,19 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
frameTime := time.Duration(int64(time.Second) / 120)
|
|
||||||
tick := time.Tick(frameTime)
|
|
||||||
for {
|
for {
|
||||||
ui.PollEvents()
|
ui.PollEvents()
|
||||||
select {
|
select {
|
||||||
default:
|
default:
|
||||||
drawing <- graphics.NewLazyCanvas()
|
drawing <- graphics.NewLazyCanvas()
|
||||||
canvas := <-drawing
|
canvas := <-drawing
|
||||||
|
|
||||||
window.Draw(func(actualCanvas graphics.Canvas) {
|
window.Draw(func(actualCanvas graphics.Canvas) {
|
||||||
canvas.Flush(actualCanvas)
|
canvas.Flush(actualCanvas)
|
||||||
})
|
})
|
||||||
// To avoid a busy loop, take a rest after drawing.
|
after := time.After(time.Duration(int64(time.Second) / 120))
|
||||||
<-tick
|
ui.PollEvents()
|
||||||
|
<-after
|
||||||
case <-quit:
|
case <-quit:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -9,14 +9,17 @@ import (
|
|||||||
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
||||||
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
||||||
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
|
||||||
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var once sync.Once
|
||||||
|
|
||||||
func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gtexture.Quad,
|
func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gtexture.Quad,
|
||||||
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
||||||
if !initialized {
|
once.Do(func() {
|
||||||
initialize()
|
initialize()
|
||||||
}
|
})
|
||||||
|
|
||||||
if len(quads) == 0 {
|
if len(quads) == 0 {
|
||||||
return
|
return
|
||||||
@ -28,14 +31,14 @@ func DrawTexture(native texture.Native, projectionMatrix [16]float32, quads []gt
|
|||||||
defer C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
defer C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
||||||
|
|
||||||
vertexAttrLocation := getAttributeLocation(shaderProgram, "vertex")
|
vertexAttrLocation := getAttributeLocation(shaderProgram, "vertex")
|
||||||
textureAttrLocation := getAttributeLocation(shaderProgram, "texture")
|
texCoordAttrLocation := getAttributeLocation(shaderProgram, "tex_coord")
|
||||||
|
|
||||||
C.glEnableClientState(C.GL_VERTEX_ARRAY)
|
C.glEnableClientState(C.GL_VERTEX_ARRAY)
|
||||||
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
||||||
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
||||||
C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation))
|
C.glEnableVertexAttribArray(C.GLuint(texCoordAttrLocation))
|
||||||
defer func() {
|
defer func() {
|
||||||
C.glDisableVertexAttribArray(C.GLuint(textureAttrLocation))
|
C.glDisableVertexAttribArray(C.GLuint(texCoordAttrLocation))
|
||||||
C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
||||||
C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
||||||
C.glDisableClientState(C.GL_VERTEX_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.glVertexAttribPointer(C.GLuint(vertexAttrLocation), 2,
|
||||||
C.GL_FLOAT, C.GL_FALSE,
|
C.GL_FLOAT, C.GL_FALSE,
|
||||||
0, unsafe.Pointer(&vertices[0]))
|
0, unsafe.Pointer(&vertices[0]))
|
||||||
C.glVertexAttribPointer(C.GLuint(textureAttrLocation), 2,
|
C.glVertexAttribPointer(C.GLuint(texCoordAttrLocation), 2,
|
||||||
C.GL_FLOAT, C.GL_FALSE,
|
C.GL_FLOAT, C.GL_FALSE,
|
||||||
0, unsafe.Pointer(&texCoords[0]))
|
0, unsafe.Pointer(&texCoords[0]))
|
||||||
C.glDrawElements(C.GL_TRIANGLES, C.GLsizei(len(indicies)),
|
C.glDrawElements(C.GL_TRIANGLES, C.GLsizei(len(indicies)),
|
||||||
|
@ -48,8 +48,6 @@ func (p *program) create() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var initialized = false
|
|
||||||
|
|
||||||
func initialize() {
|
func initialize() {
|
||||||
for _, shader := range shaders {
|
for _, shader := range shaders {
|
||||||
shader.compile()
|
shader.compile()
|
||||||
@ -63,24 +61,24 @@ func initialize() {
|
|||||||
for _, program := range programs {
|
for _, program := range programs {
|
||||||
program.create()
|
program.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
initialized = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type qualifierVariableType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
qualifierVariableTypeAttribute = iota
|
qualifierVariableTypeAttribute qualifierVariableType = iota
|
||||||
qualifierVariableTypeUniform
|
qualifierVariableTypeUniform
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
shaderLocationCache = map[int]map[string]C.GLint{
|
shaderLocationCache = map[qualifierVariableType]map[string]C.GLint{
|
||||||
qualifierVariableTypeAttribute: map[string]C.GLint{},
|
qualifierVariableTypeAttribute: map[string]C.GLint{},
|
||||||
qualifierVariableTypeUniform: map[string]C.GLint{},
|
qualifierVariableTypeUniform: map[string]C.GLint{},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func getLocation(program C.GLuint, name string, qualifierVariableType int) C.GLint {
|
func getLocation(program C.GLuint, name string, qvType qualifierVariableType) C.GLint {
|
||||||
if location, ok := shaderLocationCache[qualifierVariableType][name]; ok {
|
if location, ok := shaderLocationCache[qvType][name]; ok {
|
||||||
return location
|
return location
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,7 +87,7 @@ func getLocation(program C.GLuint, name string, qualifierVariableType int) C.GLi
|
|||||||
|
|
||||||
location := C.GLint(-1)
|
location := C.GLint(-1)
|
||||||
|
|
||||||
switch qualifierVariableType {
|
switch qvType {
|
||||||
case qualifierVariableTypeAttribute:
|
case qualifierVariableTypeAttribute:
|
||||||
location = C.glGetAttribLocation(program, (*C.GLchar)(locationName))
|
location = C.glGetAttribLocation(program, (*C.GLchar)(locationName))
|
||||||
case qualifierVariableTypeUniform:
|
case qualifierVariableTypeUniform:
|
||||||
@ -100,7 +98,7 @@ func getLocation(program C.GLuint, name string, qualifierVariableType int) C.GLi
|
|||||||
if location == -1 {
|
if location == -1 {
|
||||||
panic("glGetUniformLocation failed")
|
panic("glGetUniformLocation failed")
|
||||||
}
|
}
|
||||||
shaderLocationCache[qualifierVariableType][name] = location
|
shaderLocationCache[qvType][name] = location
|
||||||
|
|
||||||
return location
|
return location
|
||||||
}
|
}
|
||||||
|
@ -29,14 +29,14 @@ var shaders = map[shaderId]*shader{
|
|||||||
shaderVertex: &shader{
|
shaderVertex: &shader{
|
||||||
shaderType: C.GL_VERTEX_SHADER,
|
shaderType: C.GL_VERTEX_SHADER,
|
||||||
source: `
|
source: `
|
||||||
attribute vec2 vertex;
|
|
||||||
attribute vec2 texture;
|
|
||||||
uniform mat4 projection_matrix;
|
uniform mat4 projection_matrix;
|
||||||
uniform mat4 modelview_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) {
|
void main(void) {
|
||||||
tex_coord = texture;
|
vertex_out_tex_coord = tex_coord;
|
||||||
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1);
|
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1);
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
@ -45,10 +45,10 @@ void main(void) {
|
|||||||
shaderType: C.GL_FRAGMENT_SHADER,
|
shaderType: C.GL_FRAGMENT_SHADER,
|
||||||
source: `
|
source: `
|
||||||
uniform sampler2D texture;
|
uniform sampler2D texture;
|
||||||
varying vec2 tex_coord;
|
varying vec2 vertex_out_tex_coord;
|
||||||
|
|
||||||
void main(void) {
|
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 sampler2D texture;
|
||||||
uniform mat4 color_matrix;
|
uniform mat4 color_matrix;
|
||||||
uniform vec4 color_matrix_translation;
|
uniform vec4 color_matrix_translation;
|
||||||
varying vec2 tex_coord;
|
varying vec2 vertex_out_tex_coord;
|
||||||
|
|
||||||
void main(void) {
|
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;
|
gl_FragColor = (color_matrix * color) + color_matrix_translation;
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
|
Loading…
Reference in New Issue
Block a user