reorg dc6 sprite struct to include directions

This commit is contained in:
Alex Yatskov 2018-12-17 11:45:43 -08:00
parent e016bd7ec1
commit 7204f07861

View File

@ -14,7 +14,7 @@ const (
FlagIs24Bits FlagIs24Bits
) )
type FileHeader struct { type fileHeader struct {
Version uint32 Version uint32
Flags uint32 Flags uint32
Format uint32 Format uint32
@ -23,7 +23,7 @@ type FileHeader struct {
FramesPerDir uint32 FramesPerDir uint32
} }
type FrameHeader struct { type frameHeader struct {
Flip uint32 Flip uint32
Width uint32 Width uint32
Height uint32 Height uint32
@ -34,6 +34,10 @@ type FrameHeader struct {
Length uint32 Length uint32
} }
type Direction struct {
Frames []Frame
}
type Frame struct { type Frame struct {
Width int Width int
Height int Height int
@ -42,20 +46,22 @@ type Frame struct {
Data []byte Data []byte
} }
type Dc6 struct { type Sprite struct {
Frames []Frame Directions []Direction
} }
func New(reader io.ReadSeeker) (*Dc6, error) { func New(reader io.ReadSeeker) (*Sprite, error) {
sprite := new(Dc6) sprite := new(Sprite)
var fileHeader FileHeader var fileHead fileHeader
if err := binary.Read(reader, binary.LittleEndian, &fileHeader); err != nil { if err := binary.Read(reader, binary.LittleEndian, &fileHead); err != nil {
return nil, err return nil, err
} }
frameCount := int(fileHead.DirCount * fileHead.FramesPerDir)
var frameOffsets []uint32 var frameOffsets []uint32
for i := 0; i < int(fileHeader.DirCount*fileHeader.FramesPerDir); i++ { for i := 0; i < frameCount; i++ {
var frameOffset uint32 var frameOffset uint32
if err := binary.Read(reader, binary.LittleEndian, &frameOffset); err != nil { if err := binary.Read(reader, binary.LittleEndian, &frameOffset); err != nil {
return nil, err return nil, err
@ -64,37 +70,40 @@ func New(reader io.ReadSeeker) (*Dc6, error) {
frameOffsets = append(frameOffsets, frameOffset) frameOffsets = append(frameOffsets, frameOffset)
} }
for _, frameOffset := range frameOffsets { sprite.Directions = make([]Direction, fileHead.FramesPerDir)
var frameHeader FrameHeader
if _, err := reader.Seek(int64(frameOffset), io.SeekStart); err != nil { for i := 0; i < frameCount; i++ {
var frameHead frameHeader
if _, err := reader.Seek(int64(frameOffsets[i]), io.SeekStart); err != nil {
return nil, err return nil, err
} }
if err := binary.Read(reader, binary.LittleEndian, &frameHeader); err != nil { if err := binary.Read(reader, binary.LittleEndian, &frameHead); err != nil {
return nil, err return nil, err
} }
data := make([]byte, frameHeader.Width*frameHeader.Height) data := make([]byte, frameHead.Width*frameHead.Height)
writer := streaming.NewWriter(data) writer := streaming.NewWriter(data)
if err := extractFrame(reader, writer, frameHeader); err != nil { if err := extractFrame(reader, writer, frameHead); err != nil {
return nil, err return nil, err
} }
frame := Frame{ frame := Frame{
int(frameHeader.Width), int(frameHead.Width),
int(frameHeader.Height), int(frameHead.Height),
int(frameHeader.OffsetX), int(frameHead.OffsetX),
int(frameHeader.OffsetY), int(frameHead.OffsetY),
data, data,
} }
sprite.Frames = append(sprite.Frames, frame) direction := &sprite.Directions[i/int(fileHead.FramesPerDir)]
direction.Frames = append(direction.Frames, frame)
} }
return sprite, nil return sprite, nil
} }
func extractFrame(reader io.ReadSeeker, writer io.WriteSeeker, header FrameHeader) error { func extractFrame(reader io.ReadSeeker, writer io.WriteSeeker, header frameHeader) error {
var ( var (
x uint32 x uint32
y = header.Height - 1 y = header.Height - 1