2018-12-14 04:03:47 +00:00
|
|
|
package dc6
|
2018-12-17 02:07:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2018-12-17 19:29:01 +00:00
|
|
|
|
2018-12-31 17:59:58 +00:00
|
|
|
"github.com/FooSoft/lazarus/math"
|
2018-12-17 19:29:01 +00:00
|
|
|
"github.com/FooSoft/lazarus/streaming"
|
2018-12-17 02:07:24 +00:00
|
|
|
)
|
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
type fileHeader struct {
|
2018-12-17 23:09:30 +00:00
|
|
|
Version uint32
|
|
|
|
_ uint32 // unused: flags
|
|
|
|
_ uint32 // unused: format
|
|
|
|
_ uint32 // unused: skipColor
|
|
|
|
DirCount uint32
|
|
|
|
FramesPerDir uint32
|
2018-12-17 02:07:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
type frameHeader struct {
|
2018-12-17 23:09:30 +00:00
|
|
|
Flip uint32
|
|
|
|
Width uint32
|
|
|
|
Height uint32
|
2018-12-31 01:23:43 +00:00
|
|
|
OffsetX int32
|
|
|
|
OffsetY int32
|
2018-12-17 23:09:30 +00:00
|
|
|
_ uint32 // unused: allocSize
|
|
|
|
_ uint32 // unused: nextBlock
|
|
|
|
Length uint32
|
2018-12-17 02:07:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
type Direction struct {
|
|
|
|
Frames []Frame
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:07:24 +00:00
|
|
|
type Frame struct {
|
2018-12-31 17:59:58 +00:00
|
|
|
Size math.Vec2i
|
|
|
|
Offset math.Vec2i
|
|
|
|
Data []byte
|
2018-12-17 02:07:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
type Sprite struct {
|
|
|
|
Directions []Direction
|
2018-12-17 02:07:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 20:28:16 +00:00
|
|
|
func NewFromReader(reader io.ReadSeeker) (*Sprite, error) {
|
2018-12-17 19:45:43 +00:00
|
|
|
sprite := new(Sprite)
|
2018-12-17 02:07:24 +00:00
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
var fileHead fileHeader
|
|
|
|
if err := binary.Read(reader, binary.LittleEndian, &fileHead); err != nil {
|
2018-12-17 02:07:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var frameOffsets []uint32
|
2018-12-17 23:09:30 +00:00
|
|
|
for i := uint32(0); i < fileHead.DirCount*fileHead.FramesPerDir; i++ {
|
2018-12-17 02:07:24 +00:00
|
|
|
var frameOffset uint32
|
|
|
|
if err := binary.Read(reader, binary.LittleEndian, &frameOffset); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
frameOffsets = append(frameOffsets, frameOffset)
|
|
|
|
}
|
|
|
|
|
2018-12-30 02:21:33 +00:00
|
|
|
sprite.Directions = make([]Direction, fileHead.DirCount)
|
2018-12-17 19:45:43 +00:00
|
|
|
|
2018-12-17 23:09:30 +00:00
|
|
|
for i, frameOffset := range frameOffsets {
|
2018-12-17 19:45:43 +00:00
|
|
|
var frameHead frameHeader
|
2018-12-17 23:09:30 +00:00
|
|
|
if _, err := reader.Seek(int64(frameOffset), io.SeekStart); err != nil {
|
2018-12-17 02:07:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
if err := binary.Read(reader, binary.LittleEndian, &frameHead); err != nil {
|
2018-12-17 02:07:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
data := make([]byte, frameHead.Width*frameHead.Height)
|
2018-12-17 19:29:01 +00:00
|
|
|
writer := streaming.NewWriter(data)
|
2018-12-17 19:45:43 +00:00
|
|
|
if err := extractFrame(reader, writer, frameHead); err != nil {
|
2018-12-17 03:30:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-31 17:59:58 +00:00
|
|
|
var (
|
|
|
|
size = math.Vec2i{X: int(frameHead.Width), Y: int(frameHead.Height)}
|
|
|
|
offset = math.Vec2i{X: int(frameHead.OffsetX), Y: int(frameHead.OffsetY)}
|
|
|
|
frame = Frame{size, offset, data}
|
|
|
|
direction = &sprite.Directions[i/int(fileHead.FramesPerDir)]
|
|
|
|
)
|
2018-12-17 02:07:24 +00:00
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
direction.Frames = append(direction.Frames, frame)
|
2018-12-17 02:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sprite, nil
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:43 +00:00
|
|
|
func extractFrame(reader io.ReadSeeker, writer io.WriteSeeker, header frameHeader) error {
|
2018-12-17 23:09:30 +00:00
|
|
|
var x, y uint32
|
|
|
|
for readOffset := uint32(0); readOffset < header.Length; readOffset++ {
|
|
|
|
var chunk byte
|
|
|
|
if err := binary.Read(reader, binary.LittleEndian, &chunk); err != nil {
|
2018-12-17 03:30:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:09:30 +00:00
|
|
|
if chunk&0x80 > 0 {
|
|
|
|
if skipLength := uint32(chunk & 0x7f); skipLength > 0 {
|
|
|
|
x += skipLength
|
|
|
|
} else {
|
|
|
|
x = 0
|
|
|
|
y++
|
|
|
|
}
|
|
|
|
|
2018-12-17 03:30:53 +00:00
|
|
|
} else {
|
2018-12-17 23:09:30 +00:00
|
|
|
writeOffset := int64(header.Width*(header.Height-y-1) + x)
|
|
|
|
if header.Flip != 0 {
|
|
|
|
writeOffset = int64(header.Width*y + x)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writer.Seek(writeOffset, io.SeekStart); err != nil {
|
2018-12-17 03:30:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-12-17 23:09:30 +00:00
|
|
|
if _, err := io.CopyN(writer, reader, int64(chunk)); err != nil {
|
2018-12-17 03:30:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:09:30 +00:00
|
|
|
readOffset += uint32(chunk)
|
|
|
|
x += uint32(chunk)
|
2018-12-17 03:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:07:24 +00:00
|
|
|
return nil
|
|
|
|
}
|