3dphotoshoot
view src/android/amain.c @ 25:ac80210d5fbe
preparing a pc version for easier development of non-android-specifics
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 18 Jun 2015 03:12:30 +0300 |
parents | 2712c5da2e00 |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <EGL/egl.h>
5 #include <jni.h>
6 #include "amain.h"
7 #include "native_glue.h"
8 #include <android/sensor.h>
9 #include "sensordef.h"
10 #include "logger.h"
11 #include "game.h"
12 #include "camera.h"
13 #include "timer.h"
15 #define EVID_SENS_ACCEL (LOOPER_ID_USER + 0)
16 #define EVID_SENS_ROT (LOOPER_ID_USER + 1)
18 static void handle_command(struct android_app *app, int32_t cmd);
19 static int handle_input(struct android_app *app, AInputEvent *ev);
20 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
21 static int init_gl(void);
22 static void destroy_gl(void);
24 static EGLDisplay dpy;
25 static EGLSurface surf;
26 static EGLContext ctx;
28 static int redisp_pending = 1; /* TODO stop busy-looping */
30 static int width, height;
31 static int init_done;
33 static JavaVM *jvm;
34 static JNIEnv *jni;
35 static jclass activity_class;
37 static ASensorManager *sensorman;
38 static ASensor const *sens_accel;
39 static ASensor const *sens_rot;
40 static ASensorEventQueue *sens_evq_accel;
41 static ASensorEventQueue *sens_evq_rot;
43 void android_main(struct android_app *app_ptr)
44 {
45 app_dummy();
46 app = app_ptr;
48 app->onAppCmd = handle_command;
49 app->onInputEvent = handle_input;
51 start_logger();
53 jvm = app->activity->vm;
54 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
55 fprintf(stderr, "failed to attach native thread to Java VM\n");
56 exit(1);
57 }
58 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
60 {
61 int i, num_sensors;
62 ASensorList sensors;
64 sensorman = ASensorManager_getInstance();
65 num_sensors = ASensorManager_getSensorList(sensorman, &sensors);
66 printf("found %d sensors:\n", num_sensors);
68 for(i=0; i<num_sensors; i++) {
69 ASensor const *sens = sensors[i];
70 int type = ASensor_getType(sens);
71 float res = ASensor_getResolution(sens);
72 int mindel = ASensor_getMinDelay(sens);
74 printf(" %d) [%d:%s] %s - %s (res: %g, min-delay: %d)\n", i,
75 type, sensor_typestr(type), ASensor_getName(sens),
76 ASensor_getVendor(sens), res, mindel);
77 }
79 // try to get a linear acceleration sensor
80 printf("Requesting \"linear acceleration\" sensor ...\n");
81 if(!(sens_accel = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_LINEAR_ACCELERATION))) {
82 fprintf(stderr, "No linear acceleration sensor found, aborting!\n");
83 return;
84 }
86 // try to get a game rotation vector sensor
87 printf("Requesting \"game rotation vector\" sensor ...\n");
88 if(!(sens_rot = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_GAME_ROTATION_VECTOR))) {
89 printf("Requesting \"rotation vector\" sensor ...\n");
90 if(!(sens_rot = ASensorManager_getDefaultSensor(sensorman, SENSOR_TYPE_ROTATION_VECTOR))) {
91 fprintf(stderr, "No rotation sensor found, aborting!\n");
92 return;
93 }
94 }
96 if(!(sens_evq_accel = ASensorManager_createEventQueue(sensorman, app->looper,
97 EVID_SENS_ACCEL, 0, 0))) {
98 fprintf(stderr, "failed to create acceleration sensor event queue\n");
99 return;
100 }
102 if(!(sens_evq_rot = ASensorManager_createEventQueue(sensorman, app->looper,
103 EVID_SENS_ROT, 0, 0))) {
104 fprintf(stderr, "failed to create rotation sensor event queue\n");
105 return;
106 }
107 }
110 for(;;) {
111 int num_events, evid;
112 struct android_poll_source *pollsrc;
113 ASensorEvent sens_ev;
115 while((evid = ALooper_pollAll(redisp_pending ? 0 : -1, 0, &num_events, (void**)&pollsrc)) >= 0) {
117 switch(evid) {
118 case EVID_SENS_ACCEL:
119 if(sens_accel) {
120 static double prev_sec = -1.0;
121 double sec, dt;
122 double accel[3] = {0, 0, 0};
123 static double velocity[3];
125 while(ASensorEventQueue_getEvents(sens_evq_accel, &sens_ev, 1) > 0) {
126 accel[0] += sens_ev.acceleration.x;
127 accel[1] += sens_ev.acceleration.y;
128 accel[2] += sens_ev.acceleration.z;
129 }
131 sec = get_time_sec();
132 dt = prev_sec >= 0.0 ? sec - prev_sec : 0.0;
133 prev_sec = sec;
135 velocity[0] += accel[0] * dt;
136 velocity[1] += accel[1] * dt;
137 velocity[2] += accel[2] * dt;
139 game_6dof_translation(velocity[0] * dt, velocity[1] * dt, velocity[2] * dt);
140 }
141 break;
143 case EVID_SENS_ROT:
144 if(sens_rot) {
145 float qrot[4];
146 while(ASensorEventQueue_getEvents(sens_evq_rot, &sens_ev, 1) > 0) {
147 int i;
148 for(i=0; i<4; i++) {
149 qrot[i] = sens_ev.data[i];
150 }
151 }
153 game_6dof_rotation(qrot[0], qrot[1], qrot[2], qrot[3]);
154 }
155 break;
157 default:
158 if(pollsrc) {
159 pollsrc->process(app, pollsrc);
160 }
161 }
162 }
164 if(app->destroyRequested) {
165 (*jvm)->DetachCurrentThread(jvm);
166 return;
167 }
169 if(init_done && redisp_pending) {
170 game_display(get_time_msec());
171 eglSwapBuffers(dpy, surf);
172 }
173 }
174 }
176 void set_mouse_pos(int x, int y)
177 {
178 }
180 void set_mouse_cursor(int enable)
181 {
182 }
184 static void handle_command(struct android_app *app, int32_t cmd)
185 {
186 struct cam_android_platform_data cam_data;
188 switch(cmd) {
189 case APP_CMD_SAVE_STATE:
190 /* save the application state to be reloaded on restart if needed */
191 break;
193 case APP_CMD_INIT_WINDOW:
194 if(init_gl() == -1) {
195 exit(1);
196 }
198 cam_data.vm = jvm;
199 cam_data.jni = jni;
200 cam_data.activity_class = activity_class;
202 if(cam_init(&cam_data) == -1) {
203 exit(1);
204 }
206 /* initialize the application */
207 if(game_init() == -1) {
208 exit(1); /* initialization failed, quit */
209 }
210 init_done = 1;
211 break;
213 case APP_CMD_TERM_WINDOW:
214 /* cleanup */
215 init_done = 0;
216 game_shutdown();
217 destroy_gl();
218 break;
220 case APP_CMD_GAINED_FOCUS:
221 /* app focused */
222 printf("Command: APP_CMD_GAINED_FOCUS\n");
223 if(sens_accel) {
224 ASensorEventQueue_enableSensor(sens_evq_accel, sens_accel);
225 ASensorEventQueue_setEventRate(sens_evq_accel, sens_accel, 1000000 / 60);
226 }
227 if(sens_rot) {
228 ASensorEventQueue_enableSensor(sens_evq_rot, sens_rot);
229 ASensorEventQueue_setEventRate(sens_evq_rot, sens_rot, 1000000 / 60);
230 }
231 //cam_start_video();
232 break;
234 case APP_CMD_LOST_FOCUS:
235 /* app lost focus */
236 printf("Command: APP_CMD_LOST_FOCUS\n");
237 if(sens_accel) {
238 ASensorEventQueue_disableSensor(sens_evq_accel, sens_accel);
239 }
240 if(sens_rot) {
241 ASensorEventQueue_disableSensor(sens_evq_rot, sens_rot);
242 }
243 //cam_stop_video();
244 break;
246 case APP_CMD_WINDOW_RESIZED:
247 case APP_CMD_CONFIG_CHANGED:
248 {
249 int nx = ANativeWindow_getWidth(app->window);
250 int ny = ANativeWindow_getHeight(app->window);
251 if(nx != width || ny != height) {
252 game_reshape(nx, ny);
253 width = nx;
254 height = ny;
255 }
256 }
257 break;
259 default:
260 break;
261 }
262 }
264 static int handle_input(struct android_app *app, AInputEvent *ev)
265 {
266 int evtype = AInputEvent_getType(ev);
268 switch(evtype) {
269 case AINPUT_EVENT_TYPE_MOTION:
270 return handle_touch_input(app, ev);
272 default:
273 break;
274 }
275 return 0;
276 }
278 #define MAX_TOUCH_IDS 32
280 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
281 {
282 int x, y, idx, touch_id;
283 unsigned int action;
284 static int prev_pos[MAX_TOUCH_IDS][2];
286 action = AMotionEvent_getAction(ev);
288 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
289 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
290 touch_id = AMotionEvent_getPointerId(ev, idx);
292 x = AMotionEvent_getX(ev, idx);
293 y = AMotionEvent_getY(ev, idx);
295 switch(action & AMOTION_EVENT_ACTION_MASK) {
296 case AMOTION_EVENT_ACTION_DOWN:
297 case AMOTION_EVENT_ACTION_POINTER_DOWN:
298 game_mouse_button(touch_id, 0, 1, x, y);
299 if(touch_id < MAX_TOUCH_IDS) {
300 prev_pos[touch_id][0] = x;
301 prev_pos[touch_id][1] = y;
302 }
303 break;
305 case AMOTION_EVENT_ACTION_UP:
306 case AMOTION_EVENT_ACTION_POINTER_UP:
307 game_mouse_button(touch_id, 0, 0, x, y);
308 if(touch_id < MAX_TOUCH_IDS) {
309 prev_pos[touch_id][0] = x;
310 prev_pos[touch_id][1] = y;
311 }
312 break;
314 case AMOTION_EVENT_ACTION_MOVE:
315 {
316 int i, pcount = AMotionEvent_getPointerCount(ev);
317 for(i=0; i<pcount; i++) {
318 int id = AMotionEvent_getPointerId(ev, i);
319 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
320 game_mouse_motion(id, x, y);
321 prev_pos[id][0] = x;
322 prev_pos[id][1] = y;
323 }
324 }
325 }
326 break;
328 default:
329 break;
330 }
332 return 1;
333 }
336 static int init_gl(void)
337 {
338 static const int eglattr[] = {
339 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
340 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
341 EGL_RED_SIZE, 5,
342 EGL_GREEN_SIZE, 5,
343 EGL_BLUE_SIZE, 5,
344 EGL_DEPTH_SIZE, 16,
345 EGL_NONE
346 };
347 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
349 EGLConfig eglcfg;
350 int count, vis;
352 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
353 if(!dpy || !eglInitialize(dpy, 0, 0)) {
354 fprintf(stderr, "failed to initialize EGL\n");
355 destroy_gl();
356 return -1;
357 }
359 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
360 fprintf(stderr, "no matching EGL config found\n");
361 destroy_gl();
362 return -1;
363 }
365 /* configure the native window visual according to the chosen EGL config */
366 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
367 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
369 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
370 fprintf(stderr, "failed to create window\n");
371 destroy_gl();
372 return -1;
373 }
375 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
376 fprintf(stderr, "failed to create OpenGL ES context\n");
377 destroy_gl();
378 return -1;
379 }
380 eglMakeCurrent(dpy, surf, surf, ctx);
382 eglQuerySurface(dpy, surf, EGL_WIDTH, &width);
383 eglQuerySurface(dpy, surf, EGL_HEIGHT, &height);
384 game_reshape(width, height);
386 return 0;
387 }
389 static void destroy_gl(void)
390 {
391 if(!dpy) return;
393 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
395 if(ctx) {
396 eglDestroyContext(dpy, ctx);
397 ctx = 0;
398 }
399 if(surf) {
400 eglDestroySurface(dpy, surf);
401 surf = 0;
402 }
404 eglTerminate(dpy);
405 dpy = 0;
406 }