Para dibujar objetos en tu juego, deberás configurar las variables de pantalla, superficie y contexto; establecer la renderización del bucle de juego; y dibujar cada escena y objeto.
Existen dos maneras de dibujar imágenes en la pantalla para un juego en C o C++: puedes usar OpenGL ES o Vulkan.
- OpenGL ES forma parte de la especificación Open Graphics Library (OpenGL®) destinada a dispositivos móviles como los Android. Descubre cómo configurar OpenGL ES para tu juego en este tema. 
- Si usas Vulkan para tu juego, lee el Cómo comenzar a usar Vulkan . 
Antes de comenzar
Si aún no lo hiciste, configura un objeto GameActivity en tu proyecto de Android.
Cómo configurar variables de OpenGL ES
- Para renderizar tu juego, necesitarás una pantalla, una superficie, un contexto y una configuración. Agrega las siguientes variables de OpenGL ES al archivo de encabezado del motor de juego: - class NativeEngine { //... private: EGLDisplay mEglDisplay; EGLSurface mEglSurface; EGLContext mEglContext; EGLConfig mEglConfig; bool mHasFocus, mIsVisible, mHasWindow; bool mHasGLObjects; bool mIsFirstFrame; int mSurfWidth, mSurfHeight; }
- En el constructor de tu motor de juego, inicializa los valores predeterminados para las variables. - NativeEngine::NativeEngine(struct android_app *app) { //... mEglDisplay = EGL_NO_DISPLAY; mEglSurface = EGL_NO_SURFACE; mEglContext = EGL_NO_CONTEXT; mEglConfig = 0; mHasFocus = mIsVisible = mHasWindow = false; mHasGLObjects = false; mIsFirstFrame = true; mSurfWidth = mSurfHeight = 0; }
- Inicializa la pantalla que se renderizará. - bool NativeEngine::InitDisplay() { if (mEglDisplay != EGL_NO_DISPLAY) { return true; } mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (EGL_FALSE == eglInitialize(mEglDisplay, 0, 0)) { LOGE("NativeEngine: failed to init display, error %d", eglGetError()); return false; } return true; }
- La superficie puede ser un búfer fuera de la pantalla (pbuffer) asignado por EGL o una ventana asignada por el SO Android. Inicializa esta superficie: - bool NativeEngine::InitSurface() { ASSERT(mEglDisplay != EGL_NO_DISPLAY); if (mEglSurface != EGL_NO_SURFACE) { return true; } EGLint numConfigs; const EGLint attribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, // request OpenGL ES 2.0 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 16, EGL_NONE }; // Pick the first EGLConfig that matches. eglChooseConfig(mEglDisplay, attribs, &mEglConfig, 1, &numConfigs); mEglSurface = eglCreateWindowSurface(mEglDisplay, mEglConfig, mApp->window, NULL); if (mEglSurface == EGL_NO_SURFACE) { LOGE("Failed to create EGL surface, EGL error %d", eglGetError()); return false; } return true; }
- Inicializa el contexto de renderización. En este ejemplo, se crea un contexto de OpenGL ES 2.0: - bool NativeEngine::InitContext() { ASSERT(mEglDisplay != EGL_NO_DISPLAY); if (mEglContext != EGL_NO_CONTEXT) { return true; } // OpenGL ES 2.0 EGLint attribList[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, attribList); if (mEglContext == EGL_NO_CONTEXT) { LOGE("Failed to create EGL context, EGL error %d", eglGetError()); return false; } return true; }
- Establece la configuración de OpenGL ES antes de dibujar. Este ejemplo se ejecuta al principio de cada fotograma. Permite las pruebas de profundidad, establece el color claro en negro y borra los búferes de color y profundidad. - void NativeEngine::ConfigureOpenGL() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); }
Cómo renderizar con el bucle de juego
- El bucle de juego renderiza un fotograma y continúa indefinidamente hasta que el usuario cierra el juego. Entre fotogramas, el juego puede hacer lo siguiente: - Procesar los eventos, como las entradas, las salidas de audio y los eventos de redes 
- Actualizar la lógica del juego y la interfaz de usuario 
- Renderizar un fotograma en la pantalla 
 - Para renderizar un fotograma en la pantalla, se llama al método - DoFramede forma indefinida en el bucle de juego:- void NativeEngine::GameLoop() { // Loop indefinitely. while (1) { int events; struct android_poll_source* source; // If not animating, block until we get an event. while ((ALooper_pollAll(IsAnimating() ? 0 : -1, NULL, &events, (void **) &source)) >= 0) { // Process events. ... } // Render a frame. if (IsAnimating()) { DoFrame(); } } }
- En el método - DoFrame, consulta las dimensiones actuales de la superficie, solicita a- SceneManagerque renderice un fotograma e intercambia los búferes de la pantalla.- void NativeEngine::DoFrame() { ... // Query the current surface dimension. int width, height; eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &width); eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &height); // Handle dimension changes. SceneManager *mgr = SceneManager::GetInstance(); if (width != mSurfWidth || height != mSurfHeight) { mSurfWidth = width; mSurfHeight = height; mgr->SetScreenSize(mSurfWidth, mSurfHeight); glViewport(0, 0, mSurfWidth, mSurfHeight); } ... // Render scenes and objects. mgr->DoFrame(); // Swap buffers. if (EGL_FALSE == eglSwapBuffers(mEglDisplay, mEglSurface)) { HandleEglError(eglGetError()); } }
Cómo renderizar escenas y objetos
- El bucle de juego procesa una jerarquía de escenas y objetos visibles para su renderización. En el ejemplo de Endless Tunnel, un - SceneManagerrealiza un seguimiento de varias escenas, con solo una escena activa a la vez. En este ejemplo, se renderiza la escena actual:- void SceneManager::DoFrame() { if (mSceneToInstall) { InstallScene(mSceneToInstall); mSceneToInstall = NULL; } if (mHasGraphics && mCurScene) { mCurScene->DoFrame(); } }
- Según el juego que elijas, una escena puede contener fondo, texto y objetos del juego. Renderiza los elementos en el orden adecuado para tu juego. En este ejemplo, se renderizan el fondo, el texto y los widgets: - void UiScene::DoFrame() { // clear screen glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_DEPTH_TEST); RenderBackground(); // Render the "Please Wait" sign and do nothing else if (mWaitScreen) { SceneManager *mgr = SceneManager::GetInstance(); mTextRenderer->SetFontScale(WAIT_SIGN_SCALE); mTextRenderer->SetColor(1.0f, 1.0f, 1.0f); mTextRenderer->RenderText(S_PLEASE_WAIT, mgr->GetScreenAspect() * 0.5f, 0.5f); glEnable(GL_DEPTH_TEST); return; } // Render all the widgets. for (int i = 0; i < mWidgetCount; ++i) { mWidgets[i]->Render(mTrivialShader, mTextRenderer, mShapeRenderer, (mFocusWidget < 0) ? UiWidget::FOCUS_NOT_APPLICABLE : (mFocusWidget == i) ? UiWidget::FOCUS_YES : UiWidget::FOCUS_NO,tf); } glEnable(GL_DEPTH_TEST); }
Recursos
Consulta los siguientes recursos a fin de obtener más información sobre OpenGL ES y Vulkan:
- OpenGL ES: Imágenes y gráficos en Android 
- OpenGL ES: Descripción general en la fuente de Android 
- Vulkan: Introducción al NDK 
- Vulkan: Descripción general en la fuente de Android 
- Cómo comprender los bucles de juego de Android: Aprende a establecer el ritmo de los fotogramas, a formar colas de búferes, a controlar las devoluciones de llamada de VSYNC y a administrar subprocesos. 
