ecere: Fixes to build branch for Android
[sdk] / ecere / src / gfx / drivers / gl3 / egl.ec
1 #if defined(__ANDROID__) || defined(__ODROID__)
2
3 import "instance"
4
5 #include <EGL/egl.h>
6
7 EGLDisplay eglDisplay;
8 EGLSurface eglSurface;
9 EGLContext eglContext;
10 int eglWidth, eglHeight;
11
12 #if defined(__ANDROID__)
13 bool egl_init_display(ANativeWindow* window)
14 #else
15 bool egl_init_display(uint window)
16 #endif
17 {
18    const EGLint attribs[] =
19    {
20       EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
21       EGL_BLUE_SIZE, 8,
22       EGL_GREEN_SIZE, 8,
23       EGL_RED_SIZE, 8,
24       EGL_DEPTH_SIZE, 16, //24,
25       /*EGL_SAMPLE_BUFFERS, 1,
26       EGL_SAMPLES, 0, //2,*/
27       EGL_NONE
28    };
29    EGLint w, h, format;
30    EGLint numConfigs;
31    EGLConfig config;
32    EGLSurface surface;
33    EGLContext context;
34
35    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
36    eglInitialize(display, 0, 0);
37    eglChooseConfig(display, attribs, &config, 1, &numConfigs);
38    eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
39
40    surface = eglCreateWindowSurface(display, config, window, null);
41    context = eglCreateContext(display, config, null, null);
42
43    if(!eglMakeCurrent(display, surface, surface, context))
44       return false;
45
46    eglQuerySurface(display, surface, EGL_WIDTH, &w);
47    eglQuerySurface(display, surface, EGL_HEIGHT, &h);
48
49    eglDisplay = display;
50    eglContext = context;
51    eglSurface = surface;
52    eglWidth = w;
53    eglHeight = h;
54
55    return true;
56 }
57
58 void egl_term_display()
59 {
60    if(eglDisplay != EGL_NO_DISPLAY)
61    {
62       eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
63       if(eglContext != EGL_NO_CONTEXT)
64          eglDestroyContext(eglDisplay, eglContext);
65       if(eglSurface != EGL_NO_SURFACE)
66          eglDestroySurface(eglDisplay, eglSurface);
67       eglTerminate(eglDisplay);
68    }
69    eglDisplay = EGL_NO_DISPLAY;
70    eglContext = EGL_NO_CONTEXT;
71    eglSurface = EGL_NO_SURFACE;
72 }
73
74 void egl_swap_buffers()
75 {
76    eglSwapBuffers(eglDisplay, eglSurface);
77 }
78
79 #endif