graphics: Add InitializeIfNeeded function (preparing for Android)

This commit is contained in:
Hajime Hoshi 2016-05-15 04:05:57 +09:00
parent ebe6296222
commit 9f2b53d24c
4 changed files with 38 additions and 13 deletions

View File

@ -33,21 +33,11 @@ type Matrix interface {
Element(i, j int) float64
}
var shadersInitialized = false
func drawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4][4]float64, vertices []int16, geo Matrix, color Matrix, mode opengl.CompositeMode) error {
c.BlendFunc(mode)
// NOTE: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far.
// Let's use them to compare to len(quads) in the future.
if !shadersInitialized {
if err := theOpenGLState.initialize(c); err != nil {
return err
}
shadersInitialized = true
}
n := len(vertices) / 16
if n == 0 {
return nil

View File

@ -21,6 +21,7 @@ import (
)
type openGLState struct {
initialized bool
indexBufferLines opengl.Buffer
indexBufferQuads opengl.Buffer
programTexture opengl.Program
@ -43,7 +44,21 @@ const (
float32Size = 4
)
func (s *openGLState) initialize(c *opengl.Context) error {
func InitializeIfNeeded(c *opengl.Context) error {
return theOpenGLState.initializeIfNeeded(c)
}
func (s *openGLState) initializeIfNeeded(c *opengl.Context) error {
if s.initialized {
return nil
}
var zeroProgram opengl.Program
s.lastProgram = zeroProgram
s.lastProjectionMatrix = nil
s.lastModelviewMatrix = nil
s.lastColorMatrix = nil
shaderVertexModelviewNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertexModelview))
if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
@ -85,6 +100,7 @@ func (s *openGLState) initialize(c *opengl.Context) error {
}
s.indexBufferLines = c.NewBuffer(c.ElementArrayBuffer, indices, c.StaticDraw)
s.initialized = true
return nil
}

View File

@ -22,6 +22,7 @@ import (
"time"
"github.com/go-gl/glfw/v3.1/glfw"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
@ -87,6 +88,9 @@ func Init() (*opengl.Context, error) {
if err := u.context.Init(); err != nil {
return nil, err
}
if err := graphics.InitializeIfNeeded(u.context); err != nil {
return nil, err
}
return u.context, nil
}

View File

@ -21,6 +21,7 @@ import (
"time"
"github.com/gopherjs/gopherjs/js"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
@ -105,7 +106,14 @@ func (u *UserInterface) SwapBuffers() {
func Init() (*opengl.Context, error) {
// Do nothing in node.js.
if js.Global.Get("require") != js.Undefined {
return opengl.NewContext()
c, err := opengl.NewContext()
if err != nil {
return nil, err
}
if err := graphics.InitializeIfNeeded(c); err != nil {
return nil, err
}
return c, nil
}
doc := js.Global.Get("document")
@ -208,7 +216,14 @@ func Init() (*opengl.Context, error) {
// Do nothing.
})
return opengl.NewContext()
c, err := opengl.NewContext()
if err != nil {
return nil, err
}
if err := graphics.InitializeIfNeeded(c); err != nil {
return nil, err
}
return c, nil
}
func setMouseCursorFromEvent(e *js.Object) {