examples: Refactoring

This commit is contained in:
Hajime Hoshi 2018-01-22 23:07:55 +09:00
parent 13d3918ec6
commit 0893b04cbf
3 changed files with 15 additions and 0 deletions

View File

@ -40,16 +40,19 @@ func update(screen *ebiten.Image) error {
}
const (
// The offset point to render the image.
ox = 10
oy = 10
)
screen.Fill(color.NRGBA{0x00, 0x40, 0x80, 0xff})
// Draw the image with 'Source Alpha' composite mode (default).
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(ox, oy)
screen.DrawImage(ebitenImage, op)
// Draw the image with 'Lighter (a.k.a Additive)' composite mode.
op = &ebiten.DrawImageOptions{}
w, _ := ebitenImage.Size()
op.GeoM.Translate(ox+float64(w), oy)

View File

@ -45,10 +45,14 @@ func update(screen *ebiten.Image) error {
case 240 < count:
diff = float64(480-count) * 0.2
}
if ebiten.IsRunningSlowly() {
return nil
}
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
// Draw 100 Ebitens
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
for i := 0; i < 10*10; i++ {

View File

@ -47,22 +47,30 @@ var player *audio.Player
func update(screen *ebiten.Image) error {
if player == nil {
// Open a wav file.
// wavF is *os.File, which is an io.ReadCloser and io.Seeker.
wavF, err := ebitenutil.OpenFile(filepath.Join("_resources", "audio", "jab.wav"))
if err != nil {
return err
}
// Decode the wav file.
// wavS is a decoded io.ReadCloser and io.Seeker.
wavS, err := wav.Decode(audioContext, wavF)
if err != nil {
return err
}
// Create an infinite loop stream from the decoded bytes.
// s is still an io.ReadCloser and io.Seeker.
s := audio.NewInfiniteLoop(wavS, wavS.Length())
player, err = audio.NewPlayer(audioContext, s)
if err != nil {
return err
}
// Play the infinite-length stream. This never ends.
player.Play()
}