mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Compare commits
No commits in common. "24e5751ece712abfc8d54a0497a556e919b25d2e" and "4a10702f6c39279c2d4613f12622046399a3eecd" have entirely different histories.
24e5751ece
...
4a10702f6c
@ -54,7 +54,7 @@ type context struct {
|
|||||||
offscreenHeight float64
|
offscreenHeight float64
|
||||||
|
|
||||||
isOffscreenModified bool
|
isOffscreenModified bool
|
||||||
lastSwapBufferTime time.Time
|
lastDrawTime time.Time
|
||||||
|
|
||||||
skipCount int
|
skipCount int
|
||||||
|
|
||||||
@ -70,14 +70,7 @@ func newContext(game Game) *context {
|
|||||||
|
|
||||||
func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface) error {
|
func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface) error {
|
||||||
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
||||||
needsSwapBuffers, err := c.updateFrameImpl(graphicsDriver, clock.UpdateFrame(), outsideWidth, outsideHeight, deviceScaleFactor, ui, false)
|
return c.updateFrameImpl(graphicsDriver, clock.UpdateFrame(), outsideWidth, outsideHeight, deviceScaleFactor, ui, false)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := c.swapBuffersOrWait(needsSwapBuffers, graphicsDriver, ui.FPSMode() == FPSModeVsyncOn); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface) error {
|
func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface) error {
|
||||||
@ -88,32 +81,33 @@ func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsi
|
|||||||
n = 2
|
n = 2
|
||||||
}
|
}
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
needsSwapBuffers, err := c.updateFrameImpl(graphicsDriver, 1, outsideWidth, outsideHeight, deviceScaleFactor, ui, true)
|
if err := c.updateFrameImpl(graphicsDriver, 1, outsideWidth, outsideHeight, deviceScaleFactor, ui, true); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := c.swapBuffersOrWait(needsSwapBuffers, graphicsDriver, ui.FPSMode() == FPSModeVsyncOn); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface, forceDraw bool) (needsSwapBuffers bool, err error) {
|
func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface, forceDraw bool) (err error) {
|
||||||
// The given outside size can be 0 e.g. just after restoring from the fullscreen mode on Windows (#1589)
|
// The given outside size can be 0 e.g. just after restoring from the fullscreen mode on Windows (#1589)
|
||||||
// Just ignore such cases. Otherwise, creating a zero-sized framebuffer causes a panic.
|
// Just ignore such cases. Otherwise, creating a zero-sized framebuffer causes a panic.
|
||||||
if outsideWidth == 0 || outsideHeight == 0 {
|
if outsideWidth == 0 || outsideHeight == 0 {
|
||||||
return false, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
debug.FrameLogf("----\n")
|
debug.FrameLogf("----\n")
|
||||||
|
|
||||||
if err := atlas.BeginFrame(graphicsDriver); err != nil {
|
if err := atlas.BeginFrame(graphicsDriver); err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err1 := atlas.EndFrame(); err1 != nil && err == nil {
|
if err1 := atlas.EndFrame(); err1 != nil && err == nil {
|
||||||
needsSwapBuffers = false
|
err = err1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err1 := atlas.SwapBuffers(graphicsDriver); err1 != nil && err == nil {
|
||||||
err = err1
|
err = err1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -121,17 +115,17 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
|
|||||||
|
|
||||||
// Flush deferred functions, like reading pixels from GPU.
|
// Flush deferred functions, like reading pixels from GPU.
|
||||||
if err := c.processFuncsInFrame(ui); err != nil {
|
if err := c.processFuncsInFrame(ui); err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForceUpdate can be invoked even if the context is not initialized yet (#1591).
|
// ForceUpdate can be invoked even if the context is not initialized yet (#1591).
|
||||||
if w, h := c.layoutGame(outsideWidth, outsideHeight, deviceScaleFactor); w == 0 || h == 0 {
|
if w, h := c.layoutGame(outsideWidth, outsideHeight, deviceScaleFactor); w == 0 || h == 0 {
|
||||||
return false, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the input state after the layout is updated as a cursor position is affected by the layout.
|
// Update the input state after the layout is updated as a cursor position is affected by the layout.
|
||||||
if err := ui.updateInputState(); err != nil {
|
if err := ui.updateInputState(); err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that Update is called once before Draw so that Update can be used for initialization.
|
// Ensure that Update is called once before Draw so that Update can be used for initialization.
|
||||||
@ -149,15 +143,15 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err := hook.RunBeforeUpdateHooks(); err != nil {
|
if err := hook.RunBeforeUpdateHooks(); err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
if err := c.game.Update(); err != nil {
|
if err := c.game.Update(); err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Catch the error that happened at (*Image).At.
|
// Catch the error that happened at (*Image).At.
|
||||||
if err := ui.error(); err != nil {
|
if err := ui.error(); err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.tick.Add(1)
|
ui.tick.Add(1)
|
||||||
@ -166,39 +160,12 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
|
|||||||
// Update window icons during a frame, since an icon might be *ebiten.Image and
|
// Update window icons during a frame, since an icon might be *ebiten.Image and
|
||||||
// getting pixels from it needs to be in a frame (#1468).
|
// getting pixels from it needs to be in a frame (#1468).
|
||||||
if err := ui.updateIconIfNeeded(); err != nil {
|
if err := ui.updateIconIfNeeded(); err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the game.
|
// Draw the game.
|
||||||
return c.drawGame(graphicsDriver, ui, forceDraw)
|
if err := c.drawGame(graphicsDriver, ui, forceDraw); err != nil {
|
||||||
}
|
return err
|
||||||
|
|
||||||
func (c *context) swapBuffersOrWait(needsSwapBuffers bool, graphicsDriver graphicsdriver.Graphics, vsyncEnabled bool) error {
|
|
||||||
now := time.Now()
|
|
||||||
defer func() {
|
|
||||||
c.lastSwapBufferTime = now
|
|
||||||
}()
|
|
||||||
|
|
||||||
if needsSwapBuffers {
|
|
||||||
if err := atlas.SwapBuffers(graphicsDriver); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var waitTime time.Duration
|
|
||||||
if !needsSwapBuffers {
|
|
||||||
// When swapping buffers is skipped and Draw is called too early, sleep for a while to suppress CPU usages (#2890).
|
|
||||||
waitTime = time.Second / 60
|
|
||||||
} else if vsyncEnabled {
|
|
||||||
// In some environments, e.g. Linux on Parallels, SwapBuffers doesn't wait for the vsync (#2952).
|
|
||||||
// In the case when the display has high refresh rates like 240 [Hz], the wait time should be small.
|
|
||||||
waitTime = time.Millisecond
|
|
||||||
}
|
|
||||||
if waitTime > 0 {
|
|
||||||
if delta := waitTime - now.Sub(c.lastSwapBufferTime); delta > 0 {
|
|
||||||
println(waitTime.String(), now.Sub(c.lastSwapBufferTime).String())
|
|
||||||
time.Sleep(delta)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -212,7 +179,7 @@ func (c *context) newOffscreenImage(w, h int) *Image {
|
|||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics, ui *UserInterface, forceDraw bool) (needSwapBuffers bool, err error) {
|
func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics, ui *UserInterface, forceDraw bool) error {
|
||||||
if (c.offscreen.imageType == atlas.ImageTypeVolatile) != ui.IsScreenClearedEveryFrame() {
|
if (c.offscreen.imageType == atlas.ImageTypeVolatile) != ui.IsScreenClearedEveryFrame() {
|
||||||
w, h := c.offscreen.width, c.offscreen.height
|
w, h := c.offscreen.width, c.offscreen.height
|
||||||
c.offscreen.Deallocate()
|
c.offscreen.Deallocate()
|
||||||
@ -230,7 +197,7 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics, ui *UserInter
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := c.game.DrawOffscreen(); err != nil {
|
if err := c.game.DrawOffscreen(); err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxSkipCount = 4
|
const maxSkipCount = 4
|
||||||
@ -243,21 +210,28 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics, ui *UserInter
|
|||||||
c.skipCount = 0
|
c.skipCount = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.skipCount >= maxSkipCount {
|
now := time.Now()
|
||||||
return false, nil
|
defer func() {
|
||||||
|
c.lastDrawTime = now
|
||||||
|
}()
|
||||||
|
|
||||||
|
if c.skipCount < maxSkipCount {
|
||||||
|
if graphicsDriver.NeedsClearingScreen() {
|
||||||
|
// This clear is needed for fullscreen mode or some mobile platforms (#622).
|
||||||
|
c.screen.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
c.game.DrawFinalScreen(c.screenScaleAndOffsets())
|
||||||
|
|
||||||
|
// The final screen is never used as the rendering source.
|
||||||
|
// Flush its buffer here just in case.
|
||||||
|
c.screen.flushBufferIfNeeded()
|
||||||
|
} else if delta := time.Second/60 - now.Sub(c.lastDrawTime); delta > 0 {
|
||||||
|
// When swapping buffers is skipped and Draw is called too early, sleep for a while to suppress CPU usages (#2890).
|
||||||
|
time.Sleep(delta)
|
||||||
}
|
}
|
||||||
|
|
||||||
if graphicsDriver.NeedsClearingScreen() {
|
return nil
|
||||||
// This clear is needed for fullscreen mode or some mobile platforms (#622).
|
|
||||||
c.screen.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
c.game.DrawFinalScreen(c.screenScaleAndOffsets())
|
|
||||||
|
|
||||||
// The final screen is never used as the rendering source.
|
|
||||||
// Flush its buffer here just in case.
|
|
||||||
c.screen.flushBufferIfNeeded()
|
|
||||||
return true, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
|
func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
|
||||||
@ -266,13 +240,8 @@ func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFac
|
|||||||
panic("ui: Layout must return positive numbers")
|
panic("ui: Layout must return positive numbers")
|
||||||
}
|
}
|
||||||
|
|
||||||
screenWidth := outsideWidth * deviceScaleFactor
|
c.screenWidth = outsideWidth * deviceScaleFactor
|
||||||
screenHeight := outsideHeight * deviceScaleFactor
|
c.screenHeight = outsideHeight * deviceScaleFactor
|
||||||
if c.screenWidth != screenWidth || c.screenHeight != screenHeight {
|
|
||||||
c.skipCount = 0
|
|
||||||
}
|
|
||||||
c.screenWidth = screenWidth
|
|
||||||
c.screenHeight = screenHeight
|
|
||||||
c.offscreenWidth = owf
|
c.offscreenWidth = owf
|
||||||
c.offscreenHeight = ohf
|
c.offscreenHeight = ohf
|
||||||
|
|
||||||
|
@ -18,19 +18,15 @@ package ui
|
|||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
// The following JNI code works as this pseudo Java code:
|
// Basically same as:
|
||||||
//
|
//
|
||||||
// WindowService windowService = context.getSystemService(Context.WINDOW_SERVICE);
|
// WindowService windowService = context.getSystemService(Context.WINDOW_SERVICE);
|
||||||
// Display display = windowManager.getDefaultDisplay();
|
// Display display = windowManager.getDefaultDisplay();
|
||||||
// DisplayMetrics displayMetrics = new DisplayMetrics();
|
// DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||||
// display.getRealMetrics(displayMetrics);
|
// display.getRealMetrics(displayMetrics);
|
||||||
// return displayMetrics.widthPixels, displayMetrics.heightPixels, displayMetrics.density;
|
// this.deviceScale = displayMetrics.density;
|
||||||
//
|
//
|
||||||
static void displayInfo(int* width, int* height, float* scale, uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx) {
|
static float deviceScale(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx) {
|
||||||
*width = 0;
|
|
||||||
*height = 0;
|
|
||||||
*scale = 1;
|
|
||||||
|
|
||||||
JavaVM* vm = (JavaVM*)java_vm;
|
JavaVM* vm = (JavaVM*)java_vm;
|
||||||
JNIEnv* env = (JNIEnv*)jni_env;
|
JNIEnv* env = (JNIEnv*)jni_env;
|
||||||
jobject context = (jobject)ctx;
|
jobject context = (jobject)ctx;
|
||||||
@ -68,15 +64,7 @@ static void displayInfo(int* width, int* height, float* scale, uintptr_t java_vm
|
|||||||
env, display,
|
env, display,
|
||||||
(*env)->GetMethodID(env, android_view_Display, "getRealMetrics", "(Landroid/util/DisplayMetrics;)V"),
|
(*env)->GetMethodID(env, android_view_Display, "getRealMetrics", "(Landroid/util/DisplayMetrics;)V"),
|
||||||
displayMetrics);
|
displayMetrics);
|
||||||
*width =
|
const float density =
|
||||||
(*env)->GetIntField(
|
|
||||||
env, displayMetrics,
|
|
||||||
(*env)->GetFieldID(env, android_util_DisplayMetrics, "widthPixels", "I"));
|
|
||||||
*height =
|
|
||||||
(*env)->GetIntField(
|
|
||||||
env, displayMetrics,
|
|
||||||
(*env)->GetFieldID(env, android_util_DisplayMetrics, "heightPixels", "I"));
|
|
||||||
*scale =
|
|
||||||
(*env)->GetFloatField(
|
(*env)->GetFloatField(
|
||||||
env, displayMetrics,
|
env, displayMetrics,
|
||||||
(*env)->GetFieldID(env, android_util_DisplayMetrics, "density", "F"));
|
(*env)->GetFieldID(env, android_util_DisplayMetrics, "density", "F"));
|
||||||
@ -90,12 +78,15 @@ static void displayInfo(int* width, int* height, float* scale, uintptr_t java_vm
|
|||||||
(*env)->DeleteLocalRef(env, windowManager);
|
(*env)->DeleteLocalRef(env, windowManager);
|
||||||
(*env)->DeleteLocalRef(env, display);
|
(*env)->DeleteLocalRef(env, display);
|
||||||
(*env)->DeleteLocalRef(env, displayMetrics);
|
(*env)->DeleteLocalRef(env, displayMetrics);
|
||||||
|
|
||||||
|
return density;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/ebitengine/gomobile/app"
|
"github.com/ebitengine/gomobile/app"
|
||||||
|
|
||||||
@ -128,27 +119,18 @@ func (*graphicsDriverCreatorImpl) newPlayStation5() (graphicsdriver.Graphics, er
|
|||||||
return nil, errors.New("ui: PlayStation 5 is not supported in this environment")
|
return nil, errors.New("ui: PlayStation 5 is not supported in this environment")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserInterface) deviceScaleFactor() float64 {
|
||||||
|
var s float64
|
||||||
|
if err := app.RunOnJVM(func(vm, env, ctx uintptr) error {
|
||||||
|
// TODO: This might be crash when this is called from init(). How can we detect this?
|
||||||
|
s = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx)))
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
panic(fmt.Sprintf("devicescale: error %v", err))
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func dipToNativePixels(x float64, scale float64) float64 {
|
func dipToNativePixels(x float64, scale float64) float64 {
|
||||||
return x * scale
|
return x * scale
|
||||||
}
|
}
|
||||||
|
|
||||||
func dipFromNativePixels(x float64, scale float64) float64 {
|
|
||||||
return x / scale
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserInterface) displayInfo() (int, int, float64, bool) {
|
|
||||||
var cWidth, cHeight C.int
|
|
||||||
var cScale C.float
|
|
||||||
if err := app.RunOnJVM(func(vm, env, ctx uintptr) error {
|
|
||||||
C.displayInfo(&cWidth, &cHeight, &cScale, C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
// JVM is not ready yet.
|
|
||||||
// TODO: Fix gomobile to detect the error type for this case.
|
|
||||||
return 0, 0, 1, false
|
|
||||||
}
|
|
||||||
scale := float64(cScale)
|
|
||||||
width := int(dipFromNativePixels(float64(cWidth), scale))
|
|
||||||
height := int(dipFromNativePixels(float64(cHeight), scale))
|
|
||||||
return width, height, scale, true
|
|
||||||
}
|
|
||||||
|
@ -19,43 +19,31 @@ package ui
|
|||||||
//
|
//
|
||||||
// #import <UIKit/UIKit.h>
|
// #import <UIKit/UIKit.h>
|
||||||
//
|
//
|
||||||
// static void displayInfoOnMainThread(float* width, float* height, float* scale, UIView* view) {
|
// static double devicePixelRatioOnMainThread(UIView* view) {
|
||||||
// *width = 0;
|
|
||||||
// *height = 0;
|
|
||||||
// *scale = 1;
|
|
||||||
// UIWindow* window = view.window;
|
// UIWindow* window = view.window;
|
||||||
// if (!window) {
|
// if (!window) {
|
||||||
// return;
|
// return 1;
|
||||||
// }
|
// }
|
||||||
// UIWindowScene* scene = window.windowScene;
|
// UIWindowScene* scene = window.windowScene;
|
||||||
// if (!scene) {
|
// if (!scene) {
|
||||||
// return;
|
// return 1;
|
||||||
// }
|
// }
|
||||||
// CGRect bounds = scene.screen.bounds;
|
// return scene.screen.nativeScale;
|
||||||
// *width = bounds.size.width;
|
|
||||||
// *height = bounds.size.height;
|
|
||||||
// *scale = scene.screen.nativeScale;
|
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// static void displayInfo(float* width, float* height, float* scale, uintptr_t viewPtr) {
|
// static double devicePixelRatio(uintptr_t viewPtr) {
|
||||||
// *width = 0;
|
|
||||||
// *height = 0;
|
|
||||||
// *scale = 1;
|
|
||||||
// if (!viewPtr) {
|
// if (!viewPtr) {
|
||||||
// return;
|
// return 1;
|
||||||
// }
|
// }
|
||||||
// UIView* view = (__bridge UIView*)(void*)viewPtr;
|
// UIView* view = (__bridge UIView*)(void*)viewPtr;
|
||||||
// if ([NSThread isMainThread]) {
|
// if ([NSThread isMainThread]) {
|
||||||
// displayInfoOnMainThread(width, height, scale, view);
|
// return devicePixelRatioOnMainThread(view);
|
||||||
// return;
|
|
||||||
// }
|
// }
|
||||||
// __block float w, h, s;
|
// __block double scale;
|
||||||
// dispatch_sync(dispatch_get_main_queue(), ^{
|
// dispatch_sync(dispatch_get_main_queue(), ^{
|
||||||
// displayInfoOnMainThread(&w, &h, &s, view);
|
// scale = devicePixelRatioOnMainThread(view);
|
||||||
// });
|
// });
|
||||||
// *width = w;
|
// return scale;
|
||||||
// *height = h;
|
|
||||||
// *scale = s;
|
|
||||||
// }
|
// }
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
@ -125,24 +113,10 @@ func (u *UserInterface) IsGL() (bool, error) {
|
|||||||
return u.GraphicsLibrary() == GraphicsLibraryOpenGL, nil
|
return u.GraphicsLibrary() == GraphicsLibraryOpenGL, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserInterface) deviceScaleFactor() float64 {
|
||||||
|
return float64(C.devicePixelRatio(C.uintptr_t(u.uiView.Load())))
|
||||||
|
}
|
||||||
|
|
||||||
func dipToNativePixels(x float64, scale float64) float64 {
|
func dipToNativePixels(x float64, scale float64) float64 {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
func dipFromNativePixels(x float64, scale float64) float64 {
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserInterface) displayInfo() (int, int, float64, bool) {
|
|
||||||
view := u.uiView.Load()
|
|
||||||
if view == 0 {
|
|
||||||
return 0, 0, 1, false
|
|
||||||
}
|
|
||||||
|
|
||||||
var cWidth, cHeight, cScale C.float
|
|
||||||
C.displayInfo(&cWidth, &cHeight, &cScale, C.uintptr_t(view))
|
|
||||||
scale := float64(cScale)
|
|
||||||
width := int(dipFromNativePixels(float64(cWidth), scale))
|
|
||||||
height := int(dipFromNativePixels(float64(cHeight), scale))
|
|
||||||
return width, height, scale, true
|
|
||||||
}
|
|
||||||
|
@ -268,10 +268,8 @@ func (u *UserInterface) Window() Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Monitor struct {
|
type Monitor struct {
|
||||||
width int
|
deviceScaleFactor float64
|
||||||
height int
|
deviceScaleFactorOnce sync.Once
|
||||||
deviceScaleFactor float64
|
|
||||||
inited atomic.Bool
|
|
||||||
|
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
}
|
}
|
||||||
@ -282,35 +280,22 @@ func (m *Monitor) Name() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Monitor) ensureInit() {
|
func (m *Monitor) DeviceScaleFactor() float64 {
|
||||||
if m.inited.Load() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
m.m.Lock()
|
m.m.Lock()
|
||||||
defer m.m.Unlock()
|
defer m.m.Unlock()
|
||||||
// Re-check the state since the state might be changed while locking.
|
|
||||||
if m.inited.Load() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
width, height, scale, ok := theUI.displayInfo()
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
m.width = width
|
|
||||||
m.height = height
|
|
||||||
m.deviceScaleFactor = scale
|
|
||||||
m.inited.Store(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Monitor) DeviceScaleFactor() float64 {
|
// The device scale factor can be obtained after the main function starts, especially on Android.
|
||||||
m.ensureInit()
|
// Initialize this lazily.
|
||||||
|
m.deviceScaleFactorOnce.Do(func() {
|
||||||
|
// Assume that the device scale factor never changes on mobiles.
|
||||||
|
m.deviceScaleFactor = theUI.deviceScaleFactor()
|
||||||
|
})
|
||||||
return m.deviceScaleFactor
|
return m.deviceScaleFactor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Monitor) Size() (int, int) {
|
func (m *Monitor) Size() (int, int) {
|
||||||
m.ensureInit()
|
// TODO: Return a valid value.
|
||||||
return m.width, m.height
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) AppendMonitors(mons []*Monitor) []*Monitor {
|
func (u *UserInterface) AppendMonitors(mons []*Monitor) []*Monitor {
|
||||||
|
@ -32,7 +32,8 @@ func (m *MonitorType) Name() string {
|
|||||||
// DeviceScaleFactor returns a meaningful value on high-DPI display environment,
|
// DeviceScaleFactor returns a meaningful value on high-DPI display environment,
|
||||||
// otherwise DeviceScaleFactor returns 1.
|
// otherwise DeviceScaleFactor returns 1.
|
||||||
//
|
//
|
||||||
// On mobiles, DeviceScaleFactor returns 1 before the game starts e.g. in init functions.
|
// DeviceScaleFactor might panic on init function on some devices like Android.
|
||||||
|
// Then, it is not recommended to call DeviceScaleFactor from init functions.
|
||||||
func (m *MonitorType) DeviceScaleFactor() float64 {
|
func (m *MonitorType) DeviceScaleFactor() float64 {
|
||||||
return (*ui.Monitor)(m).DeviceScaleFactor()
|
return (*ui.Monitor)(m).DeviceScaleFactor()
|
||||||
}
|
}
|
||||||
@ -41,7 +42,7 @@ func (m *MonitorType) DeviceScaleFactor() float64 {
|
|||||||
// This is the same as the screen size in fullscreen mode.
|
// This is the same as the screen size in fullscreen mode.
|
||||||
// The returned value can be given to SetSize function if the perfectly fit fullscreen is needed.
|
// The returned value can be given to SetSize function if the perfectly fit fullscreen is needed.
|
||||||
//
|
//
|
||||||
// On mobiles, Size returns (0, 0) before the game starts e.g. in init functions.
|
// On mobiles, Size returns (0, 0) so far.
|
||||||
//
|
//
|
||||||
// Size's use cases are limited. If you are making a fullscreen application, you can use RunGame and
|
// Size's use cases are limited. If you are making a fullscreen application, you can use RunGame and
|
||||||
// the Game interface's Layout function instead. If you are making a not-fullscreen application but the application's
|
// the Game interface's Layout function instead. If you are making a not-fullscreen application but the application's
|
||||||
|
Loading…
Reference in New Issue
Block a user