Exemplo n.º 1
0
// Run runs renderLoop. The loop renders a frame and swaps the buffer
// at each tick received.
func renderLoopFunc(control *renderLoopControl) loop.LoopFunc {

	return func(loop loop.Loop) error {
		var window mandala.Window
		// Lock/unlock the loop to the current OS thread. This is
		// necessary because OpenGL functions should be called from
		// the same thread.
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()

		// Create an instance of ticker and immediately stop
		// it because we don't want to swap buffers before
		// initializing a rendering state.
		ticker := time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))
		ticker.Stop()

		for {
			select {
			case window = <-control.window:
				ticker.Stop()

				window.MakeContextCurrent()

				width, height := window.GetSize()
				gl.Viewport(0, 0, width, height)

				mandala.Logf("Restarting rendering loop...")
				ticker = time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))

				// Compute window radius
				windowRadius = math.Sqrt(math.Pow(float64(height), 2) + math.Pow(float64(width), 2))

				//gl.Init()
				gl.Disable(gl.DEPTH_TEST)
				// antialiasing
				gl.Enable(gl.BLEND)
				gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
				//gl.Enable(gl.LINE_SMOOTH)

			// At each tick render a frame and swap buffers.
			case <-ticker.C:
				draw()
				window.SwapBuffers()

			case event := <-control.pause:
				ticker.Stop()
				event.Paused <- true

			case <-control.resume:

			case <-loop.ShallStop():
				ticker.Stop()
				return nil
			}
		}
	}
}
Exemplo n.º 2
0
// Run runs renderLoop. The loop renders a frame and swaps the buffer
// at each tick received.
func renderLoopFunc(control *renderLoopControl) loop.LoopFunc {
	return func(loop loop.Loop) error {

		var state *gameState

		// Lock/unlock the loop to the current OS thread. This is
		// necessary because OpenGL functions should be called from
		// the same thread.
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()

		// Create an instance of ticker and immediately stop
		// it because we don't want to swap buffers before
		// initializing a rendering state.
		ticker := time.NewTicker(time.Duration(1e9 / int(FramesPerSecond)))
		ticker.Stop()

		for {
			select {
			case window := <-control.window:
				ticker.Stop()

				state = newGameState(window)

				width, height := window.GetSize()
				gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))

				ticker = time.NewTicker(time.Duration(time.Second / time.Duration(FramesPerSecond)))

			case tap := <-control.tapEvent:
				state.world.explosion(tap[0], tap[1])

			// At each tick render a frame and swap buffers.
			case <-ticker.C:
				state.draw()
				state.window.SwapBuffers()

			case event := <-control.pause:
				ticker.Stop()
				state.world.destroy()
				event.Paused <- true

			case <-control.resume:

			case <-loop.ShallStop():
				ticker.Stop()
				return nil
			}
		}
	}
}
Exemplo n.º 3
0
// Run runs renderLoop. The loop renders a frame and swaps the buffer
// at each tick received.
func renderLoopFunc(controlCh *controlCh) loop.LoopFunc {
	return func(loop loop.Loop) error {

		var state renderState

		// Lock/unlock the loop to the current OS thread. This is
		// necessary because OpenGL functions should be called from
		// the same thread.
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()

		// We don't have yet a proper rendering state so the
		// ticker should be stopped as soon as it is created.
		ticker := time.NewTicker(time.Duration(int(time.Second) / int(FRAMES_PER_SECOND)))
		ticker.Stop()

		for {
			select {
			// At each tick render a frame and swap buffers.
			case <-ticker.C:
				state.angle += 0.05
				state.cube.Rotate(state.angle, [3]float32{0, 1, 0})
				state.world.Draw()
				egl.SwapBuffers(state.eglState.Display, state.eglState.Surface)

				// Receive an EGL state from the
				// native graphics subsystem and
				// initialize a rendering state.
			case eglState := <-controlCh.eglState:
				if err := state.init(eglState); err != nil {
					panic(err)
				}
				// Now that we have a proper rendering
				// state we can start the ticker.
				ticker = time.NewTicker(time.Duration(int(time.Second) / int(FRAMES_PER_SECOND)))

			case <-controlCh.exit:
				go loop.Stop()

			case <-loop.ShallStop():
				ticker.Stop()
				egl.DestroySurface(state.eglState.Display, state.eglState.Surface)
				egl.DestroyContext(state.eglState.Display, state.eglState.Context)
				egl.Terminate(state.eglState.Display)
				return nil
			}
		}
	}
}
Exemplo n.º 4
0
// Run runs renderLoop. The loop renders a frame and swaps the buffer
// at each tick received.
func (t *TestSuite) renderLoopFunc(control *renderLoopControl) loop.LoopFunc {
	return func(loop loop.Loop) error {

		// renderState stores rendering state variables such
		// as the EGL state
		renderState := new(renderState)

		// Lock/unlock the loop to the current OS thread. This is
		// necessary because OpenGL functions should be called from
		// the same thread.
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()

		// Create an instance of ticker and immediately stop
		// it because we don't want to swap buffers before
		// initializing a rendering state.
		ticker := time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))
		ticker.Stop()

		for {
			select {
			case window := <-control.window:
				ticker.Stop()
				renderState.init(window)
				ticker = time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))

			// At each tick render a frame and swap buffers.
			case <-ticker.C:
				renderState.draw()
				renderState.window.SwapBuffers()
				t.testDraw <- true

			case <-control.resizeViewport:

			case <-control.pause:
				// store something
				ticker.Stop()

			case <-control.resume:
				// resume something

			case <-loop.ShallStop():
				ticker.Stop()
				return nil
			}
		}
	}
}
Exemplo n.º 5
0
// Run runs renderLoop. The loop renders a frame and swaps the buffer
// at each tick received.
func renderLoopFunc(control *renderLoopControl) loop.LoopFunc {
	return func(loop loop.Loop) error {
		var window mandala.Window
		// Lock/unlock the loop to the current OS thread. This is
		// necessary because OpenGL functions should be called from
		// the same thread.
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()

		// Create an instance of ticker and immediately stop
		// it because we don't want to swap buffers before
		// initializing a rendering state.
		ticker := time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))
		ticker.Stop()

		for {
			select {
			case window = <-control.window:
				ticker.Stop()

				window.MakeContextCurrent()

				width, height := window.GetSize()
				gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))

				mandala.Logf("Restarting rendering loop...")
				ticker = time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))

			// At each tick render a frame and swap buffers.
			case <-ticker.C:
				draw()
				window.SwapBuffers()

			case event := <-control.pause:
				ticker.Stop()
				event.Paused <- true

			case <-control.resume:

			case <-loop.ShallStop():
				ticker.Stop()
				return nil
			}
		}
	}
}
Exemplo n.º 6
0
// Run runs renderLoop. The loop renders a frame and swaps the buffer
// at each tick received.
func renderLoopFunc(control *renderLoopControl) loop.LoopFunc {
	return func(loop loop.Loop) error {

		// renderState keeps the rendering state variables
		// such as the EGL status
		renderState := new(renderState)

		// Lock/unlock the loop to the current OS thread. This is
		// necessary because OpenGL functions should be called from
		// the same thread.
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()

		// Create an instance of ticker and immediately stop
		// it because we don't want to swap buffers before
		// initializing a rendering state.
		ticker := time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))
		ticker.Stop()

		for {
			select {
			case window := <-control.window:
				ticker.Stop()
				renderState.init(window)
				mandala.Logf("Restarting rendering loop...")
				ticker = time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))

			// At each tick render a frame and swap buffers.
			case <-ticker.C:
				renderState.angle += 0.05
				renderState.cube.Rotate(renderState.angle, [3]float32{0, 1, 0})
				renderState.world.Draw()
				renderState.window.SwapBuffers()

			case viewport := <-control.resizeViewport:
				if renderState.world != nil {
					if viewport.width != renderState.world.Width || viewport.height != renderState.world.Height {
						mandala.Logf("Resize native window W:%v H:%v\n", viewport.width, viewport.height)
						ticker.Stop()
						renderState.world.Resize(viewport.width, viewport.height)
						ticker = time.NewTicker(time.Duration(1e9 / int(FRAMES_PER_SECOND)))
					}
				}

			case event := <-control.pause:
				renderState.savedAngle = renderState.angle
				mandala.Logf("Save an angle value of %f", renderState.savedAngle)
				ticker.Stop()
				event.Paused <- true

			case <-control.resume:
				renderState.angle = renderState.savedAngle
				mandala.Logf("Restore an angle value of %f", renderState.angle)

			case <-loop.ShallStop():
				ticker.Stop()
				return nil
			}
		}
	}
}