commit e92cab51630b6d5bde082d363d37cc7926f6199e
parent ca73fc4f2e299db6511a86e6944e836dbbc3859d
Author: bsandro <brian.drosan@gmail.com>
Date: Mon, 16 Nov 2020 03:23:43 +0300
embed assets via go generate
Diffstat:
6 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/.hgignore b/.hgignore
@@ -1 +1,2 @@
ebitest
+assets.go
diff --git a/Makefile b/Makefile
@@ -9,6 +9,11 @@ native:
@go build
+.PHONY: gen
+
+gen:
+ @go generate
+
run:
@go run .
diff --git a/BakaHa.png b/assets/BakaHa.png
Binary files differ.
diff --git a/ebitest.go b/ebitest.go
@@ -42,6 +42,10 @@ func (g *Game) Update() error {
g.init()
}
+ //if ebiten.IsDrawingSkipped() {
+ // return nil
+ //}
+
if ebiten.IsKeyPressed(ebiten.KeyQ) || ebiten.IsKeyPressed(ebiten.KeyEscape) {
// @todo find out if it is possible to gracefully stop the game via ebiten structure
return errors.New("exit")
@@ -140,6 +144,7 @@ func (g *Game) init() {
func main() {
fmt.Println("ebiten subgame")
+ fmt.Println("BakaHa.png size:", len(Assets["BakaHa.png"]))
game := &Game{}
ebiten.SetWindowSize(ScreenSize.width, ScreenSize.height)
diff --git a/generate/embed.go b/generate/embed.go
@@ -0,0 +1,18 @@
+package main
+
+import (
+ "io/ioutil"
+ "os"
+ "fmt"
+)
+
+func main() {
+ assets, _ := ioutil.ReadDir("assets")
+ out, _ := os.Create("assets.go")
+ out.Write([]byte("package main\n\nvar Assets = map[string][]byte{\n"))
+ for _, asset := range assets {
+ assetFile, _ := ioutil.ReadFile("assets/" + asset.Name())
+ fmt.Fprintf(out, "\t\"%s\": []byte(%q)", asset.Name(), string(assetFile))
+ }
+ out.Write([]byte("}\n"))
+}
diff --git a/init.go b/init.go
@@ -2,18 +2,29 @@ package main
import (
"fmt"
+ "bytes"
"log"
- //"github.com/hajimehoshi/ebiten/v2"
- "github.com/hajimehoshi/ebiten/v2/ebitenutil"
+ "github.com/hajimehoshi/ebiten/v2"
+ //"github.com/hajimehoshi/ebiten/v2/ebitenutil"
+ "image"
+ _ "image/png"
)
+//go:generate go run generate/embed.go
+
func init() {
fmt.Println("init()")
- img, _, err := ebitenutil.NewImageFromFile("BakaHa.png")
+ for fname, raw := range Assets {
+ fmt.Printf("Loaded '%s' - %d bytes\n", fname, len(raw))
+ }
+ rawImage, format, err := image.Decode(bytes.NewReader(Assets["BakaHa.png"]))
+ log.Println("bakaHa detected format:", format)
if err != nil {
log.Fatal(err)
}
+ img := ebiten.NewImageFromImage(rawImage)
+
BakaHa = img
ScreenSize.width = 640