OLD | NEW |
(Empty) | |
| 1 #include "jni/graphics.h" |
| 2 #include <GLES2/gl2.h> |
| 3 #include <GLES2/gl2ext.h> |
| 4 |
| 5 Graphics::Graphics(android_app* application, Timer* timer) |
| 6 : application_(application), |
| 7 timer_(timer), |
| 8 width_(0), |
| 9 height_(0), |
| 10 display_(EGL_NO_DISPLAY), |
| 11 surface_(EGL_NO_SURFACE), |
| 12 context_(EGL_NO_CONTEXT) { |
| 13 } |
| 14 |
| 15 const int32_t& Graphics::height() { |
| 16 return height_; |
| 17 } |
| 18 |
| 19 const int32_t& Graphics::width() { |
| 20 return width_; |
| 21 } |
| 22 |
| 23 int32_t Graphics::Start() { |
| 24 EGLint format, numConfigs, errorResult; |
| 25 EGLConfig config; |
| 26 const EGLint attributes[] = { |
| 27 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 28 EGL_NONE |
| 29 }; |
| 30 static const EGLint ctx_attribs[] = { |
| 31 EGL_CONTEXT_CLIENT_VERSION, 2, |
| 32 EGL_NONE |
| 33 }; |
| 34 |
| 35 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 36 if (display_ != EGL_NO_DISPLAY) { |
| 37 Log::Print("eglInitialize"); |
| 38 if (eglInitialize(display_, NULL, NULL)) { |
| 39 Log::Print("eglChooseConfig"); |
| 40 if (eglChooseConfig(display_, attributes, &config, 1, &numConfigs) && |
| 41 numConfigs > 0) { |
| 42 Log::Print("eglGetConfigAttrib"); |
| 43 if (eglGetConfigAttrib(display_, config, |
| 44 EGL_NATIVE_VISUAL_ID, &format)) { |
| 45 ANativeWindow_setBuffersGeometry(application_->window, 0, 0, format); |
| 46 surface_ = eglCreateWindowSurface(display_, config, |
| 47 (EGLNativeWindowType)application_->window, NULL); |
| 48 if (surface_ != EGL_NO_SURFACE) { |
| 49 Log::Print("eglCreateContext"); |
| 50 context_ = eglCreateContext(display_, config, EGL_NO_CONTEXT, |
| 51 ctx_attribs); |
| 52 if (context_ != EGL_NO_CONTEXT) { |
| 53 if (eglMakeCurrent(display_, surface_, surface_, context_) && |
| 54 eglQuerySurface(display_, surface_, EGL_WIDTH, &width_) && |
| 55 width_ > 0 && |
| 56 eglQuerySurface(display_, surface_, EGL_HEIGHT, &height_) && |
| 57 height_ > 0) { |
| 58 glViewport(0, 0, width_, height_); |
| 59 return 0; |
| 60 } |
| 61 } |
| 62 } |
| 63 } |
| 64 } |
| 65 } |
| 66 } |
| 67 Log::PrintErr("Error starting graphics"); |
| 68 Stop(); |
| 69 return -1; |
| 70 } |
| 71 |
| 72 void Graphics::Stop() { |
| 73 Log::Print("Stopping graphics"); |
| 74 if (display_ != EGL_NO_DISPLAY) { |
| 75 eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 76 if (context_ != EGL_NO_CONTEXT) { |
| 77 eglDestroyContext(display_, context_); |
| 78 context_ = EGL_NO_CONTEXT; |
| 79 } |
| 80 if (surface_ != EGL_NO_SURFACE) { |
| 81 eglDestroySurface(display_, surface_); |
| 82 surface_ = EGL_NO_SURFACE; |
| 83 } |
| 84 eglTerminate(display_); |
| 85 display_ = EGL_NO_DISPLAY; |
| 86 } |
| 87 } |
| 88 |
| 89 int32_t Graphics::Update() { |
| 90 return 0; |
| 91 } |
| 92 |
OLD | NEW |