doodle/doodle.go

202 lines
3.8 KiB
Go
Raw Normal View History

2017-10-27 01:03:11 +00:00
package doodle
import (
"fmt"
"time"
"git.kirsle.net/apps/doodle/events"
"git.kirsle.net/apps/doodle/render"
"github.com/kirsle/golog"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
2017-10-27 01:03:11 +00:00
const (
// Version number.
Version = "0.0.0-alpha"
// TargetFPS is the frame rate to cap the game to.
TargetFPS = uint32(1000 / 60) // 60 FPS
// 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
startTime time.Time
running bool
ticks uint64
events *events.State
width int32
height int32
nextSecond time.Time
canvas Grid
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{
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
}
return d
}
2017-10-27 01:03:11 +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.
log.Info("Initializing SDL")
2017-10-27 01:03:11 +00:00
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
return err
2017-10-27 01:03:11 +00:00
}
defer sdl.Quit()
// 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.
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,
d.width,
d.height,
2017-10-27 01:03:11 +00:00
sdl.WINDOW_SHOWN,
)
if err != nil {
return err
2017-10-27 01:03:11 +00:00
}
defer window.Destroy()
d.window = window
2017-10-27 01:03:11 +00:00
// Blank out the window in white.
log.Info("Creating the SDL Renderer")
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
2017-10-27 01:03:11 +00:00
if err != nil {
panic(err)
}
d.renderer = renderer
render.Renderer = renderer
defer renderer.Destroy()
log.Info("Enter Main Loop")
for d.running {
// Draw a frame and log how long it took.
start := time.Now()
err = d.Loop()
d.ticks++
elapsed := time.Now().Sub(start)
tmp := elapsed / time.Millisecond
delay := TargetFPS - uint32(tmp)
sdl.Delay(delay)
d.TrackFPS(delay)
if err != nil {
return err
}
}
log.Warn("Main Loop Exited! Shutting down...")
return nil
}
// TODO: not a global
type Pixel struct {
start bool
x int32
y int32
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,
)
}
// Grid is a 2D grid of pixels in X,Y notation.
type Grid map[Pixel]interface{}
var pixelHistory []Pixel
2017-10-27 01:03:11 +00:00
// Loop runs one loop of the game engine.
func (d *Doodle) Loop() error {
// Poll for events.
ev, err := d.events.Poll(d.ticks)
if err != nil {
log.Error("event poll error: %s", err)
return err
}
// Clear the canvas and fill it with white.
d.renderer.SetDrawColor(255, 255, 255, 255)
d.renderer.Clear()
// Clicking? Log all the pixels while doing so.
if ev.Button1.Now {
pixel := Pixel{
start: ev.Button1.Now && !ev.Button1.Last,
x: ev.CursorX.Now,
y: ev.CursorY.Now,
dx: ev.CursorX.Last,
dy: ev.CursorY.Last,
}
// Append unique new pixels.
if len(pixelHistory) == 0 || pixelHistory[len(pixelHistory)-1] != pixel {
pixelHistory = append(pixelHistory, pixel)
}
}
d.renderer.SetDrawColor(0, 0, 0, 255)
for i, pixel := range pixelHistory {
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,
)
}
}
d.renderer.DrawPoint(pixel.x, pixel.y)
2017-10-27 01:03:11 +00:00
}
// Draw the FPS.
d.DrawDebugOverlay()
d.renderer.Present()
2017-10-27 01:03:11 +00:00
return nil
}