Skip to content

Commit 2c21fde

Browse files
committed
Added high dpi support.
1 parent 032bbda commit 2c21fde

File tree

8 files changed

+40
-15
lines changed

8 files changed

+40
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
func main() {
3232
// Load fonts for normal, bold and italic text styles.
33-
fonts, err := crt.LoadFaces("./fonts/SomeFont-Regular.ttf", "./fonts/SomeFont-Bold.ttf", "./fonts/SomeFont-Italic.ttf", 72.0, 16.0)
33+
fonts, err := crt.LoadFaces("./fonts/SomeFont-Regular.ttf", "./fonts/SomeFont-Bold.ttf", "./fonts/SomeFont-Italic.ttf", crt.GetFontDPI(), 16.0)
3434
if err != nil {
3535
panic(err)
3636
}

crt.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ func NewGame(width int, height int, fonts Fonts, tty io.Reader, adapter InputAda
7373
bounds, _, _ := fonts.Normal.GlyphBounds([]rune("█")[0])
7474
size := bounds.Max.Sub(bounds.Min)
7575

76-
cellWidth := size.X.Round()
77-
cellHeight := size.Y.Round()
78-
cellOffsetY := -bounds.Min.Y.Round()
76+
cellWidth := size.X.Ceil()
77+
cellHeight := size.Y.Ceil()
78+
cellOffsetY := -bounds.Min.Y.Ceil()
7979

80-
cellsWidth := width / cellWidth
81-
cellsHeight := height / cellHeight
80+
cellsWidth := int(float64(width)*DeviceScale()) / cellWidth
81+
cellsHeight := int(float64(height)*DeviceScale()) / cellHeight
8282

8383
grid := make([][]GridCell, cellsHeight)
8484
for y := 0; y < cellsHeight; y++ {
@@ -597,14 +597,13 @@ func (g *Window) Draw(screen *ebiten.Image) {
597597
}
598598

599599
func (g *Window) Layout(outsideWidth, outsideHeight int) (int, int) {
600-
return g.cellsWidth * g.cellWidth, g.cellsHeight * g.cellHeight
600+
s := DeviceScale()
601+
return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
601602
}
602603

603604
func (g *Window) Run(title string) error {
604-
sw, sh := g.Layout(0, 0)
605-
606605
ebiten.SetScreenFilterEnabled(false)
607-
ebiten.SetWindowSize(sw, sh)
606+
ebiten.SetWindowSize(int(float64(g.cellsWidth*g.cellWidth)/DeviceScale()), int(float64(g.cellsHeight*g.cellHeight)/DeviceScale()))
608607
ebiten.SetWindowTitle(title)
609608
if err := ebiten.RunGame(g); err != nil {
610609
return err

dpi.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package crt
2+
3+
import (
4+
"github.com/hajimehoshi/ebiten/v2"
5+
"os"
6+
"strconv"
7+
)
8+
9+
// DeviceScale returns the current device scale factor.
10+
//
11+
// If the environment variable CRT_DEVICE_SCALE is set, it will be used instead.
12+
func DeviceScale() float64 {
13+
if os.Getenv("CRT_DEVICE_SCALE") != "" {
14+
s, err := strconv.ParseFloat(os.Getenv("CRT_DEVICE_SCALE"), 64)
15+
if err == nil {
16+
return s
17+
}
18+
}
19+
20+
return ebiten.DeviceScaleFactor()
21+
}
22+
23+
// GetFontDPI returns the recommended font DPI for the current device.
24+
func GetFontDPI() float64 {
25+
return 72.0 * DeviceScale()
26+
}

examples/benchmark/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func main() {
4747
enableShader := flag.Bool("shader", false, "Enable shader")
4848
flag.Parse()
4949

50-
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 9.0)
50+
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 9.0)
5151
if err != nil {
5252
panic(err)
5353
}

examples/keys/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (m model) View() string {
3838
}
3939

4040
func main() {
41-
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 16.0)
41+
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 16.0)
4242
if err != nil {
4343
panic(err)
4444
}

examples/package-manager/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func max(a, b int) int {
187187
func main() {
188188
rand.Seed(time.Now().Unix())
189189

190-
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 16.0)
190+
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 16.0)
191191
if err != nil {
192192
panic(err)
193193
}

examples/shader/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func max(a, b int) int {
188188
func main() {
189189
rand.Seed(time.Now().Unix())
190190

191-
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 16.0)
191+
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 16.0)
192192
if err != nil {
193193
panic(err)
194194
}

examples/simple/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (m model) View() string {
2929
}
3030

3131
func main() {
32-
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 16.0)
32+
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 16.0)
3333
if err != nil {
3434
panic(err)
3535
}

0 commit comments

Comments
 (0)