Move driver getters to graphicsdriver/uidriver packages

This enables to add internal functions to these packages so that
the driver selector logics can be modified.
This commit is contained in:
Hajime Hoshi 2019-07-31 22:01:45 +09:00
parent e434869dd7
commit de915a1736
10 changed files with 58 additions and 53 deletions

View File

@ -16,6 +16,7 @@ package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/uidriver"
)
// InputChars return "printable" runes read from the keyboard at the time update is called.
@ -28,7 +29,7 @@ import (
//
// InputChars is concurrent-safe.
func InputChars() []rune {
rb := uiDriver().Input().RuneBuffer()
rb := uidriver.Get().Input().RuneBuffer()
return append(make([]rune, 0, len(rb)), rb...)
}
@ -41,7 +42,7 @@ func InputChars() []rune {
//
// IsKeyPressed is concurrent-safe.
func IsKeyPressed(key Key) bool {
return uiDriver().Input().IsKeyPressed(driver.Key(key))
return uidriver.Get().Input().IsKeyPressed(driver.Key(key))
}
// CursorPosition returns a position of a mouse cursor relative to the game screen (window). The cursor position is
@ -49,7 +50,7 @@ func IsKeyPressed(key Key) bool {
//
// CursorPosition is concurrent-safe.
func CursorPosition() (x, y int) {
return uiDriver().Input().CursorPosition()
return uidriver.Get().Input().CursorPosition()
}
// Wheel returns the x and y offset of the mouse wheel or touchpad scroll.
@ -57,7 +58,7 @@ func CursorPosition() (x, y int) {
//
// Wheel is concurrent-safe.
func Wheel() (xoff, yoff float64) {
return uiDriver().Input().Wheel()
return uidriver.Get().Input().Wheel()
}
// IsMouseButtonPressed returns a boolean indicating whether mouseButton is pressed.
@ -67,7 +68,7 @@ func Wheel() (xoff, yoff float64) {
// Note that touch events not longer affect IsMouseButtonPressed's result as of 1.4.0-alpha.
// Use Touches instead.
func IsMouseButtonPressed(mouseButton MouseButton) bool {
return uiDriver().Input().IsMouseButtonPressed(driver.MouseButton(mouseButton))
return uidriver.Get().Input().IsMouseButtonPressed(driver.MouseButton(mouseButton))
}
// GamepadIDs returns a slice indicating available gamepad IDs.
@ -76,7 +77,7 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
//
// GamepadIDs always returns an empty slice on mobiles.
func GamepadIDs() []int {
return uiDriver().Input().GamepadIDs()
return uidriver.Get().Input().GamepadIDs()
}
// GamepadAxisNum returns the number of axes of the gamepad (id).
@ -85,7 +86,7 @@ func GamepadIDs() []int {
//
// GamepadAxisNum always returns 0 on mobiles.
func GamepadAxisNum(id int) int {
return uiDriver().Input().GamepadAxisNum(id)
return uidriver.Get().Input().GamepadAxisNum(id)
}
// GamepadAxis returns the float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis).
@ -94,7 +95,7 @@ func GamepadAxisNum(id int) int {
//
// GamepadAxis always returns 0 on mobiles.
func GamepadAxis(id int, axis int) float64 {
return uiDriver().Input().GamepadAxis(id, axis)
return uidriver.Get().Input().GamepadAxis(id, axis)
}
// GamepadButtonNum returns the number of the buttons of the given gamepad (id).
@ -103,7 +104,7 @@ func GamepadAxis(id int, axis int) float64 {
//
// GamepadButtonNum always returns 0 on mobiles.
func GamepadButtonNum(id int) int {
return uiDriver().Input().GamepadButtonNum(id)
return uidriver.Get().Input().GamepadButtonNum(id)
}
// IsGamepadButtonPressed returns the boolean indicating the given button of the gamepad (id) is pressed or not.
@ -115,7 +116,7 @@ func GamepadButtonNum(id int) int {
//
// IsGamepadButtonPressed always returns false on mobiles.
func IsGamepadButtonPressed(id int, button GamepadButton) bool {
return uiDriver().Input().IsGamepadButtonPressed(id, driver.GamepadButton(button))
return uidriver.Get().Input().IsGamepadButtonPressed(id, driver.GamepadButton(button))
}
// TouchIDs returns the current touch states.
@ -125,7 +126,7 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool {
//
// TouchIDs is concurrent-safe.
func TouchIDs() []int {
return uiDriver().Input().TouchIDs()
return uidriver.Get().Input().TouchIDs()
}
// TouchPosition returns the position for the touch of the specified ID.
@ -135,7 +136,7 @@ func TouchIDs() []int {
// TouchPosition is cuncurrent-safe.
func TouchPosition(id int) (int, int) {
found := false
for _, i := range uiDriver().Input().TouchIDs() {
for _, i := range uidriver.Get().Input().TouchIDs() {
if id == i {
found = true
break
@ -145,7 +146,7 @@ func TouchPosition(id int) (int, int) {
return 0, 0
}
return uiDriver().Input().TouchPosition(id)
return uidriver.Get().Input().TouchPosition(id)
}
// Touch is deprecated as of 1.7.0. Use TouchPosition instead.

View File

@ -15,7 +15,7 @@
// +build darwin,!ios
// +build !js
package ebiten
package graphicsdriver
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Foundation
@ -47,7 +47,7 @@ var (
isMetalSupportedOnce sync.Once
)
func graphicsDriver() driver.Graphics {
func Get() driver.Graphics {
isMetalSupportedOnce.Do(func() {
// On old mac devices like iMac 2011, Metal is not supported (#779).
if _, err := mtl.CreateSystemDefaultDevice(); err != nil {

View File

@ -16,13 +16,13 @@
// As the Go playground tries to compile this with CGO_ENABLED=0 and GOOS=linux, check Cgo on build tags.
package ebiten
package graphicsdriver
import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl"
)
func graphicsDriver() driver.Graphics {
func Get() driver.Graphics {
return opengl.Get()
}

View File

@ -20,13 +20,13 @@
// +build !linux,cgo !cgo
// +build !windows
package ebiten
package graphicsdriver
import (
"github.com/hajimehoshi/ebiten/internal/driver"
)
func graphicsDriver() driver.Graphics {
func Get() driver.Graphics {
if !isPlayground {
panic("ebiten: a graphics driver is not implemented on this environment")
}

View File

@ -17,13 +17,13 @@
// +build !ios
// +build !js
package ebiten
package uidriver
import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/uidriver/glfw"
)
func uiDriver() driver.UI {
func Get() driver.UI {
return glfw.Get()
}

View File

@ -14,13 +14,13 @@
// +build js
package ebiten
package uidriver
import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/uidriver/js"
)
func uiDriver() driver.UI {
func Get() driver.UI {
return js.Get()
}

View File

@ -14,13 +14,13 @@
// +build android ios
package ebiten
package uidriver
import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/uidriver/mobile"
)
func uiDriver() driver.UI {
func Get() driver.UI {
return mobile.Get()
}

View File

@ -20,13 +20,13 @@
// +build !linux,cgo !cgo
// +build !windows
package ebiten
package uidriver
import (
"github.com/hajimehoshi/ebiten/internal/driver"
)
func uiDriver() driver.UI {
func Get() driver.UI {
if !isPlayground {
panic("ebiten: a UI driver is not implemented on this environment")
}

44
run.go
View File

@ -21,6 +21,8 @@ import (
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/internal/uidriver"
"github.com/hajimehoshi/ebiten/internal/web"
)
@ -142,7 +144,7 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
defer atomic.StoreInt32(&isRunning, 0)
}
if err := uiDriver().Run(width, height, scale, title, c, graphicsDriver()); err != nil {
if err := uidriver.Get().Run(width, height, scale, title, c, graphicsdriver.Get()); err != nil {
if err == driver.RegularTermination {
return nil
}
@ -164,7 +166,7 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
theUIContext.Store(c)
atomic.StoreInt32(&isRunning, 1)
return uiDriver().RunWithoutMainLoop(width, height, scale, title, c, graphicsDriver())
return uidriver.Get().RunWithoutMainLoop(width, height, scale, title, c, graphicsdriver.Get())
}
// ScreenSizeInFullscreen returns the size in device-independent pixels when the game is fullscreen.
@ -196,7 +198,7 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
//
// ScreenSizeInFullscreen must be called on the main thread before ebiten.Run, and is concurrent-safe after ebiten.Run.
func ScreenSizeInFullscreen() (int, int) {
return uiDriver().ScreenSizeInFullscreen()
return uidriver.Get().ScreenSizeInFullscreen()
}
// MonitorSize is deprecated as of 1.8.0-alpha. Use ScreenSizeInFullscreen instead.
@ -214,7 +216,7 @@ func SetScreenSize(width, height int) {
if width <= 0 || height <= 0 {
panic("ebiten: width and height must be positive")
}
uiDriver().SetScreenSize(width, height)
uidriver.Get().SetScreenSize(width, height)
}
// SetScreenScale changes the scale of the screen.
@ -230,7 +232,7 @@ func SetScreenScale(scale float64) {
if scale <= 0 {
panic("ebiten: scale must be positive")
}
uiDriver().SetScreenScale(scale)
uidriver.Get().SetScreenScale(scale)
}
// ScreenScale returns the current screen scale.
@ -239,7 +241,7 @@ func SetScreenScale(scale float64) {
//
// ScreenScale is concurrent-safe.
func ScreenScale() float64 {
return uiDriver().ScreenScale()
return uidriver.Get().ScreenScale()
}
// IsCursorVisible returns a boolean value indicating whether
@ -249,7 +251,7 @@ func ScreenScale() float64 {
//
// IsCursorVisible is concurrent-safe.
func IsCursorVisible() bool {
return uiDriver().IsCursorVisible()
return uidriver.Get().IsCursorVisible()
}
// SetCursorVisible changes the state of cursor visiblity.
@ -258,7 +260,7 @@ func IsCursorVisible() bool {
//
// SetCursorVisible is concurrent-safe.
func SetCursorVisible(visible bool) {
uiDriver().SetCursorVisible(visible)
uidriver.Get().SetCursorVisible(visible)
}
// SetCursorVisibility is deprecated as of 1.6.0-alpha. Use SetCursorVisible instead.
@ -273,7 +275,7 @@ func SetCursorVisibility(visible bool) {
//
// IsFullscreen is concurrent-safe.
func IsFullscreen() bool {
return uiDriver().IsFullscreen()
return uidriver.Get().IsFullscreen()
}
// SetFullscreen changes the current mode to fullscreen or not.
@ -294,7 +296,7 @@ func IsFullscreen() bool {
//
// SetFullscreen is concurrent-safe.
func SetFullscreen(fullscreen bool) {
uiDriver().SetFullscreen(fullscreen)
uidriver.Get().SetFullscreen(fullscreen)
}
// IsRunnableInBackground returns a boolean value indicating whether
@ -302,7 +304,7 @@ func SetFullscreen(fullscreen bool) {
//
// IsRunnableInBackground is concurrent-safe.
func IsRunnableInBackground() bool {
return uiDriver().IsRunnableInBackground()
return uidriver.Get().IsRunnableInBackground()
}
// SetWindowDecorated sets the state if the window is decorated.
@ -316,14 +318,14 @@ func IsRunnableInBackground() bool {
//
// SetWindowDecorated is concurrent-safe.
func SetWindowDecorated(decorated bool) {
uiDriver().SetWindowDecorated(decorated)
uidriver.Get().SetWindowDecorated(decorated)
}
// IsWindowDecorated reports whether the window is decorated.
//
// IsWindowDecorated is concurrent-safe.
func IsWindowDecorated() bool {
return uiDriver().IsWindowDecorated()
return uidriver.Get().IsWindowDecorated()
}
// setWindowResizable is unexported until specification is determined (#320)
@ -341,14 +343,14 @@ func IsWindowDecorated() bool {
//
// setWindowResizable is concurrent-safe.
func setWindowResizable(resizable bool) {
uiDriver().SetWindowResizable(resizable)
uidriver.Get().SetWindowResizable(resizable)
}
// IsWindowResizable reports whether the window is resizable.
//
// IsWindowResizable is concurrent-safe.
func IsWindowResizable() bool {
return uiDriver().IsWindowResizable()
return uidriver.Get().IsWindowResizable()
}
// SetRunnableInBackground sets the state if the game runs even in background.
@ -363,7 +365,7 @@ func IsWindowResizable() bool {
//
// SetRunnableInBackground is concurrent-safe.
func SetRunnableInBackground(runnableInBackground bool) {
uiDriver().SetRunnableInBackground(runnableInBackground)
uidriver.Get().SetRunnableInBackground(runnableInBackground)
}
// SetWindowTitle sets the title of the window.
@ -372,7 +374,7 @@ func SetRunnableInBackground(runnableInBackground bool) {
//
// SetWindowTitle is concurrent-safe.
func SetWindowTitle(title string) {
uiDriver().SetWindowTitle(title)
uidriver.Get().SetWindowTitle(title)
}
// SetWindowIcon sets the icon of the game window.
@ -396,7 +398,7 @@ func SetWindowTitle(title string) {
//
// SetWindowIcon is concurrent-safe.
func SetWindowIcon(iconImages []image.Image) {
uiDriver().SetWindowIcon(iconImages)
uidriver.Get().SetWindowIcon(iconImages)
}
// DeviceScaleFactor returns a device scale factor value of the current monitor which the window belongs to.
@ -409,7 +411,7 @@ func SetWindowIcon(iconImages []image.Image) {
//
// DeviceScaleFactor must be called on the main thread before ebiten.Run, and is concurrent-safe after ebiten.Run.
func DeviceScaleFactor() float64 {
return uiDriver().DeviceScaleFactor()
return uidriver.Get().DeviceScaleFactor()
}
// IsVsyncEnabled returns a boolean value indicating whether
@ -417,7 +419,7 @@ func DeviceScaleFactor() float64 {
//
// IsVsyncEnabled is concurrent-safe.
func IsVsyncEnabled() bool {
return uiDriver().IsVsyncEnabled()
return uidriver.Get().IsVsyncEnabled()
}
// SetVsyncEnabled sets a boolean value indicating whether
@ -435,7 +437,7 @@ func IsVsyncEnabled() bool {
//
// SetVsyncEnabled is concurrent-safe.
func SetVsyncEnabled(enabled bool) {
uiDriver().SetVsyncEnabled(enabled)
uidriver.Get().SetVsyncEnabled(enabled)
}
// MaxTPS returns the current maximum TPS.

View File

@ -21,13 +21,15 @@ import (
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/shareable"
"github.com/hajimehoshi/ebiten/internal/uidriver"
)
func init() {
shareable.SetGraphicsDriver(graphicsDriver())
graphicscommand.SetGraphicsDriver(graphicsDriver())
shareable.SetGraphicsDriver(graphicsdriver.Get())
graphicscommand.SetGraphicsDriver(graphicsdriver.Get())
}
func newUIContext(f func(*Image) error) *uiContext {
@ -63,7 +65,7 @@ func (c *uiContext) SetSize(screenWidth, screenHeight int, screenScale float64)
// Round up the screensize not to cause glitches e.g. on Xperia (#622)
w := int(math.Ceil(float64(screenWidth) * screenScale))
h := int(math.Ceil(float64(screenHeight) * screenScale))
px0, py0, px1, py1 := uiDriver().ScreenPadding()
px0, py0, px1, py1 := uidriver.Get().ScreenPadding()
c.screen = newImageWithScreenFramebuffer(w+int(math.Ceil(px0+px1)), h+int(math.Ceil(py0+py1)))
c.screenWidth = w
c.screenHeight = h
@ -106,7 +108,7 @@ func (c *uiContext) Update(afterFrameUpdate func()) error {
return err
}
uiDriver().Input().ResetForFrame()
uidriver.Get().Input().ResetForFrame()
afterFrameUpdate()
}
@ -115,7 +117,7 @@ func (c *uiContext) Update(afterFrameUpdate func()) error {
op := &DrawImageOptions{}
switch vd := graphicsDriver().VDirection(); vd {
switch vd := graphicsdriver.Get().VDirection(); vd {
case driver.VDownward:
// c.screen is special: its Y axis is down to up,
// and the origin point is lower left.