Index: samples/android_sample/jni/graphics.cc |
=================================================================== |
--- samples/android_sample/jni/graphics.cc (revision 0) |
+++ samples/android_sample/jni/graphics.cc (revision 0) |
@@ -0,0 +1,102 @@ |
+#include "graphics.h" |
+#include <GLES2/gl2.h> |
+#include <GLES2/gl2ext.h> |
+ |
+Graphics::Graphics(android_app* pApplication, Timer* pTimer) : |
+ mApplication(pApplication), |
+ mTimer(pTimer), |
+ mWidth(0), |
+ mHeight(0), |
+ mDisplay(EGL_NO_DISPLAY), |
+ mSurface(EGL_NO_SURFACE), |
+ mContext(EGL_NO_CONTEXT) { |
+} |
+ |
+const int32_t& Graphics::getHeight() { |
+ return mHeight; |
+} |
+ |
+const int32_t& Graphics::getWidth() { |
+ return mWidth; |
+} |
+ |
+int32_t Graphics::start() { |
+ EGLint format, numConfigs, errorResult; |
+ EGLConfig config; |
+ const EGLint attributes[] = { |
+ EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
+ //EGL_BLUE_SIZE, 5, |
+ //EGL_GREEN_SIZE, 6, |
+ //EGL_RED_SIZE, 5, |
+ //EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
+ EGL_NONE |
+ }; |
+ static const EGLint ctx_attribs[] = { |
+ EGL_CONTEXT_CLIENT_VERSION, 2, |
+ EGL_NONE |
+ }; |
+ |
+ mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
+ if (mDisplay != EGL_NO_DISPLAY) { |
+ Log::Print("eglInitialize"); |
+ if (eglInitialize(mDisplay, NULL, NULL)) { |
+ Log::Print("eglChooseConfig"); |
+ if (eglChooseConfig(mDisplay, attributes, &config, 1, &numConfigs) && |
+ numConfigs > 0) { |
+ Log::Print("eglGetConfigAttrib"); |
+ if (eglGetConfigAttrib(mDisplay, config, |
+ EGL_NATIVE_VISUAL_ID, &format)) { |
+ ANativeWindow_setBuffersGeometry(mApplication->window, 0, 0, format); |
+ mSurface = eglCreateWindowSurface(mDisplay, config, |
+ (EGLNativeWindowType)mApplication->window, NULL); |
+ if (mSurface != EGL_NO_SURFACE) { |
+ Log::Print("eglCreateContext"); |
+ mContext = eglCreateContext(mDisplay, config, EGL_NO_CONTEXT, |
+ ctx_attribs); |
+ if (mContext != EGL_NO_CONTEXT) { |
+ if (eglMakeCurrent(mDisplay, mSurface, mSurface, mContext) && |
+ eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth) && |
+ mWidth > 0 && |
+ eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight) && |
+ mHeight > 0) { |
+ glViewport(0, 0, mWidth, mHeight); |
+ return 0; |
+ } |
+ } |
+ } |
+ } |
+ } |
+ } |
+ } |
+ Log::PrintErr("Error starting graphics"); |
+ stop(); |
+ return -1; |
+} |
+ |
+void Graphics::stop() { |
+ Log::Print("Stopping graphics"); |
+ if (mDisplay != EGL_NO_DISPLAY) { |
+ eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
+ if (mContext != EGL_NO_CONTEXT) { |
+ eglDestroyContext(mDisplay, mContext); |
+ mContext = EGL_NO_CONTEXT; |
+ } |
+ if (mSurface != EGL_NO_SURFACE) { |
+ eglDestroySurface(mDisplay, mSurface); |
+ mSurface = EGL_NO_SURFACE; |
+ } |
+ eglTerminate(mDisplay); |
+ mDisplay = EGL_NO_DISPLAY; |
+ } |
+} |
+ |
+int32_t Graphics::update() { |
+ return 0; |
+ /* |
+ if (eglSwapBuffers(mDisplay, mSurface) != EGL_TRUE) { |
+ Log::PrintErr("Error %d swapping buffers.", eglGetError()); |
+ return -1; |
+ } |
+ return 0; |
+ */ |
+} |