Add global tick counter for debugging, fix unclick state errors

This commit is contained in:
Noah 2018-06-16 20:21:42 -07:00
parent b7751507e4
commit f8fe40c5ef
2 changed files with 44 additions and 19 deletions

View File

@ -28,6 +28,7 @@ type Doodle struct {
startTime time.Time startTime time.Time
running bool running bool
ticks uint64
events *events.State events *events.State
width int32 width int32
height int32 height int32
@ -106,6 +107,7 @@ func (d *Doodle) Run() error {
// Draw a frame and log how long it took. // Draw a frame and log how long it took.
start := time.Now() start := time.Now()
err = d.Loop() err = d.Loop()
d.ticks++
elapsed := time.Now().Sub(start) elapsed := time.Now().Sub(start)
tmp := elapsed / time.Millisecond tmp := elapsed / time.Millisecond
@ -146,7 +148,7 @@ var pixelHistory []Pixel
// Loop runs one loop of the game engine. // Loop runs one loop of the game engine.
func (d *Doodle) Loop() error { func (d *Doodle) Loop() error {
// Poll for events. // Poll for events.
ev, err := d.events.Poll() ev, err := d.events.Poll(d.ticks)
if err != nil { if err != nil {
log.Error("event poll error: %s", err) log.Error("event poll error: %s", err)
return err return err
@ -166,6 +168,7 @@ func (d *Doodle) Loop() error {
dy: ev.CursorY.Last, dy: ev.CursorY.Last,
} }
// Append unique new pixels.
if len(pixelHistory) == 0 || pixelHistory[len(pixelHistory)-1] != pixel { if len(pixelHistory) == 0 || pixelHistory[len(pixelHistory)-1] != pixel {
pixelHistory = append(pixelHistory, pixel) pixelHistory = append(pixelHistory, pixel)
} }

View File

@ -10,29 +10,34 @@ import (
// State keeps track of event states. // State keeps track of event states.
type State struct { type State struct {
// Mouse buttons. // Mouse buttons.
Button1 BoolFrameState Button1 *BoolFrameState
Button2 BoolFrameState Button2 *BoolFrameState
// Cursor positions. // Cursor positions.
CursorX Int32FrameState CursorX *Int32FrameState
CursorY Int32FrameState CursorY *Int32FrameState
} }
// New creates a new event state manager. // New creates a new event state manager.
func New() *State { func New() *State {
return &State{} return &State{
Button1: &BoolFrameState{},
Button2: &BoolFrameState{},
CursorX: &Int32FrameState{},
CursorY: &Int32FrameState{},
}
} }
// Poll for events. // Poll for events.
func (s *State) Poll() (*State, error) { func (s *State) Poll(ticks uint64) (*State, error) {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) { switch t := event.(type) {
case *sdl.QuitEvent: case *sdl.QuitEvent:
return s, errors.New("quit") return s, errors.New("quit")
case *sdl.MouseMotionEvent: case *sdl.MouseMotionEvent:
if DebugMouseEvents { if DebugMouseEvents {
log.Debug("[%d ms] MouseMotion type:%d id:%d x:%d y:%d xrel:%d yrel:%d", log.Debug("[%d ms] tick:%d MouseMotion type:%d id:%d x:%d y:%d xrel:%d yrel:%d",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel, t.Timestamp, ticks, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel,
) )
} }
@ -42,8 +47,8 @@ func (s *State) Poll() (*State, error) {
s.Button1.Push(t.State == 1) s.Button1.Push(t.State == 1)
case *sdl.MouseButtonEvent: case *sdl.MouseButtonEvent:
if DebugClickEvents { if DebugClickEvents {
log.Debug("[%d ms] MouseButton type:%d id:%d x:%d y:%d button:%d state:%d", log.Debug("[%d ms] tick:%d MouseButton type:%d id:%d x:%d y:%d button:%d state:%d",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State, t.Timestamp, ticks, t.Type, t.Which, t.X, t.Y, t.Button, t.State,
) )
} }
@ -53,26 +58,43 @@ func (s *State) Poll() (*State, error) {
// Is a mouse button pressed down? // Is a mouse button pressed down?
if t.Button == 1 { if t.Button == 1 {
var eventName string
if DebugClickEvents { if DebugClickEvents {
if t.State == 1 && s.Button1.Now == false { if t.State == 1 && s.Button1.Now == false {
log.Debug("Mouse Button1 DOWN") eventName = "DOWN"
} else if t.State == 0 && s.Button1.Now == true { } else if t.State == 0 && s.Button1.Now == true {
log.Debug("Mouse Button1 UP") eventName = "UP"
} }
} }
s.Button1.Push(t.State == 1)
}
s.Button2.Push(t.Button == 3 && t.State == 1) if eventName != "" {
log.Debug("tick:%d Mouse Button1 %s BEFORE: %+v",
ticks,
eventName,
s.Button1,
)
s.Button1.Push(eventName == "DOWN")
log.Debug("tick:%d Mouse Button1 %s AFTER: %+v",
ticks,
eventName,
s.Button1,
)
// Return the event immediately.
return s, nil
}
}
// s.Button2.Push(t.Button == 3 && t.State == 1)
case *sdl.MouseWheelEvent: case *sdl.MouseWheelEvent:
if DebugMouseEvents { if DebugMouseEvents {
log.Debug("[%d ms] MouseWheel type:%d id:%d x:%d y:%d", log.Debug("[%d ms] tick:%d MouseWheel type:%d id:%d x:%d y:%d",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Timestamp, ticks, t.Type, t.Which, t.X, t.Y,
) )
} }
case *sdl.KeyboardEvent: case *sdl.KeyboardEvent:
log.Debug("[%d ms] Keyboard type:%d sym:%c modifiers:%d state:%d repeat:%d\n", log.Debug("[%d ms] tick:%d Keyboard type:%d sym:%c modifiers:%d state:%d repeat:%d\n",
t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat, t.Timestamp, ticks, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat,
) )
} }
} }