mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38: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
|
||||
// limitations under the License.
|
||||
|
||||
package ui
|
||||
package devicescale
|
||||
|
||||
/*
|
||||
|
||||
@ -63,19 +63,13 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/internal/jni"
|
||||
)
|
||||
|
||||
var (
|
||||
androidDeviceScale = 0.0
|
||||
)
|
||||
|
||||
func deviceScale() float64 {
|
||||
if 0 < androidDeviceScale {
|
||||
return androidDeviceScale
|
||||
}
|
||||
func impl() float64 {
|
||||
s := 0.0
|
||||
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
|
||||
}); 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
|
||||
|
||||
package ui
|
||||
package devicescale
|
||||
|
||||
// #cgo CFLAGS: -x objective-c
|
||||
// #cgo LDFLAGS: -framework Foundation -framework UIKit
|
||||
@ -26,6 +26,6 @@ package ui
|
||||
// }
|
||||
import "C"
|
||||
|
||||
func deviceScale() float64 {
|
||||
func impl() float64 {
|
||||
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"
|
||||
|
||||
"github.com/go-gl/glfw/v3.2/glfw"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||
)
|
||||
|
||||
@ -37,10 +39,9 @@ type userInterface struct {
|
||||
windowWidth int
|
||||
height int
|
||||
|
||||
scale float64
|
||||
cachedDeviceScale float64
|
||||
cachedGLFWScale float64
|
||||
fullscreenScale float64
|
||||
scale float64
|
||||
cachedGLFWScale float64
|
||||
fullscreenScale float64
|
||||
|
||||
running bool
|
||||
sizeChanged bool
|
||||
@ -309,9 +310,10 @@ func ScreenOffset() (float64, float64) {
|
||||
oy := 0.0
|
||||
m := glfw.GetPrimaryMonitor()
|
||||
v := m.GetVideoMode()
|
||||
d := devicescale.DeviceScale()
|
||||
_ = u.runOnMainThread(func() error {
|
||||
ox = (float64(v.Width)*u.deviceScale()/u.glfwScale() - float64(u.width)*u.actualScreenScale()) / 2
|
||||
oy = (float64(v.Height)*u.deviceScale()/u.glfwScale() - float64(u.height)*u.actualScreenScale()) / 2
|
||||
ox = (float64(v.Width)*d/u.glfwScale() - float64(u.width)*u.actualScreenScale()) / 2
|
||||
oy = (float64(v.Height)*d/u.glfwScale() - float64(u.height)*u.actualScreenScale()) / 2
|
||||
return nil
|
||||
})
|
||||
return ox, oy
|
||||
@ -395,13 +397,6 @@ func (u *userInterface) glfwScale() float64 {
|
||||
return u.cachedGLFWScale
|
||||
}
|
||||
|
||||
func (u *userInterface) deviceScale() float64 {
|
||||
if u.cachedDeviceScale == 0 {
|
||||
u.cachedDeviceScale = deviceScale()
|
||||
}
|
||||
return u.cachedDeviceScale
|
||||
}
|
||||
|
||||
func (u *userInterface) glfwSize() (int, int) {
|
||||
w := int(float64(u.windowWidth) * 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 {
|
||||
return u.getScale() * u.deviceScale()
|
||||
return u.getScale() * devicescale.DeviceScale()
|
||||
}
|
||||
|
||||
func (u *userInterface) pollEvents() {
|
||||
@ -525,7 +520,7 @@ func (u *userInterface) setScreenSize(width, height int, scale float64, fullscre
|
||||
|
||||
u.width = width
|
||||
u.windowWidth = width
|
||||
s := scale * u.deviceScale()
|
||||
s := scale * devicescale.DeviceScale()
|
||||
if int(float64(width)*s) < minWindowWidth {
|
||||
u.windowWidth = int(math.Ceil(minWindowWidth / s))
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ import (
|
||||
"unicode"
|
||||
|
||||
"github.com/gopherjs/gopherjs/js"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||
"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))
|
||||
}
|
||||
|
||||
func devicePixelRatio() float64 {
|
||||
ratio := js.Global.Get("window").Get("devicePixelRatio").Float()
|
||||
if ratio == 0 {
|
||||
ratio = 1
|
||||
}
|
||||
return ratio
|
||||
}
|
||||
|
||||
func RunMainThreadLoop(ch <-chan error) error {
|
||||
return <-ch
|
||||
}
|
||||
@ -389,7 +383,7 @@ func (u *userInterface) updateScreenSize() {
|
||||
// * Chrome just after restoring the lost context
|
||||
// * Safari
|
||||
// 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("height", int(float64(u.height)*u.actualScreenScale()))
|
||||
|
@ -18,21 +18,6 @@
|
||||
|
||||
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 {
|
||||
return 1
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||
)
|
||||
|
||||
@ -96,7 +97,7 @@ func (u *userInterface) update(g GraphicsContext) error {
|
||||
if sizeChanged {
|
||||
width = u.width
|
||||
height = u.height
|
||||
actualScale = u.scale * deviceScale()
|
||||
actualScale = u.scale * devicescale.DeviceScale()
|
||||
}
|
||||
u.sizeChanged = false
|
||||
u.m.Unlock()
|
||||
|
@ -22,30 +22,17 @@ package ui
|
||||
//
|
||||
// #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() {
|
||||
// return GetSystemMetrics(SM_CYCAPTION);
|
||||
// }
|
||||
import "C"
|
||||
|
||||
func deviceScale() float64 {
|
||||
dpi := C.int(0)
|
||||
if errmsg := C.GoString(C.getDPI(&dpi)); errmsg != "" {
|
||||
panic(errmsg)
|
||||
}
|
||||
return float64(dpi) / 96
|
||||
}
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||
)
|
||||
|
||||
func glfwScale() float64 {
|
||||
return deviceScale()
|
||||
return devicescale.DeviceScale()
|
||||
}
|
||||
|
||||
func adjustWindowPosition(x, y int) (int, int) {
|
||||
|
@ -18,13 +18,12 @@
|
||||
|
||||
package ui
|
||||
|
||||
func deviceScale() float64 {
|
||||
// TODO: Implement this
|
||||
return 1
|
||||
}
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||
)
|
||||
|
||||
func glfwScale() float64 {
|
||||
return deviceScale()
|
||||
return devicescale.DeviceScale()
|
||||
}
|
||||
|
||||
func adjustWindowPosition(x, y int) (int, int) {
|
||||
|
Loading…
Reference in New Issue
Block a user