mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
Add internal/devicescale
This commit is contained in:
parent
9578307d7b
commit
c82809867d
25
internal/devicescale/devicescale.go
Normal file
25
internal/devicescale/devicescale.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// 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 devicescale
|
||||||
|
|
||||||
|
var scale = 0.0
|
||||||
|
|
||||||
|
func DeviceScale() float64 {
|
||||||
|
if scale != 0.0 {
|
||||||
|
return scale
|
||||||
|
}
|
||||||
|
scale = impl()
|
||||||
|
return scale
|
||||||
|
}
|
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package ui
|
package devicescale
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
@ -63,19 +63,13 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/jni"
|
"github.com/hajimehoshi/ebiten/internal/jni"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func impl() float64 {
|
||||||
androidDeviceScale = 0.0
|
s := 0.0
|
||||||
)
|
|
||||||
|
|
||||||
func deviceScale() float64 {
|
|
||||||
if 0 < androidDeviceScale {
|
|
||||||
return androidDeviceScale
|
|
||||||
}
|
|
||||||
if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error {
|
if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error {
|
||||||
androidDeviceScale = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx)))
|
s = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx)))
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
panic(fmt.Sprintf("ui: error %v", err))
|
panic(fmt.Sprintf("devicescale: error %v", err))
|
||||||
}
|
}
|
||||||
return androidDeviceScale
|
return s
|
||||||
}
|
}
|
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
// +build ios
|
// +build ios
|
||||||
|
|
||||||
package ui
|
package devicescale
|
||||||
|
|
||||||
// #cgo CFLAGS: -x objective-c
|
// #cgo CFLAGS: -x objective-c
|
||||||
// #cgo LDFLAGS: -framework Foundation -framework UIKit
|
// #cgo LDFLAGS: -framework Foundation -framework UIKit
|
||||||
@ -26,6 +26,6 @@ package ui
|
|||||||
// }
|
// }
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
func deviceScale() float64 {
|
func impl() float64 {
|
||||||
return float64(C.devicePixelRatio())
|
return float64(C.devicePixelRatio())
|
||||||
}
|
}
|
29
internal/devicescale/devicescale_js.go
Normal file
29
internal/devicescale/devicescale_js.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
// +build js
|
||||||
|
|
||||||
|
package devicescale
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gopherjs/gopherjs/js"
|
||||||
|
)
|
||||||
|
|
||||||
|
func impl() float64 {
|
||||||
|
ratio := js.Global.Get("window").Get("devicePixelRatio").Float()
|
||||||
|
if ratio == 0 {
|
||||||
|
ratio = 1
|
||||||
|
}
|
||||||
|
return ratio
|
||||||
|
}
|
34
internal/devicescale/devicescale_mac.go
Normal file
34
internal/devicescale/devicescale_mac.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
// +build darwin
|
||||||
|
// +build !js
|
||||||
|
// +build !ios
|
||||||
|
|
||||||
|
package devicescale
|
||||||
|
|
||||||
|
// #cgo CFLAGS: -x objective-c
|
||||||
|
// #cgo LDFLAGS: -framework AppKit
|
||||||
|
//
|
||||||
|
// #import <AppKit/AppKit.h>
|
||||||
|
//
|
||||||
|
// static float scale() {
|
||||||
|
// NSScreen* primary = [[NSScreen screens] firstObject];
|
||||||
|
// return [primary backingScaleFactor];
|
||||||
|
// }
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
func impl() float64 {
|
||||||
|
return float64(C.scale())
|
||||||
|
}
|
41
internal/devicescale/devicescale_windows.go
Normal file
41
internal/devicescale/devicescale_windows.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
// +build !js
|
||||||
|
|
||||||
|
package devicescale
|
||||||
|
|
||||||
|
// TODO: Use golang.org/x/sys/windows (NewLazyDLL) instead of cgo.
|
||||||
|
|
||||||
|
// #cgo LDFLAGS: -lgdi32
|
||||||
|
//
|
||||||
|
// #include <windows.h>
|
||||||
|
//
|
||||||
|
// static char* getDPI(int* dpi) {
|
||||||
|
// HDC dc = GetWindowDC(0);
|
||||||
|
// *dpi = GetDeviceCaps(dc, LOGPIXELSX);
|
||||||
|
// if (!ReleaseDC(0, dc)) {
|
||||||
|
// return "ReleaseDC failed";
|
||||||
|
// }
|
||||||
|
// return "";
|
||||||
|
// }
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
func impl() float64 {
|
||||||
|
dpi := C.int(0)
|
||||||
|
if errmsg := C.GoString(C.getDPI(&dpi)); errmsg != "" {
|
||||||
|
panic(errmsg)
|
||||||
|
}
|
||||||
|
return float64(dpi) / 96
|
||||||
|
}
|
24
internal/devicescale/devicescale_xwindow.go
Normal file
24
internal/devicescale/devicescale_xwindow.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
// +build freebsd linux
|
||||||
|
// +build !js
|
||||||
|
// +build !android
|
||||||
|
|
||||||
|
package devicescale
|
||||||
|
|
||||||
|
func impl() float64 {
|
||||||
|
// TODO: Implement this
|
||||||
|
return 1
|
||||||
|
}
|
@ -27,6 +27,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-gl/glfw/v3.2/glfw"
|
"github.com/go-gl/glfw/v3.2/glfw"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,10 +39,9 @@ type userInterface struct {
|
|||||||
windowWidth int
|
windowWidth int
|
||||||
height int
|
height int
|
||||||
|
|
||||||
scale float64
|
scale float64
|
||||||
cachedDeviceScale float64
|
cachedGLFWScale float64
|
||||||
cachedGLFWScale float64
|
fullscreenScale float64
|
||||||
fullscreenScale float64
|
|
||||||
|
|
||||||
running bool
|
running bool
|
||||||
sizeChanged bool
|
sizeChanged bool
|
||||||
@ -309,9 +310,10 @@ func ScreenOffset() (float64, float64) {
|
|||||||
oy := 0.0
|
oy := 0.0
|
||||||
m := glfw.GetPrimaryMonitor()
|
m := glfw.GetPrimaryMonitor()
|
||||||
v := m.GetVideoMode()
|
v := m.GetVideoMode()
|
||||||
|
d := devicescale.DeviceScale()
|
||||||
_ = u.runOnMainThread(func() error {
|
_ = u.runOnMainThread(func() error {
|
||||||
ox = (float64(v.Width)*u.deviceScale()/u.glfwScale() - float64(u.width)*u.actualScreenScale()) / 2
|
ox = (float64(v.Width)*d/u.glfwScale() - float64(u.width)*u.actualScreenScale()) / 2
|
||||||
oy = (float64(v.Height)*u.deviceScale()/u.glfwScale() - float64(u.height)*u.actualScreenScale()) / 2
|
oy = (float64(v.Height)*d/u.glfwScale() - float64(u.height)*u.actualScreenScale()) / 2
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return ox, oy
|
return ox, oy
|
||||||
@ -395,13 +397,6 @@ func (u *userInterface) glfwScale() float64 {
|
|||||||
return u.cachedGLFWScale
|
return u.cachedGLFWScale
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) deviceScale() float64 {
|
|
||||||
if u.cachedDeviceScale == 0 {
|
|
||||||
u.cachedDeviceScale = deviceScale()
|
|
||||||
}
|
|
||||||
return u.cachedDeviceScale
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *userInterface) glfwSize() (int, int) {
|
func (u *userInterface) glfwSize() (int, int) {
|
||||||
w := int(float64(u.windowWidth) * u.getScale() * u.glfwScale())
|
w := int(float64(u.windowWidth) * u.getScale() * u.glfwScale())
|
||||||
h := int(float64(u.height) * u.getScale() * u.glfwScale())
|
h := int(float64(u.height) * u.getScale() * u.glfwScale())
|
||||||
@ -427,7 +422,7 @@ func (u *userInterface) getScale() float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) actualScreenScale() float64 {
|
func (u *userInterface) actualScreenScale() float64 {
|
||||||
return u.getScale() * u.deviceScale()
|
return u.getScale() * devicescale.DeviceScale()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) pollEvents() {
|
func (u *userInterface) pollEvents() {
|
||||||
@ -525,7 +520,7 @@ func (u *userInterface) setScreenSize(width, height int, scale float64, fullscre
|
|||||||
|
|
||||||
u.width = width
|
u.width = width
|
||||||
u.windowWidth = width
|
u.windowWidth = width
|
||||||
s := scale * u.deviceScale()
|
s := scale * devicescale.DeviceScale()
|
||||||
if int(float64(width)*s) < minWindowWidth {
|
if int(float64(width)*s) < minWindowWidth {
|
||||||
u.windowWidth = int(math.Ceil(minWindowWidth / s))
|
u.windowWidth = int(math.Ceil(minWindowWidth / s))
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,8 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -346,14 +348,6 @@ func setMouseCursorFromEvent(e *js.Object) {
|
|||||||
currentInput.setMouseCursor(int(float64(x)/scale), int(float64(y)/scale))
|
currentInput.setMouseCursor(int(float64(x)/scale), int(float64(y)/scale))
|
||||||
}
|
}
|
||||||
|
|
||||||
func devicePixelRatio() float64 {
|
|
||||||
ratio := js.Global.Get("window").Get("devicePixelRatio").Float()
|
|
||||||
if ratio == 0 {
|
|
||||||
ratio = 1
|
|
||||||
}
|
|
||||||
return ratio
|
|
||||||
}
|
|
||||||
|
|
||||||
func RunMainThreadLoop(ch <-chan error) error {
|
func RunMainThreadLoop(ch <-chan error) error {
|
||||||
return <-ch
|
return <-ch
|
||||||
}
|
}
|
||||||
@ -389,7 +383,7 @@ func (u *userInterface) updateScreenSize() {
|
|||||||
// * Chrome just after restoring the lost context
|
// * Chrome just after restoring the lost context
|
||||||
// * Safari
|
// * Safari
|
||||||
// Let's use the pixel ratio as it is here.
|
// Let's use the pixel ratio as it is here.
|
||||||
u.deviceScale = devicePixelRatio()
|
u.deviceScale = devicescale.DeviceScale()
|
||||||
|
|
||||||
canvas.Set("width", int(float64(u.width)*u.actualScreenScale()))
|
canvas.Set("width", int(float64(u.width)*u.actualScreenScale()))
|
||||||
canvas.Set("height", int(float64(u.height)*u.actualScreenScale()))
|
canvas.Set("height", int(float64(u.height)*u.actualScreenScale()))
|
||||||
|
@ -18,21 +18,6 @@
|
|||||||
|
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
// #cgo CFLAGS: -x objective-c
|
|
||||||
// #cgo LDFLAGS: -framework AppKit
|
|
||||||
//
|
|
||||||
// #import <AppKit/AppKit.h>
|
|
||||||
//
|
|
||||||
// static float scale() {
|
|
||||||
// NSScreen* primary = [[NSScreen screens] firstObject];
|
|
||||||
// return [primary backingScaleFactor];
|
|
||||||
// }
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
func deviceScale() float64 {
|
|
||||||
return float64(C.scale())
|
|
||||||
}
|
|
||||||
|
|
||||||
func glfwScale() float64 {
|
func glfwScale() float64 {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -96,7 +97,7 @@ func (u *userInterface) update(g GraphicsContext) error {
|
|||||||
if sizeChanged {
|
if sizeChanged {
|
||||||
width = u.width
|
width = u.width
|
||||||
height = u.height
|
height = u.height
|
||||||
actualScale = u.scale * deviceScale()
|
actualScale = u.scale * devicescale.DeviceScale()
|
||||||
}
|
}
|
||||||
u.sizeChanged = false
|
u.sizeChanged = false
|
||||||
u.m.Unlock()
|
u.m.Unlock()
|
||||||
|
@ -22,30 +22,17 @@ package ui
|
|||||||
//
|
//
|
||||||
// #include <windows.h>
|
// #include <windows.h>
|
||||||
//
|
//
|
||||||
// static char* getDPI(int* dpi) {
|
|
||||||
// HDC dc = GetWindowDC(0);
|
|
||||||
// *dpi = GetDeviceCaps(dc, LOGPIXELSX);
|
|
||||||
// if (!ReleaseDC(0, dc)) {
|
|
||||||
// return "ReleaseDC failed";
|
|
||||||
// }
|
|
||||||
// return "";
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// static int getCaptionHeight() {
|
// static int getCaptionHeight() {
|
||||||
// return GetSystemMetrics(SM_CYCAPTION);
|
// return GetSystemMetrics(SM_CYCAPTION);
|
||||||
// }
|
// }
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
func deviceScale() float64 {
|
import (
|
||||||
dpi := C.int(0)
|
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||||
if errmsg := C.GoString(C.getDPI(&dpi)); errmsg != "" {
|
)
|
||||||
panic(errmsg)
|
|
||||||
}
|
|
||||||
return float64(dpi) / 96
|
|
||||||
}
|
|
||||||
|
|
||||||
func glfwScale() float64 {
|
func glfwScale() float64 {
|
||||||
return deviceScale()
|
return devicescale.DeviceScale()
|
||||||
}
|
}
|
||||||
|
|
||||||
func adjustWindowPosition(x, y int) (int, int) {
|
func adjustWindowPosition(x, y int) (int, int) {
|
||||||
|
@ -18,13 +18,12 @@
|
|||||||
|
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
func deviceScale() float64 {
|
import (
|
||||||
// TODO: Implement this
|
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||||
return 1
|
)
|
||||||
}
|
|
||||||
|
|
||||||
func glfwScale() float64 {
|
func glfwScale() float64 {
|
||||||
return deviceScale()
|
return devicescale.DeviceScale()
|
||||||
}
|
}
|
||||||
|
|
||||||
func adjustWindowPosition(x, y int) (int, int) {
|
func adjustWindowPosition(x, y int) (int, int) {
|
||||||
|
Loading…
Reference in New Issue
Block a user