lazarus/tools/viewer/viewer.go

179 lines
3.5 KiB
Go
Raw Normal View History

2018-12-18 02:16:52 +00:00
package main
2018-12-22 03:44:19 +00:00
import (
"flag"
"fmt"
2018-12-22 03:44:19 +00:00
"os"
"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"
"github.com/FooSoft/lazarus/graphics"
2018-12-31 18:54:33 +00:00
"github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/platform"
"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)
}
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
2019-01-01 01:30:59 +00:00
directionIndex int
frameIndex int
}
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
}
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-22 03:44:19 +00:00
func (s *scene) Advance() error {
var (
directionIndex = s.directionIndex
frameIndex = s.frameIndex
)
2018-12-31 00:48:11 +00:00
if s.texture == nil {
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")
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) {
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()
imgui.Text("%+v", frame.Size)
2019-01-08 03:20:11 +00:00
imgui.NextColumn()
imgui.Text("Offset")
imgui.NextColumn()
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") {
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
s.updateTexture()
2018-12-31 00:48:11 +00:00
}
return nil
}
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-31 00:48:11 +00:00
}
2018-12-31 00:48:11 +00:00
if s.texture != nil {
if err := s.texture.Destroy(); err != nil {
return err
}
}
2018-12-30 01:04:37 +00:00
2018-12-31 00:48:11 +00:00
var err error
s.texture, err = platform.NewTextureFromRgb(colors, frame.Size)
2018-12-31 00:48:11 +00:00
if err != nil {
return err
}
return nil
}
2018-12-22 03:44:19 +00: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-01 18:30:35 +00:00
animation, err := loadAnimation(flag.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
2019-01-09 02:45:19 +00: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 18:30:35 +00:00
scene := &scene{animation: animation, palette: palette}
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
}
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
}
}