2017-10-27 01:03:11 +00:00
|
|
|
package doodle
|
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
import (
|
2018-06-17 02:59:23 +00:00
|
|
|
"time"
|
2017-10-27 02:26:54 +00:00
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
"git.kirsle.net/apps/doodle/render"
|
|
|
|
"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 (
|
|
|
|
// Version number.
|
2018-06-17 14:56:51 +00:00
|
|
|
Version = "0.0.1-alpha"
|
2018-06-17 02:59:23 +00:00
|
|
|
|
|
|
|
// 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-07-22 00:12:22 +00:00
|
|
|
Debug bool
|
|
|
|
Engine render.Engine
|
2017-10-27 01:03:11 +00:00
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
startTime time.Time
|
|
|
|
running bool
|
2018-06-17 03:21:42 +00:00
|
|
|
ticks uint64
|
2018-06-17 02:59:23 +00:00
|
|
|
width int32
|
|
|
|
height int32
|
|
|
|
|
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,
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
}
|
2018-07-22 03:43:01 +00:00
|
|
|
d.shell = NewShell(d)
|
2018-06-17 02:59:23 +00:00
|
|
|
|
|
|
|
if !debug {
|
|
|
|
log.Config.Level = golog.InfoLevel
|
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
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
// Run initializes SDL and starts the main loop.
|
|
|
|
func (d *Doodle) Run() error {
|
2018-07-22 00:12:22 +00:00
|
|
|
// Set up the render engine.
|
|
|
|
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
|
|
|
}
|
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-01 00:18:13 +00:00
|
|
|
d.Goto(&GUITestScene{})
|
|
|
|
// 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.
|
2018-06-17 14:56:51 +00:00
|
|
|
d.ticks++
|
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
// Poll for events.
|
2018-07-22 00:12:22 +00:00
|
|
|
ev, err := d.Engine.Poll()
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-07-22 00:12:22 +00:00
|
|
|
elapsed := time.Now().Sub(start)
|
2018-06-17 02:59:23 +00:00
|
|
|
tmp := elapsed / time.Millisecond
|
2018-06-17 14:56:51 +00:00
|
|
|
var delay uint32
|
|
|
|
if TargetFPS-int(tmp) > 0 { // make sure it won't roll under
|
|
|
|
delay = uint32(TargetFPS - int(tmp))
|
|
|
|
}
|
2018-07-22 00:12:22 +00:00
|
|
|
d.Engine.Delay(delay)
|
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-06-21 02:00:46 +00:00
|
|
|
// EditLevel loads a map from JSON into the EditorScene.
|
|
|
|
func (d *Doodle) EditLevel(filename string) error {
|
2018-06-21 01:43:14 +00:00
|
|
|
log.Info("Loading level from file: %s", filename)
|
|
|
|
scene := &EditorScene{}
|
|
|
|
err := scene.LoadLevel(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|