Made music loop for Ebiten

This commit is contained in:
David Alasow 2017-12-23 18:57:44 +01:00
parent b510b10d1a
commit 39587cf196

View File

@ -9,7 +9,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"strings" "strings"
"time"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/mp3" "github.com/hajimehoshi/ebiten/audio/mp3"
@ -44,12 +44,12 @@ var (
s = character{`Sylvie`, color.RGBA{200, 255, 200, 255}} s = character{`Sylvie`, color.RGBA{200, 255, 200, 255}}
d = character{``, color.RGBA{0, 255, 0, 0}} d = character{``, color.RGBA{0, 255, 0, 0}}
messages = []dialogue{ messages = []dialogue{
{d, `playSong ./Hisoku.mp3`}, {d, `playSong ../../Resources/Hisoku.mp3`},
{s, `Hi there! How was class?`}, {s, `Hi there! How was class?`},
{m, `Good...`}, {m, `Good...`},
{d, `I can't bring myself to admit that it all went in one ear and out the other.`}, {d, `I can't bring myself to admit that it all went in one ear and out the other.`},
{s, `Are you going home now? Wanna walk back with me?`}, {s, `Are you going home now? Wanna walk back with me?`},
{d, `playSong ./Departures.mp3`}, {d, `playSong ../../Resources/Departures.mp3`},
{m, `Sure!`}, {m, `Sure!`},
} }
mplusNormalFont font.Face mplusNormalFont font.Face
@ -57,7 +57,9 @@ var (
bg *ebiten.Image bg *ebiten.Image
audioContext *audio.Context audioContext *audio.Context
audioPlayer *audio.Player audioPlayer *audio.Player
songSize int64
playingMusic = false playingMusic = false
songDuration time.Duration
) )
func playSong(path string) { func playSong(path string) {
@ -71,13 +73,15 @@ func playSong(path string) {
check(err) check(err)
audioPlayer, err = audio.NewPlayer(audioContext, d) audioPlayer, err = audio.NewPlayer(audioContext, d)
check(err) check(err)
songSize = d.Size()
audioPlayer.Play() audioPlayer.Play()
playingMusic = true playingMusic = true
songDuration = time.Second * time.Duration(songSize) / 4 / sampleRate
} }
func init() { func init() {
f, err := ebitenutil.OpenFile("./mplus-1p-regular.ttf") f, err := ebitenutil.OpenFile("../../Resources/mplus-1p-regular.ttf")
check(err) check(err)
defer f.Close() defer f.Close()
@ -117,6 +121,13 @@ func update(screen *ebiten.Image) error {
} }
} }
if playingMusic {
if int(audioPlayer.Current().Seconds()) == int(songDuration.Seconds()) {
audioPlayer.Seek(0)
}
}
msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()) msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())
const x = 20 const x = 20
@ -134,7 +145,7 @@ func update(screen *ebiten.Image) error {
func main() { func main() {
var err error var err error
bg, _, err = ebitenutil.NewImageFromFile("./bg.jpg", ebiten.FilterNearest) bg, _, err = ebitenutil.NewImageFromFile("../../Resources/bg.jpg", ebiten.FilterNearest)
check(err) check(err)
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)