Reduce init functions

This mitigates the init-order issue on jsgo.io.
This commit is contained in:
Hajime Hoshi 2018-12-26 01:51:04 +09:00
parent de788603d6
commit 99e4c874fe
5 changed files with 68 additions and 113 deletions

View File

@ -18,24 +18,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
) )
var (
vertexShader shaderType
fragmentShader shaderType
arrayBuffer bufferType
elementArrayBuffer bufferType
dynamicDraw bufferUsage
staticDraw bufferUsage
short dataType
float dataType
zero operation
one operation
srcAlpha operation
dstAlpha operation
oneMinusSrcAlpha operation
oneMinusDstAlpha operation
)
func convertOperation(op graphics.Operation) operation { func convertOperation(op graphics.Operation) operation {
switch op { switch op {
case graphics.Zero: case graphics.Zero:

View File

@ -54,22 +54,22 @@ func getProgramID(p program) programID {
return programID(p) return programID(p)
} }
func init() { const (
vertexShader = gl.VERTEX_SHADER vertexShader = shaderType(gl.VERTEX_SHADER)
fragmentShader = gl.FRAGMENT_SHADER fragmentShader = shaderType(gl.FRAGMENT_SHADER)
arrayBuffer = gl.ARRAY_BUFFER arrayBuffer = bufferType(gl.ARRAY_BUFFER)
elementArrayBuffer = gl.ELEMENT_ARRAY_BUFFER elementArrayBuffer = bufferType(gl.ELEMENT_ARRAY_BUFFER)
dynamicDraw = gl.DYNAMIC_DRAW dynamicDraw = bufferUsage(gl.DYNAMIC_DRAW)
short = gl.SHORT short = dataType(gl.SHORT)
float = gl.FLOAT float = dataType(gl.FLOAT)
zero = gl.ZERO zero = operation(gl.ZERO)
one = gl.ONE one = operation(gl.ONE)
srcAlpha = gl.SRC_ALPHA srcAlpha = operation(gl.SRC_ALPHA)
dstAlpha = gl.DST_ALPHA dstAlpha = operation(gl.DST_ALPHA)
oneMinusSrcAlpha = gl.ONE_MINUS_SRC_ALPHA oneMinusSrcAlpha = operation(gl.ONE_MINUS_SRC_ALPHA)
oneMinusDstAlpha = gl.ONE_MINUS_DST_ALPHA oneMinusDstAlpha = operation(gl.ONE_MINUS_DST_ALPHA)
} )
type contextImpl struct { type contextImpl struct {
init bool init bool

View File

@ -47,69 +47,46 @@ func getProgramID(p program) programID {
} }
var ( var (
blend js.Value
clampToEdge js.Value
colorAttachment0 js.Value
compileStatus js.Value
framebuffer_ js.Value
framebufferBinding js.Value
framebufferComplete js.Value
linkStatus js.Value
maxTextureSize js.Value
nearest js.Value
noError js.Value
texture2d js.Value
textureMagFilter js.Value
textureMinFilter js.Value
textureWrapS js.Value
textureWrapT js.Value
triangles js.Value
rgba js.Value
unpackAlignment js.Value
unsignedByte js.Value
unsignedShort js.Value
)
func init() {
// Accessing the prototype is rquired on Safari. // Accessing the prototype is rquired on Safari.
c := js.Global().Get("WebGLRenderingContext").Get("prototype") contextPrototype = js.Global().Get("WebGLRenderingContext").Get("prototype")
vertexShader = shaderType(c.Get("VERTEX_SHADER").Int())
fragmentShader = shaderType(c.Get("FRAGMENT_SHADER").Int())
arrayBuffer = bufferType(c.Get("ARRAY_BUFFER").Int())
elementArrayBuffer = bufferType(c.Get("ELEMENT_ARRAY_BUFFER").Int())
dynamicDraw = bufferUsage(c.Get("DYNAMIC_DRAW").Int())
short = dataType(c.Get("SHORT").Int())
float = dataType(c.Get("FLOAT").Int())
zero = operation(c.Get("ZERO").Int()) vertexShader = shaderType(contextPrototype.Get("VERTEX_SHADER").Int())
one = operation(c.Get("ONE").Int()) fragmentShader = shaderType(contextPrototype.Get("FRAGMENT_SHADER").Int())
srcAlpha = operation(c.Get("SRC_ALPHA").Int()) arrayBuffer = bufferType(contextPrototype.Get("ARRAY_BUFFER").Int())
dstAlpha = operation(c.Get("DST_ALPHA").Int()) elementArrayBuffer = bufferType(contextPrototype.Get("ELEMENT_ARRAY_BUFFER").Int())
oneMinusSrcAlpha = operation(c.Get("ONE_MINUS_SRC_ALPHA").Int()) dynamicDraw = bufferUsage(contextPrototype.Get("DYNAMIC_DRAW").Int())
oneMinusDstAlpha = operation(c.Get("ONE_MINUS_DST_ALPHA").Int()) short = dataType(contextPrototype.Get("SHORT").Int())
float = dataType(contextPrototype.Get("FLOAT").Int())
blend = c.Get("BLEND") zero = operation(contextPrototype.Get("ZERO").Int())
clampToEdge = c.Get("CLAMP_TO_EDGE") one = operation(contextPrototype.Get("ONE").Int())
compileStatus = c.Get("COMPILE_STATUS") srcAlpha = operation(contextPrototype.Get("SRC_ALPHA").Int())
colorAttachment0 = c.Get("COLOR_ATTACHMENT0") dstAlpha = operation(contextPrototype.Get("DST_ALPHA").Int())
framebuffer_ = c.Get("FRAMEBUFFER") oneMinusSrcAlpha = operation(contextPrototype.Get("ONE_MINUS_SRC_ALPHA").Int())
framebufferBinding = c.Get("FRAMEBUFFER_BINDING") oneMinusDstAlpha = operation(contextPrototype.Get("ONE_MINUS_DST_ALPHA").Int())
framebufferComplete = c.Get("FRAMEBUFFER_COMPLETE")
linkStatus = c.Get("LINK_STATUS") blend = contextPrototype.Get("BLEND")
maxTextureSize = c.Get("MAX_TEXTURE_SIZE") clampToEdge = contextPrototype.Get("CLAMP_TO_EDGE")
nearest = c.Get("NEAREST") compileStatus = contextPrototype.Get("COMPILE_STATUS")
noError = c.Get("NO_ERROR") colorAttachment0 = contextPrototype.Get("COLOR_ATTACHMENT0")
rgba = c.Get("RGBA") framebuffer_ = contextPrototype.Get("FRAMEBUFFER")
texture2d = c.Get("TEXTURE_2D") framebufferBinding = contextPrototype.Get("FRAMEBUFFER_BINDING")
textureMagFilter = c.Get("TEXTURE_MAG_FILTER") framebufferComplete = contextPrototype.Get("FRAMEBUFFER_COMPLETE")
textureMinFilter = c.Get("TEXTURE_MIN_FILTER") linkStatus = contextPrototype.Get("LINK_STATUS")
textureWrapS = c.Get("TEXTURE_WRAP_S") maxTextureSize = contextPrototype.Get("MAX_TEXTURE_SIZE")
textureWrapT = c.Get("TEXTURE_WRAP_T") nearest = contextPrototype.Get("NEAREST")
triangles = c.Get("TRIANGLES") noError = contextPrototype.Get("NO_ERROR")
unpackAlignment = c.Get("UNPACK_ALIGNMENT") rgba = contextPrototype.Get("RGBA")
unsignedByte = c.Get("UNSIGNED_BYTE") texture2d = contextPrototype.Get("TEXTURE_2D")
unsignedShort = c.Get("UNSIGNED_SHORT") textureMagFilter = contextPrototype.Get("TEXTURE_MAG_FILTER")
} textureMinFilter = contextPrototype.Get("TEXTURE_MIN_FILTER")
textureWrapS = contextPrototype.Get("TEXTURE_WRAP_S")
textureWrapT = contextPrototype.Get("TEXTURE_WRAP_T")
triangles = contextPrototype.Get("TRIANGLES")
unpackAlignment = contextPrototype.Get("UNPACK_ALIGNMENT")
unsignedByte = contextPrototype.Get("UNSIGNED_BYTE")
unsignedShort = contextPrototype.Get("UNSIGNED_SHORT")
)
type contextImpl struct { type contextImpl struct {
gl js.Value gl js.Value

View File

@ -51,22 +51,22 @@ func getProgramID(p program) programID {
return programID(p.Value) return programID(p.Value)
} }
func init() { const (
vertexShader = mgl.VERTEX_SHADER vertexShader = shaderType(mgl.VERTEX_SHADER)
fragmentShader = mgl.FRAGMENT_SHADER fragmentShader = shaderType(mgl.FRAGMENT_SHADER)
arrayBuffer = mgl.ARRAY_BUFFER arrayBuffer = bufferType(mgl.ARRAY_BUFFER)
elementArrayBuffer = mgl.ELEMENT_ARRAY_BUFFER elementArrayBuffer = bufferType(mgl.ELEMENT_ARRAY_BUFFER)
dynamicDraw = mgl.DYNAMIC_DRAW dynamicDraw = bufferUsage(mgl.DYNAMIC_DRAW)
short = mgl.SHORT short = dataType(mgl.SHORT)
float = mgl.FLOAT float = dataType(mgl.FLOAT)
zero = mgl.ZERO zero = operation(mgl.ZERO)
one = mgl.ONE one = operation(mgl.ONE)
srcAlpha = mgl.SRC_ALPHA srcAlpha = operation(mgl.SRC_ALPHA)
dstAlpha = mgl.DST_ALPHA dstAlpha = operation(mgl.DST_ALPHA)
oneMinusSrcAlpha = mgl.ONE_MINUS_SRC_ALPHA oneMinusSrcAlpha = operation(mgl.ONE_MINUS_SRC_ALPHA)
oneMinusDstAlpha = mgl.ONE_MINUS_DST_ALPHA oneMinusDstAlpha = operation(mgl.ONE_MINUS_DST_ALPHA)
} )
type contextImpl struct { type contextImpl struct {
gl mgl.Context gl mgl.Context

6
run.go
View File

@ -41,13 +41,9 @@ func CurrentFPS() float64 {
var ( var (
isDrawingSkipped = int32(0) isDrawingSkipped = int32(0)
currentMaxTPS = int32(0) currentMaxTPS = int32(DefaultTPS)
) )
func init() {
atomic.StoreInt32(&currentMaxTPS, DefaultTPS)
}
func setDrawingSkipped(skipped bool) { func setDrawingSkipped(skipped bool) {
v := int32(0) v := int32(0)
if skipped { if skipped {