ui: Implement SetScreenSize/Scale for mobiles

Fixes #250
This commit is contained in:
Hajime Hoshi 2017-12-31 21:01:48 +09:00
parent 5d4c4f1283
commit 9478801076

View File

@ -20,6 +20,7 @@ import (
"errors" "errors"
"image" "image"
"runtime" "runtime"
"sync"
"time" "time"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
@ -51,21 +52,26 @@ type userInterface struct {
height int height int
scale float64 scale float64
sizeChanged bool sizeChanged bool
m sync.RWMutex
} }
var ( var (
chRender = make(chan struct{}) chRender = make(chan struct{})
chRenderEnd = make(chan struct{}) chRenderEnd = make(chan struct{})
currentUI = &userInterface{ currentUI = &userInterface{}
sizeChanged: true,
}
) )
func Run(width, height int, scale float64, title string, g GraphicsContext) error { func Run(width, height int, scale float64, title string, g GraphicsContext) error {
u := currentUI u := currentUI
u.m.Lock()
u.width = width u.width = width
u.height = height u.height = height
u.scale = scale u.scale = scale
u.sizeChanged = true
u.m.Unlock()
// title is ignored? // title is ignored?
opengl.Init() opengl.Init()
for { for {
@ -81,11 +87,25 @@ func (u *userInterface) update(g GraphicsContext) error {
chRenderEnd <- struct{}{} chRenderEnd <- struct{}{}
}() }()
if u.sizeChanged { sizeChanged := false
// Sizing also calls GL functions width, height := 0, 0
u.sizeChanged = false actualScale := 0.0
g.SetSize(u.width, u.height, u.actualScreenScale())
u.m.Lock()
sizeChanged = u.sizeChanged
if sizeChanged {
width = u.width
height = u.height
actualScale = u.scale * deviceScale()
} }
u.sizeChanged = false
u.m.Unlock()
if sizeChanged {
// Sizing also calls GL functions
g.SetSize(width, height, actualScale)
}
if err := g.Update(func() {}); err != nil { if err := g.Update(func() {}); err != nil {
return err return err
} }
@ -93,17 +113,40 @@ func (u *userInterface) update(g GraphicsContext) error {
} }
func SetScreenSize(width, height int) bool { func SetScreenSize(width, height int) bool {
// TODO: Implement currentUI.setScreenSize(width, height)
return false return true
}
func (u *userInterface) setScreenSize(width, height int) {
u.m.Lock()
if u.width != width || u.height != height {
u.width = width
u.height = height
u.sizeChanged = true
}
u.m.Unlock()
} }
func SetScreenScale(scale float64) bool { func SetScreenScale(scale float64) bool {
// TODO: Implement currentUI.setScreenScale(scale)
return false return false
} }
func (u *userInterface) setScreenScale(scale float64) {
u.m.Lock()
if u.scale != scale {
u.scale = scale
u.sizeChanged = true
}
u.m.Unlock()
}
func ScreenScale() float64 { func ScreenScale() float64 {
return currentUI.scale u := currentUI
u.m.RLock()
s := u.scale
u.m.RUnlock()
return s
} }
func ScreenOffset() (float64, float64) { func ScreenOffset() (float64, float64) {
@ -144,10 +187,6 @@ func SetWindowIcon(iconImages []image.Image) {
// Do nothing // Do nothing
} }
func (u *userInterface) actualScreenScale() float64 {
return u.scale * deviceScale()
}
func UpdateTouches(touches []Touch) { func UpdateTouches(touches []Touch) {
currentInput.updateTouches(touches) currentInput.updateTouches(touches)
} }