func main() { runtime.LockOSThread() flag.Parse() err := sdl.Init(sdl.INIT_EVERYTHING) ck(err) defer sdl.Quit() sdl.GLSetAttribute(sdl.GL_DOUBLEBUFFER, 1) screen = NewDisplay(W, H, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE) ck(err) screen.SetLogicalSize(W, H) texture, err = screen.CreateTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_STREAMING, W, H) ck(err) pixels = make([]uint32, W*H) screen.SetTitle("Mandelbrot Set") palette = make([]sdl.Color, seqlen) buf := make([]int, W*H) div = make([][]int, H) for i := range div { div[i] = buf[W*i:] } for i := range palette { hsv := sdlcolor.HSV{math.Pi / 2, 1, float64(i) / float64(len(palette)) / 0.1} palette[i] = sdl.ColorModel.Convert(hsv).(sdl.Color) } for run { event() draw() } }
func initSDL() { err := sdl.Init(sdl.INIT_VIDEO) ck(err) wflag := sdl.WINDOW_RESIZABLE if *fullscreen { wflag |= sdl.WINDOW_FULLSCREEN_DESKTOP } screen = newDisplay(W, H, wflag) screen.SetTitle("XSnake 2D") screen.SetLogicalSize(W, H) sdl.ShowCursor(0) }
func NewApp(conf Config) *App { runtime.LockOSThread() err := sdl.Init(sdl.INIT_EVERYTHING &^ sdl.INIT_AUDIO) ck(err) sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "best") wflag := sdl.WINDOW_RESIZABLE if conf.Fullscreen { wflag |= sdl.WINDOW_FULLSCREEN_DESKTOP } window, renderer, err := sdl.CreateWindowAndRenderer(DisplayWidth, DisplayHeight, wflag) ck(err) window.SetTitle("Arkanoid") renderer.SetLogicalSize(DisplayWidth, DisplayHeight) return &App{ window: window, renderer: renderer, } }
func main() { runtime.LockOSThread() log.SetFlags(0) err := sdl.Init(sdl.INIT_VIDEO) ck(err) window, renderer, err = sdl.CreateWindowAndRenderer(W, H, sdl.WINDOW_RESIZABLE) ck(err) window.SetTitle("Cosine Palette") renderer.SetLogicalSize(W, H) texture, err = renderer.CreateTexture(sdl.PIXELFORMAT_ABGR8888, sdl.TEXTUREACCESS_STREAMING, W, H) ck(err) pixels = image.NewRGBA(image.Rect(0, 0, W, H)) for { event() draw() } }
func main() { log.SetFlags(0) runtime.LockOSThread() flag.Parse() err := sdl.Init(sdl.INIT_VIDEO | sdl.INIT_TIMER) ck(err) defer sdl.Quit() sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "best") window, renderer, err = sdl.CreateWindowAndRenderer(W, H, sdl.WINDOW_RESIZABLE) ck(err) renderer.SetLogicalSize(W, H) window.SetTitle("Cat Clock") cat = loadImage(catb) eyes = loadImage(eyesb) hand.minute = makeTexture(ML*2, ML*2) hand.hour = makeTexture(HL+5, HL+5) for i := 0; i <= C; i++ { tail = append(tail, renderTail(float64(i)*math.Pi/C)) eye = append(eye, renderEye(float64(i)*math.Pi/C)) } renderer.SetTarget(nil) ticker := time.Tick(FPS) for { event() select { case <-ticker: update() default: } draw() } }
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() } }