mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Move ui_*.go back to internal/ui again
This commit is contained in:
parent
a1aed91cf9
commit
3964944deb
@ -15,12 +15,24 @@
|
||||
package ebiten
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/audio"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics/internal/opengl"
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
"image"
|
||||
)
|
||||
|
||||
var glContext *opengl.Context
|
||||
|
||||
func init() {
|
||||
ui.Init()
|
||||
ui.ExecOnUIThread(func() {
|
||||
glContext = opengl.NewContext()
|
||||
})
|
||||
audio.Init()
|
||||
audio.Start()
|
||||
}
|
||||
|
||||
// IsKeyPressed returns a boolean indicating whether key is pressed.
|
||||
func IsKeyPressed(key Key) bool {
|
||||
return ui.IsKeyPressed(ui.Key(key))
|
||||
|
@ -17,8 +17,15 @@ package ebiten
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics/internal/opengl"
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
)
|
||||
|
||||
func useGLContext(f func(*opengl.Context)) {
|
||||
ui.ExecOnUIThread(func() {
|
||||
f(glContext)
|
||||
})
|
||||
}
|
||||
|
||||
func newGraphicsContext(c *opengl.Context, screenWidth, screenHeight, screenScale int) (*graphicsContext, error) {
|
||||
f, err := graphics.NewZeroFramebuffer(c, screenWidth*screenScale, screenHeight*screenScale)
|
||||
if err != nil {
|
||||
|
@ -50,6 +50,11 @@ func audioProcess(channel int) func(e js.Object) {
|
||||
}
|
||||
|
||||
func initialize() {
|
||||
// Do nothing in node.js.
|
||||
if js.Global.Get("require") != js.Undefined {
|
||||
return
|
||||
}
|
||||
|
||||
context = js.Global.Get("AudioContext").New()
|
||||
// TODO: ScriptProcessorNode will be replaced with Audio WebWorker.
|
||||
// https://developer.mozilla.org/ja/docs/Web/API/ScriptProcessorNode
|
||||
|
@ -40,6 +40,7 @@ func toBytes(l, r []int16) []byte {
|
||||
}
|
||||
|
||||
func initialize() {
|
||||
// Creating OpenAL device must be done after initializing UI. I'm not sure the reason.
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
runtime.LockOSThread()
|
||||
|
@ -66,7 +66,27 @@ type context struct {
|
||||
|
||||
var lastFramebuffer Framebuffer
|
||||
|
||||
func NewContext(gl *webgl.Context) *Context {
|
||||
func NewContext() *Context {
|
||||
var gl *webgl.Context
|
||||
|
||||
if js.Global.Get("require") == js.Undefined {
|
||||
// TODO: Define id?
|
||||
canvas := js.Global.Get("document").Call("querySelector", "canvas")
|
||||
var err error
|
||||
gl, err = webgl.NewContext(canvas, &webgl.ContextAttributes{
|
||||
Alpha: true,
|
||||
PremultipliedAlpha: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
// Use headless-gl for testing.
|
||||
nodeGl := js.Global.Call("require", "gl")
|
||||
webglContext := nodeGl.Call("createContext", 16, 16)
|
||||
gl = &webgl.Context{Object: webglContext}
|
||||
}
|
||||
|
||||
c := &Context{
|
||||
Nearest: Filter(gl.NEAREST),
|
||||
Linear: Filter(gl.LINEAR),
|
||||
|
@ -21,7 +21,7 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
func UpdateInput(window *glfw.Window, scale int) error {
|
||||
func updateInput(window *glfw.Window, scale int) error {
|
||||
return currentInput.update(window, scale)
|
||||
}
|
||||
|
||||
|
@ -14,30 +14,18 @@
|
||||
|
||||
// +build !js
|
||||
|
||||
package ebiten
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
glfw "github.com/go-gl/glfw3"
|
||||
"github.com/hajimehoshi/ebiten/internal/audio"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics/internal/opengl"
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
var currentUI *userInterface
|
||||
|
||||
func useGLContext(f func(*opengl.Context)) {
|
||||
ch := make(chan struct{})
|
||||
currentUI.funcs <- func() {
|
||||
defer close(ch)
|
||||
f(currentUI.glContext)
|
||||
}
|
||||
<-ch
|
||||
}
|
||||
|
||||
func init() {
|
||||
func Init() {
|
||||
runtime.LockOSThread()
|
||||
|
||||
glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) {
|
||||
@ -61,23 +49,48 @@ func init() {
|
||||
go func() {
|
||||
runtime.LockOSThread()
|
||||
u.window.MakeContextCurrent()
|
||||
u.glContext = opengl.NewContext()
|
||||
glfw.SwapInterval(1)
|
||||
for f := range u.funcs {
|
||||
f()
|
||||
}
|
||||
}()
|
||||
|
||||
audio.Init()
|
||||
|
||||
currentUI = u
|
||||
}
|
||||
|
||||
func ExecOnUIThread(f func()) {
|
||||
ch := make(chan struct{})
|
||||
currentUI.funcs <- func() {
|
||||
defer close(ch)
|
||||
f()
|
||||
}
|
||||
<-ch
|
||||
}
|
||||
|
||||
func Start(width, height, scale int, title string) (actualScale int, err error) {
|
||||
return currentUI.start(width, height, scale, title)
|
||||
}
|
||||
|
||||
func Terminate() {
|
||||
currentUI.terminate()
|
||||
}
|
||||
|
||||
func DoEvents() error {
|
||||
return currentUI.doEvents()
|
||||
}
|
||||
|
||||
func IsClosed() bool {
|
||||
return currentUI.isClosed()
|
||||
}
|
||||
|
||||
func SwapBuffers() {
|
||||
currentUI.swapBuffers()
|
||||
}
|
||||
|
||||
type userInterface struct {
|
||||
window *glfw.Window
|
||||
scale int
|
||||
glContext *opengl.Context
|
||||
funcs chan func()
|
||||
window *glfw.Window
|
||||
scale int
|
||||
funcs chan func()
|
||||
}
|
||||
|
||||
func (u *userInterface) start(width, height, scale int, title string) (actualScale int, err error) {
|
||||
@ -122,14 +135,12 @@ func (u *userInterface) start(width, height, scale int, title string) (actualSca
|
||||
windowWidth, _ := window.GetFramebufferSize()
|
||||
actualScale = windowWidth / width
|
||||
|
||||
audio.Start()
|
||||
|
||||
return actualScale, nil
|
||||
}
|
||||
|
||||
func (u *userInterface) pollEvents() error {
|
||||
glfw.PollEvents()
|
||||
return ui.UpdateInput(u.window, u.scale)
|
||||
return updateInput(u.window, u.scale)
|
||||
}
|
||||
|
||||
func (u *userInterface) doEvents() error {
|
@ -14,19 +14,38 @@
|
||||
|
||||
// +build js
|
||||
|
||||
package ebiten
|
||||
package ui
|
||||
|
||||
import (
|
||||
"github.com/gopherjs/gopherjs/js"
|
||||
"github.com/gopherjs/webgl"
|
||||
"github.com/hajimehoshi/ebiten/internal/audio"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics/internal/opengl"
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ExecOnUIThread(f func()) {
|
||||
f()
|
||||
}
|
||||
|
||||
func Start(width, height, scale int, title string) (actualScale int, err error) {
|
||||
return currentUI.start(width, height, scale, title)
|
||||
}
|
||||
|
||||
func Terminate() {
|
||||
currentUI.terminate()
|
||||
}
|
||||
|
||||
func DoEvents() error {
|
||||
return currentUI.doEvents()
|
||||
}
|
||||
|
||||
func IsClosed() bool {
|
||||
return currentUI.isClosed()
|
||||
}
|
||||
|
||||
func SwapBuffers() {
|
||||
currentUI.swapBuffers()
|
||||
}
|
||||
|
||||
var canvas js.Object
|
||||
var context *opengl.Context
|
||||
|
||||
type userInterface struct{}
|
||||
|
||||
@ -38,10 +57,6 @@ func shown() bool {
|
||||
return !js.Global.Get("document").Get("hidden").Bool()
|
||||
}
|
||||
|
||||
func useGLContext(f func(*opengl.Context)) {
|
||||
f(context)
|
||||
}
|
||||
|
||||
func vsync() {
|
||||
ch := make(chan struct{})
|
||||
// TODO: In iOS8, this is called at every 1/30[sec] frame.
|
||||
@ -57,7 +72,7 @@ func (*userInterface) doEvents() error {
|
||||
for !shown() {
|
||||
vsync()
|
||||
}
|
||||
ui.CurrentInput().UpdateGamepads()
|
||||
currentInput.UpdateGamepads()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -73,12 +88,9 @@ func (*userInterface) swapBuffers() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
func init() {
|
||||
func Init() {
|
||||
// Do nothing in node.js.
|
||||
if js.Global.Get("require") != js.Undefined {
|
||||
// Use headless-gl for testing.
|
||||
nodeGl := js.Global.Call("require", "gl")
|
||||
webglContext := nodeGl.Call("createContext", 16, 16)
|
||||
context = opengl.NewContext(&webgl.Context{Object: webglContext})
|
||||
return
|
||||
}
|
||||
|
||||
@ -115,15 +127,6 @@ func init() {
|
||||
canvasStyle := canvas.Get("style")
|
||||
canvasStyle.Set("position", "absolute")
|
||||
|
||||
webglContext, err := webgl.NewContext(canvas, &webgl.ContextAttributes{
|
||||
Alpha: true,
|
||||
PremultipliedAlpha: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
context = opengl.NewContext(webglContext)
|
||||
|
||||
// Make the canvas focusable.
|
||||
canvas.Call("setAttribute", "tabindex", 1)
|
||||
canvas.Get("style").Set("outline", "none")
|
||||
@ -132,25 +135,25 @@ func init() {
|
||||
canvas.Call("addEventListener", "keydown", func(e js.Object) {
|
||||
e.Call("preventDefault")
|
||||
code := e.Get("keyCode").Int()
|
||||
ui.CurrentInput().KeyDown(code)
|
||||
currentInput.KeyDown(code)
|
||||
})
|
||||
canvas.Call("addEventListener", "keyup", func(e js.Object) {
|
||||
e.Call("preventDefault")
|
||||
code := e.Get("keyCode").Int()
|
||||
ui.CurrentInput().KeyUp(code)
|
||||
currentInput.KeyUp(code)
|
||||
})
|
||||
|
||||
// Mouse
|
||||
canvas.Call("addEventListener", "mousedown", func(e js.Object) {
|
||||
e.Call("preventDefault")
|
||||
button := e.Get("button").Int()
|
||||
ui.CurrentInput().MouseDown(button)
|
||||
currentInput.MouseDown(button)
|
||||
setMouseCursorFromEvent(e)
|
||||
})
|
||||
canvas.Call("addEventListener", "mouseup", func(e js.Object) {
|
||||
e.Call("preventDefault")
|
||||
button := e.Get("button").Int()
|
||||
ui.CurrentInput().MouseUp(button)
|
||||
currentInput.MouseUp(button)
|
||||
setMouseCursorFromEvent(e)
|
||||
})
|
||||
canvas.Call("addEventListener", "mousemove", func(e js.Object) {
|
||||
@ -165,14 +168,14 @@ func init() {
|
||||
// TODO: Need to create indimendent touch functions?
|
||||
canvas.Call("addEventListener", "touchstart", func(e js.Object) {
|
||||
e.Call("preventDefault")
|
||||
ui.CurrentInput().MouseDown(0)
|
||||
currentInput.MouseDown(0)
|
||||
touches := e.Get("changedTouches")
|
||||
touch := touches.Index(0)
|
||||
setMouseCursorFromEvent(touch)
|
||||
})
|
||||
canvas.Call("addEventListener", "touchend", func(e js.Object) {
|
||||
e.Call("preventDefault")
|
||||
ui.CurrentInput().MouseUp(0)
|
||||
currentInput.MouseUp(0)
|
||||
touches := e.Get("changedTouches")
|
||||
touch := touches.Index(0)
|
||||
setMouseCursorFromEvent(touch)
|
||||
@ -188,8 +191,6 @@ func init() {
|
||||
window.Call("addEventListener", "gamepadconnected", func(e js.Object) {
|
||||
// Do nothing.
|
||||
})
|
||||
|
||||
audio.Init()
|
||||
}
|
||||
|
||||
func setMouseCursorFromEvent(e js.Object) {
|
||||
@ -198,7 +199,7 @@ func setMouseCursorFromEvent(e js.Object) {
|
||||
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
||||
x -= rect.Get("left").Int()
|
||||
y -= rect.Get("top").Int()
|
||||
ui.CurrentInput().SetMouseCursor(x/scale, y/scale)
|
||||
currentInput.SetMouseCursor(x/scale, y/scale)
|
||||
}
|
||||
|
||||
func devicePixelRatio() int {
|
||||
@ -229,7 +230,5 @@ func (*userInterface) start(width, height, scale int, title string) (actualScale
|
||||
|
||||
canvas.Call("focus")
|
||||
|
||||
audio.Start()
|
||||
|
||||
return actualScale, nil
|
||||
}
|
12
run.go
12
run.go
@ -17,6 +17,7 @@ package ebiten
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/audio"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics/internal/opengl"
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -37,12 +38,11 @@ func CurrentFPS() float64 {
|
||||
// but this is not strictly guaranteed.
|
||||
// If you need to care about time, you need to check current time every time f is called.
|
||||
func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
ui := currentUI
|
||||
actualScale, err := ui.start(width, height, scale, title)
|
||||
actualScale, err := ui.Start(width, height, scale, title)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer ui.terminate()
|
||||
defer ui.Terminate()
|
||||
|
||||
var graphicsContext *graphicsContext
|
||||
useGLContext(func(c *opengl.Context) {
|
||||
@ -55,10 +55,10 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
frames := 0
|
||||
t := time.Now().UnixNano()
|
||||
for {
|
||||
if err := ui.doEvents(); err != nil {
|
||||
if err := ui.DoEvents(); err != nil {
|
||||
return err
|
||||
}
|
||||
if ui.isClosed() {
|
||||
if ui.IsClosed() {
|
||||
return nil
|
||||
}
|
||||
if err := graphicsContext.preUpdate(); err != nil {
|
||||
@ -72,7 +72,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
}
|
||||
// TODO: I'm not sure this is 'Update'. Is 'Tick' better?
|
||||
audio.Update()
|
||||
ui.swapBuffers()
|
||||
ui.SwapBuffers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user