Created
May 4, 2017 14:41
-
-
Save faiface/1ef3eb4f19cb3efbbf49b59b31652283 to your computer and use it in GitHub Desktop.
Replace "Pacifico.ttf" with arbitrary TTF font file path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"math/rand" | |
"os" | |
"time" | |
_ "image/jpeg" | |
_ "image/png" | |
"github.com/faiface/pixel" | |
"github.com/faiface/pixel/pixelgl" | |
"github.com/faiface/pixel/text" | |
"github.com/golang/freetype/truetype" | |
"golang.org/x/image/colornames" | |
"golang.org/x/image/font" | |
) | |
func loadTTF(path string, options *truetype.Options) (font.Face, error) { | |
file, err := os.Open(path) | |
if err != nil { | |
return nil, err | |
} | |
defer file.Close() | |
b, err := ioutil.ReadAll(file) | |
if err != nil { | |
return nil, err | |
} | |
ttf, err := truetype.Parse(b) | |
if err != nil { | |
return nil, err | |
} | |
return truetype.NewFace(ttf, options), nil | |
} | |
func testBig() { | |
cfg := pixelgl.WindowConfig{ | |
Title: "Pixel Rocks!", | |
Bounds: pixel.R(0, 0, 1024, 768), | |
Resizable: true, | |
VSync: true, | |
} | |
win, err := pixelgl.NewWindow(cfg) | |
if err != nil { | |
panic(err) | |
} | |
face, err := loadTTF("Pacifico.ttf", &truetype.Options{ | |
Size: 72, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
txt := text.New(face, text.ASCII) | |
txt.Orig = pixel.Y(win.Bounds().H()) | |
txt.Color(colornames.Black) | |
txt.LineHeight(1.5 * txt.Atlas().LineHeight()) | |
frames := 0 | |
second := time.Tick(time.Second) | |
for !win.Closed() { | |
if win.JustPressed(pixelgl.KeySpace) { | |
txt.Clear() | |
txt.Dot = txt.Orig | |
for txt.Dot.Y() > -22 { | |
for txt.Dot.X() < win.Bounds().W() { | |
fmt.Fprint(txt, string(text.ASCII[rand.Intn(len(text.ASCII))])) | |
} | |
fmt.Fprintln(txt) | |
} | |
} | |
win.Clear(colornames.White) | |
txt.Draw(win) | |
win.Update() | |
frames++ | |
select { | |
case <-second: | |
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames)) | |
frames = 0 | |
default: | |
} | |
} | |
} | |
func main() { | |
pixelgl.Run(testBig) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment