Esempio n. 1
0
func (g *GameEngine) startScrolling(start allegro.MouseButtonDown) {
	timer := allegro.CreateTimer(float64(1) / 30)
	mouseEvent := allegro.GetMouseEventSource()
	defer mouseEvent.StopGetEvents()
	es := []*allegro.EventSource{mouseEvent,
		timer.GetEventSource()}
	timer.Start()
	defer timer.Destroy()

	x, y := start.X, start.Y
	stopped := false
	for ev := range allegro.GetEvents(es) {
		switch tev := ev.(type) {
		case allegro.MouseButtonUp:
			if tev.Button == start.Button {
				stopped = true
			}
		case allegro.TimerEvent:
			viewport := g.displayEngine.GetViewport()
			viewport.Move((x-start.X)/30, (y-start.Y)/30)
			g.displayEngine.SetViewport(viewport)
		case allegro.MouseAxesEvent:
			x, y = tev.X, tev.Y
		}
		g.statusLock.RLock()
		stopped = stopped || g.stopped
		g.statusLock.RUnlock()
		if stopped {
			break
		}
	}
}
Esempio n. 2
0
func handleEvents(disp *allegro.Display) {
	sources := [...]*allegro.EventSource{
		disp.GetEventSource(),
		allegro.GetKeyboardEventSource(),
		allegro.GetMouseEventSource()}
	ch := allegro.GetEvents(sources[:])
	for ev := range ch {
		switch ev := ev.(type) {
		case allegro.DisplayResizeEvent:
			disp.AcknowledgeResize()
		case allegro.MouseButtonDown:
			alFuncs <- DisplayColor
		case allegro.DisplayCloseEvent:
			return
		case allegro.KeyDownEvent:
			if ev.Keycode == allegro.KEY_ESCAPE {
				return
			}
		}
	}
}
Esempio n. 3
0
func (g *GameEngine) eventHandler() {
	src := g.displayEngine.Display.GetEventSource()
	defer src.StopGetEvents()
	es := []*allegro.EventSource{src,
		allegro.GetKeyboardEventSource(),
		allegro.GetMouseEventSource()}
	queue := allegro.GetEvents(es)
	stopped := false
	for !stopped {
		ev := <-queue
		switch tev := ev.(type) {
		case allegro.KeyCharEvent:
			g.handleKeyChar(tev)
		case allegro.MouseButtonDown:
			g.handleMouseDown(tev)
		}
		g.statusLock.RLock()
		stopped = g.stopped
		g.statusLock.RUnlock()
	}
}
Esempio n. 4
0
func (d *DisplayEngine) eventHandler() {
	src := d.Display.GetEventSource()
	defer src.StopGetEvents()
	es := []*allegro.EventSource{src, allegro.GetMouseEventSource()}
	queue := allegro.GetEvents(es)
	stopped := false
	for !stopped {
		ev := <-queue
		switch tev := ev.(type) {
		case allegro.DisplayCloseEvent:
			d.statusLock.Lock()
			d.running = false
			d.statusLock.Unlock()
		case allegro.DisplayResizeEvent:
			d.handleResize(tev)
		case allegro.MouseButtonDown:
			tx, ty := d.viewport.ScreenCoordinatesToTile(tev.X, tev.Y, d.config)
			log.Printf("S: (%v, %v) T: (%v, %v)", tev.X, tev.Y, tx, ty)
		}
		d.statusLock.RLock()
		stopped = !d.running
		d.statusLock.RUnlock()
	}
}
Esempio n. 5
0
func (ge *GameEngine) MainLoop() {
	timer := allegro.CreateTimer(1 / float64(ge.tickRate))
	es := []*allegro.EventSource{timer.GetEventSource()}
	queue := allegro.GetEvents(es)
	stopped := false

	go ge.eventHandler()

	ge.statusLock.Lock()
	ge.stopped = false
	ge.statusLock.Unlock()
	tick := 0
	for !stopped {
		ev := <-queue
		if _, ok := ev.(allegro.TimerEvent); ok {
			ge.world.Tick(tick)
		}
		tick++

		ge.statusLock.RLock()
		stopped = ge.stopped
		ge.statusLock.RUnlock()
	}
}