lazarus/tools/viewer/main.go

170 lines
3.4 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"
2018-12-22 03:44:19 +00:00
)
func loadPalette(path string) (*dat.Palette, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
}
defer fp.Close()
return dat.NewFromReader(fp)
}
func loadSprite(path string) (*dc6.Sprite, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
}
defer fp.Close()
return dc6.NewFromReader(fp)
}
type scene struct {
sprite *dc6.Sprite
palette *dat.Palette
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 {
return "Viewer"
}
2019-01-01 00:52:53 +00:00
func (s *scene) Destroy(window *platform.Window) error {
return s.texture.Destroy()
}
2018-12-22 03:44:19 +00:00
func (s *scene) Advance(window *platform.Window) error {
var (
directionIndex = s.directionIndex
frameIndex = s.frameIndex
)
2018-12-31 00:48:11 +00:00
if s.texture == nil {
if err := s.updateTexture(window); err != nil {
return err
}
}
2019-01-01 01:30:59 +00:00
imgui := window.Imgui()
imgui.DialogBegin("DC6 Viewer")
imgui.Image(s.texture)
direction := s.sprite.Directions[directionIndex]
2019-01-01 01:30:59 +00:00
if imgui.SliderInt("Direction", &directionIndex, 0, len(s.sprite.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)
2018-12-31 17:59:58 +00:00
imgui.Text(fmt.Sprintf("Size: %+v", frame.Size))
imgui.Text(fmt.Sprintf("Offset: %+v", frame.Offset))
2019-01-01 00:52:53 +00:00
if imgui.Button("Exit") {
window.SetScene(nil)
}
2019-01-01 01:30:59 +00:00
imgui.DialogEnd()
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(window)
}
return nil
}
func (s *scene) updateTexture(window *platform.Window) error {
frame := s.sprite.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
2018-12-31 18:54:33 +00:00
s.texture, err = window.CreateTextureRgb(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)
}
sprite, err := loadSprite(flag.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
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()
}
scene := &scene{sprite: sprite, palette: palette}
2019-01-01 01:10:45 +00:00
window, err := platform.NewWindow("viewer", math.Vec2i{X: 1024, Y: 768}, scene)
2018-12-18 02:16:52 +00:00
if 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 window.Destroy()
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
}
}