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
|
|
|
"image/color"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-12-30 02:10:26 +00:00
|
|
|
"path/filepath"
|
2018-12-18 02:16:52 +00:00
|
|
|
|
2018-12-30 01:04:37 +00:00
|
|
|
imgui "github.com/FooSoft/imgui-go"
|
2018-12-22 03:44:19 +00:00
|
|
|
"github.com/FooSoft/lazarus/formats/dat"
|
|
|
|
"github.com/FooSoft/lazarus/formats/dc6"
|
2018-12-29 19:52:30 +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)
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
type scene struct {
|
2018-12-30 02:10:26 +00:00
|
|
|
sprite *dc6.Sprite
|
|
|
|
palette *dat.Palette
|
2018-12-29 19:52:30 +00:00
|
|
|
texture *platform.Texture
|
2018-12-30 02:10:26 +00:00
|
|
|
|
|
|
|
directionIndex int32
|
|
|
|
frameIndex int32
|
2018-12-29 19:52:30 +00:00
|
|
|
}
|
2018-12-18 02:16:52 +00:00
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func (s *scene) Init(window *platform.Window) error {
|
2018-12-30 02:10:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scene) Shutdown(window *platform.Window) error {
|
|
|
|
if s.texture == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.texture.Destroy()
|
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 (s *scene) Advance(window *platform.Window) error {
|
2018-12-30 02:10:26 +00:00
|
|
|
var (
|
|
|
|
directionIndex = s.directionIndex
|
|
|
|
frameIndex = s.frameIndex
|
|
|
|
)
|
|
|
|
|
|
|
|
direction := s.sprite.Directions[directionIndex]
|
|
|
|
if imgui.SliderInt("Direction", &directionIndex, 0, int32(len(s.sprite.Directions))-1) {
|
|
|
|
frameIndex = 0
|
|
|
|
}
|
|
|
|
frame := direction.Frames[frameIndex]
|
|
|
|
imgui.SliderInt("Frame", &frameIndex, 0, int32(len(direction.Frames))-1)
|
|
|
|
|
2018-12-30 02:21:33 +00:00
|
|
|
imgui.Text(fmt.Sprintf("Height: %d", frame.Height))
|
|
|
|
imgui.Text(fmt.Sprintf("Width: %d", frame.Width))
|
|
|
|
imgui.Text(fmt.Sprintf("OffsetX: %d", frame.OffsetX))
|
|
|
|
imgui.Text(fmt.Sprintf("OffsetY: %d", frame.OffsetY))
|
|
|
|
|
2018-12-30 02:10:26 +00:00
|
|
|
if s.texture == nil || directionIndex != s.directionIndex || frameIndex != s.frameIndex {
|
|
|
|
colors := make([]color.RGBA, frame.Width*frame.Height)
|
|
|
|
for y := 0; y < frame.Height; y++ {
|
|
|
|
for x := 0; x < frame.Width; x++ {
|
|
|
|
colors[y*frame.Width+x] = s.palette.Colors[frame.Data[y*frame.Width+x]]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s.texture, err = window.CreateTextureRgba(colors, frame.Width, frame.Height)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 01:04:37 +00:00
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
window.RenderTexture(
|
|
|
|
s.texture,
|
|
|
|
math.Rect4i{X: 0, Y: 0, W: 256, H: 256},
|
|
|
|
math.Rect4i{X: 0, Y: 0, W: 256, H: 256},
|
|
|
|
)
|
|
|
|
|
2018-12-30 02:10:26 +00:00
|
|
|
s.directionIndex = directionIndex
|
|
|
|
s.frameIndex = frameIndex
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
sprite, err := loadSprite(flag.Arg(0))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-12-30 02:21:33 +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
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
platform.Init()
|
|
|
|
defer platform.Shutdown()
|
|
|
|
|
2018-12-30 02:10:26 +00:00
|
|
|
scene := &scene{sprite: sprite, palette: palette}
|
|
|
|
window, err := platform.CreateWindow("Viewer", 1280, 720, scene)
|
2018-12-18 02:16:52 +00:00
|
|
|
if err != nil {
|
2018-12-22 03:44:19 +00:00
|
|
|
log.Fatal(err)
|
2018-12-18 02:16:52 +00:00
|
|
|
}
|
2018-12-29 19:52:30 +00:00
|
|
|
defer window.Destroy()
|
2018-12-18 02:16:52 +00:00
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
if err := platform.ProcessEvents(); err != nil {
|
|
|
|
log.Fatal(err)
|
2018-12-18 02:16:52 +00:00
|
|
|
}
|
|
|
|
}
|