Convert animator to use sprite sheets - unpolished WIP but works..

This commit is contained in:
stevenhowes
2022-01-18 22:19:53 +00:00
parent 2bf460b999
commit f73e065e70
+41 -43
View File
@@ -8,9 +8,9 @@ package GoRetro
*/ */
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
"path"
"time" "time"
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
@@ -22,14 +22,25 @@ type animator struct {
current string // The current sequence current string // The current sequence
lastFrameChange time.Time // When we last ticked between frames lastFrameChange time.Time // When we last ticked between frames
finished bool // We're on the last frame finished bool // We're on the last frame
tex *sdl.Texture // The frames
} }
func NewAnimator( func NewAnimator(
container *Element, container *Element,
imagepath string,
sequences map[string]*Sequence, sequences map[string]*Sequence,
defaultSequence string) *animator { defaultSequence string,
renderer *sdl.Renderer) *animator {
var an animator var an animator
imagepath = Config.DataDir + imagepath
tex, err := loadTextureFromBMP(imagepath, renderer)
if err != nil {
fmt.Println("texture err ", err)
}
an.tex = tex
an.container = container an.container = container
an.sequences = sequences an.sequences = sequences
an.current = defaultSequence an.current = defaultSequence
@@ -54,12 +65,11 @@ func (an *animator) onUpdate() error {
} }
func (an *animator) onDraw() error { func (an *animator) onDraw() error {
tex := an.sequences[an.current].texture()
return drawTexture( return drawTexture(
tex, an.tex,
VectorInt32{-1, -1}, VectorInt32{an.sequences[an.current].frames[an.sequences[an.current].frame].W, an.sequences[an.current].frames[an.sequences[an.current].frame].H},
VectorInt32{0, 0}, VectorInt32{an.sequences[an.current].frames[an.sequences[an.current].frame].X, an.sequences[an.current].frames[an.sequences[an.current].frame].Y},
an.container.Position, an.container.Position,
an.container.Rotation, an.container.Rotation,
an.container.Renderer) an.container.Renderer)
@@ -70,73 +80,61 @@ func (an *animator) onCollision(other *Element) error {
} }
func (an *animator) setSequence(name string) bool { func (an *animator) setSequence(name string) bool {
_, ok := an.sequences[name] return false
if ok { }
// If we *are* changing sequence, change the name and reset the frame
if an.current != name {
// Reset the old sequence to frame 0
sequence := an.sequences[an.current]
sequence.resetFrame()
// Use the new sequence //-----------------------------------------------------------------------------
an.current = name
an.lastFrameChange = time.Now() type SequenceFrame struct {
} X int32 `json:"x"`
} Y int32 `json:"y"`
return ok W int32 `json:"w"`
H int32 `json:"h"`
} }
type Sequence struct { type Sequence struct {
textures []*sdl.Texture // The frames
frame int // Current frame frame int // Current frame
sampleRate float64 // Frames per second sampleRate float64 // Frames per second
loop bool // Does this sequence play continuously? loop bool // Does this sequence play continuously?
frames []SequenceFrame
} }
func NewSequence( func NewSequence(
filepath string, // Path to the folder for this sequence indexpath string,
sampleRate float64, sampleRate float64,
loop bool, loop bool) (*Sequence, error) {
renderer *sdl.Renderer) (*Sequence, error) {
var seq Sequence var seq Sequence
filepath = Config.DataDir + filepath indexpath = Config.DataDir + indexpath
// Get a list of frames jsonFile, err := os.Open(indexpath)
files, err := ioutil.ReadDir(filepath)
if err != nil { if err != nil {
return nil, fmt.Errorf("reading directory %v: %v", filepath, err) fmt.Println(err)
os.Exit(1)
} }
jsonParser := json.NewDecoder(jsonFile)
for _, file := range files { if err = jsonParser.Decode(&seq.frames); err != nil {
filename := path.Join(filepath, file.Name()) fmt.Println("parser err ", err)
// Load this frame and turn it into a texture
tex, err := loadTextureFromBMP(filename, renderer)
if err != nil {
return nil, fmt.Errorf("loading sequence frame: %v", err)
}
seq.textures = append(seq.textures, tex)
} }
defer jsonFile.Close()
fmt.Println(seq.frames)
seq.frame = 0
seq.sampleRate = sampleRate seq.sampleRate = sampleRate
seq.loop = loop seq.loop = loop
return &seq, nil return &seq, nil
} }
func (seq *Sequence) texture() *sdl.Texture {
return seq.textures[seq.frame]
}
func (seq *Sequence) resetFrame() { func (seq *Sequence) resetFrame() {
seq.frame = 0 seq.frame = 0
} }
func (seq *Sequence) nextFrame() bool { func (seq *Sequence) nextFrame() bool {
// If we're at the end // If we're at the end
if seq.frame == len(seq.textures)-1 { if seq.frame == len(seq.frames)-1 {
if seq.loop { if seq.loop {
// Reset for a looping sequence // Reset for a looping sequence
seq.resetFrame() seq.resetFrame()