From c93075d1dc3d7f3649eedb435323b1a81cfb25bf Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 11 Aug 2019 14:31:16 +0900 Subject: [PATCH] Add ebitenmobileview package for internal usage This is a preparation for the ebitenmobile command. --- mobile/ebitenmobileview/ebitenmobileview.go | 76 +++++++++++++++++++ mobile/{ => ebitenmobileview}/impl_empty.go | 2 +- mobile/{ => ebitenmobileview}/impl_mobile.go | 2 +- .../{ => ebitenmobileview}/touches_android.go | 2 +- mobile/{ => ebitenmobileview}/touches_ios.go | 2 +- .../{ => ebitenmobileview}/touches_mobile.go | 2 +- mobile/mobile.go | 39 +++------- 7 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 mobile/ebitenmobileview/ebitenmobileview.go rename mobile/{ => ebitenmobileview}/impl_empty.go (97%) rename mobile/{ => ebitenmobileview}/impl_mobile.go (98%) rename mobile/{ => ebitenmobileview}/touches_android.go (97%) rename mobile/{ => ebitenmobileview}/touches_ios.go (98%) rename mobile/{ => ebitenmobileview}/touches_mobile.go (97%) diff --git a/mobile/ebitenmobileview/ebitenmobileview.go b/mobile/ebitenmobileview/ebitenmobileview.go new file mode 100644 index 000000000..397119667 --- /dev/null +++ b/mobile/ebitenmobileview/ebitenmobileview.go @@ -0,0 +1,76 @@ +// 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 ebitenmobileview offers functions for OpenGL/Metal view of mobiles. +// +// The functions are not intended for public usages, and there is no guarantee of +// backward compatibility. +package ebitenmobileview + +import ( + "runtime" + "sync" + + "github.com/hajimehoshi/ebiten" +) + +var ( + // mobileMutex is a mutex required for each function. + // For example, on Android, Update can be called from a different thread: + // https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer + mobileMutex sync.Mutex +) + +func Run(width, height int, scale float64, title string) { + mobileMutex.Lock() + defer mobileMutex.Unlock() + + if updateFunc == nil { + panic("ebitenmobileview: SetUpdateFunc must be called before Run") + } + start(updateFunc, width, height, scale, title) +} + +func Update() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + mobileMutex.Lock() + defer mobileMutex.Unlock() + + return update() +} + +func UpdateTouchesOnAndroid(action int, id int, x, y int) { + mobileMutex.Lock() + defer mobileMutex.Unlock() + + updateTouchesOnAndroid(action, id, x, y) +} + +func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) { + mobileMutex.Lock() + defer mobileMutex.Unlock() + + updateTouchesOnIOSImpl(phase, ptr, x, y) +} + +var updateFunc func(*ebiten.Image) error + +func SetUpdateFunc(f func(*ebiten.Image) error) { + mobileMutex.Lock() + defer mobileMutex.Unlock() + + updateFunc = f +} diff --git a/mobile/impl_empty.go b/mobile/ebitenmobileview/impl_empty.go similarity index 97% rename from mobile/impl_empty.go rename to mobile/ebitenmobileview/impl_empty.go index 1b207ca81..f34df18c4 100644 --- a/mobile/impl_empty.go +++ b/mobile/ebitenmobileview/impl_empty.go @@ -16,7 +16,7 @@ // +build !android // +build !ios -package mobile +package ebitenmobileview import ( "github.com/hajimehoshi/ebiten" diff --git a/mobile/impl_mobile.go b/mobile/ebitenmobileview/impl_mobile.go similarity index 98% rename from mobile/impl_mobile.go rename to mobile/ebitenmobileview/impl_mobile.go index e4edf2458..cc1072903 100644 --- a/mobile/impl_mobile.go +++ b/mobile/ebitenmobileview/impl_mobile.go @@ -14,7 +14,7 @@ // +build android ios -package mobile +package ebitenmobileview import ( "errors" diff --git a/mobile/touches_android.go b/mobile/ebitenmobileview/touches_android.go similarity index 97% rename from mobile/touches_android.go rename to mobile/ebitenmobileview/touches_android.go index 150472f94..abc1b035c 100644 --- a/mobile/touches_android.go +++ b/mobile/ebitenmobileview/touches_android.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package mobile +package ebitenmobileview func updateTouchesOnAndroid(action int, id int, x, y int) { switch action { diff --git a/mobile/touches_ios.go b/mobile/ebitenmobileview/touches_ios.go similarity index 98% rename from mobile/touches_ios.go rename to mobile/ebitenmobileview/touches_ios.go index b644dd8de..4871e1d48 100644 --- a/mobile/touches_ios.go +++ b/mobile/ebitenmobileview/touches_ios.go @@ -14,7 +14,7 @@ // +build ios -package mobile +package ebitenmobileview import ( "fmt" diff --git a/mobile/touches_mobile.go b/mobile/ebitenmobileview/touches_mobile.go similarity index 97% rename from mobile/touches_mobile.go rename to mobile/ebitenmobileview/touches_mobile.go index b2eb3edae..10caab934 100644 --- a/mobile/touches_mobile.go +++ b/mobile/ebitenmobileview/touches_mobile.go @@ -14,7 +14,7 @@ // +build android ios -package mobile +package ebitenmobileview import ( "github.com/hajimehoshi/ebiten/internal/uidriver/mobile" diff --git a/mobile/mobile.go b/mobile/mobile.go index a968ae969..bbf07d24a 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -21,17 +21,8 @@ package mobile import ( - "runtime" - "sync" - "github.com/hajimehoshi/ebiten" -) - -var ( - // mobileMutex is a mutex required for each function. - // For example, on Android, Update can be called from a different thread: - // https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer - mobileMutex sync.Mutex + "github.com/hajimehoshi/ebiten/mobile/ebitenmobileview" ) // Start starts the game and returns immediately. @@ -44,10 +35,8 @@ var ( // // Start always returns nil as of 1.5.0-alpha. func Start(f func(*ebiten.Image) error, width, height int, scale float64, title string) error { - mobileMutex.Lock() - defer mobileMutex.Unlock() - - start(f, width, height, scale, title) + ebitenmobileview.SetUpdateFunc(f) + ebitenmobileview.Run(width, height, scale, title) return nil } @@ -64,13 +53,7 @@ func Start(f func(*ebiten.Image) error, width, height int, scale float64, title // // Update returns error when 1) OpenGL error happens, or 2) f in Start returns error samely as ebiten.Run. func Update() error { - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - mobileMutex.Lock() - defer mobileMutex.Unlock() - - return update() + return ebitenmobileview.Update() } // UpdateTouchesOnAndroid updates the touch state on Android. @@ -107,10 +90,7 @@ func Update() error { // // For more details, see https://github.com/hajimehoshi/ebiten/wiki/Android. func UpdateTouchesOnAndroid(action int, id int, x, y int) { - mobileMutex.Lock() - defer mobileMutex.Unlock() - - updateTouchesOnAndroid(action, id, x, y) + ebitenmobileview.UpdateTouchesOnAndroid(action, id, x, y) } // UpdateTouchesOnIOS updates the touch state on iOS. @@ -151,8 +131,9 @@ func UpdateTouchesOnAndroid(action int, id int, x, y int) { // // For more details, see https://github.com/hajimehoshi/ebiten/wiki/iOS. func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) { - mobileMutex.Lock() - defer mobileMutex.Unlock() - - updateTouchesOnIOSImpl(phase, ptr, x, y) + ebitenmobileview.UpdateTouchesOnIOS(phase, ptr, x, y) +} + +func SetUpdateFunc(f func(*ebiten.Image) error) { + ebitenmobileview.SetUpdateFunc(f) }