mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
examples: Add comments
This commit is contained in:
parent
94f5f35423
commit
8a26841a88
@ -31,14 +31,23 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
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())
|
text += string(ebiten.InputChars())
|
||||||
|
|
||||||
|
// Adjust the string to be at most 10 lines.
|
||||||
ss := strings.Split(text, "\n")
|
ss := strings.Split(text, "\n")
|
||||||
if len(ss) > 10 {
|
if len(ss) > 10 {
|
||||||
text = strings.Join(ss[len(ss)-10:], "\n")
|
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") {
|
if ebiten.IsKeyPressed(ebiten.KeyEnter) && !strings.HasSuffix(text, "\n") {
|
||||||
text += "\n"
|
text += "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the backspace key is pressed, remove one character.
|
||||||
bsPressed := ebiten.IsKeyPressed(ebiten.KeyBackspace)
|
bsPressed := ebiten.IsKeyPressed(ebiten.KeyBackspace)
|
||||||
if !bsPrevPressed && bsPressed {
|
if !bsPrevPressed && bsPressed {
|
||||||
if len(text) >= 1 {
|
if len(text) >= 1 {
|
||||||
@ -53,6 +62,7 @@ func update(screen *ebiten.Image) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Blink the cursor.
|
||||||
t := text
|
t := text
|
||||||
if counter%60 < 30 {
|
if counter%60 < 30 {
|
||||||
t += "_"
|
t += "_"
|
||||||
|
@ -38,21 +38,27 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
var err error
|
||||||
|
// Initialize audio context.
|
||||||
audioContext, err = audio.NewContext(sampleRate)
|
audioContext, err = audio.NewContext(sampleRate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
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")
|
f, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decode wav-formatted data and retrieve decoded PCM stream.
|
||||||
d, err := wav.Decode(audioContext, f)
|
d, err := wav.Decode(audioContext, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create an audio.Player that has one stream.
|
||||||
audioPlayer, err = audio.NewPlayer(audioContext, d)
|
audioPlayer, err = audio.NewPlayer(audioContext, d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -61,6 +67,8 @@ func init() {
|
|||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func update(screen *ebiten.Image) error {
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyP) && !audioPlayer.IsPlaying() {
|
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.Rewind()
|
||||||
audioPlayer.Play()
|
audioPlayer.Play()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user