Skip to content

Commit

Permalink
Added high dpi support.
Browse files Browse the repository at this point in the history
  • Loading branch information
BigJk committed May 23, 2023
1 parent 032bbda commit 2c21fde
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

func main() {
// Load fonts for normal, bold and italic text styles.
fonts, err := crt.LoadFaces("./fonts/SomeFont-Regular.ttf", "./fonts/SomeFont-Bold.ttf", "./fonts/SomeFont-Italic.ttf", 72.0, 16.0)
fonts, err := crt.LoadFaces("./fonts/SomeFont-Regular.ttf", "./fonts/SomeFont-Bold.ttf", "./fonts/SomeFont-Italic.ttf", crt.GetFontDPI(), 16.0)
if err != nil {
panic(err)
}
Expand Down
17 changes: 8 additions & 9 deletions crt.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func NewGame(width int, height int, fonts Fonts, tty io.Reader, adapter InputAda
bounds, _, _ := fonts.Normal.GlyphBounds([]rune("█")[0])
size := bounds.Max.Sub(bounds.Min)

cellWidth := size.X.Round()
cellHeight := size.Y.Round()
cellOffsetY := -bounds.Min.Y.Round()
cellWidth := size.X.Ceil()
cellHeight := size.Y.Ceil()
cellOffsetY := -bounds.Min.Y.Ceil()

cellsWidth := width / cellWidth
cellsHeight := height / cellHeight
cellsWidth := int(float64(width)*DeviceScale()) / cellWidth
cellsHeight := int(float64(height)*DeviceScale()) / cellHeight

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

func (g *Window) Layout(outsideWidth, outsideHeight int) (int, int) {
return g.cellsWidth * g.cellWidth, g.cellsHeight * g.cellHeight
s := DeviceScale()
return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
}

func (g *Window) Run(title string) error {
sw, sh := g.Layout(0, 0)

ebiten.SetScreenFilterEnabled(false)
ebiten.SetWindowSize(sw, sh)
ebiten.SetWindowSize(int(float64(g.cellsWidth*g.cellWidth)/DeviceScale()), int(float64(g.cellsHeight*g.cellHeight)/DeviceScale()))
ebiten.SetWindowTitle(title)
if err := ebiten.RunGame(g); err != nil {
return err
Expand Down
26 changes: 26 additions & 0 deletions dpi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package crt

import (
"github.com/hajimehoshi/ebiten/v2"
"os"
"strconv"
)

// DeviceScale returns the current device scale factor.
//
// If the environment variable CRT_DEVICE_SCALE is set, it will be used instead.
func DeviceScale() float64 {
if os.Getenv("CRT_DEVICE_SCALE") != "" {
s, err := strconv.ParseFloat(os.Getenv("CRT_DEVICE_SCALE"), 64)
if err == nil {
return s
}
}

return ebiten.DeviceScaleFactor()
}

// GetFontDPI returns the recommended font DPI for the current device.
func GetFontDPI() float64 {
return 72.0 * DeviceScale()
}
2 changes: 1 addition & 1 deletion examples/benchmark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
enableShader := flag.Bool("shader", false, "Enable shader")
flag.Parse()

fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 9.0)
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 9.0)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/keys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (m model) View() string {
}

func main() {
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 16.0)
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 16.0)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/package-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func max(a, b int) int {
func main() {
rand.Seed(time.Now().Unix())

fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 16.0)
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 16.0)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func max(a, b int) int {
func main() {
rand.Seed(time.Now().Unix())

fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 16.0)
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 16.0)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (m model) View() string {
}

func main() {
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", 72.0, 16.0)
fonts, err := crt.LoadFaces("./fonts/IosevkaTermNerdFontMono-Regular.ttf", "./fonts/IosevkaTermNerdFontMono-Bold.ttf", "./fonts/IosevkaTermNerdFontMono-Italic.ttf", crt.GetFontDPI(), 16.0)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 2c21fde

Please sign in to comment.