9 #include "android_native_app_glue.h"
13 static void handle_command(struct android_app *app, int32_t cmd);
14 static int handle_input(struct android_app *app, AInputEvent *ev);
15 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
16 static int init_gl(void);
17 static void destroy_gl(void);
18 static unsigned long get_time_msec(void);
20 struct android_app *app;
22 static EGLDisplay dpy;
23 static EGLSurface surf;
24 static EGLContext ctx;
25 static int init_done, paused;
27 static int width, height;
29 static long start_time;
32 void android_main(struct android_app *app_ptr)
36 app->onAppCmd = handle_command;
37 app->onInputEvent = handle_input;
43 struct android_poll_source *pollsrc;
45 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
47 pollsrc->process(app, pollsrc);
51 if(app->destroyRequested) {
54 if(init_done && !paused) {
55 time_msec = (long)get_time_msec() - start_time;
57 eglSwapBuffers(dpy, surf);
62 void swap_buffers(void)
64 eglSwapBuffers(dpy, surf);
67 static void handle_command(struct android_app *app, int32_t cmd)
73 paused = 1; /* TODO: handle timers */
81 case APP_CMD_INIT_WINDOW:
85 if(demo_init() == -1) {
88 demo_reshape(width, height);
89 start_time = (long)get_time_msec();
94 case APP_CMD_TERM_WINDOW:
100 case APP_CMD_WINDOW_RESIZED:
101 case APP_CMD_CONFIG_CHANGED:
102 xsz = ANativeWindow_getWidth(app->window);
103 ysz = ANativeWindow_getHeight(app->window);
104 if(xsz != width || ysz != height) {
105 printf("reshape(%d, %d)\n", xsz, ysz);
106 demo_reshape(xsz, ysz);
113 case APP_CMD_SAVE_STATE:
114 case APP_CMD_GAINED_FOCUS:
115 case APP_CMD_LOST_FOCUS:
122 static int handle_input(struct android_app *app, AInputEvent *ev)
124 int evtype = AInputEvent_getType(ev);
127 case AINPUT_EVENT_TYPE_MOTION:
128 return handle_touch_input(app, ev);
136 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
138 int i, pcount, x, y, idx;
140 static int prev_pos[2];
142 action = AMotionEvent_getAction(ev);
144 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
145 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
147 x = AMotionEvent_getX(ev, idx);
148 y = AMotionEvent_getY(ev, idx);
150 switch(action & AMOTION_EVENT_ACTION_MASK) {
151 case AMOTION_EVENT_ACTION_DOWN:
152 case AMOTION_EVENT_ACTION_POINTER_DOWN:
153 demo_mouse(0, 1, x, y);
159 case AMOTION_EVENT_ACTION_UP:
160 case AMOTION_EVENT_ACTION_POINTER_UP:
161 demo_mouse(0, 0, x, y);
167 case AMOTION_EVENT_ACTION_MOVE:
168 pcount = AMotionEvent_getPointerCount(ev);
169 for(i=0; i<pcount; i++) {
170 int id = AMotionEvent_getPointerId(ev, i);
187 static int init_gl(void)
189 static const int eglattr[] = {
190 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
191 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
198 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
203 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
204 if(!dpy || !eglInitialize(dpy, 0, 0)) {
205 fprintf(stderr, "failed to initialize EGL\n");
210 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
211 fprintf(stderr, "no matching EGL config found\n");
216 /* configure the native window visual according to the chosen EGL config */
217 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
218 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
220 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
221 fprintf(stderr, "failed to create window\n");
226 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
227 fprintf(stderr, "failed to create OpenGL ES context\n");
231 eglMakeCurrent(dpy, surf, surf, ctx);
233 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
234 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
238 static void destroy_gl(void)
242 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
245 eglDestroyContext(dpy, ctx);
249 eglDestroySurface(dpy, surf);
257 static unsigned long get_time_msec(void)
260 static struct timespec ts0;
262 clock_gettime(CLOCK_MONOTONIC, &ts);
263 if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) {
267 return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000;