예제 #1
0
파일: screen.go 프로젝트: mantyr/go-sound
// RenderLinesWithEvents renders multiple channels of samples to screen, and draws events.
func (s *Screen) RenderLinesWithEvents(lines []Line, events <-chan interface{}, sampleRate int) {
	s.lines = lines

	runtime.LockOSThread()

	// NOTE: It appears that glfw 3.1 uses its own internal error callback.
	// glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) {
	// log.Fatalf("%v: %s\n", err, desc)
	// })
	if err := glfw.Init(); err != nil {
		log.Fatalf("Can't init glfw: %v!", err)
	}
	defer glfw.Terminate()

	window, err := glfw.CreateWindow(s.width, s.height, "Muse", nil, nil)
	if err != nil {
		log.Fatalf("CreateWindow failed: %s", err)
	}
	if aw, ah := window.GetSize(); aw != s.width || ah != s.height {
		log.Fatalf("Window doesn't have the requested size: want %d,%d got %d,%d", s.width, s.height, aw, ah)
	}
	window.MakeContextCurrent()

	// Must gl.Init() *after* MakeContextCurrent
	if err := gl.Init(); err != nil {
		log.Fatalf("Can't init gl: %v!", err)
	}

	// Set window up to be [0, -1.0] -> [width, 1.0], black.
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translated(-1, 0, 0)
	gl.Scaled(2/float64(s.width), 1.0, 1.0)
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)

	// Actually start writing data to the buffer
	for i, _ := range s.lines {
		s.lines[i].valueBuffer = types.NewBuffer(int(float64(s.width) / s.pixelsPerSample))
		s.lines[i].valueBuffer.GoPushChannel(s.lines[i].Values, sampleRate)
	}
	if events != nil {
		s.eventBuffer.GoPushChannel(events, sampleRate)
	}

	// Keep drawing while we still can (and should).
	for !window.ShouldClose() && !s.bufferFinished() {
		if window.GetKey(glfw.KeyEscape) == glfw.Press {
			break
		}
		gl.Clear(gl.COLOR_BUFFER_BIT)
		s.drawSignal()
		window.SwapBuffers()
		glfw.PollEvents()
	}

	// Keep window around, only close on esc.
	for !window.ShouldClose() && window.GetKey(glfw.KeyEscape) != glfw.Press {
		glfw.PollEvents()
	}
}
예제 #2
0
func main() {
	flag.Parse()

	if *filename == "REQUIRED" {
		print("You must include a filename to load. See --help.\n")
		os.Exit(-2)
	}

	runtime.LockOSThread()
	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

    	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(640, 320, "chip8", nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()

	if err := gl.Init(); err != nil {
		panic(err)
	}

	gl.ClearColor(0, 0, 0, 0)
	gl.MatrixMode(gl.PROJECTION)

	gl.Ortho(0, 64, 32, 0, 0, 1)

	myChip8.initialize()

	myChip8.loadGame(*filename)

	for !window.ShouldClose() {

		myChip8.emulateCycle(window)

		if(myChip8.drawFlag) {
			myChip8.drawGraphics()
      		window.SwapBuffers()
			time.Sleep(0 * time.Millisecond)
		}

        glfw.PollEvents()
		myChip8.setKeys(window);
	} 
}