2018-12-18 02:16:52 +00:00
|
|
|
package main
|
|
|
|
|
2018-12-22 03:44:19 +00:00
|
|
|
import (
|
2018-12-30 02:10:26 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-12-22 03:44:19 +00:00
|
|
|
"os"
|
2018-12-30 02:10:26 +00:00
|
|
|
"path/filepath"
|
2018-12-31 21:40:02 +00:00
|
|
|
"time"
|
2018-12-18 02:16:52 +00:00
|
|
|
|
2018-12-22 03:44:19 +00:00
|
|
|
"github.com/FooSoft/lazarus/formats/dat"
|
|
|
|
"github.com/FooSoft/lazarus/formats/dc6"
|
2019-01-01 01:42:00 +00:00
|
|
|
"github.com/FooSoft/lazarus/graphics"
|
2018-12-31 18:54:33 +00:00
|
|
|
"github.com/FooSoft/lazarus/math"
|
2018-12-29 19:52:30 +00:00
|
|
|
"github.com/FooSoft/lazarus/platform"
|
2019-01-07 01:00:19 +00:00
|
|
|
"github.com/FooSoft/lazarus/platform/imgui"
|
2018-12-22 03:44:19 +00:00
|
|
|
)
|
|
|
|
|
2019-01-09 02:45:19 +00:00
|
|
|
func loadPalette(path string) (*dat.Palette, error) {
|
2018-12-22 03:44:19 +00:00
|
|
|
fp, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
return dat.NewFromReader(fp)
|
|
|
|
}
|
|
|
|
|
2019-01-09 02:45:19 +00:00
|
|
|
func loadAnimation(path string) (*dc6.Animation, error) {
|
2018-12-22 03:44:19 +00:00
|
|
|
fp, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
return dc6.NewFromReader(fp)
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
type scene struct {
|
2019-01-09 02:45:19 +00:00
|
|
|
animation *dc6.Animation
|
|
|
|
palette *dat.Palette
|
2019-01-01 18:30:35 +00:00
|
|
|
texture graphics.Texture
|
2018-12-30 02:10:26 +00:00
|
|
|
|
2019-01-01 01:30:59 +00:00
|
|
|
directionIndex int
|
|
|
|
frameIndex int
|
2018-12-29 19:52:30 +00:00
|
|
|
}
|
2018-12-18 02:16:52 +00:00
|
|
|
|
2019-01-01 01:10:45 +00:00
|
|
|
func (s *scene) Name() string {
|
2019-01-01 04:44:30 +00:00
|
|
|
return "viewer"
|
2019-01-01 01:10:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 01:00:19 +00:00
|
|
|
func (s *scene) Destroy() error {
|
2019-01-04 03:05:19 +00:00
|
|
|
if s.texture != nil {
|
|
|
|
return s.texture.Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-12-29 19:52:30 +00:00
|
|
|
}
|
2018-12-22 03:44:19 +00:00
|
|
|
|
2019-01-07 01:00:19 +00:00
|
|
|
func (s *scene) Advance() error {
|
2018-12-30 02:10:26 +00:00
|
|
|
var (
|
|
|
|
directionIndex = s.directionIndex
|
|
|
|
frameIndex = s.frameIndex
|
|
|
|
)
|
|
|
|
|
2018-12-31 00:48:11 +00:00
|
|
|
if s.texture == nil {
|
2019-01-07 01:00:19 +00:00
|
|
|
if err := s.updateTexture(); err != nil {
|
2018-12-31 00:48:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 01:11:22 +00:00
|
|
|
imgui.Begin("DC6 Viewer")
|
2019-01-01 01:42:00 +00:00
|
|
|
imgui.Image(s.texture)
|
2019-01-01 18:30:35 +00:00
|
|
|
direction := s.animation.Directions[directionIndex]
|
|
|
|
if imgui.SliderInt("Direction", &directionIndex, 0, len(s.animation.Directions)-1) {
|
2018-12-30 02:10:26 +00:00
|
|
|
frameIndex = 0
|
|
|
|
}
|
|
|
|
frame := direction.Frames[frameIndex]
|
2019-01-01 01:30:59 +00:00
|
|
|
imgui.SliderInt("Frame", &frameIndex, 0, len(direction.Frames)-1)
|
2019-01-08 03:20:11 +00:00
|
|
|
imgui.Columns(2)
|
|
|
|
imgui.Text("Size")
|
|
|
|
imgui.NextColumn()
|
2019-01-08 03:25:48 +00:00
|
|
|
imgui.Text("%+v", frame.Size)
|
2019-01-08 03:20:11 +00:00
|
|
|
imgui.NextColumn()
|
|
|
|
imgui.Text("Offset")
|
|
|
|
imgui.NextColumn()
|
2019-01-08 03:25:48 +00:00
|
|
|
imgui.Text("%+v", frame.Offset)
|
2019-01-08 03:20:11 +00:00
|
|
|
imgui.Columns(1)
|
2019-01-01 00:52:53 +00:00
|
|
|
if imgui.Button("Exit") {
|
2019-01-07 01:00:19 +00:00
|
|
|
platform.WindowSetScene(nil)
|
2019-01-01 00:52:53 +00:00
|
|
|
}
|
2019-01-07 01:11:22 +00:00
|
|
|
imgui.End()
|
2019-01-01 00:52:53 +00:00
|
|
|
|
2018-12-31 00:48:11 +00:00
|
|
|
if directionIndex != s.directionIndex || frameIndex != s.frameIndex {
|
|
|
|
s.directionIndex = directionIndex
|
|
|
|
s.frameIndex = frameIndex
|
2019-01-07 01:00:19 +00:00
|
|
|
s.updateTexture()
|
2018-12-31 00:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 01:00:19 +00:00
|
|
|
func (s *scene) updateTexture() error {
|
2019-01-01 18:30:35 +00:00
|
|
|
frame := s.animation.Directions[s.directionIndex].Frames[s.frameIndex]
|
2018-12-31 18:54:33 +00:00
|
|
|
colors := make([]math.Color3b, frame.Size.X*frame.Size.Y)
|
2018-12-31 17:59:58 +00:00
|
|
|
for y := 0; y < frame.Size.Y; y++ {
|
|
|
|
for x := 0; x < frame.Size.X; x++ {
|
|
|
|
colors[y*frame.Size.X+x] = s.palette.Colors[frame.Data[y*frame.Size.X+x]]
|
2018-12-30 02:10:26 +00:00
|
|
|
}
|
2018-12-31 00:48:11 +00:00
|
|
|
}
|
2018-12-30 02:10:26 +00:00
|
|
|
|
2018-12-31 00:48:11 +00:00
|
|
|
if s.texture != nil {
|
|
|
|
if err := s.texture.Destroy(); err != nil {
|
2018-12-30 02:10:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 01:04:37 +00:00
|
|
|
|
2018-12-31 00:48:11 +00:00
|
|
|
var err error
|
2019-01-07 01:00:19 +00:00
|
|
|
s.texture, err = platform.NewTextureFromRgb(colors, frame.Size)
|
2018-12-31 00:48:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-29 19:52:30 +00:00
|
|
|
|
2018-12-30 02:10:26 +00:00
|
|
|
return nil
|
2018-12-29 19:52:30 +00:00
|
|
|
}
|
2018-12-22 03:44:19 +00:00
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func main() {
|
2018-12-30 02:10:26 +00:00
|
|
|
var (
|
|
|
|
palettePath = flag.String("palette", "", "path to palette file")
|
|
|
|
)
|
|
|
|
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] file\n", filepath.Base(os.Args[0]))
|
|
|
|
fmt.Fprintf(os.Stderr, "Parameters:\n\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if flag.NArg() < 1 {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2019-01-01 18:30:35 +00:00
|
|
|
animation, err := loadAnimation(flag.Arg(0))
|
2018-12-30 02:10:26 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-01-09 02:45:19 +00:00
|
|
|
var palette *dat.Palette
|
2018-12-30 02:10:26 +00:00
|
|
|
if len(*palettePath) > 0 {
|
|
|
|
palette, err = loadPalette(*palettePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-12-30 02:21:33 +00:00
|
|
|
} else {
|
|
|
|
palette = dat.NewFromGrayscale()
|
2018-12-30 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 18:30:35 +00:00
|
|
|
scene := &scene{animation: animation, palette: palette}
|
2019-01-07 01:00:19 +00:00
|
|
|
if err := platform.WindowCreate("Viewer", math.Vec2i{X: 1024, Y: 768}, scene); err != nil {
|
2018-12-31 21:40:02 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
2018-12-18 02:16:52 +00:00
|
|
|
}
|
2019-01-07 01:00:19 +00:00
|
|
|
defer platform.WindowDestroy()
|
2018-12-18 02:16:52 +00:00
|
|
|
|
2018-12-31 21:40:02 +00:00
|
|
|
for {
|
|
|
|
run, err := platform.Advance()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if !run {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(time.Millisecond * 25)
|
2018-12-18 02:16:52 +00:00
|
|
|
}
|
|
|
|
}
|