commit 7105f9ec35402fc15ed0fcb8cfdd115f433c9677
parent 2d914ffbe94bd2d82f5f95c710887e59cb33dde1
Author: bsandro <brian.drosan@gmail.com>
Date: Tue, 17 Nov 2020 04:35:20 +0300
tiled background image
Diffstat:
3 files changed, 81 insertions(+), 13 deletions(-)
diff --git a/assets/grass.jpg b/assets/grass.jpg
Binary files differ.
diff --git a/ebitest.go b/ebitest.go
@@ -8,6 +8,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
_ "image"
_ "image/png"
+ _ "image/jpeg"
"log"
"math"
"math/rand"
@@ -21,8 +22,8 @@ var ScreenSize struct {
width, height int
}
-// BakaHa initialized in init()
-var BakaHa *ebiten.Image
+// images initialized in init()
+var Images = make(map[string]*ebiten.Image)
type Sprite struct {
img *ebiten.Image
@@ -112,6 +113,9 @@ func (sprite *Sprite) Update() error {
func (g *Game) Draw(screen *ebiten.Image) {
defer g.ShowDebug(screen)
+ // background
+ tileImage(screen, Images["grass.jpg"])
+
for _, sprite := range g.sprites {
//w, h := sprite.img.Size()
sprite.opt.GeoM.Reset()
@@ -141,13 +145,12 @@ func (g *Game) init() {
y := float64(ScreenSize.height) / 2.0
angle := rand.Float64() * math.Pi * 2.0
fmt.Printf("initial x, y, angle: %0.2f %0.2f %0.2f\n", x, y, angle)
- g.sprites = append(g.sprites, &Sprite{BakaHa, ebiten.DrawImageOptions{}, x, y, angle, 0.0 /* rotation */})
+ g.sprites = append(g.sprites, makeSprite("BakaHa2.webp", x, y, angle))
}
}
func main() {
fmt.Println("ebiten subgame")
- fmt.Println("BakaHa.png size:", len(Assets["BakaHa.png"]))
game := &Game{}
ebiten.SetWindowSize(ScreenSize.width, ScreenSize.height)
@@ -158,7 +161,63 @@ func main() {
}
}
+func tileImage(screen *ebiten.Image, img *ebiten.Image) {
+ width, height := float32(ScreenSize.width), float32(ScreenSize.height)
+ sx, sy := -width/2.0, -height/2.0
+ var x, y float32
+ vs := []ebiten.Vertex{
+ {
+ DstX: x,
+ DstY: y,
+ SrcX: sx,
+ SrcY: sy,
+ ColorR: 1,
+ ColorG: 1,
+ ColorB: 1,
+ ColorA: 1,
+ },
+ {
+ DstX: x + width,
+ DstY: y,
+ SrcX: sx + width,
+ SrcY: sy,
+ ColorR: 1,
+ ColorG: 1,
+ ColorB: 1,
+ ColorA: 1,
+ },
+ {
+ DstX: x,
+ DstY: y + height,
+ SrcX: sx,
+ SrcY: sy + height,
+ ColorR: 1,
+ ColorG: 1,
+ ColorB: 1,
+ ColorA: 1,
+ },
+ {
+ DstX: x + width,
+ DstY: y + height,
+ SrcX: sx + width,
+ SrcY: sy + height,
+ ColorR: 1,
+ ColorG: 1,
+ ColorB: 1,
+ ColorA: 1,
+ },
+ }
+ op := &ebiten.DrawTrianglesOptions{}
+ op.Address = ebiten.AddressRepeat
+ screen.DrawTriangles(vs, []uint16{0,1,2,1,2,3}, img, op)
+}
+
+func makeSprite(name string, x float64, y float64, angle float64) (*Sprite) {
+ return &Sprite{Images[name], ebiten.DrawImageOptions{}, x, y, angle, 0.0 /* rotation */}
+}
+
func (g *Game) ShowDebug(screen *ebiten.Image) {
msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS())
ebitenutil.DebugPrint(screen, msg)
}
+
diff --git a/init.go b/init.go
@@ -6,26 +6,35 @@ import (
"github.com/hajimehoshi/ebiten/v2"
_ "golang.org/x/image/webp"
"image"
+ _ "image/jpeg"
"log"
)
//go:generate go run generate/embed.go
+func getImage(name string) (*ebiten.Image, error) {
+ rawImage, format, err := image.Decode(bytes.NewReader(Assets[name]))
+ log.Printf("%s detected format: %s\n", name, format)
+ return ebiten.NewImageFromImage(rawImage), err
+}
+
func init() {
fmt.Println("init()")
for fname, raw := range Assets {
fmt.Printf("Loaded '%s' - %d bytes\n", fname, len(raw))
}
- rawImage, format, err := image.Decode(bytes.NewReader(Assets["BakaHa3.webp"]))
- log.Println("bakaHa detected format:", format)
- if err != nil {
- log.Fatal(err)
- }
- img := ebiten.NewImageFromImage(rawImage)
+ imageNames := []string{"BakaHa2.webp", "grass.jpg"}
- BakaHa = img
+ for _, name := range imageNames {
+ image, err := getImage(name)
+ if err != nil {
+ log.Fatal(err)
+ }
+ // some global memory storage
+ Images[name] = image
+ }
- ScreenSize.width = 640
- ScreenSize.height = 480
+ ScreenSize.width = 1024
+ ScreenSize.height = 768
}