3dphotoshoot
view src/android/amain.c @ 5:31cc6615428d
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 21 May 2015 13:47:04 +0300 |
parents | 38377f54527a |
children | e31e23ead56f |
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 "timer.h"
12 static void handle_command(struct android_app *app, int32_t cmd);
13 static int handle_input(struct android_app *app, AInputEvent *ev);
14 static int handle_touch_input(struct android_app *app, AInputEvent *ev);
15 static int init_gl(void);
16 static void destroy_gl(void);
18 static EGLDisplay dpy;
19 static EGLSurface surf;
20 static EGLContext ctx;
22 static struct android_app *app;
23 static int win_width, win_height;
24 static int init_done;
26 static JavaVM *jvm;
27 static JNIEnv *jni;
28 static jclass activity_class;
30 static unsigned int camtex;
32 static int call_foo()
33 {
34 jmethodID foo_id;
36 printf("call_foo() [activity_class=%p]\n", (void*)activity_class);
38 if(!(foo_id = (*jni)->GetStaticMethodID(jni, activity_class, "foo", "()V"))) {
39 fprintf(stderr, "static method foo not found\n");
40 return -1;
41 }
43 (*jni)->CallStaticIntMethod(jni, activity_class, foo_id);
45 return 0;
46 }
48 void android_main(struct android_app *app_ptr)
49 {
50 app_dummy();
51 app = app_ptr;
53 app->onAppCmd = handle_command;
54 app->onInputEvent = handle_input;
56 start_logger();
58 jvm = app->activity->vm;
59 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
60 fprintf(stderr, "failed to attach native thread to Java VM\n");
61 exit(1);
62 }
63 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
65 for(;;) {
66 int num_events;
67 struct android_poll_source *pollsrc;
69 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
70 if(pollsrc) {
71 pollsrc->process(app, pollsrc);
72 }
73 }
75 if(app->destroyRequested) {
76 (*jvm)->DetachCurrentThread(jvm);
77 return;
78 }
80 if(init_done) {
81 call_foo();
82 game_display(get_time_msec());
83 eglSwapBuffers(dpy, surf);
84 }
85 }
86 }
88 void set_mouse_pos(int x, int y)
89 {
90 }
92 void set_mouse_cursor(int enable)
93 {
94 }
96 static void handle_command(struct android_app *app, int32_t cmd)
97 {
98 switch(cmd) {
99 case APP_CMD_SAVE_STATE:
100 /* save the application state to be reloaded on restart if needed */
101 break;
103 case APP_CMD_INIT_WINDOW:
104 if(init_gl() == -1) {
105 exit(1);
106 }
108 // create the camera texture
109 glGenTextures(1, &camtex);
110 glBindTexture(GL_TEXTURE_EXTERNAL_OES, camtex);
111 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
112 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113 assert(glGetError() == GL_NO_ERROR);
115 /* initialize the application */
116 if(game_init() == -1) {
117 exit(1); /* initialization failed, quit */
118 }
119 init_done = 1;
120 break;
122 case APP_CMD_TERM_WINDOW:
123 /* cleanup */
124 init_done = 0;
125 game_shutdown();
126 destroy_gl();
127 break;
129 case APP_CMD_GAINED_FOCUS:
130 /* app focused */
131 break;
133 case APP_CMD_LOST_FOCUS:
134 /* app lost focus */
135 break;
137 case APP_CMD_WINDOW_RESIZED:
138 case APP_CMD_CONFIG_CHANGED:
139 {
140 int nx = ANativeWindow_getWidth(app->window);
141 int ny = ANativeWindow_getHeight(app->window);
142 if(nx != win_width || ny != win_height) {
143 game_reshape(nx, ny);
144 win_width = nx;
145 win_height = ny;
146 }
147 }
148 break;
150 default:
151 break;
152 }
153 }
155 static int handle_input(struct android_app *app, AInputEvent *ev)
156 {
157 int evtype = AInputEvent_getType(ev);
159 switch(evtype) {
160 case AINPUT_EVENT_TYPE_MOTION:
161 return handle_touch_input(app, ev);
163 default:
164 break;
165 }
166 return 0;
167 }
169 #define MAX_TOUCH_IDS 32
171 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
172 {
173 int x, y, idx, touch_id;
174 unsigned int action;
175 static int prev_pos[MAX_TOUCH_IDS][2];
177 action = AMotionEvent_getAction(ev);
179 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
180 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
181 touch_id = AMotionEvent_getPointerId(ev, idx);
183 x = AMotionEvent_getX(ev, idx);
184 y = AMotionEvent_getY(ev, idx);
186 switch(action & AMOTION_EVENT_ACTION_MASK) {
187 case AMOTION_EVENT_ACTION_DOWN:
188 case AMOTION_EVENT_ACTION_POINTER_DOWN:
189 game_mouse_button(touch_id, 0, 1, x, y);
190 if(touch_id < MAX_TOUCH_IDS) {
191 prev_pos[touch_id][0] = x;
192 prev_pos[touch_id][1] = y;
193 }
194 break;
196 case AMOTION_EVENT_ACTION_UP:
197 case AMOTION_EVENT_ACTION_POINTER_UP:
198 game_mouse_button(touch_id, 0, 0, x, y);
199 if(touch_id < MAX_TOUCH_IDS) {
200 prev_pos[touch_id][0] = x;
201 prev_pos[touch_id][1] = y;
202 }
203 break;
205 case AMOTION_EVENT_ACTION_MOVE:
206 {
207 int i, pcount = AMotionEvent_getPointerCount(ev);
208 for(i=0; i<pcount; i++) {
209 int id = AMotionEvent_getPointerId(ev, i);
210 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
211 game_mouse_motion(id, x, y);
212 prev_pos[id][0] = x;
213 prev_pos[id][1] = y;
214 }
215 }
216 }
217 break;
219 default:
220 break;
221 }
223 return 1;
224 }
227 static int init_gl(void)
228 {
229 static const int eglattr[] = {
230 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
231 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
232 EGL_RED_SIZE, 5,
233 EGL_GREEN_SIZE, 5,
234 EGL_BLUE_SIZE, 5,
235 EGL_DEPTH_SIZE, 16,
236 EGL_NONE
237 };
238 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE };
240 EGLConfig eglcfg;
241 int count, vis;
243 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
244 if(!dpy || !eglInitialize(dpy, 0, 0)) {
245 fprintf(stderr, "failed to initialize EGL\n");
246 destroy_gl();
247 return -1;
248 }
250 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
251 fprintf(stderr, "no matching EGL config found\n");
252 destroy_gl();
253 return -1;
254 }
256 /* configure the native window visual according to the chosen EGL config */
257 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
258 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
260 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
261 fprintf(stderr, "failed to create window\n");
262 destroy_gl();
263 return -1;
264 }
266 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
267 fprintf(stderr, "failed to create OpenGL ES context\n");
268 destroy_gl();
269 return -1;
270 }
271 eglMakeCurrent(dpy, surf, surf, ctx);
273 eglQuerySurface(dpy, surf, EGL_WIDTH, &win_width);
274 eglQuerySurface(dpy, surf, EGL_HEIGHT, &win_height);
275 game_reshape(win_width, win_height);
277 return 0;
278 }
280 static void destroy_gl(void)
281 {
282 if(!dpy) return;
284 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
286 if(ctx) {
287 eglDestroyContext(dpy, ctx);
288 ctx = 0;
289 }
290 if(surf) {
291 eglDestroySurface(dpy, surf);
292 surf = 0;
293 }
295 eglTerminate(dpy);
296 dpy = 0;
297 }