func main() { spec := &sdl.AudioSpec{ Freq: sampleHz, Format: sdl.AUDIO_U8, Channels: 2, Samples: sampleHz, Callback: sdl.AudioCallback(C.SineWave), } sdl.OpenAudio(spec, nil) sdl.PauseAudio(0) time.Sleep(1 * time.Second) }
func main() { if err := sdl.Init(sdl.INIT_AUDIO); err != nil { log.Println(err) return } defer sdl.Quit() spec := &sdl.AudioSpec{ Freq: sampleHz, Format: sdl.AUDIO_U8, Channels: 2, Samples: sampleHz, Callback: sdl.AudioCallback(C.SineWave), } sdl.OpenAudio(spec, nil) sdl.PauseAudio(false) time.Sleep(1 * time.Second) }
// win==nil requests a full-screen window func Run(win WindowSpec) int { // All SDL calls must come from same thread. runtime.LockOSThread() defer runtime.UnlockOSThread() err := sdl.Init(sdl.INIT_EVERYTHING) if err != nil { panic(err) } defer sdl.Quit() // Install audio callback spec := &sdl.AudioSpec{ Freq: SampleRate, Format: sdl.AUDIO_F32SYS, Channels: 1, Samples: 4096, Callback: sdl.AudioCallback(C.getSoundSamplesAdaptor), } audioDevice, err := sdl.OpenAudioDevice("", false, spec, nil, 0) if err != nil { fmt.Fprintf(os.Stderr, "Failed to open audio device: %v\n", err) panic(err) } if audioDevice < 2 { fmt.Fprintf(os.Stderr, "Audio device=%v < 2 contrary to SDL-2 documentation\n", audioDevice, err) } sdl.PauseAudioDevice(audioDevice, false) // Create window var window *sdl.Window if win != nil { // Partial screen winWidth, winHeight := win.Size() window, err = sdl.CreateWindow(win.Title(), sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int(winWidth), int(winHeight), sdl.WINDOW_SHOWN) } else { // Full screen if isMacOS { // Contrary to https://wiki.libsdl.org/SDL_CreateWindow, on MacOS 10.11.1 // a call to sdl.CreateWindow in fullscreen mode *does* use the w and h parameters. // So ask what the display size is. var mode sdl.DisplayMode err = sdl.GetDesktopDisplayMode(0, &mode) if err != nil { fmt.Fprintf(os.Stderr, "Cannot get desktop display mode") panic(err) } window, err = sdl.CreateWindow("", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int(mode.W), int(mode.H), sdl.WINDOW_SHOWN|sdl.WINDOW_FULLSCREEN_DESKTOP) } else { window, err = sdl.CreateWindow("", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 0, 0, sdl.WINDOW_SHOWN|sdl.WINDOW_FULLSCREEN_DESKTOP) } } if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %v\n", err) panic(err) } defer window.Destroy() // Create renderer width, height := window.GetSize() renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %v\n", err) panic(err) } defer renderer.Destroy() var nTex = 1 if isMacOS { // Work around MacOS bug via double buffering nTex = 2 } // Create texture tex := make([]*sdl.Texture, nTex) for i := range tex { tex[i], err = renderer.CreateTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_STREAMING, width, height) if err != nil { fmt.Fprintf(os.Stderr, "renderer.CreateTexture: %v\n", err) panic(err) } defer tex[i].Destroy() } for _, r := range renderClientList { r.Init(int32(width), int32(height)) } // Loop until quit for i := 0; ; i ^= nTex - 1 { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch e := event.(type) { case *sdl.QuitEvent: return 0 case *sdl.MouseMotionEvent: // Go equivalent of SDL_PRESSED seems to be missing, so compare with zero. if e.State != 0 { forwardMouseEvent(MouseDrag, int32(e.X), int32(e.Y)) } else { forwardMouseEvent(MouseMove, int32(e.X), int32(e.Y)) } case *sdl.MouseButtonEvent: switch e.Type { case sdl.MOUSEBUTTONDOWN: forwardMouseEvent(MouseDown, int32(e.X), int32(e.Y)) case sdl.MOUSEBUTTONUP: forwardMouseEvent(MouseUp, int32(e.X), int32(e.Y)) } case *sdl.KeyDownEvent: var k Key if 0x20 <= e.Keysym.Sym && e.Keysym.Sym < 0x7F { // Printable ASCII k = Key(e.Keysym.Sym) } else { // Try special character table k = keyMap[e.Keysym.Sym] } if k != 0 { forwardKeyEvent(k) } } } pixels, pitch := lockTexture(tex[i], width, height) pm := MakePixMap(int32(width), int32(height), pixels, int32(pitch)) for _, r := range renderClientList { r.Render(pm) } tex[i].Unlock() err := renderer.Clear() if err != nil { fmt.Fprintf(os.Stderr, "renderer.Clear: %v", err) panic(err) } renderer.Copy(tex[i], nil, nil) if err != nil { fmt.Fprintf(os.Stderr, "renderer.Copy: %v", err) panic(err) } renderer.Present() } }
func main() { flags := flag.Args() if len(flags) != 1 { fmt.Println("Chippy - CHIP8 Emulator") fmt.Println("Copyright (C) 2016 Andreas T Jonsson") fmt.Printf("Version: %v\n\n", chip8.Version) fmt.Printf("usage: chippy [program]\n\n") return } sdl.Init(sdl.INIT_EVERYTHING) defer sdl.Quit() window, err := sdl.CreateWindow("", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN) if err != nil { log.Fatalln(err) } defer window.Destroy() renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { log.Fatalln(err) } defer renderer.Destroy() renderer.SetLogicalSize(800, 600) renderer.SetDrawColor(0, 0, 0, 255) texture, err := renderer.CreateTexture(sdl.PIXELFORMAT_BGR24, sdl.TEXTUREACCESS_STREAMING, 64, 32) if err != nil { log.Fatalln(err) } var specIn sdl.AudioSpec specIn.Channels = 1 specIn.Format = sdl.AUDIO_U8 specIn.Freq = 11025 specIn.Samples = 4096 specIn.Callback = sdl.AudioCallback(C.audioCallback) if err := sdl.OpenAudio(&specIn, nil); err != nil { log.Fatalln(err) } defer sdl.CloseAudio() m := &machine{ programPath: flags[0], cpuSpeedHz: defaultCPUSpeed, texture: texture, renderer: renderer, videoWidth: 64, video: make([]byte, 64*32*3), } defer func() { m.texture.Destroy() }() updateTitle(window, m) sys := chip8.NewSystem(m) cpuSpeedHz := m.cpuSpeedHz tickRender := time.Tick(time.Second / 65) tickCPU := time.Tick(time.Second / cpuSpeedHz) for { select { case <-tickRender: for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: return case *sdl.KeyUpEvent: switch t.Keysym.Sym { case sdl.K_ESCAPE: return case sdl.K_BACKSPACE: sys.Reset() case sdl.K_g: toggleFullscreen(window) case sdl.K_p: if m.cpuSpeedHz < 2000 { m.cpuSpeedHz += 100 } case sdl.K_m: if m.cpuSpeedHz > 100 { m.cpuSpeedHz -= 100 } case sdl.K_d: if t.Keysym.Mod&sdl.KMOD_CTRL != 0 { dumpSystem(sys, flags[0]) } } } } if cpuSpeedHz != m.cpuSpeedHz { cpuSpeedHz = m.cpuSpeedHz updateTitle(window, m) tickCPU = time.Tick(time.Second / m.cpuSpeedHz) } sys.Refresh() renderer.Clear() texture.Update(nil, unsafe.Pointer(&m.video[0]), m.videoWidth*3) renderer.Copy(m.texture, nil, nil) renderer.Present() case <-tickCPU: if err := sys.Step(); err != nil { dumpSystem(sys, flags[0]) log.Fatalln(err) } } } }