func event() { for { ev := sdl.PollEvent() if ev == nil { break } switch ev := ev.(type) { case sdl.QuitEvent: os.Exit(0) case sdl.KeyDownEvent: switch ev.Sym { case sdl.K_ESCAPE: os.Exit(0) case sdl.K_UP: if dir != 1 { dir = 3 } case sdl.K_DOWN: if dir != 3 { dir = 1 } case sdl.K_LEFT: if dir != 2 { dir = 4 } case sdl.K_RIGHT: if dir != 4 { dir = 2 } } } } }
func event() { const step = 0.1 for { ev := sdl.PollEvent() if ev == nil { break } switch e := ev.(type) { case sdl.QuitEvent: run = false case sdl.KeyDownEvent: switch e.Sym { case sdl.K_ESCAPE: run = false case sdl.K_LEFT: center -= complex(step, 0) case sdl.K_RIGHT: center += complex(step, 0) case sdl.K_DOWN: center -= complex(0, step) case sdl.K_UP: center += complex(0, step) case sdl.K_a: view += 0.1 case sdl.K_z: view -= 0.1 } } } }
func event() { for { ev := sdl.PollEvent() if ev == nil { break } switch ev := ev.(type) { case sdl.QuitEvent: os.Exit(0) case sdl.KeyDownEvent: switch ev.Sym { case sdl.K_ESCAPE: os.Exit(0) } } } }
func (c *App) event() { for { ev := sdl.PollEvent() if ev == nil { break } switch ev := ev.(type) { case sdl.QuitEvent: c.run = false case sdl.KeyDownEvent: switch ev.Sym { case sdl.K_ESCAPE: c.run = false case sdl.K_SPACE: c.ball.Moving = true case sdl.K_RETURN: c.Reset() } case sdl.MouseButtonDownEvent: switch ev.Button { case sdl.BUTTON_LEFT: c.ball.Moving = true case sdl.BUTTON_RIGHT: c.Reset() } case sdl.MouseWheelEvent: switch b := &c.ball; { case ev.Y < 0: if b.Speed > 1 { b.Speed-- } case ev.Y > 0: if b.Speed < 10 { b.Speed++ } } } } }
func main() { runtime.LockOSThread() var fullscreen bool flag.Usage = usage flag.Float64Var(&v0, "v", v0, "maximum start velocity") flag.Float64Var(&xmin, "x", xmin, "left boundary") flag.Float64Var(&xmax, "X", xmax, "right boundary") flag.Float64Var(&ymin, "y", ymin, "top boundary") flag.Float64Var(&ymax, "Y", ymax, "bottom boundary") flag.Float64Var(&dt, "t", dt, "time step") flag.IntVar(&refresh, "F", refresh, "clear every number of frames (0: off)") flag.IntVar(&N, "N", N, "number of particles") flag.IntVar(&size, "s", size, "size of particle (number of pixels)") flag.BoolVar(&fullscreen, "fs", false, "fullscreen") flag.Parse() A = make([]Particle, N) B = make([]Particle, N) prev = A cur = B rand.Seed(time.Now().UnixNano()) err := sdl.Init(sdl.INIT_VIDEO) ck(err) wflag := sdl.WINDOW_RESIZABLE if fullscreen { wflag |= sdl.WINDOW_FULLSCREEN_DESKTOP } screen = NewDisplay(640, 480, wflag) screen.SetDrawColor(White) screen.Clear() screen.Present() screen.SetTitle("Molecular Dynamics") sdl.ShowCursor(0) i := 0 reset() for { sw, sh := screen.Size() viewport.W = int32(sw) viewport.H = int32(sh) if refresh != 0 { if i++; i == refresh { draw(viewport, White) i = 0 } } for i := range cur { cur[i] = Particle{} } for n := 0; n < N; n++ { p := &prev[n] for m := 0; m < n; m++ { q := &prev[m] dx1 := math.Abs(p.x - q.x) dx2 := xmax - xmin - dx1 dx := min(dx1, dx2) dy1 := math.Abs(p.y - q.y) dy2 := ymax - ymin - dy1 dy := min(dy1, dy2) R := dx*dx + dy*dy if R >= 9 || R == 0 { continue } R = 1 / math.Sqrt(R) R2 := R * R R4 := R2 * R2 R6 := R4 * R2 R12 := R6 * R6 F := 24 * (2*R12 - R6) if p.x < q.x { dx = -dx } if p.y < q.y { dy = -dy } if dx1 > dx2 { dx = -dx } if dy1 > dy2 { dy = -dy } dx *= F dy *= F cur[n].ax += dx cur[n].ay += dy cur[m].ax -= dx cur[m].ay -= dy } } for j := 0; j < N; j++ { p := &prev[j] q := &cur[j] q.x = 2*p.x - p.prevx + q.ax*dt*dt q.y = 2*p.y - p.prevy + q.ay*dt*dt q.vx = (q.x - p.prevx) / (2 * dt) q.vy = (q.y - p.prevy) / (2 * dt) q.prevx = p.x q.prevy = p.y if q.x > xmax { q.x -= xmax - xmin q.prevx -= xmax - xmin } if q.x < xmin { q.x += xmax - xmin q.prevx += xmax - xmin } if q.y > ymax { q.y -= ymax - ymin q.prevy -= ymax - ymin } if q.y < ymin { q.y += ymax - ymin q.prevy += ymax - ymin } q.col = p.col x := float64(viewport.W)*(q.x-xmin)/(xmax-xmin) + float64(viewport.X) y := float64(viewport.H)*(q.y-ymin)/(ymax-ymin) + float64(viewport.Y) draw(sdl.Rect{int32(x), int32(y), int32(size), int32(size)}, p.col) } prev, cur = cur, prev screen.Present() for { ev := sdl.PollEvent() if ev == nil { break } switch ev := ev.(type) { case sdl.QuitEvent: os.Exit(0) case sdl.KeyDownEvent: switch ev.Sym { case sdl.K_q, sdl.K_ESCAPE: os.Exit(0) case sdl.K_r: reset() case sdl.K_f: draw(viewport, White) } case sdl.WindowEvent: switch ev.Event { case sdl.WINDOWEVENT_EXPOSED: draw(viewport, White) case sdl.WINDOWEVENT_RESIZED: sw, sh := screen.Size() viewport.W, viewport.H = int32(sw), int32(sh) draw(viewport, White) } } } } }
func main() { runtime.LockOSThread() log.SetFlags(0) err := sdl.Init(sdl.INIT_VIDEO) ck(err) screen := NewDisplay(500, 500, sdl.WINDOW_RESIZABLE) screen.SetLogicalSize(500, 500) screen.SetTitle("Make It Alive!") foo := 3 bar := 2 unit := []sdl.Point{ {-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}, } var p, z map[sdl.Point]bool var q map[sdl.Point]int for { screen.SetDrawColor(sdlcolor.Black) screen.Clear() z = make(map[sdl.Point]bool) for { ev := sdl.PollEvent() if ev == nil { break } switch ev := ev.(type) { case sdl.QuitEvent: os.Exit(0) case sdl.KeyDownEvent: switch ev.Sym { case sdl.K_ESCAPE: os.Exit(0) } case sdl.MouseButtonDownEvent: for _, u := range unit { n := sdl.Point{ev.X + u.X, ev.Y + u.Y} z[n] = true } } } for i := range q { if q[i] == foo { z[i] = true if p[i] { screen.SetDrawColor(sdl.Color{250, 150, 150, 255}) } else { screen.SetDrawColor(sdl.Color{150, 150, 250, 255}) } screen.DrawPoint(int(i.X), int(i.Y)) } else if q[i] == bar && p[i] { z[i] = true screen.SetDrawColor(sdl.Color{250, 150, 150, 255}) screen.DrawPoint(int(i.X), int(i.Y)) } } p = z q = make(map[sdl.Point]int) for v := range p { for _, u := range unit { n := sdl.Point{v.X + u.X, v.Y + u.Y} q[n] = q[n] + 1 } } screen.Present() } }
func main() { runtime.LockOSThread() err := sdl.Init(sdl.INIT_EVERYTHING) if err != nil { log.Fatalln(err) } defer sdl.Quit() window, renderer, err := sdl.CreateWindowAndRenderer(W, H, sdl.WINDOW_RESIZABLE) if err != nil { log.Fatalln(err) } texture, err := renderer.CreateTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_STREAMING, W, H) if err != nil { log.Fatalln(err) } pixels := make([]uint32, W*H) window.SetTitle("Gasket") renderer.SetLogicalSize(W, H) v := []Vec2{ {0.5, 0}, {0, 1}, {1, 1}, } p := Vec2{0.5, 0.5} loop: for { for { ev := sdl.PollEvent() if ev == nil { break } switch ev := ev.(type) { case sdl.QuitEvent: break loop case sdl.KeyDownEvent: switch ev.Sym { case sdl.K_ESCAPE: break loop } } } r, g, b := rand.Intn(256), rand.Intn(256), rand.Intn(256) n := rand.Intn(3) q := v[n].Add(p).Scale(.5) x := int(q[0] * W) y := int(q[1] * H) if y*W+x < len(pixels) { pixels[y*W+x] = 255<<24 | uint32(r)<<16 | uint32(g)<<8 | uint32(b) } p = q renderer.SetDrawColor(sdlcolor.Black) renderer.Clear() texture.Update(nil, pixels, W*4) renderer.Copy(texture, nil, nil) renderer.Present() } }