Example #1
0
File: gui.go Project: cjyar/Go-SDL
func (win *window) eventLoop() {
	if win.ec == nil {
		win.ec = make(chan interface{})
	}

eloop:
	for win.events {
		var ev sdl.Event
		for ev.Poll() {
			switch ev.Type {
			case sdl.KEYUP:
				key := ev.Keyboard().Keysym.Sym
				win.ec <- gui.KeyEvent{int(-key)}
			case sdl.KEYDOWN:
				key := ev.Keyboard().Keysym.Sym
				win.ec <- gui.KeyEvent{int(key)}
			case sdl.MOUSEMOTION:
				m := ev.MouseMotion()
				win.ec <- gui.MouseEvent{
					Buttons: int(m.State),
					Loc:     image.Pt(int(m.X), int(m.Y)),
					Nsec:    time.Nanoseconds(),
				}
			case sdl.MOUSEBUTTONUP, sdl.MOUSEBUTTONDOWN:
				m := ev.MouseButton()
				win.ec <- gui.MouseEvent{
					Buttons: int(sdl.GetMouseState(nil, nil)),
					Loc:     image.Pt(int(m.X), int(m.Y)),
					Nsec:    time.Nanoseconds(),
				}
			case sdl.VIDEORESIZE:
				r := ev.Resize()
				win.ec <- gui.ConfigEvent{image.Config{
					win.Screen().ColorModel(),
					int(r.W),
					int(r.H),
				}}
			case sdl.QUIT:
				break eloop
			}
		}
	}

	close(win.ec)
}
Example #2
0
func (engine *SDLEngine) sdlRenderLoop() {
	runtime.LockOSThread()
	for {
		var event sdl.Event
		for event.Poll() {
			switch event.Type {
			case sdl.MOUSEMOTION:
				m := event.MouseMotion()
				engine.mouseEventChan <- MouseEvent{int32(m.X), int32(m.Y), int32(m.Xrel), int32(m.Yrel), int32(m.State)}
			case sdl.KEYDOWN, sdl.KEYUP:
				k := event.Keyboard()
				engine.keyEventChan <- KeyEvent{uint32(k.Keysym.Sym), uint32(k.Type)}
			case sdl.QUIT:
				engine.systemEventChan <- SystemEvent{SystemQuit}
				return
			}
		}
		engine.frameEventChan <- FrameEvent{0.0}
	}
	runtime.UnlockOSThread()
}