3dphotoshoot

annotate src/android/amain.c @ 2:cf5964db7ff3

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