2018-03-18 20:41:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2019-12-29 20:41:41 +00:00
|
|
|
"image"
|
2018-03-18 20:41:17 +00:00
|
|
|
"log"
|
2018-03-21 05:08:24 +00:00
|
|
|
"math"
|
2018-03-18 20:41:17 +00:00
|
|
|
"os"
|
|
|
|
|
2020-11-21 19:27:09 +00:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2018-03-18 20:41:17 +00:00
|
|
|
|
2020-03-21 13:37:20 +00:00
|
|
|
"code.ur.gs/lupine/ordoor/internal/assetstore"
|
2020-06-01 01:08:53 +01:00
|
|
|
"code.ur.gs/lupine/ordoor/internal/config"
|
2019-12-31 01:55:58 +00:00
|
|
|
"code.ur.gs/lupine/ordoor/internal/ui"
|
2018-03-18 20:41:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-01 01:08:53 +01:00
|
|
|
configFile = flag.String("config", "config.toml", "Config file")
|
|
|
|
engine = flag.String("engine", "", "Override engine to use")
|
|
|
|
|
|
|
|
setName = flag.String("set", "", "Name of a set, e.g., map01")
|
2020-03-22 22:12:20 +00:00
|
|
|
|
|
|
|
winX = flag.Int("win-x", 1280, "Pre-scaled window X dimension")
|
|
|
|
winY = flag.Int("win-y", 1024, "Pre-scaled window Y dimension")
|
2018-03-18 20:41:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type env struct {
|
2020-03-21 13:37:20 +00:00
|
|
|
set *assetstore.Set
|
2019-12-29 20:41:41 +00:00
|
|
|
step int
|
|
|
|
state state
|
|
|
|
lastState state
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type state struct {
|
|
|
|
objIdx int
|
|
|
|
spriteIdx int
|
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
zoom float64
|
|
|
|
origin image.Point
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2020-06-01 01:08:53 +01:00
|
|
|
if *configFile == "" || *setName == "" {
|
2018-03-18 20:41:17 +00:00
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-06-01 01:08:53 +01:00
|
|
|
cfg, err := config.Load(*configFile, *engine)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assets, err := assetstore.New(cfg.DefaultEngine())
|
2018-03-18 20:41:17 +00:00
|
|
|
if err != nil {
|
2020-03-21 13:37:20 +00:00
|
|
|
log.Fatal(err)
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 13:37:20 +00:00
|
|
|
set, err := assets.Set(*setName)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't load set %s: %v", *setName, err)
|
2019-12-29 20:41:41 +00:00
|
|
|
}
|
2018-03-18 20:41:17 +00:00
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
state := state{zoom: 8.0}
|
|
|
|
env := &env{
|
2020-03-21 13:37:20 +00:00
|
|
|
set: set,
|
2019-12-29 20:41:41 +00:00
|
|
|
state: state,
|
|
|
|
lastState: state,
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 22:12:20 +00:00
|
|
|
win, err := ui.NewWindow(env, "View Set: "+*setName, *winX, *winY)
|
2020-03-22 02:58:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Couldn't create window: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
win.OnKeyUp(ebiten.KeyLeft, env.changeObjIdx(-1))
|
|
|
|
win.OnKeyUp(ebiten.KeyRight, env.changeObjIdx(+1))
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
win.OnKeyUp(ebiten.KeyUp, env.changeSpriteIdx(+1))
|
|
|
|
win.OnKeyUp(ebiten.KeyDown, env.changeSpriteIdx(-1))
|
2018-03-18 20:41:17 +00:00
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
win.OnMouseWheel(env.changeZoom)
|
2018-03-18 20:41:17 +00:00
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
// Main thread now belongs to ebiten
|
2020-03-22 02:58:52 +00:00
|
|
|
if err := win.Run(); err != nil {
|
2019-12-29 20:41:41 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 02:58:52 +00:00
|
|
|
func (e *env) Update(screenX, screenY int) error {
|
2020-03-21 13:37:20 +00:00
|
|
|
curObj, err := e.curObject()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
if e.step == 0 || e.lastState != e.state {
|
|
|
|
log.Printf(
|
2020-03-21 13:37:20 +00:00
|
|
|
"new state: object=%d/%d (%s) numFrames=%d sprite=%d zoom=%.2f",
|
2019-12-29 20:41:41 +00:00
|
|
|
e.state.objIdx,
|
2020-03-21 13:37:20 +00:00
|
|
|
e.set.NumObjects,
|
|
|
|
curObj.Name,
|
|
|
|
curObj.NumSprites,
|
2019-12-29 20:41:41 +00:00
|
|
|
e.state.spriteIdx,
|
|
|
|
e.state.zoom,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.step += 1
|
|
|
|
e.lastState = e.state
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-18 20:41:17 +00:00
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
func (e *env) Draw(screen *ebiten.Image) error {
|
2020-03-21 13:37:20 +00:00
|
|
|
sprite, err := e.curSprite()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-13 01:37:44 +01:00
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
cam := ebiten.GeoM{}
|
|
|
|
cam.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
|
2018-03-18 20:41:17 +00:00
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
// TODO: centre the image
|
2018-03-18 20:41:17 +00:00
|
|
|
|
2020-11-21 19:27:09 +00:00
|
|
|
screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: cam})
|
|
|
|
|
|
|
|
return nil
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
func (e *env) changeObjIdx(by int) func() {
|
|
|
|
return func() {
|
|
|
|
old := e.state.objIdx
|
|
|
|
e.state.objIdx += by
|
|
|
|
|
|
|
|
if e.state.objIdx < 0 {
|
|
|
|
e.state.objIdx = 0
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 13:37:20 +00:00
|
|
|
if e.state.objIdx > e.set.NumObjects-1 {
|
|
|
|
e.state.objIdx = e.set.NumObjects - 1
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
// reset sprite index when object changes
|
|
|
|
if old != e.state.objIdx {
|
|
|
|
e.state.spriteIdx = 0
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-29 20:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *env) changeSpriteIdx(by int) func() {
|
|
|
|
return func() {
|
2020-03-21 13:37:20 +00:00
|
|
|
obj, err := e.curObject()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Encountered %v trying to change sprite index", err)
|
|
|
|
return
|
|
|
|
}
|
2018-03-18 20:41:17 +00:00
|
|
|
|
2020-03-21 13:37:20 +00:00
|
|
|
e.state.spriteIdx += by
|
2019-12-29 20:41:41 +00:00
|
|
|
if e.state.spriteIdx < 0 {
|
|
|
|
e.state.spriteIdx = 0
|
|
|
|
}
|
|
|
|
|
2020-03-21 13:37:20 +00:00
|
|
|
if e.state.spriteIdx > obj.NumSprites-1 {
|
|
|
|
e.state.spriteIdx = obj.NumSprites - 1
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-29 20:41:41 +00:00
|
|
|
}
|
2018-03-21 05:08:24 +00:00
|
|
|
|
2019-12-29 20:41:41 +00:00
|
|
|
func (e *env) changeZoom(_, y float64) {
|
2018-03-21 05:08:24 +00:00
|
|
|
// Zoom in and out with the mouse wheel
|
2019-12-29 20:41:41 +00:00
|
|
|
e.state.zoom *= math.Pow(1.2, y)
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 13:37:20 +00:00
|
|
|
func (e *env) curObject() (*assetstore.Object, error) {
|
|
|
|
return e.set.Object(e.state.objIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *env) curSprite() (*assetstore.Sprite, error) {
|
|
|
|
obj, err := e.curObject()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj.Sprite(e.state.spriteIdx)
|
2018-03-18 20:41:17 +00:00
|
|
|
}
|