3dphotoshoot

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