mirror of
https://github.com/stevenhowes/GoRetro.git
synced 2026-05-26 15:53:31 +01:00
Convert animator to use sprite sheets - unpolished WIP but works..
This commit is contained in:
+41
-43
@@ -8,9 +8,9 @@ package GoRetro
|
||||
*/
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
@@ -22,14 +22,25 @@ type animator struct {
|
||||
current string // The current sequence
|
||||
lastFrameChange time.Time // When we last ticked between frames
|
||||
finished bool // We're on the last frame
|
||||
tex *sdl.Texture // The frames
|
||||
}
|
||||
|
||||
func NewAnimator(
|
||||
container *Element,
|
||||
imagepath string,
|
||||
sequences map[string]*Sequence,
|
||||
defaultSequence string) *animator {
|
||||
defaultSequence string,
|
||||
renderer *sdl.Renderer) *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.sequences = sequences
|
||||
an.current = defaultSequence
|
||||
@@ -54,12 +65,11 @@ func (an *animator) onUpdate() error {
|
||||
}
|
||||
|
||||
func (an *animator) onDraw() error {
|
||||
tex := an.sequences[an.current].texture()
|
||||
|
||||
return drawTexture(
|
||||
tex,
|
||||
VectorInt32{-1, -1},
|
||||
VectorInt32{0, 0},
|
||||
an.tex,
|
||||
VectorInt32{an.sequences[an.current].frames[an.sequences[an.current].frame].W, an.sequences[an.current].frames[an.sequences[an.current].frame].H},
|
||||
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.Rotation,
|
||||
an.container.Renderer)
|
||||
@@ -70,73 +80,61 @@ func (an *animator) onCollision(other *Element) error {
|
||||
}
|
||||
|
||||
func (an *animator) setSequence(name string) bool {
|
||||
_, ok := an.sequences[name]
|
||||
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()
|
||||
return false
|
||||
}
|
||||
|
||||
// Use the new sequence
|
||||
an.current = name
|
||||
an.lastFrameChange = time.Now()
|
||||
}
|
||||
}
|
||||
return ok
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
type SequenceFrame struct {
|
||||
X int32 `json:"x"`
|
||||
Y int32 `json:"y"`
|
||||
W int32 `json:"w"`
|
||||
H int32 `json:"h"`
|
||||
}
|
||||
|
||||
type Sequence struct {
|
||||
textures []*sdl.Texture // The frames
|
||||
frame int // Current frame
|
||||
sampleRate float64 // Frames per second
|
||||
loop bool // Does this sequence play continuously?
|
||||
frames []SequenceFrame
|
||||
}
|
||||
|
||||
func NewSequence(
|
||||
filepath string, // Path to the folder for this sequence
|
||||
indexpath string,
|
||||
sampleRate float64,
|
||||
loop bool,
|
||||
renderer *sdl.Renderer) (*Sequence, error) {
|
||||
loop bool) (*Sequence, error) {
|
||||
|
||||
var seq Sequence
|
||||
|
||||
filepath = Config.DataDir + filepath
|
||||
indexpath = Config.DataDir + indexpath
|
||||
|
||||
// Get a list of frames
|
||||
files, err := ioutil.ReadDir(filepath)
|
||||
jsonFile, err := os.Open(indexpath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading directory %v: %v", filepath, err)
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
filename := path.Join(filepath, file.Name())
|
||||
|
||||
// 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)
|
||||
jsonParser := json.NewDecoder(jsonFile)
|
||||
if err = jsonParser.Decode(&seq.frames); err != nil {
|
||||
fmt.Println("parser err ", err)
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
|
||||
fmt.Println(seq.frames)
|
||||
|
||||
seq.frame = 0
|
||||
seq.sampleRate = sampleRate
|
||||
seq.loop = loop
|
||||
|
||||
return &seq, nil
|
||||
}
|
||||
|
||||
func (seq *Sequence) texture() *sdl.Texture {
|
||||
return seq.textures[seq.frame]
|
||||
}
|
||||
|
||||
func (seq *Sequence) resetFrame() {
|
||||
seq.frame = 0
|
||||
}
|
||||
|
||||
func (seq *Sequence) nextFrame() bool {
|
||||
// If we're at the end
|
||||
if seq.frame == len(seq.textures)-1 {
|
||||
if seq.frame == len(seq.frames)-1 {
|
||||
if seq.loop {
|
||||
// Reset for a looping sequence
|
||||
seq.resetFrame()
|
||||
|
||||
Reference in New Issue
Block a user