Beispiel #1
0
func main() {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	defer glfw.Terminate()

	mandala.Verbose = true

	if !glfw.Init() {
		panic("Can't init glfw!")
	}

	// Enable OpenGL ES 2.0.
	glfw.WindowHint(glfw.ClientApi, glfw.OpenglEsApi)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	window, err := glfw.CreateWindow(Width, Height, "gltext black-box testing", nil, nil)
	if err != nil {
		panic(err)
	}

	glfw.SwapInterval(0)
	mandala.Init(window)

	go prettytest.Run(new(testing.T), testlib.NewTestSuite(outputPath))

	for !window.ShouldClose() {
		glfw.WaitEvents()
	}
}
Beispiel #2
0
func main() {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	defer glfw.Terminate()

	gorgasm.Verbose = true

	if !glfw.Init() {
		panic("Can't init glfw!")
	}

	// Enable OpenGL ES 2.0.
	glfw.WindowHint(glfw.ClientApi, glfw.OpenglEsApi)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	window, err := glfw.CreateWindow(testlib.Width, testlib.Height, "Gorgasm Test", nil, nil)
	if err != nil {
		panic(err)
	}

	gorgasm.Init(window)

	go prettytest.Run(new(testing.T), testlib.NewTestSuite())

	for !window.ShouldClose() {
		glfw.WaitEvents()
	}
}
Beispiel #3
0
// Run should be called by your main function, and will block
// indefinitely. This is necessary because some platforms expect calls
// from the main thread.
func Run() {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	// ensure we have at least 2 procs, due to the thread conditions we
	// have to work with.
	procs := runtime.GOMAXPROCS(0)
	if procs < 2 {
		runtime.GOMAXPROCS(2)
	}

	glfw.Init()
	defer glfw.Terminate()
	setupGlfw()

	mainchan := make(chan func(), 32)
	mainc = mainchan

	for {
		select {
		case fn := <-mainchan:
			fn()
		default:
			glfw.WaitEvents()
		}
	}
}
Beispiel #4
0
// ListenForEvents should be called by your main function, and will
// block indefinitely. This is necessary because some platforms expect
// calls from the main thread.
func ListenForEvents() error {
	// ensure we have at least 2 procs, due to the thread conditions we
	// have to work with.
	procs := runtime.GOMAXPROCS(0)
	if procs < 2 {
		runtime.GOMAXPROCS(2)
	}

	glfw.Init()
	defer glfw.Terminate()
	setupGlfw()

	t0 := time.Now()
	for {
		select {
		case fn, ok := <-mainc:
			if !ok {
				return nil
			}
			fn()
		default:
			// when under heavy activity, sleep and poll instead to
			// lessen sysmon() churn in Go's scheduler. with this
			// method, OS X's Activity Monitor reports over 1000 sleeps
			// a second, which is ridiculous, but better than 3000.
			// haven't been able to create a minimal reproduction of
			// this, but when tweaking values in runtime/proc.c's sysmon
			// func, it is obvious this is a scheduler issue.
			// TODO: best workaround is probably to write this entire
			// loop in C, which should keep the sysmon thread from
			// waking up (from these syscalls, anyways).
			dt := time.Now().Sub(t0)
			if dt < 150*time.Millisecond {
				time.Sleep(15 * time.Millisecond)
				glfw.PollEvents()
			} else {
				t0 = time.Now()
				glfw.WaitEvents()
			}
		}
	}
	return nil
}
Beispiel #5
0
func main() {
	runtime.LockOSThread()

	glfw.SetErrorCallback(errorCallback)

	if !glfw.Init() {
		panic("Can't init glfw!")
	}
	defer glfw.Terminate()

	// must be done in main thread or we get a nasty stderr message from glfw,
	// although it does seem to 'work'
	window, err := glfw.CreateWindow(Width, Height, Title, nil, nil)
	if err != nil {
		panic(err)
	}

	// separate thread for drawing so that we don't block on the event thread.
	// most obvious benefit is that we continue to render during window
	// resizes.
	go func() {
		runtime.LockOSThread()

		window.MakeContextCurrent()
		glfw.SwapInterval(1)
		gl.Init()
		if err := initScene(); err != nil {
			fmt.Fprintf(os.Stderr, "init: %s\n", err)
			return
		}

		for !window.ShouldClose() {
			drawScene()
			window.SwapBuffers()
		}
		os.Exit(0)
	}()

	for {
		glfw.WaitEvents()
	}
}
Beispiel #6
0
// Run should be called by your main function, and will block
// indefinitely. This is necessary because some platforms expect calls
// from the main thread, thus so does GLFW.
func Run() {
	// ensure we have at least 2 procs, due to the thread conditions we
	// have to work with.
	procs := runtime.GOMAXPROCS(0)
	if procs < 2 {
		runtime.GOMAXPROCS(2)
	}

	glfw.Init()
	defer glfw.Terminate()
	setupGlfw()

	for {
		select {
		case fn := <-mainc:
			fn()
		default:
			glfw.WaitEvents()
		}
	}
}
Beispiel #7
0
func main() {

	runtime.LockOSThread()

	verbose := flag.Bool("verbose", false, "produce verbose output")
	debug := flag.Bool("debug", false, "produce debug output")
	size := flag.String("size", "320x480", "set the size of the window")

	flag.Parse()

	if *verbose {
		mandala.Verbose = true
	}

	if *debug {
		mandala.Debug = true
	}

	dims := strings.Split(strings.ToLower(*size), "x")
	width, err := strconv.Atoi(dims[0])
	if err != nil {
		panic(err)
	}
	height, err := strconv.Atoi(dims[1])
	if err != nil {
		panic(err)
	}

	if !glfw.Init() {
		panic("Can't init glfw!")
	}
	defer glfw.Terminate()

	// Enable OpenGL ES 2.0.
	glfw.WindowHint(glfw.ClientApi, glfw.OpenglEsApi)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	window, err := glfw.CreateWindow(width, height, "{{.AppName}}", nil, nil)
	if err != nil {
		panic(err)
	}

	mandala.Init(window)

	// Create a rendering loop control struct containing a set of
	// channels that control rendering.
	renderLoopControl := newRenderLoopControl()

	// Start the rendering loop
	loop.GoRecoverable(
		renderLoopFunc(renderLoopControl),
		func(rs loop.Recoverings) (loop.Recoverings, error) {
			for _, r := range rs {
				log.Printf("%s\n%s", r.Reason, mandala.Stacktrace())
			}
			return rs, fmt.Errorf("Unrecoverable loop\n")
		},
	)
	// Start the event loop
	loop.GoRecoverable(
		eventLoopFunc(renderLoopControl),
		func(rs loop.Recoverings) (loop.Recoverings, error) {
			for _, r := range rs {
				log.Printf("%s\n%s", r.Reason, mandala.Stacktrace())
			}
			return rs, fmt.Errorf("Unrecoverable loop\n")
		},
	)

	for !window.ShouldClose() {
		glfw.WaitEvents()
	}

}
Beispiel #8
0
func CreateWindow(width, height int, name string, fullscreen bool, delegate WindowDelegate, legacy bool) error {
	if !glfw.Init() {
		return errors.New("Failed to initialize GLFW")
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.DepthBits, 16)
	if !legacy {
		glfw.WindowHint(glfw.ContextVersionMajor, 3)
		glfw.WindowHint(glfw.ContextVersionMinor, 2)
		glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
		glfw.WindowHint(glfw.OpenglForwardCompatible, 1)
	}

	var monitor *glfw.Monitor = nil
	var err error = nil
	if fullscreen {
		monitor, err = glfw.GetPrimaryMonitor()
		if err != nil {
			return err
		}

		vidModes, _ := monitor.GetVideoModes()
		maxResolution := vidModes[len(vidModes)-1]
		width = maxResolution.Width
		height = maxResolution.Height
	}
	window, err := glfw.CreateWindow(width, height, name, monitor, nil)
	if err != nil {
		return err
	}

	if fullscreen {
		window.SetInputMode(glfw.Cursor, glfw.CursorDisabled)
	}

	start := time.Now()
	last := start
	doSimulation := func() {
		now := time.Now()
		gameTime := GameTime{
			now,
			now.Sub(start),
			now.Sub(last),
		}
		delegate.Simulate(gameTime)
		last = now
	}

	bindEvents(window, &IdleSimulatorWindowDelegator{
		WindowDelegator{delegate},
		doSimulation,
	})
	window.MakeContextCurrent()
	glfw.SwapInterval(1)
	delegate.Init(window)
	frameWidth, frameHeight := window.GetFramebufferSize()
	delegate.Reshape(window, frameWidth, frameHeight)
	for !window.ShouldClose() {
		doSimulation()
		if delegate.NeedsRender() {
			delegate.Draw(window)
		}
		window.SwapBuffers()
		if delegate.IsIdle() {
			glfw.WaitEvents()
		} else {
			glfw.PollEvents()
		}
	}
	return nil
}