ui: Change UserInterface to be an interface

This commit is contained in:
Hajime Hoshi 2016-05-20 00:07:04 +09:00
parent d5d9e2c73e
commit d09bb63f71
6 changed files with 88 additions and 54 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
.DS_Store
*~
*.aar
*.apk

View File

@ -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++
}

25
internal/ui/ui.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}