-
Notifications
You must be signed in to change notification settings - Fork 14
/
utilities.go
57 lines (49 loc) · 1.37 KB
/
utilities.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package tilepix
import (
"image"
"io"
"os"
"github.com/gopxl/pixel"
log "github.com/sirupsen/logrus"
)
// loadPicture loads picture data from a Reader and will decode based using the built in image
// package.
func loadPicture(img io.Reader) (pixel.Picture, error) {
imgDecoded, _, err := image.Decode(img)
if err != nil {
log.WithError(err).Error("loadPicture: could not decode image")
return nil, err
}
return pixel.PictureDataFromImage(imgDecoded), nil
}
func loadSprite(img io.Reader) (*pixel.Sprite, pixel.Picture, error) {
pic, err := loadPicture(img)
if err != nil {
log.WithError(err).Error("loadSprite: could not load picture")
return nil, nil, err
}
sprite := pixel.NewSprite(pic, pic.Bounds())
return sprite, pic, nil
}
func loadSpriteFromFile(path string) (*pixel.Sprite, pixel.Picture, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0444)
if err != nil {
log.WithError(err).WithField("Filepath", path).Error("loadSpriteFromFile: could not open file")
return nil, nil, err
}
defer f.Close()
return loadSprite(f)
}
func tileIDToCoord(tID ID, numColumns int, numRows int) (x int, y int) {
tIDInt := int(tID)
x = tIDInt % numColumns
y = numRows - (tIDInt / numColumns) - 1
return
}
func indexToGamePos(idx int, width int, height int) pixel.Vec {
gamePos := pixel.V(
float64(idx%width),
float64(height)-float64(idx/width)-1,
)
return gamePos
}