Example #1
0
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)
}
Example #2
0
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)
}
Example #3
0
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)
			}
		}
	}
}