github.com/Kintar/etxt@v0.0.0-20221224033739-2fc69f000137/examples/ebiten/hello_world/main.go (about)

     1  package main
     2  
     3  import (
     4  	"image/color"
     5  	"log"
     6  	"time"
     7  )
     8  import "github.com/hajimehoshi/ebiten/v2"
     9  import "github.com/Kintar/etxt"
    10  
    11  // NOTICE: this is the example from the readme, but it's not
    12  //         easy to use, as it works differently from others and
    13  //         expects specific fonts and paths. You probably don't
    14  //         want to go changing all this manually... but if you
    15  //         do, check the lines with !! comments at the end.
    16  
    17  type Game struct{ txtRenderer *etxt.Renderer }
    18  
    19  func (self *Game) Layout(int, int) (int, int) { return 400, 400 }
    20  func (self *Game) Update() error              { return nil }
    21  func (self *Game) Draw(screen *ebiten.Image) {
    22  	// hacky color computation
    23  	millis := time.Now().UnixMilli()
    24  	blue := (millis / 16) % 512
    25  	if blue >= 256 {
    26  		blue = 511 - blue
    27  	}
    28  	changingColor := color.RGBA{0, 255, uint8(blue), 255}
    29  
    30  	// set relevant text renderer properties and draw
    31  	self.txtRenderer.SetTarget(screen)
    32  	self.txtRenderer.SetColor(changingColor)
    33  	self.txtRenderer.Draw("Hello World!", 200, 200)
    34  }
    35  
    36  func main() {
    37  	// load font library
    38  	fontLib := etxt.NewFontLibrary()
    39  	_, _, err := fontLib.ParseDirFonts("game_dir/assets/fonts") // !!
    40  	if err != nil {
    41  		log.Fatal("Error while loading fonts: " + err.Error())
    42  	}
    43  
    44  	// check that we have the fonts we want
    45  	// (shown for completeness, you don't need this in most cases)
    46  	expectedFonts := []string{"Roboto Bold", "Carter One"} // !!
    47  	for _, fontName := range expectedFonts {
    48  		if !fontLib.HasFont(fontName) {
    49  			log.Fatal("missing font: " + fontName)
    50  		}
    51  	}
    52  
    53  	// check that the fonts have the characters we want
    54  	// (shown for completeness, you don't need this in most cases)
    55  	err = fontLib.EachFont(checkMissingRunes)
    56  	if err != nil {
    57  		log.Fatal(err)
    58  	}
    59  
    60  	// create a new text renderer and configure it
    61  	txtRenderer := etxt.NewStdRenderer()
    62  	glyphsCache := etxt.NewDefaultCache(10 * 1024 * 1024) // 10MB
    63  	txtRenderer.SetCacheHandler(glyphsCache.NewHandler())
    64  	txtRenderer.SetFont(fontLib.GetFont(expectedFonts[0]))
    65  	txtRenderer.SetAlign(etxt.YCenter, etxt.XCenter)
    66  	txtRenderer.SetSizePx(64)
    67  
    68  	// run the "game"
    69  	ebiten.SetWindowSize(400, 400)
    70  	err = ebiten.RunGame(&Game{txtRenderer})
    71  	if err != nil {
    72  		log.Fatal(err)
    73  	}
    74  }
    75  
    76  // helper function used with FontLibrary.EachFont to make sure
    77  // all loaded fonts contain the characters or alphabet we want
    78  func checkMissingRunes(name string, font *etxt.Font) error {
    79  	const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    80  	const symbols = "0123456789 .,;:!?-()[]{}_&#@"
    81  
    82  	missing, err := etxt.GetMissingRunes(font, letters+symbols)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	if len(missing) > 0 {
    87  		log.Fatalf("Font '%s' missing runes: %s", name, string(missing))
    88  	}
    89  	return nil
    90  }