X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fandroid%2Fmain.c;h=2ec85b6a4ed241362a7adb63bbd6f7bc0594a390;hb=71cf764907df2869a2ac33cbf19aff5710760e23;hp=7b971d59ca582e0fb7245e797e48f884b8e74643;hpb=aa9f7c423c8c4111b3e1d0a7bbe73e376533ed80;p=andemo diff --git a/src/android/main.c b/src/android/main.c index 7b971d5..2ec85b6 100644 --- a/src/android/main.c +++ b/src/android/main.c @@ -1,17 +1,28 @@ +#include +#include #include #include #include "demo.h" #include "android_native_app_glue.h" +static void handle_command(struct android_app *app, int32_t cmd); +static int handle_input(struct android_app *app, AInputEvent *ev); +static int handle_touch_input(struct android_app *app, AInputEvent *ev); +static int init_gl(void); +static void destroy_gl(void); + static struct android_app *app; static EGLDisplay dpy; static EGLSurface surf; static EGLContext ctx; +static int running; + +static int width, height; + void android_main(struct android_app *app_ptr) { - app_dummy(); app = app_ptr; app->onAppCmd = handle_command; @@ -23,16 +34,202 @@ void android_main(struct android_app *app_ptr) while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) { if(pollsrc) { - pollsrc->process(ap, pollsrc); + pollsrc->process(app, pollsrc); } } if(app->destroyRequested) { return; } - if(!paused) { + if(running) { demo_display(); eglSwapBuffers(dpy, surf); } } } + +static void handle_command(struct android_app *app, int32_t cmd) +{ + int xsz, ysz; + + switch(cmd) { + case APP_CMD_PAUSE: + running = 0; /* TODO: handle timers */ + break; + case APP_CMD_RESUME: + running = 1; + break; + + case APP_CMD_INIT_WINDOW: + if(init_gl() == -1) { + exit(1); + } + if(demo_init() == -1) { + exit(1); + } + demo_reshape(width, height); + running = 1; + break; + + case APP_CMD_TERM_WINDOW: + running = 0; + demo_cleanup(); + destroy_gl(); + break; + + case APP_CMD_WINDOW_RESIZED: + case APP_CMD_CONFIG_CHANGED: + xsz = ANativeWindow_getWidth(app->window); + ysz = ANativeWindow_getHeight(app->window); + if(xsz != width || ysz != height) { + printf("reshape(%d, %d)\n", xsz, ysz); + demo_reshape(xsz, ysz); + width = xsz; + height = ysz; + } + break; + + /* + case APP_CMD_SAVE_STATE: + case APP_CMD_GAINED_FOCUS: + case APP_CMD_LOST_FOCUS: + */ + default: + break; + } +} + +static int handle_input(struct android_app *app, AInputEvent *ev) +{ + int evtype = AInputEvent_getType(ev); + + switch(evtype) { + case AINPUT_EVENT_TYPE_MOTION: + return handle_touch_input(app, ev); + + default: + break; + } + return 0; +} + +static int handle_touch_input(struct android_app *app, AInputEvent *ev) +{ + int i, pcount, x, y, idx; + unsigned int action; + static int prev_pos[2]; + + action = AMotionEvent_getAction(ev); + + idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> + AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; + + x = AMotionEvent_getX(ev, idx); + y = AMotionEvent_getY(ev, idx); + + switch(action & AMOTION_EVENT_ACTION_MASK) { + case AMOTION_EVENT_ACTION_DOWN: + case AMOTION_EVENT_ACTION_POINTER_DOWN: + demo_mouse(0, 1, x, y); + + prev_pos[0] = x; + prev_pos[1] = y; + break; + + case AMOTION_EVENT_ACTION_UP: + case AMOTION_EVENT_ACTION_POINTER_UP: + demo_mouse(0, 0, x, y); + + prev_pos[0] = x; + prev_pos[1] = y; + break; + + case AMOTION_EVENT_ACTION_MOVE: + pcount = AMotionEvent_getPointerCount(ev); + for(i=0; iwindow, 0, 0, vis); + + if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) { + fprintf(stderr, "failed to create window\n"); + destroy_gl(); + return -1; + } + + if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) { + fprintf(stderr, "failed to create OpenGL ES context\n"); + destroy_gl(); + return -1; + } + eglMakeCurrent(dpy, surf, surf, ctx); + + eglQuerySurface(dpy, surf, EGL_WIDTH, &width); + eglQuerySurface(dpy, surf, EGL_HEIGHT, &height); + return 0; +} + +static void destroy_gl(void) +{ + if(!dpy) return; + + eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + + if(ctx) { + eglDestroyContext(dpy, ctx); + ctx = 0; + } + if(surf) { + eglDestroySurface(dpy, surf); + surf = 0; + } + + eglTerminate(dpy); + dpy = 0; +}