examples: Add comments

This commit is contained in:
Hajime Hoshi 2018-01-31 00:25:01 +09:00
parent 94f5f35423
commit 8a26841a88
2 changed files with 18 additions and 0 deletions

View File

@ -31,14 +31,23 @@ var (
)
func update(screen *ebiten.Image) error {
// Add a string from InputChars, that returns string input by users.
// Note that InputChars result changes every frame, so you need to call this
// every frame.
text += string(ebiten.InputChars())
// Adjust the string to be at most 10 lines.
ss := strings.Split(text, "\n")
if len(ss) > 10 {
text = strings.Join(ss[len(ss)-10:], "\n")
}
// If the enter key is pressed, add a line break.
if ebiten.IsKeyPressed(ebiten.KeyEnter) && !strings.HasSuffix(text, "\n") {
text += "\n"
}
// If the backspace key is pressed, remove one character.
bsPressed := ebiten.IsKeyPressed(ebiten.KeyBackspace)
if !bsPrevPressed && bsPressed {
if len(text) >= 1 {
@ -53,6 +62,7 @@ func update(screen *ebiten.Image) error {
return nil
}
// Blink the cursor.
t := text
if counter%60 < 30 {
t += "_"

View File

@ -38,21 +38,27 @@ var (
func init() {
var err error
// Initialize audio context.
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
// Open a wav file.
// Note that f.Close() should not be closed in this init function
// since audio.Player manages stream state.
f, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
if err != nil {
log.Fatal(err)
}
// Decode wav-formatted data and retrieve decoded PCM stream.
d, err := wav.Decode(audioContext, f)
if err != nil {
log.Fatal(err)
}
// Create an audio.Player that has one stream.
audioPlayer, err = audio.NewPlayer(audioContext, d)
if err != nil {
log.Fatal(err)
@ -61,6 +67,8 @@ func init() {
func update(screen *ebiten.Image) error {
if ebiten.IsKeyPressed(ebiten.KeyP) && !audioPlayer.IsPlaying() {
// As audioPlayer has one stream and remembers the playing position,
// rewinding is needed before playing when reusing audioPlayer.
audioPlayer.Rewind()
audioPlayer.Play()
}