Add ebitenmobileview package for internal usage

This is a preparation for the ebitenmobile command.
This commit is contained in:
Hajime Hoshi 2019-08-11 14:31:16 +09:00
parent d469084fd3
commit c93075d1dc
7 changed files with 91 additions and 34 deletions

View File

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

View File

@ -16,7 +16,7 @@
// +build !android
// +build !ios
package mobile
package ebitenmobileview
import (
"github.com/hajimehoshi/ebiten"

View File

@ -14,7 +14,7 @@
// +build android ios
package mobile
package ebitenmobileview
import (
"errors"

View File

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

View File

@ -14,7 +14,7 @@
// +build ios
package mobile
package ebitenmobileview
import (
"fmt"

View File

@ -14,7 +14,7 @@
// +build android ios
package mobile
package ebitenmobileview
import (
"github.com/hajimehoshi/ebiten/internal/uidriver/mobile"

View File

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