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() { runtime.LockOSThread() flag.Parse() buildPalette() sdl.Init(sdl.INIT_VIDEO) defer sdl.Quit() sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1) if sdl.SetVideoMode(512, 512, 32, sdl.OPENGL) == nil { panic("sdl error") } if gl.Init() != 0 { panic("gl error") } sdl.WM_SetCaption("Gomandel", "Gomandel") gl.Enable(gl.TEXTURE_2D) gl.Viewport(0, 0, 512, 512) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 512, 512, 0, -1, 1) gl.ClearColor(0, 0, 0, 0) //----------------------------------------------------------------------------- var dndDragging bool = false var dndStart Point var dndEnd Point var tex gl.Texture var tc TexCoords var lastProgress int initialRect := Rect{-1.5, -1.5, 3, 3} rect := initialRect rc := new(MandelbrotRequest) rc.MakeRequest(512, 512, rect) rc.WaitFor(Small, &tex, &tc) running := true e := new(sdl.Event) for running { for e.Poll() { switch e.Type { case sdl.QUIT: running = false case sdl.MOUSEBUTTONDOWN: dndDragging = true sdl.GetMouseState(&dndStart.X, &dndStart.Y) dndEnd = dndStart case sdl.MOUSEBUTTONUP: dndDragging = false sdl.GetMouseState(&dndEnd.X, &dndEnd.Y) if e.MouseButton().Button == 3 { rect = initialRect } else { rect = rectFromSelection(dndStart, dndEnd, 512, 512, rect) tc = texCoordsFromSelection(dndStart, dndEnd, 512, 512, tc) } // make request rc.MakeRequest(512, 512, rect) case sdl.MOUSEMOTION: if dndDragging { sdl.GetMouseState(&dndEnd.X, &dndEnd.Y) } } } // if we're waiting for a result, check if it's ready p := rc.Update(&tex, &tc) if p != -1 { lastProgress = p } gl.Clear(gl.COLOR_BUFFER_BIT) tex.Bind(gl.TEXTURE_2D) drawQuad(0, 0, 512, 512, tc.TX, tc.TY, tc.TX2, tc.TY2) gl.BindTexture(gl.TEXTURE_2D, 0) if dndDragging { drawSelection(dndStart, dndEnd) } drawProgress(512, 512, lastProgress, rc.Pending) sdl.GL_SwapBuffers() } }