etxt
v0.0.9
ETXT是Golang中矢量1文本渲染的軟件包,該渲染旨在與Hajime Hoshi製造的2D遊戲引擎Ebitengine一起使用。
儘管Ebitengine已經包含Ebiten/V2/Text/V2軟件包,但ETXT具有一些優勢:
font.Face 。ETXT不做什麼:
如果您不熟悉版式的術語和概念,我強烈建議您閱讀弗泰普(Freetype)glyph公約的第一章;您可以在Internet上找到的關於主題的最佳參考。
更少的談話和更多的代碼!
package main
import ( "math" ; "image/color" )
import "github.com/hajimehoshi/ebiten/v2"
import "github.com/tinne26/etxt"
import "github.com/tinne26/fonts/liberation/lbrtserif"
const WordsPerSec = 2.71828
var Words = [] string {
"solitude" , "joy" , "ride" , "whisper" , "leaves" , "cookie" ,
"hearts" , "disdain" , "simple" , "death" , "sea" , "shallow" ,
"self" , "rhyme" , "childish" , "sky" , "tic" , "tac" , "boom" ,
}
// ---- Ebitengine's Game interface implementation ----
type Game struct { text * etxt. Renderer ; wordIndex float64 }
func ( self * Game ) Layout ( winWidth int , winHeight int ) ( int , int ) {
scale := ebiten . DeviceScaleFactor () // *
// * ebiten.Monitor().DeviceScaleFactor() in >=v2.7.0
self . text . SetScale ( scale ) // relevant for HiDPI
canvasWidth := int ( math . Ceil ( float64 ( winWidth ) * scale ))
canvasHeight := int ( math . Ceil ( float64 ( winHeight ) * scale ))
return canvasWidth , canvasHeight
}
func ( self * Game ) Update () error {
newIndex := ( self . wordIndex + WordsPerSec / 60.0 )
self . wordIndex = math . Mod ( newIndex , float64 ( len ( Words )))
return nil
}
func ( self * Game ) Draw ( canvas * ebiten. Image ) {
// background color
canvas . Fill (color. RGBA { 229 , 255 , 222 , 255 })
// get screen center position
bounds := canvas . Bounds () // assumes origin (0, 0)
x , y := bounds . Dx () / 2 , bounds . Dy () / 2
// draw text
word := Words [ int ( self . wordIndex )]
self . text . Draw ( canvas , word , x , y )
}
// ---- main function ----
func main () {
// create text renderer, set the font and cache
renderer := etxt . NewRenderer ()
renderer . SetFont ( lbrtserif . Font ())
renderer . Utils (). SetCache8MiB ()
// adjust main text style properties
renderer . SetColor (color. RGBA { 239 , 91 , 91 , 255 })
renderer . SetAlign ( etxt . Center )
renderer . SetSize ( 72 )
// set up Ebitengine and start the game
ebiten . SetWindowTitle ( "etxt/examples/ebiten/words" )
err := ebiten . RunGame ( & Game { text : renderer })
if err != nil { panic ( err ) }
}您可以嘗試使用2 :
go run github.com/tinne26/etxt/examples/ebiten/words@latest
另外,您可以訪問https://tinne26.github.io/etxt-examples/,然後單擊Web版本的第一個示例。
這是一個非常簡單且獨立的例子。如果您想了解更多信息,請確保查看ETXT/示例!
是的,您可以使用-tags gtxt對其進行編譯。請注意, gtxt將使文本繪圖發生在CPU上,因此請勿嘗試將其用於實時應用程序。特別是,請注意不要意外地將gtxt與Ebitengine一起使用(在許多情況下它們是兼容的,但性能會消失)。
etxt/test的說明。如果使用類似像素的矢量字體,請閱讀以下技巧。 ↩
您需要Golang> = 1.18,如果您以前從未使用過Ebitengine,則可能需要安裝一些依賴項(通常僅在Linux或FreeBSD上)。 ↩