ebiten: add RunGameOptions.DisableHiDPI

Closes #2987
This commit is contained in:
Hajime Hoshi 2024-08-19 23:43:48 +09:00
parent 789388618d
commit 35f9b1c224
4 changed files with 38 additions and 2 deletions

View File

@ -15,9 +15,11 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
_ "image/jpeg" _ "image/jpeg"
"log" "log"
"runtime"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil" "github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -93,7 +95,16 @@ func (g *Game) Draw(screen *ebiten.Image) {
op.Filter = ebiten.FilterLinear op.Filter = ebiten.FilterLinear
screen.DrawImage(g.highDPIImage, op) screen.DrawImage(g.highDPIImage, op)
ebitenutil.DebugPrint(screen, fmt.Sprintf("(Init) Device Scale Ratio: %0.2f", scale)) msg := fmt.Sprintf("Device Scale Ratio: %0.2f", scale)
if runtime.GOOS == "js" {
if !*flagDisable {
msg += "\nHiDPI rendering is enabled. You can disable HiDPI by -disable flag."
} else {
msg += "\nHiDPI rendering is disabled."
}
}
// TODO: The texts might be too small. Improve legibility.
ebitenutil.DebugPrint(screen, msg)
} }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
@ -103,10 +114,16 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s) return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
} }
var flagDisable = flag.Bool("disable", false, "disables HiDPI rendering (only on browsers)")
func main() { func main() {
flag.Parse()
ebiten.SetWindowSize(640, 480) ebiten.SetWindowSize(640, 480)
ebiten.SetWindowTitle("High DPI (Ebitengine Demo)") ebiten.SetWindowTitle("High DPI (Ebitengine Demo)")
if err := ebiten.RunGame(NewGame()); err != nil { op := &ebiten.RunGameOptions{}
op.DisableHiDPI = *flagDisable
if err := ebiten.RunGameWithOptions(NewGame(), op); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -175,6 +175,7 @@ type RunOptions struct {
ScreenTransparent bool ScreenTransparent bool
SkipTaskbar bool SkipTaskbar bool
SingleThread bool SingleThread bool
DisableHiDPI bool
X11ClassName string X11ClassName string
X11InstanceName string X11InstanceName string
} }

View File

@ -96,6 +96,7 @@ type userInterfaceImpl struct {
cursorShape CursorShape cursorShape CursorShape
onceUpdateCalled bool onceUpdateCalled bool
lastCaptureExitTime time.Time lastCaptureExitTime time.Time
hiDPIEnabled bool
context *context context *context
inputState InputState inputState InputState
@ -465,6 +466,7 @@ func (u *UserInterface) init() error {
runnableOnUnfocused: true, runnableOnUnfocused: true,
savedCursorX: math.NaN(), savedCursorX: math.NaN(),
savedCursorY: math.NaN(), savedCursorY: math.NaN(),
hiDPIEnabled: true,
} }
// document is undefined on node.js // document is undefined on node.js
@ -762,6 +764,8 @@ func (u *UserInterface) shouldFocusFirst(options *RunOptions) bool {
func (u *UserInterface) initOnMainThread(options *RunOptions) error { func (u *UserInterface) initOnMainThread(options *RunOptions) error {
u.setRunning(true) u.setRunning(true)
u.hiDPIEnabled = !options.DisableHiDPI
if u.shouldFocusFirst(options) { if u.shouldFocusFirst(options) {
canvas.Call("focus") canvas.Call("focus")
} }
@ -815,6 +819,10 @@ func (m *Monitor) Name() string {
} }
func (m *Monitor) DeviceScaleFactor() float64 { func (m *Monitor) DeviceScaleFactor() float64 {
if !theUI.hiDPIEnabled {
return 1
}
if m.deviceScaleFactor != 0 { if m.deviceScaleFactor != 0 {
return m.deviceScaleFactor return m.deviceScaleFactor
} }

10
run.go
View File

@ -274,6 +274,15 @@ type RunGameOptions struct {
// The default (zero) value is false, which means that the single thread mode is disabled. // The default (zero) value is false, which means that the single thread mode is disabled.
SingleThread bool SingleThread bool
// DisableHiDPI indicates whether the rendering for HiDPI is disabled or not.
// If HiDPI is disabled, the device scale factor is always 1 i.e. Monitor's DeviceScaleFactor always returns 1.
// This is useful to get a better performance on HiDPI displays, in the expense of rendering quality.
//
// DisableHiDPI is available only on browsers.
//
// The default (zero) value is false, which means that HiDPI is enabled.
DisableHiDPI bool
// X11DisplayName is a class name in the ICCCM WM_CLASS window property. // X11DisplayName is a class name in the ICCCM WM_CLASS window property.
X11ClassName string X11ClassName string
@ -703,6 +712,7 @@ func toUIRunOptions(options *RunGameOptions) *ui.RunOptions {
ScreenTransparent: options.ScreenTransparent, ScreenTransparent: options.ScreenTransparent,
SkipTaskbar: options.SkipTaskbar, SkipTaskbar: options.SkipTaskbar,
SingleThread: options.SingleThread, SingleThread: options.SingleThread,
DisableHiDPI: options.DisableHiDPI,
X11ClassName: options.X11ClassName, X11ClassName: options.X11ClassName,
X11InstanceName: options.X11InstanceName, X11InstanceName: options.X11InstanceName,
} }