forked from oakmound/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet.go
48 lines (40 loc) · 1.24 KB
/
sheet.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
package render
import (
"image"
"github.com/oakmound/oak/oakerr"
)
// Sheet is a 2D array of image rgbas
type Sheet [][]*image.RGBA
// SubSprite gets a sprite from a sheet at the given location
func (sh *Sheet) SubSprite(x, y int) *Sprite {
return NewSprite(0, 0, (*sh)[x][y])
}
// ToSprites returns this sheet as a 2D array of Sprites
func (sh *Sheet) ToSprites() [][]*Sprite {
sprites := make([][]*Sprite, len(*sh))
for x, row := range *sh {
sprites[x] = make([]*Sprite, len(row))
for y := range row {
sprites[x][y] = sh.SubSprite(x, y)
}
}
return sprites
}
// NewSheetSequence creates a Sequence from a sheet and a list of x,y frame coordinates.
// A sequence will be created by getting the sheet's [i][i+1]th elements incrementally
// from the input frames. If the number of input frames is uneven, an error is returned.
func NewSheetSequence(sheet *Sheet, fps float64, frames ...int) (*Sequence, error) {
if len(frames)%2 != 0 {
return nil, oakerr.IndivisibleInput{
InputName: "frames",
IsList: true,
MustDivideBy: 2,
}
}
sh := *sheet
mods := make([]Modifiable, len(frames)/2)
for i := 0; i < len(frames); i += 2 {
mods[i/2] = NewSprite(0, 0, sh[frames[i]][frames[i+1]])
}
return NewSequence(fps, mods...), nil
}