Move back driver getters to ebiten package

This commit is contained in:
Hajime Hoshi 2019-09-03 01:42:53 +09:00
parent ba08a5bb55
commit 52900fac79
10 changed files with 53 additions and 58 deletions

View File

@ -15,7 +15,7 @@
// +build darwin,!ios
// +build !js
package graphicsdriver
package ebiten
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Foundation
@ -47,7 +47,7 @@ var (
isMetalSupportedOnce sync.Once
)
func Get() driver.Graphics {
func graphicsDriver() 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 graphicsdriver
package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl"
)
func Get() driver.Graphics {
func graphicsDriver() driver.Graphics {
return opengl.Get()
}

View File

@ -26,7 +26,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver"
)
func Get() driver.Graphics {
func graphicsDriver() driver.Graphics {
if !driver.IsPlayground {
panic("ebiten: a graphics driver is not implemented on this environment: isn't cgo disabled?")
}

View File

@ -16,7 +16,6 @@ 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.
@ -29,7 +28,7 @@ import (
//
// InputChars is concurrent-safe.
func InputChars() []rune {
rb := uidriver.Get().Input().RuneBuffer()
rb := uiDriver().Input().RuneBuffer()
return append(make([]rune, 0, len(rb)), rb...)
}
@ -60,7 +59,7 @@ func IsKeyPressed(key Key) bool {
keys = append(keys, driver.Key(key))
}
for _, k := range keys {
if uidriver.Get().Input().IsKeyPressed(k) {
if uiDriver().Input().IsKeyPressed(k) {
return true
}
}
@ -72,7 +71,7 @@ func IsKeyPressed(key Key) bool {
//
// CursorPosition is concurrent-safe.
func CursorPosition() (x, y int) {
return uidriver.Get().Input().CursorPosition()
return uiDriver().Input().CursorPosition()
}
// Wheel returns the x and y offset of the mouse wheel or touchpad scroll.
@ -80,7 +79,7 @@ func CursorPosition() (x, y int) {
//
// Wheel is concurrent-safe.
func Wheel() (xoff, yoff float64) {
return uidriver.Get().Input().Wheel()
return uiDriver().Input().Wheel()
}
// IsMouseButtonPressed returns a boolean indicating whether mouseButton is pressed.
@ -90,7 +89,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.Get().Input().IsMouseButtonPressed(driver.MouseButton(mouseButton))
return uiDriver().Input().IsMouseButtonPressed(driver.MouseButton(mouseButton))
}
// GamepadIDs returns a slice indicating available gamepad IDs.
@ -99,7 +98,7 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
//
// GamepadIDs always returns an empty slice on mobiles.
func GamepadIDs() []int {
return uidriver.Get().Input().GamepadIDs()
return uiDriver().Input().GamepadIDs()
}
// GamepadAxisNum returns the number of axes of the gamepad (id).
@ -108,7 +107,7 @@ func GamepadIDs() []int {
//
// GamepadAxisNum always returns 0 on mobiles.
func GamepadAxisNum(id int) int {
return uidriver.Get().Input().GamepadAxisNum(id)
return uiDriver().Input().GamepadAxisNum(id)
}
// GamepadAxis returns the float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis).
@ -117,7 +116,7 @@ func GamepadAxisNum(id int) int {
//
// GamepadAxis always returns 0 on mobiles.
func GamepadAxis(id int, axis int) float64 {
return uidriver.Get().Input().GamepadAxis(id, axis)
return uiDriver().Input().GamepadAxis(id, axis)
}
// GamepadButtonNum returns the number of the buttons of the given gamepad (id).
@ -126,7 +125,7 @@ func GamepadAxis(id int, axis int) float64 {
//
// GamepadButtonNum always returns 0 on mobiles.
func GamepadButtonNum(id int) int {
return uidriver.Get().Input().GamepadButtonNum(id)
return uiDriver().Input().GamepadButtonNum(id)
}
// IsGamepadButtonPressed returns the boolean indicating the given button of the gamepad (id) is pressed or not.
@ -138,7 +137,7 @@ func GamepadButtonNum(id int) int {
//
// IsGamepadButtonPressed always returns false on mobiles.
func IsGamepadButtonPressed(id int, button GamepadButton) bool {
return uidriver.Get().Input().IsGamepadButtonPressed(id, driver.GamepadButton(button))
return uiDriver().Input().IsGamepadButtonPressed(id, driver.GamepadButton(button))
}
// TouchIDs returns the current touch states.
@ -148,7 +147,7 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool {
//
// TouchIDs is concurrent-safe.
func TouchIDs() []int {
return uidriver.Get().Input().TouchIDs()
return uiDriver().Input().TouchIDs()
}
// TouchPosition returns the position for the touch of the specified ID.
@ -158,7 +157,7 @@ func TouchIDs() []int {
// TouchPosition is cuncurrent-safe.
func TouchPosition(id int) (int, int) {
found := false
for _, i := range uidriver.Get().Input().TouchIDs() {
for _, i := range uiDriver().Input().TouchIDs() {
if id == i {
found = true
break
@ -168,7 +167,7 @@ func TouchPosition(id int) (int, int) {
return 0, 0
}
return uidriver.Get().Input().TouchPosition(id)
return uiDriver().Input().TouchPosition(id)
}
// Touch is deprecated as of 1.7.0. Use TouchPosition instead.

44
run.go
View File

@ -20,8 +20,6 @@ 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"
)
var _ = __EBITEN_REQUIRES_GO_VERSION_1_12_OR_LATER__
@ -131,7 +129,7 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
c := newUIContext(f)
theUIContext.Store(c)
if err := uidriver.Get().Run(width, height, scale, title, c, graphicsdriver.Get()); err != nil {
if err := uiDriver().Run(width, height, scale, title, c, graphicsDriver()); err != nil {
if err == driver.RegularTermination {
return nil
}
@ -152,7 +150,7 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
c := newUIContext(f)
theUIContext.Store(c)
return uidriver.Get().RunWithoutMainLoop(width, height, scale, title, c, graphicsdriver.Get())
return uiDriver().RunWithoutMainLoop(width, height, scale, title, c, graphicsDriver())
}
// ScreenSizeInFullscreen returns the size in device-independent pixels when the game is fullscreen.
@ -184,7 +182,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.Get().ScreenSizeInFullscreen()
return uiDriver().ScreenSizeInFullscreen()
}
// MonitorSize is deprecated as of 1.8.0-alpha. Use ScreenSizeInFullscreen instead.
@ -202,7 +200,7 @@ func SetScreenSize(width, height int) {
if width <= 0 || height <= 0 {
panic("ebiten: width and height must be positive")
}
uidriver.Get().SetScreenSize(width, height)
uiDriver().SetScreenSize(width, height)
}
// SetScreenScale changes the scale of the screen.
@ -218,7 +216,7 @@ func SetScreenScale(scale float64) {
if scale <= 0 {
panic("ebiten: scale must be positive")
}
uidriver.Get().SetScreenScale(scale)
uiDriver().SetScreenScale(scale)
}
// ScreenScale returns the current screen scale.
@ -227,7 +225,7 @@ func SetScreenScale(scale float64) {
//
// ScreenScale is concurrent-safe.
func ScreenScale() float64 {
return uidriver.Get().ScreenScale()
return uiDriver().ScreenScale()
}
// IsCursorVisible returns a boolean value indicating whether
@ -237,7 +235,7 @@ func ScreenScale() float64 {
//
// IsCursorVisible is concurrent-safe.
func IsCursorVisible() bool {
return uidriver.Get().IsCursorVisible()
return uiDriver().IsCursorVisible()
}
// SetCursorVisible changes the state of cursor visiblity.
@ -246,7 +244,7 @@ func IsCursorVisible() bool {
//
// SetCursorVisible is concurrent-safe.
func SetCursorVisible(visible bool) {
uidriver.Get().SetCursorVisible(visible)
uiDriver().SetCursorVisible(visible)
}
// SetCursorVisibility is deprecated as of 1.6.0-alpha. Use SetCursorVisible instead.
@ -261,7 +259,7 @@ func SetCursorVisibility(visible bool) {
//
// IsFullscreen is concurrent-safe.
func IsFullscreen() bool {
return uidriver.Get().IsFullscreen()
return uiDriver().IsFullscreen()
}
// SetFullscreen changes the current mode to fullscreen or not.
@ -282,7 +280,7 @@ func IsFullscreen() bool {
//
// SetFullscreen is concurrent-safe.
func SetFullscreen(fullscreen bool) {
uidriver.Get().SetFullscreen(fullscreen)
uiDriver().SetFullscreen(fullscreen)
}
// IsRunnableInBackground returns a boolean value indicating whether
@ -290,7 +288,7 @@ func SetFullscreen(fullscreen bool) {
//
// IsRunnableInBackground is concurrent-safe.
func IsRunnableInBackground() bool {
return uidriver.Get().IsRunnableInBackground()
return uiDriver().IsRunnableInBackground()
}
// SetWindowDecorated sets the state if the window is decorated.
@ -304,14 +302,14 @@ func IsRunnableInBackground() bool {
//
// SetWindowDecorated is concurrent-safe.
func SetWindowDecorated(decorated bool) {
uidriver.Get().SetWindowDecorated(decorated)
uiDriver().SetWindowDecorated(decorated)
}
// IsWindowDecorated reports whether the window is decorated.
//
// IsWindowDecorated is concurrent-safe.
func IsWindowDecorated() bool {
return uidriver.Get().IsWindowDecorated()
return uiDriver().IsWindowDecorated()
}
// setWindowResizable is unexported until specification is determined (#320)
@ -329,14 +327,14 @@ func IsWindowDecorated() bool {
//
// setWindowResizable is concurrent-safe.
func setWindowResizable(resizable bool) {
uidriver.Get().SetWindowResizable(resizable)
uiDriver().SetWindowResizable(resizable)
}
// IsWindowResizable reports whether the window is resizable.
//
// IsWindowResizable is concurrent-safe.
func IsWindowResizable() bool {
return uidriver.Get().IsWindowResizable()
return uiDriver().IsWindowResizable()
}
// SetRunnableInBackground sets the state if the game runs even in background.
@ -351,7 +349,7 @@ func IsWindowResizable() bool {
//
// SetRunnableInBackground is concurrent-safe.
func SetRunnableInBackground(runnableInBackground bool) {
uidriver.Get().SetRunnableInBackground(runnableInBackground)
uiDriver().SetRunnableInBackground(runnableInBackground)
}
// SetWindowTitle sets the title of the window.
@ -360,7 +358,7 @@ func SetRunnableInBackground(runnableInBackground bool) {
//
// SetWindowTitle is concurrent-safe.
func SetWindowTitle(title string) {
uidriver.Get().SetWindowTitle(title)
uiDriver().SetWindowTitle(title)
}
// SetWindowIcon sets the icon of the game window.
@ -384,7 +382,7 @@ func SetWindowTitle(title string) {
//
// SetWindowIcon is concurrent-safe.
func SetWindowIcon(iconImages []image.Image) {
uidriver.Get().SetWindowIcon(iconImages)
uiDriver().SetWindowIcon(iconImages)
}
// DeviceScaleFactor returns a device scale factor value of the current monitor which the window belongs to.
@ -397,7 +395,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.Get().DeviceScaleFactor()
return uiDriver().DeviceScaleFactor()
}
// IsVsyncEnabled returns a boolean value indicating whether
@ -405,7 +403,7 @@ func DeviceScaleFactor() float64 {
//
// IsVsyncEnabled is concurrent-safe.
func IsVsyncEnabled() bool {
return uidriver.Get().IsVsyncEnabled()
return uiDriver().IsVsyncEnabled()
}
// SetVsyncEnabled sets a boolean value indicating whether
@ -423,7 +421,7 @@ func IsVsyncEnabled() bool {
//
// SetVsyncEnabled is concurrent-safe.
func SetVsyncEnabled(enabled bool) {
uidriver.Get().SetVsyncEnabled(enabled)
uiDriver().SetVsyncEnabled(enabled)
}
// MaxTPS returns the current maximum TPS.

View File

@ -21,15 +21,13 @@ 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.Get())
graphicscommand.SetGraphicsDriver(graphicsdriver.Get())
shareable.SetGraphicsDriver(graphicsDriver())
graphicscommand.SetGraphicsDriver(graphicsDriver())
}
func newUIContext(f func(*Image) error) *uiContext {
@ -64,7 +62,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.Get().ScreenPadding()
px0, py0, px1, py1 := uiDriver().ScreenPadding()
c.screen = newScreenFramebufferImage(w+int(math.Ceil(px0+px1)), h+int(math.Ceil(py0+py1)))
c.screenWidth = w
c.screenHeight = h
@ -98,7 +96,7 @@ func (c *uiContext) Update(afterFrameUpdate func()) error {
return err
}
uidriver.Get().Input().ResetForFrame()
uiDriver().Input().ResetForFrame()
afterFrameUpdate()
}
@ -107,7 +105,7 @@ func (c *uiContext) Update(afterFrameUpdate func()) error {
op := &DrawImageOptions{}
switch vd := graphicsdriver.Get().VDirection(); vd {
switch vd := graphicsDriver().VDirection(); vd {
case driver.VDownward:
// c.screen is special: its Y axis is down to up,
// and the origin point is lower left.

View File

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

View File

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

View File

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

View File

@ -20,15 +20,15 @@
// +build !linux,cgo !cgo
// +build !windows
package uidriver
package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/driver"
)
func Get() driver.UI {
func uiDriver() driver.UI {
if !driver.IsPlayground {
panic("ebiten: a UI driver is not implemented on this environment")
panic("ebiten: a UI driver is not implemented on this environment (Cgo is not enabled?)")
}
// TODO: Implement this
return nil