Made music loop for Ebiten

This commit is contained in:
David Alasow 2017-12-23 18:57:44 +01:00
parent b510b10d1a
commit 39587cf196
1 changed files with 16 additions and 5 deletions

View File

@ -9,7 +9,7 @@ import (
"io/ioutil"
"log"
"strings"
"time"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/mp3"
@ -44,12 +44,12 @@ var (
s = character{`Sylvie`, color.RGBA{200, 255, 200, 255}}
d = character{``, color.RGBA{0, 255, 0, 0}}
messages = []dialogue{
{d, `playSong ./Hisoku.mp3`},
{d, `playSong ../../Resources/Hisoku.mp3`},
{s, `Hi there! How was class?`},
{m, `Good...`},
{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?`},
{d, `playSong ./Departures.mp3`},
{d, `playSong ../../Resources/Departures.mp3`},
{m, `Sure!`},
}
mplusNormalFont font.Face
@ -57,7 +57,9 @@ var (
bg *ebiten.Image
audioContext *audio.Context
audioPlayer *audio.Player
songSize int64
playingMusic = false
songDuration time.Duration
)
func playSong(path string) {
@ -71,13 +73,15 @@ func playSong(path string) {
check(err)
audioPlayer, err = audio.NewPlayer(audioContext, d)
check(err)
songSize = d.Size()
audioPlayer.Play()
playingMusic = true
songDuration = time.Second * time.Duration(songSize) / 4 / sampleRate
}
func init() {
f, err := ebitenutil.OpenFile("./mplus-1p-regular.ttf")
f, err := ebitenutil.OpenFile("../../Resources/mplus-1p-regular.ttf")
check(err)
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())
const x = 20
@ -134,7 +145,7 @@ func update(screen *ebiten.Image) error {
func main() {
var err error
bg, _, err = ebitenutil.NewImageFromFile("./bg.jpg", ebiten.FilterNearest)
bg, _, err = ebitenutil.NewImageFromFile("../../Resources/bg.jpg", ebiten.FilterNearest)
check(err)
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil {
log.Fatal(err)