2017-10-27 01:03:11 +00:00
|
|
|
package doodle
|
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
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/events"
|
|
|
|
"git.kirsle.net/apps/doodle/render"
|
|
|
|
"github.com/kirsle/golog"
|
2017-10-27 02:26:54 +00:00
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
2018-06-17 02:59:23 +00:00
|
|
|
"github.com/veandco/go-sdl2/ttf"
|
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 {
|
|
|
|
Debug bool
|
|
|
|
|
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
|
|
|
events *events.State
|
|
|
|
width int32
|
|
|
|
height int32
|
|
|
|
|
|
|
|
nextSecond time.Time
|
|
|
|
canvas Grid
|
2017-10-27 02:26:54 +00:00
|
|
|
|
2017-10-27 01:03:11 +00:00
|
|
|
window *sdl.Window
|
|
|
|
renderer *sdl.Renderer
|
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes the game object.
|
|
|
|
func New(debug bool) *Doodle {
|
|
|
|
d := &Doodle{
|
2018-06-17 02:59:23 +00:00
|
|
|
Debug: debug,
|
|
|
|
startTime: time.Now(),
|
|
|
|
events: events.New(),
|
|
|
|
running: true,
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
canvas: Grid{},
|
|
|
|
|
|
|
|
nextSecond: time.Now().Add(1 * time.Second),
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-10-27 01:03:11 +00:00
|
|
|
// Initialize SDL.
|
2018-06-17 02:59:23 +00:00
|
|
|
log.Info("Initializing SDL")
|
2017-10-27 01:03:11 +00:00
|
|
|
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
|
2017-10-27 02:26:54 +00:00
|
|
|
return err
|
2017-10-27 01:03:11 +00:00
|
|
|
}
|
|
|
|
defer sdl.Quit()
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
// Initialize SDL_TTF.
|
|
|
|
log.Info("Initializing SDL_TTF")
|
|
|
|
if err := ttf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-27 01:03:11 +00:00
|
|
|
// Create our window.
|
2018-06-17 02:59:23 +00:00
|
|
|
log.Info("Creating the Main Window")
|
2017-10-27 01:03:11 +00:00
|
|
|
window, err := sdl.CreateWindow(
|
|
|
|
"Doodle v"+Version,
|
|
|
|
sdl.WINDOWPOS_CENTERED,
|
|
|
|
sdl.WINDOWPOS_CENTERED,
|
2017-10-27 02:26:54 +00:00
|
|
|
d.width,
|
|
|
|
d.height,
|
2017-10-27 01:03:11 +00:00
|
|
|
sdl.WINDOW_SHOWN,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2017-10-27 02:26:54 +00:00
|
|
|
return err
|
2017-10-27 01:03:11 +00:00
|
|
|
}
|
|
|
|
defer window.Destroy()
|
2017-10-27 02:26:54 +00:00
|
|
|
d.window = window
|
2017-10-27 01:03:11 +00:00
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
// Blank out the window in white.
|
2018-06-17 02:59:23 +00:00
|
|
|
log.Info("Creating the SDL Renderer")
|
2017-10-27 02:26:54 +00:00
|
|
|
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
|
2017-10-27 01:03:11 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-10-27 02:26:54 +00:00
|
|
|
d.renderer = renderer
|
2018-06-17 02:59:23 +00:00
|
|
|
render.Renderer = renderer
|
2017-10-27 02:26:54 +00:00
|
|
|
defer renderer.Destroy()
|
|
|
|
|
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-06-17 14:56:51 +00:00
|
|
|
d.ticks++
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
// Draw a frame and log how long it took.
|
|
|
|
start := time.Now()
|
2017-10-27 02:26:54 +00:00
|
|
|
err = d.Loop()
|
2018-06-17 14:56:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
elapsed := time.Now().Sub(start)
|
|
|
|
|
2018-06-17 14:56:51 +00:00
|
|
|
// Delay to maintain the target frames per second.
|
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-06-17 02:59:23 +00:00
|
|
|
sdl.Delay(delay)
|
|
|
|
|
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)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: not a global
|
|
|
|
type Pixel struct {
|
|
|
|
start bool
|
|
|
|
x int32
|
|
|
|
y int32
|
2018-06-17 02:59:23 +00:00
|
|
|
dx int32
|
|
|
|
dy int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Pixel) String() string {
|
|
|
|
return fmt.Sprintf("(%d,%d) delta (%d,%d)",
|
|
|
|
p.x, p.y,
|
|
|
|
p.dx, p.dy,
|
|
|
|
)
|
2017-10-27 02:26:54 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
// Grid is a 2D grid of pixels in X,Y notation.
|
|
|
|
type Grid map[Pixel]interface{}
|
|
|
|
|
2018-06-17 14:56:51 +00:00
|
|
|
// TODO: a linked list instead of a slice
|
2017-10-27 02:26:54 +00:00
|
|
|
var pixelHistory []Pixel
|
2017-10-27 01:03:11 +00:00
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
// Loop runs one loop of the game engine.
|
|
|
|
func (d *Doodle) Loop() error {
|
|
|
|
// Poll for events.
|
2018-06-17 03:21:42 +00:00
|
|
|
ev, err := d.events.Poll(d.ticks)
|
2018-06-17 02:59:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("event poll error: %s", err)
|
|
|
|
return err
|
2017-10-27 02:26:54 +00:00
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
|
2018-06-17 14:56:51 +00:00
|
|
|
// Taking a screenshot?
|
|
|
|
if ev.ScreenshotKey.Pressed() {
|
|
|
|
log.Info("Taking a screenshot")
|
|
|
|
d.Screenshot()
|
2018-06-17 17:29:57 +00:00
|
|
|
d.SaveLevel()
|
2018-06-17 14:56:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
// Clear the canvas and fill it with white.
|
2017-10-27 02:26:54 +00:00
|
|
|
d.renderer.SetDrawColor(255, 255, 255, 255)
|
2018-06-17 02:59:23 +00:00
|
|
|
d.renderer.Clear()
|
2017-10-27 02:26:54 +00:00
|
|
|
|
|
|
|
// Clicking? Log all the pixels while doing so.
|
2018-06-17 02:59:23 +00:00
|
|
|
if ev.Button1.Now {
|
2017-10-27 02:26:54 +00:00
|
|
|
pixel := Pixel{
|
2018-06-17 14:56:51 +00:00
|
|
|
start: ev.Button1.Pressed(),
|
2018-06-17 02:59:23 +00:00
|
|
|
x: ev.CursorX.Now,
|
|
|
|
y: ev.CursorY.Now,
|
2018-06-17 14:56:51 +00:00
|
|
|
dx: ev.CursorX.Now,
|
|
|
|
dy: ev.CursorY.Now,
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 03:21:42 +00:00
|
|
|
// Append unique new pixels.
|
2018-06-17 02:59:23 +00:00
|
|
|
if len(pixelHistory) == 0 || pixelHistory[len(pixelHistory)-1] != pixel {
|
2018-06-17 14:56:51 +00:00
|
|
|
// If not a start pixel, make the delta coord the previous one.
|
|
|
|
if !pixel.start && len(pixelHistory) > 0 {
|
|
|
|
prev := pixelHistory[len(pixelHistory)-1]
|
|
|
|
pixel.dx = prev.x
|
|
|
|
pixel.dy = prev.y
|
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
pixelHistory = append(pixelHistory, pixel)
|
2018-06-17 14:56:51 +00:00
|
|
|
|
|
|
|
// Save in the pixel canvas map.
|
|
|
|
d.canvas[pixel] = nil
|
2017-10-27 02:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.renderer.SetDrawColor(0, 0, 0, 255)
|
|
|
|
for i, pixel := range pixelHistory {
|
2018-06-17 02:59:23 +00:00
|
|
|
if !pixel.start && i > 0 {
|
|
|
|
prev := pixelHistory[i-1]
|
|
|
|
if prev.x == pixel.x && prev.y == pixel.y {
|
|
|
|
d.renderer.DrawPoint(pixel.x, pixel.y)
|
|
|
|
} else {
|
|
|
|
d.renderer.DrawLine(
|
|
|
|
pixel.x,
|
|
|
|
pixel.y,
|
|
|
|
prev.x,
|
|
|
|
prev.y,
|
|
|
|
)
|
|
|
|
}
|
2017-10-27 02:26:54 +00:00
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
d.renderer.DrawPoint(pixel.x, pixel.y)
|
2017-10-27 01:03:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
// Draw the FPS.
|
|
|
|
d.DrawDebugOverlay()
|
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
d.renderer.Present()
|
2017-10-27 01:03:11 +00:00
|
|
|
|
2017-10-27 02:26:54 +00:00
|
|
|
return nil
|
|
|
|
}
|