ui: Refactoring: Simplify initializing

This commit is contained in:
Hajime Hoshi 2016-07-23 20:25:52 +09:00
parent 0276be6c69
commit 1953539e22
5 changed files with 51 additions and 53 deletions

View File

@ -1,33 +0,0 @@
// Copyright 2016 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ui
import (
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
var glContext *opengl.Context
func GLContext() *opengl.Context {
return glContext
}
func init() {
var err error
glContext, err = initialize()
if err != nil {
panic(err)
}
}

View File

@ -44,11 +44,21 @@ func CurrentUI() UserInterface {
return currentUI return currentUI
} }
func initialize() (*opengl.Context, error) { func GLContext() *opengl.Context {
return currentUI.context
}
func init() {
if err := initialize(); err != nil {
panic(err)
}
}
func initialize() error {
runtime.LockOSThread() runtime.LockOSThread()
if err := glfw.Init(); err != nil { if err := glfw.Init(); err != nil {
return nil, err return err
} }
glfw.WindowHint(glfw.Visible, glfw.False) glfw.WindowHint(glfw.Visible, glfw.False)
glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.Resizable, glfw.False)
@ -58,7 +68,7 @@ func initialize() (*opengl.Context, error) {
// As start, create an window with temporary size to create OpenGL context thread. // As start, create an window with temporary size to create OpenGL context thread.
window, err := glfw.CreateWindow(16, 16, "", nil, nil) window, err := glfw.CreateWindow(16, 16, "", nil, nil)
if err != nil { if err != nil {
return nil, err return err
} }
u := &userInterface{ u := &userInterface{
@ -81,10 +91,9 @@ func initialize() (*opengl.Context, error) {
}() }()
currentUI = u currentUI = u
if err := <-ch; err != nil { if err := <-ch; err != nil {
return nil, err return err
} }
return nil
return u.context, nil
} }
func Main() error { func Main() error {

View File

@ -136,14 +136,30 @@ func touchEventToTouches(e *js.Object) []touch {
return t return t
} }
func initialize() (*opengl.Context, error) { var glContext *opengl.Context
func GLContext() *opengl.Context {
if glContext != nil {
return glContext
}
var err error
glContext, err = opengl.NewContext()
if err != nil {
panic(err)
}
return glContext
}
func init() {
if err := initialize(); err != nil {
panic(err)
}
}
func initialize() error {
// Do nothing in node.js. // Do nothing in node.js.
if js.Global.Get("require") != js.Undefined { if js.Global.Get("require") != js.Undefined {
c, err := opengl.NewContext() return nil
if err != nil {
return nil, err
}
return c, nil
} }
doc := js.Global.Get("document") doc := js.Global.Get("document")
@ -255,12 +271,7 @@ func initialize() (*opengl.Context, error) {
close(currentUI.contextRestored) close(currentUI.contextRestored)
currentUI.contextRestored = nil currentUI.contextRestored = nil
}) })
return nil
c, err := opengl.NewContext()
if err != nil {
return nil, err
}
return c, nil
} }
func setMouseCursorFromEvent(e *js.Object) { func setMouseCursorFromEvent(e *js.Object) {

View File

@ -24,8 +24,18 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics/opengl" "github.com/hajimehoshi/ebiten/internal/graphics/opengl"
) )
func initialize() (*opengl.Context, error) { var glContext *opengl.Context
return opengl.NewContext()
func GLContext() *opengl.Context {
if glContext != nil {
return glContext
}
var err error
glContext, err = opengl.NewContext()
if err != nil {
panic(err)
}
return glContext
} }
func Main() error { func Main() error {

View File

@ -91,6 +91,7 @@ func (p *pixels) hasHistoryWith(target *Image) bool {
} }
func (p *pixels) resetHistoryIfNeeded(image *graphics.Image, target *Image, context *opengl.Context) error { func (p *pixels) resetHistoryIfNeeded(image *graphics.Image, target *Image, context *opengl.Context) error {
// TODO: Return error when the main loop is not running yet.
if p.drawImageHistory == nil { if p.drawImageHistory == nil {
return nil return nil
} }