Noah Petherbridge
0a8bce708e
Improvements to the Zoom feature: * Actor position and size within your level scales up and down appropriately. The canvas size of the actor is scaled and its canvas is told the Zoom number of the parent so it will render its own graphic scaled correctly too. Other features: * "Experimental" tab added to the Settings window as a UI version of the --experimental CLI option. The option saves persistently to disk. * The "Replace Palette" experimental feature now works better. Debating whether it's a useful feature to even have.
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package level
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/filesystem"
|
|
"github.com/vmihailenco/msgpack"
|
|
)
|
|
|
|
// ToBinary serializes the level to binary format.
|
|
func (m *Level) ToBinary() ([]byte, error) {
|
|
header := filesystem.MakeHeader(filesystem.BinLevelType)
|
|
out := bytes.NewBuffer(header)
|
|
encoder := msgpack.NewEncoder(out)
|
|
err := encoder.Encode(m)
|
|
return out.Bytes(), err
|
|
}
|
|
|
|
// WriteBinary writes a level to binary format on disk.
|
|
func (m *Level) WriteBinary(filename string) error {
|
|
bin, err := m.ToBinary()
|
|
if err != nil {
|
|
return fmt.Errorf("Level.WriteBinary: encode error: %s", err)
|
|
}
|
|
|
|
err = ioutil.WriteFile(filename, bin, 0755)
|
|
if err != nil {
|
|
return fmt.Errorf("Level.WriteBinary: WriteFile error: %s", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// LoadBinary loads a map from binary file on disk.
|
|
func LoadBinary(filename string) (*Level, error) {
|
|
fh, err := os.Open(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer fh.Close()
|
|
|
|
// Read and verify the file header from the binary format.
|
|
err = filesystem.ReadHeader(filesystem.BinLevelType, fh)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Decode the file from disk.
|
|
m := New()
|
|
decoder := msgpack.NewDecoder(fh)
|
|
err = decoder.Decode(&m)
|
|
if err != nil {
|
|
return m, fmt.Errorf("level.LoadBinary: decode error: %s", err)
|
|
}
|
|
|
|
// Fill in defaults.
|
|
if m.Wallpaper == "" {
|
|
m.Wallpaper = DefaultWallpaper
|
|
}
|
|
|
|
// Inflate the chunk metadata to map the pixels to their palette indexes.
|
|
m.Inflate()
|
|
return m, err
|
|
}
|