2018-03-24 21:47:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2019-12-29 15:38:49 +00:00
|
|
|
"image"
|
2018-03-24 21:47:34 +00:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-12-29 15:38:49 +00:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2018-03-24 21:47:34 +00:00
|
|
|
|
2019-12-31 01:55:58 +00:00
|
|
|
"code.ur.gs/lupine/ordoor/internal/conv"
|
|
|
|
"code.ur.gs/lupine/ordoor/internal/data"
|
|
|
|
"code.ur.gs/lupine/ordoor/internal/ui"
|
2018-03-24 21:47:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gamePath = flag.String("game-path", "./orig", "Path to a WH40K: Chaos Gate installation")
|
|
|
|
objFile = flag.String("obj", "", "Path to a .obj file, e.g. ./orig/Obj/TZEENTCH.OBJ")
|
|
|
|
)
|
|
|
|
|
|
|
|
type env struct {
|
2019-12-29 17:30:21 +00:00
|
|
|
obj *conv.Object
|
|
|
|
step int
|
|
|
|
|
|
|
|
state state
|
|
|
|
lastState state
|
2018-03-24 21:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type state struct {
|
|
|
|
spriteIdx int
|
|
|
|
|
2019-12-29 15:38:49 +00:00
|
|
|
zoom float64
|
|
|
|
origin image.Point
|
2018-03-24 21:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *gamePath == "" || *objFile == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawObj, err := data.LoadObject(*objFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load %s: %v", *objFile, err)
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:38:49 +00:00
|
|
|
obj, err := conv.ConvertObject(rawObj, filepath.Base(*objFile))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to convert %s: %v", *objFile, err)
|
|
|
|
}
|
2018-03-24 21:47:34 +00:00
|
|
|
|
2019-12-29 17:30:21 +00:00
|
|
|
state := state{
|
|
|
|
zoom: 6.0,
|
|
|
|
origin: image.Point{0, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
env := &env{
|
|
|
|
obj: obj,
|
|
|
|
state: state,
|
|
|
|
lastState: state,
|
2019-12-29 15:38:49 +00:00
|
|
|
}
|
2018-03-24 21:47:34 +00:00
|
|
|
|
|
|
|
win, err := ui.NewWindow("View Object: " + *objFile)
|
|
|
|
if err != nil {
|
2019-12-29 15:38:49 +00:00
|
|
|
log.Fatal(err)
|
2018-03-24 21:47:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 17:30:21 +00:00
|
|
|
win.OnKeyUp(ebiten.KeyMinus, env.changeSprite(-1))
|
|
|
|
win.OnKeyUp(ebiten.KeyEqual, env.changeSprite(+1))
|
|
|
|
win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(+4, +0))
|
|
|
|
win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(-4, +0))
|
|
|
|
win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, +4))
|
|
|
|
win.OnKeyUp(ebiten.KeyDown, env.changeOrigin(+0, -4))
|
|
|
|
win.OnMouseWheel(env.changeZoom)
|
|
|
|
|
2019-12-29 15:38:49 +00:00
|
|
|
// The main thread now belongs to ebiten
|
|
|
|
if err := win.Run(env.Update, env.Draw); err != nil {
|
|
|
|
log.Fatal(err)
|
2018-03-24 21:47:34 +00:00
|
|
|
}
|
2019-12-29 15:38:49 +00:00
|
|
|
}
|
2018-03-24 21:47:34 +00:00
|
|
|
|
2019-12-29 15:38:49 +00:00
|
|
|
func (e *env) Update() error {
|
2019-12-29 17:30:21 +00:00
|
|
|
if e.step == 0 || e.lastState != e.state {
|
2019-12-29 15:38:49 +00:00
|
|
|
log.Printf(
|
|
|
|
"new state: numSprites=%d sprite=%d zoom=%.2f, origin=%+v",
|
|
|
|
len(e.obj.Sprites),
|
2019-12-29 17:30:21 +00:00
|
|
|
e.state.spriteIdx,
|
|
|
|
e.state.zoom,
|
|
|
|
e.state.origin,
|
2019-12-29 15:38:49 +00:00
|
|
|
)
|
|
|
|
}
|
2018-03-24 21:47:34 +00:00
|
|
|
|
2019-12-29 17:30:21 +00:00
|
|
|
// This should be the final action
|
|
|
|
e.step += 1
|
|
|
|
e.lastState = e.state
|
2019-12-29 15:38:49 +00:00
|
|
|
|
|
|
|
return nil
|
2018-03-24 21:47:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 15:38:49 +00:00
|
|
|
func (e *env) Draw(screen *ebiten.Image) error {
|
|
|
|
sprite := e.obj.Sprites[e.state.spriteIdx]
|
2018-03-24 21:47:34 +00:00
|
|
|
|
2019-12-29 15:38:49 +00:00
|
|
|
cam := ebiten.GeoM{}
|
|
|
|
cam.Translate(float64(e.state.origin.X), float64(e.state.origin.Y)) // Move to origin
|
|
|
|
cam.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
|
2018-03-24 21:47:34 +00:00
|
|
|
|
2019-12-29 15:38:49 +00:00
|
|
|
return screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: cam})
|
2018-03-24 21:47:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 17:30:21 +00:00
|
|
|
func (e *env) changeSprite(by int) func() {
|
|
|
|
return func() {
|
|
|
|
e.state.spriteIdx += by
|
2018-03-24 21:47:34 +00:00
|
|
|
|
2019-12-29 17:30:21 +00:00
|
|
|
if e.state.spriteIdx < 0 {
|
|
|
|
e.state.spriteIdx = 0
|
2018-03-24 21:47:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 17:30:21 +00:00
|
|
|
if e.state.spriteIdx > len(e.obj.Sprites)-1 {
|
|
|
|
e.state.spriteIdx = len(e.obj.Sprites) - 1
|
|
|
|
}
|
2019-12-29 15:38:49 +00:00
|
|
|
}
|
2019-12-29 17:30:21 +00:00
|
|
|
}
|
2019-12-29 15:38:49 +00:00
|
|
|
|
2019-12-29 17:30:21 +00:00
|
|
|
func (e *env) changeOrigin(byX, byY int) func() {
|
|
|
|
return func() {
|
|
|
|
e.state.origin.X += byX
|
|
|
|
e.state.origin.Y += byY
|
2019-12-29 15:38:49 +00:00
|
|
|
}
|
2019-12-29 17:30:21 +00:00
|
|
|
}
|
2019-12-29 15:38:49 +00:00
|
|
|
|
2019-12-29 17:30:21 +00:00
|
|
|
func (e *env) changeZoom(_, y float64) {
|
2018-03-24 21:47:34 +00:00
|
|
|
// Zoom in and out with the mouse wheel
|
2019-12-29 17:30:21 +00:00
|
|
|
e.state.zoom *= math.Pow(1.2, y)
|
2018-03-24 21:47:34 +00:00
|
|
|
}
|