Esempio n. 1
0
func (state *renderState) init(eglState *platform.EGLState) error {
	state.eglState = eglState

	display := eglState.Display
	surface := eglState.Surface
	context := eglState.Context
	width := eglState.SurfaceWidth
	height := eglState.SurfaceHeight

	if ok := egl.MakeCurrent(display, surface, surface, context); !ok {
		return egl.NewError(egl.GetError())
	}

	// Create and setup the 3D world
	state.world = cubelib.NewWorld(width, height)
	state.world.SetCamera(0.0, 0.0, 5.0)

	state.cube = cubelib.NewCube()

	if err := state.cube.AttachTextureFromFile(TEXTURE_PNG); err != nil {
		return err
	}

	state.world.Attach(state.cube)
	state.angle = 0.0

	return nil
}
Esempio n. 2
0
// MakeContextCurrent bind the OpenGL context to the current thread.
func (win *window) MakeContextCurrent() {
	if ok := egl.MakeCurrent(
		win.eglState.Display,
		win.eglState.Surface,
		win.eglState.Surface,
		win.eglState.Context); !ok {
		Fatalf("%s", egl.NewError(egl.GetError()))
	}
}
Esempio n. 3
0
func (m *mainLoop) loop(init chan<- struct{}) {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	looper := C.ALooper_prepare(C.ALOOPER_PREPARE_ALLOW_NON_CALLBACKS)
	if looper == nil {
		panic("ALooper_prepare returned nil")
	}
	m.looper = looper
	init <- struct{}{}
	for {
		select {
		case <-m.quit:
			if m.renderState != nil && m.renderState.ctx != egl.Context(unsafe.Pointer(nil)) {
				if !egl.MakeCurrent(m.renderState.disp, egl.Surface(unsafe.Pointer(nil)), egl.Surface(unsafe.Pointer(nil)), egl.Context(unsafe.Pointer(nil))) {
					panic("Error: eglMakeCurrent() failed\n")
				}
			}
			m.ack <- struct{}{}
			break
		case <-m.resume:
			m.running = true
			m.ack <- struct{}{}
		case <-m.pause:
			m.running = false
			m.width, m.height = 0, 0
			m.ack <- struct{}{}
		case m.focused = <-m.focus:
			m.width, m.height = 0, 0
			m.ack <- struct{}{}
		case m.renderState = <-m.render:
			m.ack <- struct{}{}
		case inputQ := <-m.input:
			if inputQ != nil {
				C.AInputQueue_attachLooper(inputQ, m.looper, LOOPER_ID_INPUT, nil, nil)
			} else {
				C.AInputQueue_detachLooper(m.inputQ)
			}
			m.inputQ = inputQ
			m.ack <- struct{}{}
		default:
			m.frame()
		}
	}
}
Esempio n. 4
0
func (t *testCreateEGLContextSuite) TestMakeCurrent() {
	egl.BindAPI(egl.OPENGL_ES_API)
	context := egl.CreateContext(
		t.display,
		t.config,
		egl.NO_CONTEXT,
		&contextAttr[0])
	surface := egl.CreateWindowSurface(
		t.display,
		t.config,
		testWin,
		nil)
	t.True(egl.MakeCurrent(
		t.display,
		surface,
		surface,
		context,
	))
}
Esempio n. 5
0
func (m *mainLoop) frame() {
	var timeout C.int = 0
	if !m.isRunning() {
		timeout = -1
	}
	ident := C.ALooper_pollAll(timeout, nil, nil, nil)
	switch ident {
	case LOOPER_ID_INPUT:
		if m.inputQ != nil {
			m.processInput(m.inputQ)
		}
	case C.ALOOPER_POLL_ERROR:
		log.Fatalf("ALooper_pollAll returned ALOOPER_POLL_ERROR\n")
	}
	if m.isRunning() {
		m.checkSize()
		createCtx := m.renderState.ctx == egl.Context(unsafe.Pointer(nil))
		if createCtx {
			log.Printf("Creating context\n")
			ctx_attribs := [...]int32{
				egl.CONTEXT_CLIENT_VERSION, 2,
				egl.NONE,
			}

			m.renderState.ctx = egl.CreateContext(m.renderState.disp, m.renderState.conf, egl.NO_CONTEXT, &ctx_attribs[0])
			if m.renderState.ctx == egl.Context(unsafe.Pointer(nil)) {
				panic("Error: eglCreateContext failed\n")
			}
		}

		if !egl.MakeCurrent(m.renderState.disp, m.renderState.surf, m.renderState.surf, m.renderState.ctx) {
			panic("Error: eglMakeCurrent() failed\n")
		}
		if createCtx {
			m.game.initGL()
		}
		m.game.drawFrame()
		egl.SwapBuffers(m.renderState.disp, m.renderState.surf)
	}
}