3dphotoshoot

annotate src/android/amain.c @ 6:e31e23ead56f

GAMO TO XRISTO KAI TIN PANAGIA TOU ANDROID
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 May 2015 19:03:00 +0300
parents 31cc6615428d
children 9fc7d52f578d
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@5 3 #include <assert.h>
nuclear@0 4 #include <EGL/egl.h>
nuclear@1 5 #include <jni.h>
nuclear@1 6 #include "native_glue.h"
nuclear@0 7 #include "logger.h"
nuclear@0 8 #include "game.h"
nuclear@6 9 #include "camera.h"
nuclear@0 10 #include "timer.h"
nuclear@0 11
nuclear@0 12
nuclear@0 13 static void handle_command(struct android_app *app, int32_t cmd);
nuclear@0 14 static int handle_input(struct android_app *app, AInputEvent *ev);
nuclear@0 15 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
nuclear@0 16 static int init_gl(void);
nuclear@0 17 static void destroy_gl(void);
nuclear@0 18
nuclear@0 19 static EGLDisplay dpy;
nuclear@0 20 static EGLSurface surf;
nuclear@0 21 static EGLContext ctx;
nuclear@0 22
nuclear@0 23 static struct android_app *app;
nuclear@0 24 static int win_width, win_height;
nuclear@0 25 static int init_done;
nuclear@0 26
nuclear@1 27 static JavaVM *jvm;
nuclear@1 28 static JNIEnv *jni;
nuclear@1 29 static jclass activity_class;
nuclear@1 30
nuclear@5 31 static unsigned int camtex;
nuclear@5 32
nuclear@1 33
nuclear@1 34
nuclear@0 35 void android_main(struct android_app *app_ptr)
nuclear@0 36 {
nuclear@0 37 app_dummy();
nuclear@0 38 app = app_ptr;
nuclear@0 39
nuclear@0 40 app->onAppCmd = handle_command;
nuclear@0 41 app->onInputEvent = handle_input;
nuclear@0 42
nuclear@0 43 start_logger();
nuclear@0 44
nuclear@1 45 jvm = app->activity->vm;
nuclear@1 46 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
nuclear@1 47 fprintf(stderr, "failed to attach native thread to Java VM\n");
nuclear@1 48 exit(1);
nuclear@1 49 }
nuclear@2 50 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
nuclear@1 51
nuclear@0 52 for(;;) {
nuclear@0 53 int num_events;
nuclear@0 54 struct android_poll_source *pollsrc;
nuclear@0 55
nuclear@0 56 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
nuclear@0 57 if(pollsrc) {
nuclear@0 58 pollsrc->process(app, pollsrc);
nuclear@0 59 }
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 if(app->destroyRequested) {
nuclear@1 63 (*jvm)->DetachCurrentThread(jvm);
nuclear@0 64 return;
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 if(init_done) {
nuclear@0 68 game_display(get_time_msec());
nuclear@0 69 eglSwapBuffers(dpy, surf);
nuclear@0 70 }
nuclear@0 71 }
nuclear@0 72 }
nuclear@0 73
nuclear@0 74 void set_mouse_pos(int x, int y)
nuclear@0 75 {
nuclear@0 76 }
nuclear@0 77
nuclear@0 78 void set_mouse_cursor(int enable)
nuclear@0 79 {
nuclear@0 80 }
nuclear@0 81
nuclear@0 82 static void handle_command(struct android_app *app, int32_t cmd)
nuclear@0 83 {
nuclear@6 84 struct cam_android_platform_data cam_data;
nuclear@6 85
nuclear@0 86 switch(cmd) {
nuclear@0 87 case APP_CMD_SAVE_STATE:
nuclear@0 88 /* save the application state to be reloaded on restart if needed */
nuclear@0 89 break;
nuclear@0 90
nuclear@0 91 case APP_CMD_INIT_WINDOW:
nuclear@0 92 if(init_gl() == -1) {
nuclear@0 93 exit(1);
nuclear@0 94 }
nuclear@5 95
nuclear@6 96 cam_data.vm = jvm;
nuclear@6 97 cam_data.jni = jni;
nuclear@6 98 cam_data.activity_class = activity_class;
nuclear@6 99
nuclear@6 100 if(cam_init(&cam_data) == -1) {
nuclear@6 101 exit(1);
nuclear@6 102 }
nuclear@6 103 cam_start_video();
nuclear@5 104
nuclear@0 105 /* initialize the application */
nuclear@0 106 if(game_init() == -1) {
nuclear@0 107 exit(1); /* initialization failed, quit */
nuclear@0 108 }
nuclear@0 109 init_done = 1;
nuclear@0 110 break;
nuclear@0 111
nuclear@0 112 case APP_CMD_TERM_WINDOW:
nuclear@0 113 /* cleanup */
nuclear@0 114 init_done = 0;
nuclear@0 115 game_shutdown();
nuclear@6 116 cam_shutdown();
nuclear@0 117 destroy_gl();
nuclear@0 118 break;
nuclear@0 119
nuclear@0 120 case APP_CMD_GAINED_FOCUS:
nuclear@0 121 /* app focused */
nuclear@6 122 cam_start_video();
nuclear@0 123 break;
nuclear@0 124
nuclear@0 125 case APP_CMD_LOST_FOCUS:
nuclear@0 126 /* app lost focus */
nuclear@6 127 cam_stop_video();
nuclear@0 128 break;
nuclear@0 129
nuclear@0 130 case APP_CMD_WINDOW_RESIZED:
nuclear@0 131 case APP_CMD_CONFIG_CHANGED:
nuclear@0 132 {
nuclear@0 133 int nx = ANativeWindow_getWidth(app->window);
nuclear@0 134 int ny = ANativeWindow_getHeight(app->window);
nuclear@0 135 if(nx != win_width || ny != win_height) {
nuclear@0 136 game_reshape(nx, ny);
nuclear@0 137 win_width = nx;
nuclear@0 138 win_height = ny;
nuclear@0 139 }
nuclear@0 140 }
nuclear@0 141 break;
nuclear@0 142
nuclear@0 143 default:
nuclear@0 144 break;
nuclear@0 145 }
nuclear@0 146 }
nuclear@0 147
nuclear@0 148 static int handle_input(struct android_app *app, AInputEvent *ev)
nuclear@0 149 {
nuclear@0 150 int evtype = AInputEvent_getType(ev);
nuclear@0 151
nuclear@0 152 switch(evtype) {
nuclear@0 153 case AINPUT_EVENT_TYPE_MOTION:
nuclear@0 154 return handle_touch_input(app, ev);
nuclear@0 155
nuclear@0 156 default:
nuclear@0 157 break;
nuclear@0 158 }
nuclear@0 159 return 0;
nuclear@0 160 }
nuclear@0 161
nuclear@0 162 #define MAX_TOUCH_IDS 32
nuclear@0 163
nuclear@0 164 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
nuclear@0 165 {
nuclear@0 166 int x, y, idx, touch_id;
nuclear@0 167 unsigned int action;
nuclear@0 168 static int prev_pos[MAX_TOUCH_IDS][2];
nuclear@0 169
nuclear@0 170 action = AMotionEvent_getAction(ev);
nuclear@0 171
nuclear@0 172 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
nuclear@0 173 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
nuclear@0 174 touch_id = AMotionEvent_getPointerId(ev, idx);
nuclear@0 175
nuclear@0 176 x = AMotionEvent_getX(ev, idx);
nuclear@0 177 y = AMotionEvent_getY(ev, idx);
nuclear@0 178
nuclear@0 179 switch(action & AMOTION_EVENT_ACTION_MASK) {
nuclear@0 180 case AMOTION_EVENT_ACTION_DOWN:
nuclear@0 181 case AMOTION_EVENT_ACTION_POINTER_DOWN:
nuclear@0 182 game_mouse_button(touch_id, 0, 1, x, y);
nuclear@0 183 if(touch_id < MAX_TOUCH_IDS) {
nuclear@0 184 prev_pos[touch_id][0] = x;
nuclear@0 185 prev_pos[touch_id][1] = y;
nuclear@0 186 }
nuclear@0 187 break;
nuclear@0 188
nuclear@0 189 case AMOTION_EVENT_ACTION_UP:
nuclear@0 190 case AMOTION_EVENT_ACTION_POINTER_UP:
nuclear@0 191 game_mouse_button(touch_id, 0, 0, x, y);
nuclear@0 192 if(touch_id < MAX_TOUCH_IDS) {
nuclear@0 193 prev_pos[touch_id][0] = x;
nuclear@0 194 prev_pos[touch_id][1] = y;
nuclear@0 195 }
nuclear@0 196 break;
nuclear@0 197
nuclear@0 198 case AMOTION_EVENT_ACTION_MOVE:
nuclear@0 199 {
nuclear@0 200 int i, pcount = AMotionEvent_getPointerCount(ev);
nuclear@0 201 for(i=0; i<pcount; i++) {
nuclear@0 202 int id = AMotionEvent_getPointerId(ev, i);
nuclear@0 203 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
nuclear@0 204 game_mouse_motion(id, x, y);
nuclear@0 205 prev_pos[id][0] = x;
nuclear@0 206 prev_pos[id][1] = y;
nuclear@0 207 }
nuclear@0 208 }
nuclear@0 209 }
nuclear@0 210 break;
nuclear@0 211
nuclear@0 212 default:
nuclear@0 213 break;
nuclear@0 214 }
nuclear@0 215
nuclear@0 216 return 1;
nuclear@0 217 }
nuclear@0 218
nuclear@0 219
nuclear@0 220 static int init_gl(void)
nuclear@0 221 {
nuclear@0 222 static const int eglattr[] = {
nuclear@0 223 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
nuclear@0 224 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
nuclear@0 225 EGL_RED_SIZE, 5,
nuclear@0 226 EGL_GREEN_SIZE, 5,
nuclear@0 227 EGL_BLUE_SIZE, 5,
nuclear@0 228 EGL_DEPTH_SIZE, 16,
nuclear@0 229 EGL_NONE
nuclear@0 230 };
nuclear@0 231 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE };
nuclear@0 232
nuclear@0 233 EGLConfig eglcfg;
nuclear@0 234 int count, vis;
nuclear@0 235
nuclear@0 236 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
nuclear@0 237 if(!dpy || !eglInitialize(dpy, 0, 0)) {
nuclear@0 238 fprintf(stderr, "failed to initialize EGL\n");
nuclear@0 239 destroy_gl();
nuclear@0 240 return -1;
nuclear@0 241 }
nuclear@0 242
nuclear@0 243 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
nuclear@0 244 fprintf(stderr, "no matching EGL config found\n");
nuclear@0 245 destroy_gl();
nuclear@0 246 return -1;
nuclear@0 247 }
nuclear@0 248
nuclear@0 249 /* configure the native window visual according to the chosen EGL config */
nuclear@0 250 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
nuclear@0 251 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
nuclear@0 252
nuclear@0 253 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
nuclear@0 254 fprintf(stderr, "failed to create window\n");
nuclear@0 255 destroy_gl();
nuclear@0 256 return -1;
nuclear@0 257 }
nuclear@0 258
nuclear@0 259 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
nuclear@0 260 fprintf(stderr, "failed to create OpenGL ES context\n");
nuclear@0 261 destroy_gl();
nuclear@0 262 return -1;
nuclear@0 263 }
nuclear@0 264 eglMakeCurrent(dpy, surf, surf, ctx);
nuclear@0 265
nuclear@0 266 eglQuerySurface(dpy, surf, EGL_WIDTH, &win_width);
nuclear@0 267 eglQuerySurface(dpy, surf, EGL_HEIGHT, &win_height);
nuclear@0 268 game_reshape(win_width, win_height);
nuclear@0 269
nuclear@0 270 return 0;
nuclear@0 271 }
nuclear@0 272
nuclear@0 273 static void destroy_gl(void)
nuclear@0 274 {
nuclear@0 275 if(!dpy) return;
nuclear@0 276
nuclear@0 277 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
nuclear@0 278
nuclear@0 279 if(ctx) {
nuclear@0 280 eglDestroyContext(dpy, ctx);
nuclear@0 281 ctx = 0;
nuclear@0 282 }
nuclear@0 283 if(surf) {
nuclear@0 284 eglDestroySurface(dpy, surf);
nuclear@0 285 surf = 0;
nuclear@0 286 }
nuclear@0 287
nuclear@0 288 eglTerminate(dpy);
nuclear@0 289 dpy = 0;
nuclear@0 290 }