examples: Use embedded resources (#552)

This commit is contained in:
Hajime Hoshi 2018-03-15 01:16:41 +09:00
parent 908ff43d2e
commit 2555e3f226
18 changed files with 109 additions and 51 deletions

View File

@ -17,12 +17,14 @@
package main
import (
"bytes"
"image"
"image/color"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -63,11 +65,12 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Additive Blending (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,6 +17,7 @@
package main
import (
"bytes"
"fmt"
"image"
"image/color"
@ -26,6 +27,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -50,11 +52,12 @@ var (
)
func init() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
panic(err)
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
groundImage, _ = ebiten.NewImage(screenWidth*2, screenHeight*2/3+50, ebiten.FilterDefault)
perspectiveGroundImage, _ = ebiten.NewImage(screenWidth*2, screenHeight, ebiten.FilterDefault)

View File

@ -17,12 +17,14 @@
package main
import (
"bytes"
"image"
"image/color"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -66,11 +68,12 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,7 +17,9 @@
package main
import (
"bytes"
"fmt"
"image"
_ "image/jpeg"
"log"
"math"
@ -26,6 +28,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
"github.com/hajimehoshi/ebiten/inpututil"
)
@ -68,11 +71,12 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Context Lost (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,11 +17,14 @@
package main
import (
"bytes"
"image"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -57,14 +60,15 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
// Specifying filter on NewImage(FromImage) is just for backward compatibility.
// Now specifying filter at DrawImageOptions is recommended.
// Specify FilterDefault here, that means to prefer filter specified at DrawImageOptions.
ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Filter (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,12 +17,14 @@
package main
import (
"bytes"
"image"
"image/color"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -82,11 +84,12 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Flood fill with solid colors (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,13 +17,16 @@
package main
import (
"bytes"
"fmt"
"image"
_ "image/jpeg"
"log"
"math"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -124,11 +127,12 @@ Inverted: %s [I]`, hue, saturation, value, msgInverted)
}
func main() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "HSV (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,12 +17,14 @@
package main
import (
"bytes"
"image"
_ "image/jpeg"
"log"
"math"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -55,11 +57,12 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Hue (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,12 +17,15 @@
package main
import (
"bytes"
"fmt"
"image"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -35,11 +38,11 @@ var (
)
func init() {
var err error
bgImage, _, err = ebitenutil.NewImageFromFile("_resources/images/tile.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Tile_png))
if err != nil {
log.Fatal(err)
}
bgImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
var (

View File

@ -17,6 +17,7 @@
package main
import (
"bytes"
"image"
"image/color"
_ "image/jpeg"
@ -24,7 +25,7 @@ import (
"math"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -44,16 +45,17 @@ var (
)
func init() {
var err error
bgImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
bgImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
fgImage, _, err = ebitenutil.NewImageFromFile("_resources/images/fiveyears.jpg", ebiten.FilterDefault)
img, _, err = image.Decode(bytes.NewReader(images.FiveYears_jpg))
if err != nil {
log.Fatal(err)
}
fgImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
maskedFgImage, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterDefault)

View File

@ -17,11 +17,13 @@
package main
import (
"bytes"
"image"
_ "image/jpeg"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -37,11 +39,11 @@ var (
)
func init() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
func update(screen *ebiten.Image) error {

View File

@ -17,12 +17,13 @@
package main
import (
"bytes"
"image"
_ "image/jpeg"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -65,11 +66,12 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,12 +17,14 @@
package main
import (
"bytes"
"image"
_ "image/jpeg"
"log"
"math"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -59,11 +61,12 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Rotate (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -17,7 +17,9 @@
package main
import (
"bytes"
"fmt"
"image"
_ "image/png"
"log"
"math"
@ -25,6 +27,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -90,15 +93,19 @@ var (
)
func init() {
img, _, err := ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
if err != nil {
log.Fatal(err)
}
w, h := img.Size()
ebitenImage, _ = ebiten.NewImage(w, h, ebiten.FilterDefault)
origEbitenImage, _ := ebiten.NewImageFromImage(img, ebiten.FilterDefault)
w, h := origEbitenImage.Size()
ebitenImage, _ = ebiten.NewImage(w, h, ebiten.FilterNearest)
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1, 1, 1, 0.5)
ebitenImage.DrawImage(img, op)
ebitenImage.DrawImage(origEbitenImage, op)
for i := range sprites.sprites {
w, h := ebitenImage.Size()
x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h)

View File

@ -17,8 +17,10 @@
package main
import (
"bytes"
"errors"
"fmt"
"image"
_ "image/png"
"log"
"math"
@ -26,6 +28,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
"github.com/hajimehoshi/ebiten/inpututil"
)
@ -92,15 +95,19 @@ var (
)
func init() {
img, _, err := ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
if err != nil {
log.Fatal(err)
}
w, h := img.Size()
ebitenImage, _ = ebiten.NewImage(w, h, ebiten.FilterDefault)
origEbitenImage, _ := ebiten.NewImageFromImage(img, ebiten.FilterDefault)
w, h := origEbitenImage.Size()
ebitenImage, _ = ebiten.NewImage(w, h, ebiten.FilterNearest)
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1, 1, 1, 0.5)
ebitenImage.DrawImage(img, op)
ebitenImage.DrawImage(origEbitenImage, op)
for i := range sprites.sprites {
w, h := ebitenImage.Size()
x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h)

View File

@ -17,6 +17,7 @@
package main
import (
"bytes"
"fmt"
"image"
_ "image/png"
@ -24,6 +25,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
)
const (
@ -41,11 +43,11 @@ var (
)
func init() {
var err error
tilesImage, _, err = ebitenutil.NewImageFromFile("_resources/images/tiles.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Tiles_png))
if err != nil {
log.Fatal(err)
}
tilesImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
var (

View File

@ -17,6 +17,7 @@
package main
import (
"bytes"
"image"
"image/color"
_ "image/png"
@ -28,7 +29,7 @@ import (
"golang.org/x/image/font/gofont/goregular"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
"github.com/hajimehoshi/ebiten/inpututil"
"github.com/hajimehoshi/ebiten/text"
)
@ -44,11 +45,11 @@ var (
)
func init() {
var err error
uiImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ui.png", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.UI_png))
if err != nil {
log.Fatal(err)
}
uiImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
tt, err := truetype.Parse(goregular.TTF)
if err != nil {

View File

@ -17,6 +17,7 @@
package main
import (
"bytes"
"fmt"
"image"
"image/color"
@ -28,6 +29,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
"github.com/hajimehoshi/ebiten/inpututil"
)
@ -152,11 +154,11 @@ FPS: %0.2f`, x, y, ebiten.CurrentFPS())
func main() {
fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor())
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterDefault)
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})