lazarus/tools/viewer/viewer.go

183 lines
3.6 KiB
Go
Raw Normal View History

2018-12-17 18:16:52 -08:00
package main
2018-12-21 19:44:19 -08:00
import (
"flag"
"fmt"
2018-12-21 19:44:19 -08:00
"os"
"path/filepath"
2018-12-31 13:40:02 -08:00
"time"
2018-12-17 18:16:52 -08:00
2018-12-21 19:44:19 -08:00
"github.com/FooSoft/lazarus/formats/dat"
"github.com/FooSoft/lazarus/formats/dc6"
"github.com/FooSoft/lazarus/graphics"
2018-12-31 10:54:33 -08:00
"github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/platform"
"github.com/FooSoft/lazarus/platform/imgui"
2018-12-21 19:44:19 -08:00
)
2019-01-08 18:45:19 -08:00
func loadPalette(path string) (*dat.Palette, error) {
2018-12-21 19:44:19 -08:00
fp, err := os.Open(path)
if err != nil {
return nil, err
}
defer fp.Close()
return dat.NewFromReader(fp)
}
2019-01-08 18:45:19 -08:00
func loadAnimation(path string) (*dc6.Animation, error) {
2018-12-21 19:44:19 -08:00
fp, err := os.Open(path)
if err != nil {
return nil, err
}
defer fp.Close()
return dc6.NewFromReader(fp)
}
type scene struct {
2019-01-08 18:45:19 -08:00
animation *dc6.Animation
palette *dat.Palette
2019-01-01 10:30:35 -08:00
texture graphics.Texture
2018-12-31 17:30:59 -08:00
directionIndex int
frameIndex int
}
2018-12-17 18:16:52 -08:00
2018-12-31 17:10:45 -08:00
func (s *scene) Name() string {
2018-12-31 20:44:30 -08:00
return "viewer"
2018-12-31 17:10:45 -08:00
}
func (s *scene) Destroy() error {
2019-01-03 19:05:19 -08:00
if s.texture != nil {
return s.texture.Destroy()
}
return nil
}
2018-12-21 19:44:19 -08:00
func (s *scene) Advance() error {
var (
directionIndex = s.directionIndex
frameIndex = s.frameIndex
)
2018-12-30 16:48:11 -08:00
if s.texture == nil {
if err := s.updateTexture(); err != nil {
2018-12-30 16:48:11 -08:00
return err
}
}
2019-01-09 19:23:26 -08:00
imgui.SetNextWindowSize(math.Vec2i{X: 300, Y: 400})
2019-01-06 17:11:22 -08:00
imgui.Begin("DC6 Viewer")
imgui.Image(s.texture)
2019-01-01 10:30:35 -08:00
direction := s.animation.Directions[directionIndex]
if imgui.SliderInt("Direction", &directionIndex, 0, len(s.animation.Directions)-1) {
frameIndex = 0
}
frame := direction.Frames[frameIndex]
2018-12-31 17:30:59 -08:00
imgui.SliderInt("Frame", &frameIndex, 0, len(direction.Frames)-1)
2019-01-07 19:20:11 -08:00
imgui.Columns(2)
imgui.Text("Size")
imgui.NextColumn()
imgui.Text("%+v", frame.Size)
2019-01-07 19:20:11 -08:00
imgui.NextColumn()
imgui.Text("Offset")
imgui.NextColumn()
imgui.Text("%+v", frame.Offset)
2019-01-06 17:11:22 -08:00
imgui.End()
2018-12-31 16:52:53 -08:00
2018-12-30 16:48:11 -08:00
if directionIndex != s.directionIndex || frameIndex != s.frameIndex {
s.directionIndex = directionIndex
s.frameIndex = frameIndex
s.updateTexture()
2018-12-30 16:48:11 -08:00
}
return nil
}
func (s *scene) updateTexture() error {
2019-01-01 10:30:35 -08:00
frame := s.animation.Directions[s.directionIndex].Frames[s.frameIndex]
2018-12-31 10:54:33 -08:00
colors := make([]math.Color3b, frame.Size.X*frame.Size.Y)
2018-12-31 09:59:58 -08: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 16:48:11 -08:00
}
2018-12-30 16:48:11 -08:00
if s.texture != nil {
if err := s.texture.Destroy(); err != nil {
return err
}
}
2018-12-29 17:04:37 -08:00
2018-12-30 16:48:11 -08:00
var err error
s.texture, err = platform.NewTextureFromRgb(colors, frame.Size)
2018-12-30 16:48:11 -08:00
if err != nil {
return err
}
return nil
}
2018-12-21 19:44:19 -08:00
func main() {
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-08 19:22:47 -08:00
if err := platform.Initialize(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
2019-01-08 19:21:30 -08:00
defer platform.Shutdown()
2019-01-01 10:30:35 -08:00
animation, err := loadAnimation(flag.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
2019-01-08 18:45:19 -08:00
var palette *dat.Palette
if len(*palettePath) > 0 {
palette, err = loadPalette(*palettePath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
} else {
palette = dat.NewFromGrayscale()
}
2019-01-01 10:30:35 -08:00
scene := &scene{animation: animation, palette: palette}
if err := platform.WindowCreate("Viewer", math.Vec2i{X: 1024, Y: 768}, scene); err != nil {
2018-12-31 13:40:02 -08:00
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
2018-12-17 18:16:52 -08:00
}
defer platform.WindowDestroy()
2018-12-17 18:16:52 -08:00
2018-12-31 13:40:02 -08: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-17 18:16:52 -08:00
}
}