example: Embed resources (#552)

This commit is contained in:
Hajime Hoshi 2018-03-14 03:05:59 +09:00
parent 7061024ece
commit cff14cbbd4
18 changed files with 114 additions and 81 deletions

View File

@ -19,7 +19,6 @@ package twenty48
import (
"errors"
"image/color"
"io/ioutil"
"log"
"math/rand"
"sort"
@ -29,7 +28,7 @@ import (
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/text"
)
@ -40,18 +39,7 @@ var (
)
func init() {
f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
tt, err := truetype.Parse(b)
tt, err := truetype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}

View File

@ -31,6 +31,7 @@ import (
"github.com/hajimehoshi/ebiten/audio/mp3"
"github.com/hajimehoshi/ebiten/audio/wav"
"github.com/hajimehoshi/ebiten/ebitenutil"
raudio "github.com/hajimehoshi/ebiten/examples/resources/audio"
"github.com/hajimehoshi/ebiten/inpututil"
)
@ -68,15 +69,7 @@ func playerBarRect() (x, y, w, h int) {
func NewPlayer(audioContext *audio.Context) (*Player, error) {
const bytesPerSample = 4 // TODO: This should be defined in audio package
wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
if err != nil {
return nil, err
}
mp3F, err := ebitenutil.OpenFile("_resources/audio/classic.mp3")
if err != nil {
return nil, err
}
s, err := mp3.Decode(audioContext, mp3F)
s, err := mp3.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Classic_mp3))
if err != nil {
return nil, err
}
@ -96,7 +89,7 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
}
player.audioPlayer.Play()
go func() {
s, err := wav.Decode(audioContext, wavF)
s, err := wav.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Jab_wav))
if err != nil {
log.Fatal(err)
return

View File

@ -19,12 +19,12 @@ package main
import (
"fmt"
"log"
"path/filepath"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/wav"
"github.com/hajimehoshi/ebiten/ebitenutil"
raudio "github.com/hajimehoshi/ebiten/examples/resources/audio"
)
const (
@ -47,16 +47,9 @@ 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)
wavS, err := wav.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Jab_wav))
if err != nil {
return err
}

View File

@ -18,7 +18,6 @@ package blocks
import (
"image/color"
"io/ioutil"
"log"
"strings"
@ -26,7 +25,7 @@ import (
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/text"
)
@ -39,18 +38,7 @@ var (
)
func init() {
f, err := ebitenutil.OpenFile("_resources/fonts/arcade_n.ttf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
tt, err := truetype.Parse(b)
tt, err := truetype.Parse(fonts.ArcadeN_ttf)
if err != nil {
log.Fatal(err)
}

View File

@ -19,7 +19,6 @@ package main
import (
"fmt"
"image/color"
"io/ioutil"
"log"
"math/rand"
"strings"
@ -29,7 +28,7 @@ import (
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/text"
)
@ -94,18 +93,7 @@ func init() {
}
func init() {
f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
tt, err := truetype.Parse(b)
tt, err := truetype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}

View File

@ -19,7 +19,6 @@ package main
import (
"fmt"
"image/color"
"io/ioutil"
"log"
"math"
@ -29,6 +28,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/inpututil"
"github.com/hajimehoshi/ebiten/text"
)
@ -38,18 +38,7 @@ var (
)
func init() {
f, err := ebitenutil.OpenFile("_resources/fonts/arcade_n.ttf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
tt, err := truetype.Parse(b)
tt, err := truetype.Parse(fonts.ArcadeN_ttf)
if err != nil {
log.Fatal(err)
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,27 @@
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate file2byteslice -package=audio -input=../_resources/audio/classic.mp3 -output=./audio/classic.go -var=Classic_mp3
//go:generate file2byteslice -package=audio -input=../_resources/audio/jab.wav -output=./audio/jab.go -var=Jab_wav
//go:generate file2byteslice -package=fonts -input=../_resources/fonts/arcade_n.ttf -output=./fonts/arcaden.go -var=ArcadeN_ttf
//go:generate file2byteslice -package=fonts -input=../_resources/fonts/mplus-1p-regular.ttf -output=./fonts/mplus1pregular.go -var=MPlus1pRegular_ttf
//go:generate file2byteslice -package=images -input=../_resources/images/ebiten.png -output=./images/ebiten.go -var=Ebiten_png
//go:generate file2byteslice -package=images -input=../_resources/images/fiveyears.jpg -output=./images/fiveyears.go -var=FiveYears_jpg
//go:generate file2byteslice -package=images -input=../_resources/images/gophers.jpg -output=./images/gophers.go -var=Gophers_jpg
//go:generate file2byteslice -package=images -input=../_resources/images/tile.png -output=./images/tile.go -var=Tile_png
//go:generate file2byteslice -package=images -input=../_resources/images/tiles.png -output=./images/tiles.go -var=Tiles_png
//go:generate file2byteslice -package=images -input=../_resources/images/ui.png -output=./images/ui.go -var=UI_png
//go:generate gofmt -s -w .
package resources

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,6 @@
// Code generated by file2byteslice. DO NOT EDIT.
// (gofmt is fine after generating)
package images
var UI_png = []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x80\x00\x00\x00\x80\b\x06\x00\x00\x00\xc3>a\xcb\x00\x00\x02\x1eIDATx\x9c\xed\xdb1N\xe3P\x10\x80\xe1\xf1*\x05U(\"A\x81\x1b:\x8a4\x94\\\x82\xc3\xd09\a\x80\x06q\x98p\b\x1f\x03J(\"A\xd2ЙbY\x89EX\xbbzI\x88\xa3\xf9\xbe\n)\x1a\x824?\xc6\xe89\xd5\xed\xedm,\x16\x8b.\n\xd4u]\x8d\xc7\xe38<<,\x9a\x8f\x88\xea\xf2\xf2\xb2p\x94M\x18-\x16\x8b\xee\xe2\xe2\xa2h\xb8m\xdb\xe2\xd9\x0f]DT\xeb|\x03\xd6\xf3k\xd7?\x00\xbb%\x80\xe4\x04\x90\x9c\x00\x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00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\xe7\x1dpX\xe8\f\x9d\xb6W:\x00\x00\x00\x00IEND\xaeB`\x82")

View File

@ -23,6 +23,7 @@ import (
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/wav"
"github.com/hajimehoshi/ebiten/ebitenutil"
raudio "github.com/hajimehoshi/ebiten/examples/resources/audio"
)
const (
@ -44,16 +45,22 @@ func init() {
log.Fatal(err)
}
// Open a wav file.
// Note that f.Close() should not be closed in this init function
// In this example, embeded resource "Jab_wav" is used.
//
// If you want to use a wav file, open this and pass the file stream to wav.Decode.
// Note that file's Close() should not be closed here
// since audio.Player manages stream state.
f, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
if err != nil {
log.Fatal(err)
}
//
// f, err := os.Open("jab.wav")
// if err != nil {
// return err
// }
//
// d, err := wav.Decode(audioContext, f)
// ...
// Decode wav-formatted data and retrieve decoded PCM stream.
d, err := wav.Decode(audioContext, f)
d, err := wav.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Jab_wav))
if err != nil {
log.Fatal(err)
}