From 1953539e2259ef84035af0a8b3d226a37866adf3 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 23 Jul 2016 20:25:52 +0900 Subject: [PATCH] ui: Refactoring: Simplify initializing --- internal/ui/init.go | 33 --------------------------------- internal/ui/ui_glfw.go | 21 +++++++++++++++------ internal/ui/ui_js.go | 35 +++++++++++++++++++++++------------ internal/ui/ui_mobile.go | 14 ++++++++++++-- pixels.go | 1 + 5 files changed, 51 insertions(+), 53 deletions(-) delete mode 100644 internal/ui/init.go diff --git a/internal/ui/init.go b/internal/ui/init.go deleted file mode 100644 index 2876c7c36..000000000 --- a/internal/ui/init.go +++ /dev/null @@ -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) - } -} diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index cd87b5ade..ee71fccc6 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -44,11 +44,21 @@ func CurrentUI() UserInterface { 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() if err := glfw.Init(); err != nil { - return nil, err + return err } glfw.WindowHint(glfw.Visible, 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. window, err := glfw.CreateWindow(16, 16, "", nil, nil) if err != nil { - return nil, err + return err } u := &userInterface{ @@ -81,10 +91,9 @@ func initialize() (*opengl.Context, error) { }() currentUI = u if err := <-ch; err != nil { - return nil, err + return err } - - return u.context, nil + return nil } func Main() error { diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index a86c39505..811e60021 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -136,14 +136,30 @@ func touchEventToTouches(e *js.Object) []touch { 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. if js.Global.Get("require") != js.Undefined { - c, err := opengl.NewContext() - if err != nil { - return nil, err - } - return c, nil + return nil } doc := js.Global.Get("document") @@ -255,12 +271,7 @@ func initialize() (*opengl.Context, error) { close(currentUI.contextRestored) currentUI.contextRestored = nil }) - - c, err := opengl.NewContext() - if err != nil { - return nil, err - } - return c, nil + return nil } func setMouseCursorFromEvent(e *js.Object) { diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index e9fdfdc7f..47b63a5c2 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -24,8 +24,18 @@ import ( "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) -func initialize() (*opengl.Context, error) { - return opengl.NewContext() +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 Main() error { diff --git a/pixels.go b/pixels.go index feed15c4a..ce1ac7f4b 100644 --- a/pixels.go +++ b/pixels.go @@ -91,6 +91,7 @@ func (p *pixels) hasHistoryWith(target *Image) bool { } 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 { return nil }