func main() { flag.Parse() var done bool var keys []uint8 sdl.Init(sdl.INIT_VIDEO) var screen = sdl.SetVideoMode(640, 480, 16, sdl.OPENGL|sdl.RESIZABLE) if screen == nil { sdl.Quit() panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n") } sdl.WM_SetCaption("Gears", "gears") init_() reshape(int(screen.W), int(screen.H)) done = false for !done { var event sdl.Event idle() for event.Poll() { switch event.Type { case sdl.VIDEORESIZE: screen = sdl.SetVideoMode(int(event.Resize().W), int(event.Resize().H), 16, sdl.OPENGL|sdl.RESIZABLE) if screen != nil { reshape(int(screen.W), int(screen.H)) } else { panic("we couldn't set the new video mode??") } break case sdl.QUIT: done = true break } } keys = sdl.GetKeyState() handleKeyPress(keys) if keys[sdl.K_ESCAPE] != 0 { done = true } draw() } sdl.Quit() return }
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) }
func main() { flag.Parse() var done bool var keys []uint8 sdl.Init(sdl.INIT_VIDEO) var screen = sdl.SetVideoMode(300, 300, 16, sdl.OPENGL|sdl.RESIZABLE) if screen == nil { sdl.Quit() panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n") } sdl.WM_SetCaption("Gears", "gears") init_() reshape(int(screen.W), int(screen.H)) done = false for !done { var event sdl.Event idle() for event.Poll() { switch event.Type { case sdl.VIDEORESIZE: screen = sdl.SetVideoMode(int(event.Resize().W), int(event.Resize().H), 16, sdl.OPENGL|sdl.RESIZABLE) if screen != nil { reshape(int(screen.W), int(screen.H)) } else { panic("we couldn't set the new video mode??") } break case sdl.QUIT: done = true break } } keys = sdl.GetKeyState() if keys[sdl.K_ESCAPE] != 0 { done = true } if keys[sdl.K_UP] != 0 { view_rotx += 5.0 } if keys[sdl.K_DOWN] != 0 { view_rotx -= 5.0 } if keys[sdl.K_LEFT] != 0 { view_roty += 5.0 } if keys[sdl.K_RIGHT] != 0 { view_roty -= 5.0 } if keys[sdl.K_z] != 0 { if (sdl.GetModState() & sdl.KMOD_RSHIFT) != 0 { view_rotz -= 5.0 } else { view_rotz += 5.0 } } draw() } sdl.Quit() return }