diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index d5db026ce..f867a10cb 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -68,7 +68,7 @@ func Init() *opengl.Context { return u.context } -func Start(width, height, scale int, title string) (framebufferScale int, err error) { +func Start(width, height, scale int, title string) error { return currentUI.start(width, height, scale, title) } @@ -88,14 +88,16 @@ func SwapBuffers() { currentUI.swapBuffers() } -func SetScreenSize(width, height int) (bool, int) { - result := currentUI.setScreenSize(width, height, currentUI.scale) - return result, currentUI.framebufferScale() +func SetScreenSize(width, height int) bool { + return currentUI.setScreenSize(width, height, currentUI.scale) } -func SetScreenScale(scale int) (bool, int) { - result := currentUI.setScreenSize(currentUI.width, currentUI.height, scale) - return result, currentUI.framebufferScale() +func SetScreenScale(scale int) bool { + return currentUI.setScreenSize(currentUI.width, currentUI.height, scale) +} + +func ActualScale() int { + return currentUI.actualScale() } type userInterface struct { @@ -104,16 +106,16 @@ type userInterface struct { height int scale int deviceScaleFactor float64 - framebufferScale_ int + framebufferScale int context *opengl.Context } -func (u *userInterface) start(width, height, scale int, title string) (framebufferScale int, err error) { +func (u *userInterface) start(width, height, scale int, title string) error { m := glfw.GetPrimaryMonitor() v := m.GetVideoMode() mw, _ := m.GetPhysicalSize() u.deviceScaleFactor = 1 - u.framebufferScale_ = 1 + u.framebufferScale = 1 // mw can be 0 on some environment like Linux VM if 0 < mw { dpi := float64(v.Width) * 25.4 / float64(mw) @@ -128,15 +130,15 @@ func (u *userInterface) start(width, height, scale int, title string) (framebuff y := (v.Height - height*u.windowScale()) / 3 u.window.SetPos(x, y) - return u.framebufferScale(), nil + return nil } func (u *userInterface) windowScale() int { return int(float64(u.scale) * u.deviceScaleFactor) } -func (u *userInterface) framebufferScale() int { - return u.windowScale() * u.framebufferScale_ +func (u *userInterface) actualScale() int { + return u.windowScale() * u.framebufferScale } func (u *userInterface) pollEvents() error { @@ -207,6 +209,6 @@ event: } // This is usually 1, but sometimes more than 1 (e.g. Retina Mac) fw, _ := window.GetFramebufferSize() - u.framebufferScale_ = fw / width / u.windowScale() + u.framebufferScale = fw / width / u.windowScale() return true } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 25257267a..b3fa0e7f6 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -29,7 +29,7 @@ func Now() int64 { return int64(js.Global.Get("performance").Call("now").Float() * float64(time.Millisecond)) } -func Start(width, height, scale int, title string) (actualScale int, err error) { +func Start(width, height, scale int, title string) error { return currentUI.start(width, height, scale, title) } @@ -49,18 +49,18 @@ func SwapBuffers() { currentUI.swapBuffers() } -func SetScreenSize(width, height int) (bool, int) { +func SetScreenSize(width, height int) bool { scale := canvas.Get("dataset").Get("ebitenScale").Int() - result := currentUI.setScreenSize(width, height, scale) - actualScale := canvas.Get("dataset").Get("ebitenActualScale").Int() - return result, actualScale + return currentUI.setScreenSize(width, height, scale) } -func SetScreenScale(scale int) (bool, int) { +func SetScreenScale(scale int) bool { width, height := currentUI.size() - result := currentUI.setScreenSize(width, height, scale) - actualScale := canvas.Get("dataset").Get("ebitenActualScale").Int() - return result, actualScale + return currentUI.setScreenSize(width, height, scale) +} + +func ActualScale() int { + return canvas.Get("dataset").Get("ebitenActualScale").Int() } var canvas *js.Object @@ -240,14 +240,12 @@ func devicePixelRatio() int { return ratio } -func (u *userInterface) start(width, height, scale int, title string) (actualScale int, err error) { +func (u *userInterface) start(width, height, scale int, title string) error { doc := js.Global.Get("document") doc.Set("title", title) u.setScreenSize(width, height, scale) canvas.Call("focus") - - actualScale = canvas.Get("dataset").Get("ebitenActualScale").Int() - return actualScale, nil + return nil } func (*userInterface) size() (width, height int) { diff --git a/run.go b/run.go index c6f7c44c0..f0cb46e64 100644 --- a/run.go +++ b/run.go @@ -48,14 +48,13 @@ func Run(f func(*Image) error, width, height, scale int, title string) error { runContext.running = false }() - actualScale, err := ui.Start(width, height, scale, title) - if err != nil { + if err := ui.Start(width, height, scale, title); err != nil { return err } defer ui.Terminate() glContext.Check() - graphicsContext, err := newGraphicsContext(width, height, actualScale) + graphicsContext, err := newGraphicsContext(width, height, ui.ActualScale()) if err != nil { return err } @@ -67,21 +66,17 @@ func Run(f func(*Image) error, width, height, scale int, title string) error { // TODO: setSize should be called after swapping buffers? if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight || 0 < runContext.newScreenScale { changed := false - actualScale := 0 if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight { - c, a := ui.SetScreenSize(runContext.newScreenWidth, runContext.newScreenHeight) + c := ui.SetScreenSize(runContext.newScreenWidth, runContext.newScreenHeight) changed = changed || c - actualScale = a } if 0 < runContext.newScreenScale { - c, a := ui.SetScreenScale(runContext.newScreenScale) + c := ui.SetScreenScale(runContext.newScreenScale) changed = changed || c - // actualScale of SetScreenState is more reliable than one of SetScreenSize - actualScale = a } if changed { w, h := runContext.newScreenWidth, runContext.newScreenHeight - if err := graphicsContext.setSize(w, h, actualScale); err != nil { + if err := graphicsContext.setSize(w, h, ui.ActualScale()); err != nil { return err } }