From d09bb63f7145b44218ef7f2ad30dbed68d00ad1c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 20 May 2016 00:07:04 +0900 Subject: [PATCH] ui: Change UserInterface to be an interface --- .gitignore | 2 ++ internal/loop/run.go | 5 +++- internal/ui/ui.go | 25 +++++++++++++++++++ internal/ui/ui_glfw.go | 38 ++++++++++++++-------------- internal/ui/ui_js.go | 54 +++++++++++++++++++++------------------- internal/ui/ui_mobile.go | 18 +++++++------- 6 files changed, 88 insertions(+), 54 deletions(-) create mode 100644 internal/ui/ui.go diff --git a/.gitignore b/.gitignore index 538c8c553..4392f62e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .DS_Store *~ +*.aar +*.apk diff --git a/internal/loop/run.go b/internal/loop/run.go index 989896348..a458339a8 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -104,6 +104,7 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err if err := ui.CurrentUI().Start(width, height, scale, title); err != nil { return err } + // TODO: Use the error value defer ui.CurrentUI().Terminate() frames := 0 @@ -143,7 +144,9 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err return err } } - ui.CurrentUI().SwapBuffers() + if err := ui.CurrentUI().SwapBuffers(); err != nil { + return err + } beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps) frames++ } diff --git a/internal/ui/ui.go b/internal/ui/ui.go new file mode 100644 index 000000000..81e6b0b33 --- /dev/null +++ b/internal/ui/ui.go @@ -0,0 +1,25 @@ +// Copyright 2015 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 + +type UserInterface interface { + Start(width, height, scale int, title string) error + Update() (interface{}, error) + SwapBuffers() error + Terminate() error + ScreenScale() int + SetScreenSize(width, height int) bool + SetScreenScale(scale int) bool +} diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 0aee5adb2..9fe9a0fdf 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -27,7 +27,7 @@ import ( "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) -type UserInterface struct { +type userInterface struct { window *glfw.Window width int height int @@ -39,9 +39,9 @@ type UserInterface struct { sizeChanged bool } -var currentUI *UserInterface +var currentUI *userInterface -func CurrentUI() *UserInterface { +func CurrentUI() UserInterface { return currentUI } @@ -62,7 +62,7 @@ func initialize() (*opengl.Context, error) { return nil, err } - u := &UserInterface{ + u := &userInterface{ window: window, funcs: make(chan func()), sizeChanged: true, @@ -98,7 +98,7 @@ func Main() error { return currentUI.main() } -func (u *UserInterface) main() error { +func (u *userInterface) main() error { // TODO: Check this is done on the main thread. for f := range u.funcs { f() @@ -106,7 +106,7 @@ func (u *UserInterface) main() error { return nil } -func (u *UserInterface) runOnMainThread(f func()) { +func (u *userInterface) runOnMainThread(f func()) { if u.funcs == nil { // already closed return @@ -119,7 +119,7 @@ func (u *UserInterface) runOnMainThread(f func()) { <-ch } -func (u *UserInterface) SetScreenSize(width, height int) bool { +func (u *userInterface) SetScreenSize(width, height int) bool { r := false u.runOnMainThread(func() { r = u.setScreenSize(width, height, u.scale) @@ -127,7 +127,7 @@ func (u *UserInterface) SetScreenSize(width, height int) bool { return r } -func (u *UserInterface) SetScreenScale(scale int) bool { +func (u *userInterface) SetScreenScale(scale int) bool { r := false u.runOnMainThread(func() { r = u.setScreenSize(u.width, u.height, scale) @@ -135,7 +135,7 @@ func (u *UserInterface) SetScreenScale(scale int) bool { return r } -func (u *UserInterface) ScreenScale() int { +func (u *userInterface) ScreenScale() int { s := 0 u.runOnMainThread(func() { s = u.scale @@ -143,7 +143,7 @@ func (u *UserInterface) ScreenScale() int { return s } -func (u *UserInterface) Start(width, height, scale int, title string) error { +func (u *userInterface) Start(width, height, scale int, title string) error { var err error u.runOnMainThread(func() { m := glfw.GetPrimaryMonitor() @@ -174,20 +174,20 @@ func (u *UserInterface) Start(width, height, scale int, title string) error { return err } -func (u *UserInterface) windowScale() int { +func (u *userInterface) windowScale() int { return u.scale * int(u.deviceScale) } -func (u *UserInterface) actualScreenScale() int { +func (u *userInterface) actualScreenScale() int { return u.windowScale() * u.framebufferScale } -func (u *UserInterface) pollEvents() error { +func (u *userInterface) pollEvents() error { glfw.PollEvents() return currentInput.update(u.window, u.windowScale()) } -func (u *UserInterface) Update() (interface{}, error) { +func (u *userInterface) Update() (interface{}, error) { shouldClose := false u.runOnMainThread(func() { shouldClose = u.window.ShouldClose() @@ -237,21 +237,23 @@ func (u *UserInterface) Update() (interface{}, error) { return RenderEvent{}, nil } -func (u *UserInterface) Terminate() { +func (u *userInterface) Terminate() error { u.runOnMainThread(func() { glfw.Terminate() }) close(u.funcs) u.funcs = nil + return nil } -func (u *UserInterface) SwapBuffers() { +func (u *userInterface) SwapBuffers() error { u.runOnMainThread(func() { u.swapBuffers() }) + return nil } -func (u *UserInterface) swapBuffers() { +func (u *userInterface) swapBuffers() { // The bound framebuffer must be the default one (0) before swapping buffers. u.context.BindZeroFramebuffer() u.context.RunOnContextThread(func() error { @@ -260,7 +262,7 @@ func (u *UserInterface) swapBuffers() { }) } -func (u *UserInterface) setScreenSize(width, height, scale int) bool { +func (u *userInterface) setScreenSize(width, height, scale int) bool { if u.width == width && u.height == height && u.scale == scale { return false } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 801215911..74a0740ae 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -24,36 +24,19 @@ import ( "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) -func (u *UserInterface) SetScreenSize(width, height int) bool { - return u.setScreenSize(width, height, u.scale) -} - -func (u *UserInterface) SetScreenScale(scale int) bool { - width, height := u.size() - return u.setScreenSize(width, height, scale) -} - -func (u *UserInterface) ScreenScale() int { - return u.scale -} - -func (u *UserInterface) ActualScreenScale() int { - return u.scale * int(u.deviceScale) -} - var canvas *js.Object -type UserInterface struct { +type userInterface struct { scale int deviceScale float64 sizeChanged bool } -var currentUI = &UserInterface{ +var currentUI = &userInterface{ sizeChanged: true, } -func CurrentUI() *UserInterface { +func CurrentUI() UserInterface { return currentUI } @@ -80,7 +63,24 @@ func vsync() { <-ch } -func (u *UserInterface) Update() (interface{}, error) { +func (u *userInterface) SetScreenSize(width, height int) bool { + return u.setScreenSize(width, height, u.scale) +} + +func (u *userInterface) SetScreenScale(scale int) bool { + width, height := u.size() + return u.setScreenSize(width, height, scale) +} + +func (u *userInterface) ScreenScale() int { + return u.scale +} + +func (u *userInterface) ActualScreenScale() int { + return u.scale * int(u.deviceScale) +} + +func (u *userInterface) Update() (interface{}, error) { currentInput.UpdateGamepads() if u.sizeChanged { u.sizeChanged = false @@ -96,15 +96,17 @@ func (u *UserInterface) Update() (interface{}, error) { return RenderEvent{}, nil } -func (u *UserInterface) Terminate() { +func (u *userInterface) Terminate() error { // Do nothing. + return nil } -func (u *UserInterface) SwapBuffers() { +func (u *userInterface) SwapBuffers() error { vsync() for !shown() { vsync() } + return nil } func initialize() (*opengl.Context, error) { @@ -252,7 +254,7 @@ func Main() error { return nil } -func (u *UserInterface) Start(width, height, scale int, title string) error { +func (u *userInterface) Start(width, height, scale int, title string) error { doc := js.Global.Get("document") doc.Set("title", title) u.setScreenSize(width, height, scale) @@ -260,7 +262,7 @@ func (u *UserInterface) Start(width, height, scale int, title string) error { return nil } -func (u *UserInterface) size() (width, height int) { +func (u *userInterface) size() (width, height int) { a := int(u.ActualScreenScale()) if a == 0 { // a == 0 only on the initial state. @@ -271,7 +273,7 @@ func (u *UserInterface) size() (width, height int) { return } -func (u *UserInterface) setScreenSize(width, height, scale int) bool { +func (u *userInterface) setScreenSize(width, height, scale int) bool { w, h := u.size() s := u.scale if w == width && h == height && s == scale { diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index f675ff6ba..40485378e 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -29,37 +29,37 @@ func Main() error { return nil } -type UserInterface struct { +type userInterface struct { } -func CurrentUI() *UserInterface { +func CurrentUI() UserInterface { return nil } -func (u *UserInterface) Start(width, height, scale int, title string) error { +func (u *userInterface) Start(width, height, scale int, title string) error { return nil } -func (u *UserInterface) Terminate() error { +func (u *userInterface) Terminate() error { return nil } -func (u *UserInterface) Update() (interface{}, error) { +func (u *userInterface) Update() (interface{}, error) { return nil, nil } -func (u *UserInterface) SwapBuffers() error { +func (u *userInterface) SwapBuffers() error { return nil } -func (u *UserInterface) SetScreenSize(width, height int) bool { +func (u *userInterface) SetScreenSize(width, height int) bool { return false } -func (u *UserInterface) SetScreenScale(scale int) bool { +func (u *userInterface) SetScreenScale(scale int) bool { return false } -func (u *UserInterface) ScreenScale() int { +func (u *userInterface) ScreenScale() int { return 1 }