2017-10-27 01:03:11 +00:00
|
|
|
package doodle
|
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
import (
|
2018-09-26 17:04:46 +00:00
|
|
|
"fmt"
|
2019-07-02 22:24:46 +00:00
|
|
|
"path/filepath"
|
2018-09-26 17:04:46 +00:00
|
|
|
"strings"
|
2018-06-17 02:59:23 +00:00
|
|
|
"time"
|
2017-10-27 02:26:54 +00:00
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/events"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
2019-06-25 21:57:11 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/branding"
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/enum"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2019-06-27 18:57:26 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/shmem"
|
2018-06-17 02:59:23 +00:00
|
|
|
"github.com/kirsle/golog"
|
2017-10-27 02:26:54 +00:00
|
|
|
)
|
2017-10-27 01:03:11 +00:00
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
const (
|
|
|
|
// TargetFPS is the frame rate to cap the game to.
|
2018-06-17 14:56:51 +00:00
|
|
|
TargetFPS = 1000 / 60 // 60 FPS
|
2018-06-17 02:59:23 +00:00
|
|
|
|
|
|
|
// Millisecond64 is a time.Millisecond casted to float64.
|
|
|
|
Millisecond64 = float64(time.Millisecond)
|
|
|
|
)
|
2017-10-27 01:03:11 +00:00
|
|
|
|
|
|
|
// Doodle is the game object.
|
|
|
|
type Doodle struct {
|
2018-08-11 00:19:47 +00:00
|
|
|
Debug bool
|
|
|
|
Engine render.Engine
|
|
|
|
engineReady bool
|
2017-10-27 01:03:11 +00:00
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Easy access to the event state, for the debug overlay to use.
|
|
|
|
// Might not be thread safe.
|
|
|
|
event *events.State
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
startTime time.Time
|
|
|
|
running bool
|
2018-10-19 20:31:58 +00:00
|
|
|
width int
|
|
|
|
height int
|
2018-06-17 02:59:23 +00:00
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// Command line shell options.
|
|
|
|
shell Shell
|
|
|
|
|
2018-07-26 02:38:54 +00:00
|
|
|
Scene Scene
|
2017-10-27 01:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes the game object.
|
2018-07-22 00:12:22 +00:00
|
|
|
func New(debug bool, engine render.Engine) *Doodle {
|
2017-10-27 01:03:11 +00:00
|
|
|
d := &Doodle{
|
2018-06-17 02:59:23 +00:00
|
|
|
Debug: debug,
|
2018-07-22 00:12:22 +00:00
|
|
|
Engine: engine,
|
2018-06-17 02:59:23 +00:00
|
|
|
startTime: time.Now(),
|
|
|
|
running: true,
|
2018-10-19 20:31:58 +00:00
|
|
|
width: balance.Width,
|
|
|
|
height: balance.Height,
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
2018-07-22 03:43:01 +00:00
|
|
|
d.shell = NewShell(d)
|
2018-06-17 02:59:23 +00:00
|
|
|
|
2019-06-27 18:57:26 +00:00
|
|
|
// Make the render engine globally available. TODO: for wasm/ToBitmap
|
|
|
|
shmem.CurrentRenderEngine = engine
|
|
|
|
shmem.Flash = d.Flash
|
|
|
|
|
2019-04-16 06:07:15 +00:00
|
|
|
if debug {
|
|
|
|
log.Logger.Config.Level = golog.DebugLevel
|
2019-06-25 21:57:11 +00:00
|
|
|
// DebugOverlay = true // on by default in debug mode, F3 to disable
|
2017-10-27 01:03:11 +00:00
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
return d
|
|
|
|
}
|
2017-10-27 01:03:11 +00:00
|
|
|
|
2019-06-27 01:36:54 +00:00
|
|
|
// SetWindowSize sets the size of the Doodle window.
|
|
|
|
func (d *Doodle) SetWindowSize(width, height int) {
|
|
|
|
d.width = width
|
|
|
|
d.height = height
|
|
|
|
}
|
|
|
|
|
2019-06-25 21:57:11 +00:00
|
|
|
// Title returns the game's preferred window title.
|
|
|
|
func (d *Doodle) Title() string {
|
|
|
|
return fmt.Sprintf("%s v%s", branding.AppName, branding.Version)
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// SetupEngine sets up the rendering engine.
|
|
|
|
func (d *Doodle) SetupEngine() error {
|
2018-07-22 00:12:22 +00:00
|
|
|
if err := d.Engine.Setup(); err != nil {
|
2017-10-27 02:26:54 +00:00
|
|
|
return err
|
2017-10-27 01:03:11 +00:00
|
|
|
}
|
2018-08-11 00:19:47 +00:00
|
|
|
d.engineReady = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run initializes SDL and starts the main loop.
|
|
|
|
func (d *Doodle) Run() error {
|
|
|
|
if !d.engineReady {
|
|
|
|
if err := d.SetupEngine(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 02:26:54 +00:00
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
// Set up the default scene.
|
2018-07-26 02:38:54 +00:00
|
|
|
if d.Scene == nil {
|
2018-08-05 19:54:57 +00:00
|
|
|
// d.Goto(&GUITestScene{})
|
2019-06-25 21:57:11 +00:00
|
|
|
// d.NewMap()
|
|
|
|
d.Goto(&MainScene{})
|
2018-06-21 01:43:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
log.Info("Enter Main Loop")
|
2017-10-27 02:26:54 +00:00
|
|
|
for d.running {
|
2018-07-22 03:43:01 +00:00
|
|
|
d.Engine.Clear(render.White)
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
start := time.Now() // Record how long this frame took.
|
2019-07-05 23:04:36 +00:00
|
|
|
shmem.Tick++
|
2018-06-17 14:56:51 +00:00
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
// Poll for events.
|
2018-07-22 00:12:22 +00:00
|
|
|
ev, err := d.Engine.Poll()
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
shmem.Cursor = render.NewPoint(ev.CursorX.Now, ev.CursorY.Now)
|
2019-06-27 03:33:24 +00:00
|
|
|
if ev.EnterKey.Now {
|
|
|
|
log.Info("MainLoop sees enter key now")
|
|
|
|
}
|
|
|
|
if ev.KeyName.Now != "" {
|
|
|
|
log.Info("MainLoop sees key %s", ev.KeyName.Now)
|
|
|
|
}
|
2018-06-21 01:43:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("event poll error: %s", err)
|
2018-07-22 00:12:22 +00:00
|
|
|
d.running = false
|
|
|
|
break
|
|
|
|
}
|
2018-10-19 20:31:58 +00:00
|
|
|
d.event = ev
|
2018-07-22 00:12:22 +00:00
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// Command line shell.
|
|
|
|
if d.shell.Open {
|
|
|
|
} else if ev.EnterKey.Read() {
|
|
|
|
log.Debug("Shell: opening shell")
|
|
|
|
d.shell.Open = true
|
|
|
|
} else {
|
|
|
|
// Global event handlers.
|
|
|
|
if ev.EscapeKey.Read() {
|
|
|
|
log.Error("Escape key pressed, shutting down")
|
|
|
|
d.running = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-04-19 23:21:04 +00:00
|
|
|
if ev.KeyName.Now == "F3" {
|
|
|
|
DebugOverlay = !DebugOverlay
|
|
|
|
ev.KeyName.Read()
|
|
|
|
} else if ev.KeyName.Now == "F4" {
|
|
|
|
DebugCollision = !DebugCollision
|
|
|
|
ev.KeyName.Read()
|
|
|
|
}
|
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// Run the scene's logic.
|
2018-07-26 02:38:54 +00:00
|
|
|
err = d.Scene.Loop(d, ev)
|
2018-07-22 03:43:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-21 01:43:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// Draw the scene.
|
2018-07-26 02:38:54 +00:00
|
|
|
d.Scene.Draw(d)
|
2018-07-22 03:43:01 +00:00
|
|
|
|
|
|
|
// Draw the shell.
|
|
|
|
err = d.shell.Draw(d, ev)
|
2018-06-17 14:56:51 +00:00
|
|
|
if err != nil {
|
2018-07-22 03:43:01 +00:00
|
|
|
log.Error("shell error: %s", err)
|
|
|
|
d.running = false
|
|
|
|
break
|
2018-06-17 14:56:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
// Draw the debug overlay over all scenes.
|
2018-07-26 02:38:54 +00:00
|
|
|
d.DrawDebugOverlay()
|
2018-07-22 00:12:22 +00:00
|
|
|
|
|
|
|
// Render the pixels to the screen.
|
2018-07-22 03:43:01 +00:00
|
|
|
err = d.Engine.Present()
|
2018-07-22 00:12:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("draw error: %s", err)
|
|
|
|
d.running = false
|
|
|
|
break
|
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
|
2018-06-17 14:56:51 +00:00
|
|
|
// Delay to maintain the target frames per second.
|
|
|
|
var delay uint32
|
2019-04-10 01:28:08 +00:00
|
|
|
if !fpsDoNotCap {
|
|
|
|
elapsed := time.Now().Sub(start)
|
|
|
|
tmp := elapsed / time.Millisecond
|
|
|
|
if TargetFPS-int(tmp) > 0 { // make sure it won't roll under
|
|
|
|
delay = uint32(TargetFPS - int(tmp))
|
|
|
|
}
|
|
|
|
d.Engine.Delay(delay)
|
2018-06-17 14:56:51 +00:00
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
|
2018-06-17 14:56:51 +00:00
|
|
|
// Track how long this frame took to measure FPS over time.
|
2018-06-17 02:59:23 +00:00
|
|
|
d.TrackFPS(delay)
|
2018-07-22 03:43:01 +00:00
|
|
|
|
|
|
|
// Consume any lingering key sym.
|
|
|
|
ev.KeyName.Read()
|
2017-10-27 02:26:54 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
log.Warn("Main Loop Exited! Shutting down...")
|
2017-10-27 02:26:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// NewMap loads a new map in Edit Mode.
|
|
|
|
func (d *Doodle) NewMap() {
|
|
|
|
log.Info("Starting a new map")
|
|
|
|
scene := &EditorScene{}
|
|
|
|
d.Goto(scene)
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// NewDoodad loads a new Doodad in Edit Mode.
|
|
|
|
func (d *Doodle) NewDoodad(size int) {
|
2019-04-20 00:23:37 +00:00
|
|
|
if balance.FreeVersion {
|
|
|
|
d.Flash("Doodad editor is not available in your version of the game.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
log.Info("Starting a new doodad")
|
|
|
|
scene := &EditorScene{
|
|
|
|
DrawingType: enum.DoodadDrawing,
|
|
|
|
DoodadSize: size,
|
|
|
|
}
|
|
|
|
d.Goto(scene)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditDrawing loads a drawing (Level or Doodad) in Edit Mode.
|
|
|
|
func (d *Doodle) EditDrawing(filename string) error {
|
|
|
|
log.Info("Loading drawing from file: %s", filename)
|
2019-07-02 22:24:46 +00:00
|
|
|
ext := strings.ToLower(filepath.Ext(filename))
|
2018-09-26 17:04:46 +00:00
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
scene := &EditorScene{
|
|
|
|
Filename: filename,
|
|
|
|
OpenFile: true,
|
2018-06-21 01:43:14 +00:00
|
|
|
}
|
2018-09-26 17:04:46 +00:00
|
|
|
|
|
|
|
switch ext {
|
2019-07-02 22:24:46 +00:00
|
|
|
case ".level":
|
|
|
|
case ".map":
|
2019-04-10 00:35:44 +00:00
|
|
|
log.Info("is a Level type")
|
2018-09-26 17:04:46 +00:00
|
|
|
scene.DrawingType = enum.LevelDrawing
|
2019-07-02 22:24:46 +00:00
|
|
|
case ".doodad":
|
2019-04-20 00:23:37 +00:00
|
|
|
if balance.FreeVersion {
|
|
|
|
return fmt.Errorf("Doodad editor not supported in your version of the game")
|
|
|
|
}
|
2018-09-26 17:04:46 +00:00
|
|
|
scene.DrawingType = enum.DoodadDrawing
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("file extension '%s' doesn't indicate its drawing type", ext)
|
|
|
|
}
|
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
d.Goto(scene)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-21 02:00:46 +00:00
|
|
|
// PlayLevel loads a map from JSON into the PlayScene.
|
|
|
|
func (d *Doodle) PlayLevel(filename string) error {
|
|
|
|
log.Info("Loading level from file: %s", filename)
|
2018-07-24 03:10:53 +00:00
|
|
|
scene := &PlayScene{
|
|
|
|
Filename: filename,
|
2018-06-21 02:00:46 +00:00
|
|
|
}
|
|
|
|
d.Goto(scene)
|
|
|
|
return nil
|
|
|
|
}
|