gameLibTests/Ebiten/VN/main.go

154 lines
3.3 KiB
Go
Raw Normal View History

2017-12-22 16:52:21 +01:00
package main
import (
2017-12-22 23:13:31 +01:00
"fmt"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"image/color"
2017-12-22 16:52:21 +01:00
_ "image/jpeg"
2017-12-22 23:13:31 +01:00
"io/ioutil"
"log"
"strings"
2017-12-23 18:57:44 +01:00
"time"
2017-12-22 23:13:31 +01:00
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/mp3"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/text"
2017-12-22 16:52:21 +01:00
)
const (
2017-12-22 23:13:31 +01:00
screenWidth = 640
screenHeight = 480
sampleRate = 44100
2017-12-22 16:52:21 +01:00
)
type dialogue struct {
Speaker character
2017-12-22 23:13:31 +01:00
Text string
2017-12-22 16:52:21 +01:00
}
type character struct {
2017-12-22 23:13:31 +01:00
Name string
2017-12-22 16:52:21 +01:00
Color color.RGBA
}
2017-12-23 10:14:41 +01:00
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
2017-12-22 16:52:21 +01:00
var (
2017-12-22 23:13:31 +01:00
m = character{`Me`, color.RGBA{200, 200, 255, 255}}
s = character{`Sylvie`, color.RGBA{200, 255, 200, 255}}
d = character{``, color.RGBA{0, 255, 0, 0}}
2017-12-22 16:52:21 +01:00
messages = []dialogue{
2017-12-23 18:57:44 +01:00
{d, `playSong ../../Resources/Hisoku.mp3`},
2017-12-22 23:10:41 +01:00
{s, `Hi there! How was class?`},
2017-12-22 23:13:31 +01:00
{m, `Good...`},
2017-12-22 16:52:21 +01:00
{d, `I can't bring myself to admit that it all went in one ear and out the other.`},
2017-12-22 23:13:31 +01:00
{s, `Are you going home now? Wanna walk back with me?`},
2017-12-23 18:57:44 +01:00
{d, `playSong ../../Resources/Departures.mp3`},
2017-12-22 23:13:31 +01:00
{m, `Sure!`},
2017-12-22 16:52:21 +01:00
}
2017-12-22 23:13:31 +01:00
mplusNormalFont font.Face
line = 0
bg *ebiten.Image
audioContext *audio.Context
audioPlayer *audio.Player
2017-12-23 18:57:44 +01:00
songSize int64
2017-12-22 23:13:31 +01:00
playingMusic = false
2017-12-23 18:57:44 +01:00
songDuration time.Duration
2017-12-22 16:52:21 +01:00
)
2017-12-22 23:10:41 +01:00
func playSong(path string) {
2017-12-22 23:13:31 +01:00
var err error
if playingMusic {
audioPlayer.Close()
}
f, err := ebitenutil.OpenFile(path)
2017-12-23 10:14:41 +01:00
check(err)
2017-12-22 23:13:31 +01:00
d, err := mp3.Decode(audioContext, f)
2017-12-23 10:14:41 +01:00
check(err)
2017-12-22 23:13:31 +01:00
audioPlayer, err = audio.NewPlayer(audioContext, d)
2017-12-23 10:14:41 +01:00
check(err)
2017-12-23 18:57:44 +01:00
songSize = d.Size()
2017-12-22 23:13:31 +01:00
audioPlayer.Play()
playingMusic = true
2017-12-23 18:57:44 +01:00
songDuration = time.Second * time.Duration(songSize) / 4 / sampleRate
2017-12-22 23:10:41 +01:00
}
2017-12-22 16:52:21 +01:00
func init() {
2017-12-23 18:57:44 +01:00
f, err := ebitenutil.OpenFile("../../Resources/mplus-1p-regular.ttf")
2017-12-23 10:14:41 +01:00
check(err)
2017-12-22 23:13:31 +01:00
defer f.Close()
2017-12-22 16:52:21 +01:00
2017-12-22 23:13:31 +01:00
b, err := ioutil.ReadAll(f)
2017-12-23 10:14:41 +01:00
check(err)
2017-12-22 23:13:31 +01:00
tt, err := truetype.Parse(b)
2017-12-23 10:14:41 +01:00
check(err)
2017-12-22 23:13:31 +01:00
const dpi = 72
mplusNormalFont = truetype.NewFace(tt, &truetype.Options{
Size: 24,
DPI: dpi,
Hinting: font.HintingFull,
})
audioContext, err = audio.NewContext(sampleRate)
2017-12-23 10:14:41 +01:00
check(err)
2017-12-22 23:13:31 +01:00
}
2017-12-22 16:52:21 +01:00
func update(screen *ebiten.Image) error {
2017-12-22 23:13:31 +01:00
if ebiten.IsRunningSlowly() {
return nil
}
action := strings.Split(messages[line].Text, " ")
for _, v := range ebiten.InputChars() {
if v == 32 {
if line+1 != len(messages) {
line++
}
}
}
action = strings.Split(messages[line].Text, " ")
if action[0] == "playSong" {
if line+1 != len(messages) {
playSong(action[1])
line++
}
}
2017-12-23 18:57:44 +01:00
if playingMusic {
if int(audioPlayer.Current().Seconds()) == int(songDuration.Seconds()) {
audioPlayer.Seek(0)
}
}
2017-12-22 23:13:31 +01:00
msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())
const x = 20
op := &ebiten.DrawImageOptions{}
screen.DrawImage(bg, op)
ebitenutil.DrawRect(screen, 30, 350, screenWidth-60, 100, color.RGBA{128, 128, 128, 200})
text.Draw(screen, msg, mplusNormalFont, x, 40, color.White)
text.Draw(screen, messages[line].Speaker.Name, mplusNormalFont, 50, 370, messages[line].Speaker.Color)
text.Draw(screen, messages[line].Text, mplusNormalFont, 50, 400, color.White)
return nil
2017-12-22 16:52:21 +01:00
}
func main() {
var err error
2017-12-23 18:57:44 +01:00
bg, _, err = ebitenutil.NewImageFromFile("../../Resources/bg.jpg", ebiten.FilterNearest)
2017-12-23 10:14:41 +01:00
check(err)
2017-12-22 23:13:31 +01:00
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}