2017-10-27 01:03:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-18 01:21:15 +00:00
|
|
|
"errors"
|
2019-04-10 02:17:56 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-06-18 01:21:15 +00:00
|
|
|
"regexp"
|
2017-10-27 02:26:54 +00:00
|
|
|
"runtime"
|
2022-04-17 00:50:40 +00:00
|
|
|
"runtime/pprof"
|
2019-04-10 02:17:56 +00:00
|
|
|
"sort"
|
2020-06-18 01:21:15 +00:00
|
|
|
"strconv"
|
2019-04-10 02:17:56 +00:00
|
|
|
"time"
|
2019-04-10 00:35:44 +00:00
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/assets"
|
|
|
|
doodle "git.kirsle.net/SketchyMaze/doodle/pkg"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/branding"
|
2024-04-19 03:23:07 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/branding/builds"
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/chatbot"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/gamepad"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
2022-09-25 00:45:54 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/native"
|
2024-04-19 05:12:56 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/plus/bootstrap"
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/shmem"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/sound"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/sprites"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/usercfg"
|
2021-10-11 22:57:33 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2019-12-31 02:13:28 +00:00
|
|
|
"git.kirsle.net/go/render/sdl"
|
2020-11-15 23:20:15 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-02-20 02:25:36 +00:00
|
|
|
sdl2 "github.com/veandco/go-sdl2/sdl"
|
2019-04-10 02:17:56 +00:00
|
|
|
|
|
|
|
_ "image/png"
|
2017-10-27 01:03:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Build number is the git commit hash.
|
|
|
|
var (
|
2019-04-10 02:17:56 +00:00
|
|
|
Build = "<dynamic>"
|
|
|
|
BuildDate string
|
2017-10-27 01:03:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-04-10 02:17:56 +00:00
|
|
|
if BuildDate == "" {
|
|
|
|
BuildDate = time.Now().Format(time.RFC3339)
|
|
|
|
}
|
2019-05-07 00:06:40 +00:00
|
|
|
|
|
|
|
// Use all the CPU cores for collision detection and other load balanced
|
|
|
|
// goroutine work in the app.
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2017-10-27 01:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-10-27 02:26:54 +00:00
|
|
|
runtime.LockOSThread()
|
2017-10-27 01:03:11 +00:00
|
|
|
|
2024-04-19 05:12:56 +00:00
|
|
|
bootstrap.InitPlugins()
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "doodle"
|
2019-06-24 00:52:48 +00:00
|
|
|
app.Usage = fmt.Sprintf("%s - %s", branding.AppName, branding.Summary)
|
2019-04-20 00:23:37 +00:00
|
|
|
|
2021-06-20 05:14:41 +00:00
|
|
|
// Load user settings from disk ASAP.
|
|
|
|
if err := usercfg.Load(); err != nil {
|
|
|
|
log.Error("Error loading user settings (defaults will be used): %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:57:33 +00:00
|
|
|
// Set default user settings.
|
|
|
|
if usercfg.Current.CrosshairColor == render.Invisible {
|
|
|
|
usercfg.Current.CrosshairColor = balance.DefaultCrosshairColor
|
|
|
|
}
|
|
|
|
|
2022-02-20 02:25:36 +00:00
|
|
|
// Set GameController style.
|
|
|
|
gamepad.SetStyle(gamepad.Style(usercfg.Current.ControllerStyle))
|
|
|
|
|
2024-04-19 03:23:07 +00:00
|
|
|
app.Version = fmt.Sprintf("%s build %s. Built on %s",
|
|
|
|
builds.Version,
|
2019-04-10 02:17:56 +00:00
|
|
|
Build,
|
|
|
|
BuildDate,
|
|
|
|
)
|
|
|
|
|
|
|
|
app.Flags = []cli.Flag{
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "enable debug level logging",
|
2019-04-10 02:17:56 +00:00
|
|
|
},
|
2022-04-17 00:50:40 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "pprof",
|
|
|
|
Usage: "record pprof metrics to a filename",
|
|
|
|
},
|
2020-12-29 02:28:18 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "chdir",
|
|
|
|
Usage: "working directory for the game's runtime package",
|
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "edit",
|
|
|
|
Aliases: []string{"e"},
|
|
|
|
Usage: "edit the map given on the command line (instead of play it)",
|
2019-04-10 02:17:56 +00:00
|
|
|
},
|
2020-06-18 01:21:15 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "window",
|
|
|
|
Aliases: []string{"w"},
|
2020-07-10 02:38:37 +00:00
|
|
|
Usage: "set the window size (e.g. -w 1024x768) or special value: desktop, mobile, landscape, maximized",
|
2020-06-18 01:21:15 +00:00
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.BoolFlag{
|
2019-04-10 02:17:56 +00:00
|
|
|
Name: "guitest",
|
|
|
|
Usage: "enter the GUI Test scene on startup",
|
|
|
|
},
|
2020-11-20 04:08:38 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "experimental",
|
|
|
|
Usage: "enable experimental Feature Flags",
|
|
|
|
},
|
2021-04-01 02:16:33 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "offline",
|
|
|
|
Usage: "offline mode, disables check for new updates",
|
|
|
|
},
|
2018-06-17 17:29:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
app.Action = func(c *cli.Context) error {
|
2024-04-19 03:23:07 +00:00
|
|
|
log.Info("Starting %s %s", app.Name, app.Version)
|
|
|
|
|
2020-12-29 02:28:18 +00:00
|
|
|
// --chdir into a different working directory? e.g. for Flatpak especially.
|
|
|
|
if doodlePath := c.String("chdir"); doodlePath != "" {
|
|
|
|
if err := os.Chdir(doodlePath); err != nil {
|
|
|
|
log.Error("--chdir: couldn't enter '%s': %s", doodlePath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 00:50:40 +00:00
|
|
|
// Recording pprof stats?
|
|
|
|
if cpufile := c.String("pprof"); cpufile != "" {
|
|
|
|
log.Info("Saving CPU profiling data to %s", cpufile)
|
|
|
|
fh, err := os.Create(cpufile)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("--pprof: can't create file: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
|
|
|
if err := pprof.StartCPUProfile(fh); err != nil {
|
|
|
|
log.Error("pprof: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
var filename string
|
|
|
|
if c.NArg() > 0 {
|
|
|
|
filename = c.Args().Get(0)
|
|
|
|
}
|
|
|
|
|
2020-06-18 01:21:15 +00:00
|
|
|
// Setting a custom resolution?
|
2023-12-09 22:59:31 +00:00
|
|
|
var maximize = true
|
2020-06-18 01:21:15 +00:00
|
|
|
if c.String("window") != "" {
|
|
|
|
if err := setResolution(c.String("window")); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-12-09 22:59:31 +00:00
|
|
|
maximize = false
|
2020-06-18 01:21:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 04:08:38 +00:00
|
|
|
// Enable feature flags?
|
2021-09-12 04:18:22 +00:00
|
|
|
if c.Bool("experimental") || usercfg.Current.EnableFeatures {
|
2020-11-20 04:08:38 +00:00
|
|
|
balance.FeaturesOn()
|
|
|
|
}
|
|
|
|
|
2021-04-01 02:16:33 +00:00
|
|
|
// Offline mode?
|
|
|
|
if c.Bool("offline") {
|
|
|
|
shmem.OfflineMode = true
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
// SDL engine.
|
|
|
|
engine := sdl.New(
|
2019-06-24 00:52:48 +00:00
|
|
|
fmt.Sprintf("%s v%s", branding.AppName, branding.Version),
|
2019-04-10 02:17:56 +00:00
|
|
|
balance.Width,
|
|
|
|
balance.Height,
|
|
|
|
)
|
2018-07-22 00:12:22 +00:00
|
|
|
|
2022-02-20 02:25:36 +00:00
|
|
|
// Activate game controller event support.
|
|
|
|
sdl2.GameControllerEventState(1)
|
|
|
|
|
2019-06-28 05:54:46 +00:00
|
|
|
// Load the SDL fonts in from bindata storage.
|
2021-07-14 01:02:57 +00:00
|
|
|
if fonts, err := assets.AssetDir("assets/fonts"); err == nil {
|
2019-06-28 05:54:46 +00:00
|
|
|
for _, file := range fonts {
|
2021-07-14 01:02:57 +00:00
|
|
|
data, err := assets.Asset("assets/fonts/" + file)
|
2019-06-28 05:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sdl.InstallFont(file, data)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-05-23 03:07:48 +00:00
|
|
|
// Preload all sound effects.
|
|
|
|
sound.PreloadAll()
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
game := doodle.New(c.Bool("debug"), engine)
|
|
|
|
game.SetupEngine()
|
2021-12-31 00:31:45 +00:00
|
|
|
|
2023-12-09 22:59:31 +00:00
|
|
|
// Start with maximized window unless -w was given.
|
|
|
|
if maximize {
|
|
|
|
log.Info("Maximize window")
|
|
|
|
engine.Maximize()
|
|
|
|
}
|
|
|
|
|
2022-09-25 02:05:42 +00:00
|
|
|
// Reload usercfg - if their settings.json doesn't exist, we try and pick a
|
|
|
|
// default "hide touch hints" based on touch device presence - which is only
|
|
|
|
// known after SetupEngine.
|
|
|
|
usercfg.Load()
|
|
|
|
|
2022-09-25 00:45:54 +00:00
|
|
|
// Hide the mouse cursor over the window, we draw our own sprite image for it.
|
|
|
|
if !native.HasTouchscreen(engine) {
|
|
|
|
engine.ShowCursor(false)
|
|
|
|
}
|
2022-05-05 05:38:26 +00:00
|
|
|
|
2021-12-31 00:31:45 +00:00
|
|
|
// Set the app window icon.
|
|
|
|
if engine, ok := game.Engine.(*sdl.Renderer); ok {
|
|
|
|
if icon, err := sprites.LoadImage(game.Engine, balance.WindowIcon); err == nil {
|
|
|
|
engine.SetWindowIcon(icon.Image)
|
|
|
|
} else {
|
|
|
|
log.Error("Couldn't load WindowIcon (%s): %s", balance.WindowIcon, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
if c.Bool("guitest") {
|
|
|
|
game.Goto(&doodle.GUITestScene{})
|
|
|
|
} else if filename != "" {
|
|
|
|
if c.Bool("edit") {
|
|
|
|
game.EditFile(filename)
|
|
|
|
} else {
|
|
|
|
game.PlayLevel(filename)
|
|
|
|
}
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
2020-07-10 02:38:37 +00:00
|
|
|
|
|
|
|
// Maximizing the window? with `-w maximized`
|
|
|
|
if c.String("window") == "maximized" {
|
|
|
|
log.Info("Maximize main window")
|
|
|
|
engine.Maximize()
|
|
|
|
}
|
|
|
|
|
2020-12-29 02:28:18 +00:00
|
|
|
// Log what Doodle thinks its working directory is, for debugging.
|
|
|
|
pwd, _ := os.Getwd()
|
|
|
|
log.Debug("PWD: %s", pwd)
|
|
|
|
|
2022-01-09 03:21:08 +00:00
|
|
|
// Initialize the developer shell chatbot easter egg.
|
|
|
|
chatbot.Setup()
|
|
|
|
|
2022-09-25 00:45:54 +00:00
|
|
|
// Log some basic environment details.
|
|
|
|
w, h := engine.WindowSize()
|
|
|
|
log.Info("Has touchscreen? %+v Window size: %dx%d", native.HasTouchscreen(engine), w, h)
|
|
|
|
|
2019-04-10 02:17:56 +00:00
|
|
|
game.Run()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(cli.FlagsByName(app.Flags))
|
|
|
|
sort.Sort(cli.CommandsByName(app.Commands))
|
|
|
|
|
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
2020-07-10 02:38:37 +00:00
|
|
|
log.Error(err.Error())
|
2018-06-17 17:29:57 +00:00
|
|
|
}
|
2017-10-27 01:03:11 +00:00
|
|
|
}
|
2020-06-18 01:21:15 +00:00
|
|
|
|
|
|
|
func setResolution(value string) error {
|
|
|
|
switch value {
|
2020-07-10 02:38:37 +00:00
|
|
|
case "desktop", "maximized":
|
2020-06-18 01:21:15 +00:00
|
|
|
return nil
|
|
|
|
case "mobile":
|
|
|
|
balance.Width = 375
|
|
|
|
balance.Height = 812
|
2021-06-20 05:14:41 +00:00
|
|
|
if !usercfg.Current.Initialized {
|
|
|
|
usercfg.Current.HorizontalToolbars = true
|
|
|
|
}
|
2020-06-18 01:21:15 +00:00
|
|
|
case "landscape":
|
|
|
|
balance.Width = 812
|
|
|
|
balance.Height = 375
|
|
|
|
default:
|
|
|
|
var re = regexp.MustCompile(`^(\d+?)x(\d+?)$`)
|
|
|
|
m := re.FindStringSubmatch(value)
|
|
|
|
if len(m) == 0 {
|
|
|
|
return errors.New("--window: must be of the form WIDTHxHEIGHT, i.e. " +
|
|
|
|
"1024x768, or special keywords desktop, mobile, or landscape.")
|
|
|
|
}
|
|
|
|
|
|
|
|
w, _ := strconv.Atoi(m[1])
|
|
|
|
h, _ := strconv.Atoi(m[2])
|
|
|
|
balance.Width = w
|
|
|
|
balance.Height = h
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|