-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
39 lines (31 loc) · 863 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"log/slog"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
func initImages(m map[int]string) ([]*ebiten.Image, error) {
var imgs []*ebiten.Image
for _, path := range m {
img, _, err := ebitenutil.NewImageFromFile(path)
if err != nil {
return nil, err
}
if opts.ResizeImages {
img = resizeImage(img)
}
imgs = append(imgs, img)
}
return imgs, nil
}
func resizeImage(img *ebiten.Image) *ebiten.Image {
newImg := ebiten.NewImage(opts.Width, opts.Height)
if opts.Verbose {
slog.Info("resizing image", "old", img.Bounds(), "new", newImg.Bounds())
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(img.Bounds().Dx())/2, -float64(img.Bounds().Dy())/2)
op.GeoM.Translate(float64(opts.Width)/2, float64(opts.Height)/2)
newImg.DrawImage(img, op)
return newImg
}