3dphotoshoot

view src/android/amain.c @ 4:38377f54527a

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