ui: Refactoring: Add deviceScale struct

This commit is contained in:
Hajime Hoshi 2018-10-02 23:06:56 +09:00
parent 635d624b77
commit c494b53822
3 changed files with 48 additions and 25 deletions

View File

@ -0,0 +1,38 @@
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ui
import (
"github.com/hajimehoshi/ebiten/internal/devicescale"
)
type deviceScale struct {
val float64
frame int64
lastUpdated int64
}
func (d *deviceScale) Update() {
d.frame++
}
func (d *deviceScale) Get() float64 {
// As devicescale.DeviceScale accesses OS API, not call this too often.
if d.val == 0 || d.frame-d.lastUpdated > 30 {
d.val = devicescale.DeviceScale()
d.lastUpdated = d.frame
}
return d.val
}

View File

@ -28,7 +28,6 @@ import (
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/hajimehoshi/ebiten/internal/devicescale"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/input"
"github.com/hajimehoshi/ebiten/internal/opengl"
@ -51,10 +50,8 @@ type userInterface struct {
runnableInBackground bool
vsync bool
deviceScale float64
frame int64
deviceScaleUpdated int64
lastActualScale float64
deviceScale deviceScale
lastActualScale float64
initFullscreen bool
initCursorVisible bool
@ -388,7 +385,7 @@ func ScreenPadding() (x0, y0, x1, y1 float64) {
m := glfw.GetPrimaryMonitor()
v := m.GetVideoMode()
d := devicescale.DeviceScale()
d := u.deviceScale.Get()
mx := float64(v.Width) * d / glfwScale()
my := float64(v.Height) * d / glfwScale()
@ -545,12 +542,7 @@ func (u *userInterface) getScale() float64 {
// actualScreenScale must be called from the main thread.
func (u *userInterface) actualScreenScale() float64 {
// As devicescale.DeviceScale accesses OS API, not call this too often.
if u.deviceScale == 0 || u.frame-u.deviceScaleUpdated > 30 {
u.deviceScale = devicescale.DeviceScale()
u.deviceScaleUpdated = u.frame
}
return u.getScale() * u.deviceScale
return u.getScale() * u.deviceScale.Get()
}
// pollEvents must be called from the main thread.
@ -639,7 +631,7 @@ func (u *userInterface) loop(g GraphicsContext) error {
return err
}
u.frame++
u.deviceScale.Update()
u.m.Lock()
vsync := u.vsync
u.m.Unlock()
@ -688,7 +680,7 @@ func (u *userInterface) setScreenSize(width, height int, scale float64, fullscre
u.width = width
u.windowWidth = width
s := scale * devicescale.DeviceScale()
s := scale * u.deviceScale.Get()
if int(float64(width)*s) < minWindowWidth {
u.windowWidth = int(math.Ceil(minWindowWidth / s))
}

View File

@ -22,7 +22,6 @@ import (
"github.com/gopherjs/gopherwasm/js"
"github.com/hajimehoshi/ebiten/internal/devicescale"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/input"
"github.com/hajimehoshi/ebiten/internal/opengl"
@ -42,10 +41,8 @@ type userInterface struct {
windowFocus bool
pageVisible bool
deviceScale float64
frame int64
deviceScaleUpdated int64
lastActualScale float64
deviceScale deviceScale
lastActualScale float64
}
var currentUI = &userInterface{
@ -178,11 +175,7 @@ func (u *userInterface) actualScreenScale() float64 {
// * Chrome just after restoring the lost context
// * Safari
// Let's use the devicePixelRatio as it is here.
if u.deviceScale == 0 || u.frame-u.deviceScaleUpdated > 30 {
u.deviceScale = devicescale.DeviceScale()
u.deviceScaleUpdated = u.frame
}
return u.getScale() * u.deviceScale
return u.getScale() * u.deviceScale.Get()
}
func (u *userInterface) updateGraphicsContext(g GraphicsContext) {
@ -240,7 +233,7 @@ func (u *userInterface) loop(g GraphicsContext) error {
close(ch)
return
}
u.frame++
u.deviceScale.Update()
if u.vsync {
requestAnimationFrame.Invoke(cf)
} else {