From 2b578982d6f67ab0c6e6ef6d8356a78b17a408dd Mon Sep 17 00:00:00 2001 From: Brett Bronson Date: Sun, 5 May 2024 19:54:19 -0700 Subject: [PATCH] Updated to ebiten 2.7x. Updated code to handle changes in device scale factor and updated examples for this as well. --- ebitenbackend.go | 13 ++++--- examples/custom_font/README.md | 12 +++++++ examples/custom_font/main.go | 4 ++- gameproxy.go | 12 ++++++- go.mod | 15 ++++---- go.sum | 63 +++++++++------------------------- 6 files changed, 55 insertions(+), 64 deletions(-) diff --git a/ebitenbackend.go b/ebitenbackend.go index 997c486..84a805f 100644 --- a/ebitenbackend.go +++ b/ebitenbackend.go @@ -133,16 +133,15 @@ func (b *BackendBridge) CreateWindow(title string, width, height int) { imgui.PlotCreateContext() imgui.ImNodesCreateContext() - ebiten.SetWindowTitle(title) - ebiten.SetWindowSize( - width*int(ebiten.DeviceScaleFactor()), - height*int(ebiten.DeviceScaleFactor()), - ) + sf := ebiten.Monitor().DeviceScaleFactor() + imgui.CurrentStyle().ScaleAllSizes(float32(sf)) + ebiten.SetWindowTitle(title) + ebiten.SetWindowSize(int(float64(width)*sf), int(float64(height)*sf)) b.io.SetDisplaySize( imgui.Vec2{ - X: float32(width * int(ebiten.DeviceScaleFactor())), - Y: float32(width * int(ebiten.DeviceScaleFactor())), + X: float32(float64(width) * sf), + Y: float32(float64(height) * sf), }, ) diff --git a/examples/custom_font/README.md b/examples/custom_font/README.md index 53ab03b..3f5daf9 100644 --- a/examples/custom_font/README.md +++ b/examples/custom_font/README.md @@ -1,3 +1,15 @@ +# Fonts in imgui +By default, imgui comes with a 13px font called ProggyClean. If a user has not set up their own fonts, +then the builtin font is used. Unfortunately this font is limited to 13px and will be pixelated when the +monitor's scale factor is set higher than `1`. + +# HiDPI? +Please refer to https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#q-how-should-i-handle-dpi-in-my-application if you have any questions about how HiDPI is handled in imgui. +This is typically up to the user to implement and is implemented in this example. + +This library will also scale the current imgui style during window creation. + + # Building Please be sure to include these tags when building to disable sdl and glfw backends in imgui: diff --git a/examples/custom_font/main.go b/examples/custom_font/main.go index a52296e..342706d 100644 --- a/examples/custom_font/main.go +++ b/examples/custom_font/main.go @@ -6,16 +6,18 @@ import ( "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/vector" "image/color" + "math" "unsafe" ) const ( fontFilename = "Sharpie_Complete/Fonts/WEB/fonts/Sharpie-Regular.ttf" - fontSize = 24 ) var ( adapter = backend.NewEbitenAdapter() + // scale font size based on ebiten's scale factor + fontSize = float32(math.Floor(24 * ebiten.Monitor().DeviceScaleFactor())) ) type MyGame struct{} diff --git a/gameproxy.go b/gameproxy.go index dc4a602..8f1bb45 100644 --- a/gameproxy.go +++ b/gameproxy.go @@ -69,10 +69,20 @@ func (g *GameProxy) Update() error { } io.SetDeltaTime(1.0 / 60.0) - // Check that fonts are built. + + if io.Fonts().FontCount() == 0 { + // The font atlas is empty. Add the default font and set up + // scaling to match ebiten's scale factor. It's recommended to set up fonts on + // your own (per imgui docs) because the default font does not scale well. + io.SetFontGlobalScale(float32(ebiten.Monitor().DeviceScaleFactor())) + + io.Fonts().AddFontDefault() + } + if !io.Fonts().IsBuilt() { _, _, _, _ = io.Fonts().GetTextureDataAsRGBA32() } + imgui.NewFrame() err := g.game.Update() imgui.EndFrame() diff --git a/go.mod b/go.mod index 032b477..aad13c8 100644 --- a/go.mod +++ b/go.mod @@ -4,15 +4,14 @@ go 1.22 require ( github.com/AllenDang/cimgui-go v0.0.0-20240429075623-6996cd326381 - github.com/hajimehoshi/ebiten/v2 v2.6.6 + github.com/hajimehoshi/ebiten/v2 v2.7.3 ) require ( - github.com/ebitengine/purego v0.6.0 // indirect - github.com/jezek/xgb v1.1.0 // indirect - golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63 // indirect - golang.org/x/image v0.12.0 // indirect - golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 // indirect - golang.org/x/sync v0.6.0 // indirect - golang.org/x/sys v0.12.0 // indirect + github.com/ebitengine/gomobile v0.0.0-20240429094902-cf88669c3591 // indirect + github.com/ebitengine/hideconsole v1.0.0 // indirect + github.com/ebitengine/purego v0.7.1 // indirect + github.com/jezek/xgb v1.1.1 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.20.0 // indirect ) diff --git a/go.sum b/go.sum index 87a1df1..748c468 100644 --- a/go.sum +++ b/go.sum @@ -1,49 +1,18 @@ github.com/AllenDang/cimgui-go v0.0.0-20240429075623-6996cd326381 h1:QzMkqFkxaNsxCYU81W6QE7vPgzq+pGiKXVvsY8KxBcU= github.com/AllenDang/cimgui-go v0.0.0-20240429075623-6996cd326381/go.mod h1:e6feXR4FrATVY/UrWS3si3KCJOm0wruwbxVI/B85fUM= -github.com/ebitengine/purego v0.6.0 h1:Yo9uBc1x+ETQbfEaf6wcBsjrQfCEnh/gaGUg7lguEJY= -github.com/ebitengine/purego v0.6.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= -github.com/hajimehoshi/ebiten/v2 v2.6.6 h1:E5X87Or4VwKZIKjeC9+Vr4ComhZAz9h839myF4Q21kc= -github.com/hajimehoshi/ebiten/v2 v2.6.6/go.mod h1:gKgQI26zfoSb6j5QbrEz2L6nuHMbAYwrsXa5qsGrQKo= -github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk= -github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63 h1:3AGKexOYqL+ztdWdkB1bDwXgPBuTS/S8A4WzuTvJ8Cg= -golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0= -golang.org/x/image v0.12.0 h1:w13vZbU4o5rKOFFR8y7M+c4A5jXDC0uXTdHYRP8X2DQ= -golang.org/x/image v0.12.0/go.mod h1:Lu90jvHG7GfemOIcldsh9A2hS01ocl6oNO7ype5mEnk= -golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 h1:Q6NT8ckDYNcwmi/bmxe+XbiDMXqMRW1xFBtJ+bIpie4= -golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57/go.mod h1:wEyOn6VvNW7tcf+bW/wBz1sehi2s2BZ4TimyR7qZen4= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= -golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +github.com/ebitengine/gomobile v0.0.0-20240429094902-cf88669c3591 h1:iCauZ8Q3KW2EHpj+Re4qRU4/Y7ustOTpNV6m8hx5BqE= +github.com/ebitengine/gomobile v0.0.0-20240429094902-cf88669c3591/go.mod h1:7zsHIJi2c+Jo+wIL9wD+ADWzMbtG8jhUQpplXIrZ7EI= +github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE= +github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A= +github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA= +github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= +github.com/hajimehoshi/ebiten/v2 v2.7.3 h1:lDpj8KbmmjzwD19rsjXNkyelicu0XGvklZW6/tjrgNs= +github.com/hajimehoshi/ebiten/v2 v2.7.3/go.mod h1:1vjyPw+h3n30rfTOpIsbWRXSxZ0Oz1cYc6Tq/2DKoQg= +github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4= +github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= +golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8= +golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=