3dphotoshoot

view 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
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(const char *s, int n)
30 {
31 jmethodID foo_id;
32 jstring jstr;
33 int res;
35 printf("call_foo() [activity_class=%p]\n", (void*)activity_class);
37 if(!(foo_id = (*jni)->GetStaticMethodID(jni, activity_class, "foo", "(Ljava/lang/String;I)I"))) {
38 fprintf(stderr, "static method foo not found\n");
39 return -1;
40 }
42 jstr = (*jni)->NewStringUTF(jni, s);
43 res = (*jni)->CallStaticIntMethod(jni, activity_class, foo_id, jstr, n);
44 (*jni)->DeleteLocalRef(jni, jstr);
46 return res;
47 }
49 void android_main(struct android_app *app_ptr)
50 {
51 app_dummy();
52 app = app_ptr;
54 app->onAppCmd = handle_command;
55 app->onInputEvent = handle_input;
57 start_logger();
59 jvm = app->activity->vm;
60 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
61 fprintf(stderr, "failed to attach native thread to Java VM\n");
62 exit(1);
63 }
64 activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz);
66 printf("JNI call returned: %d\n", call_foo("testing C->java jni calls", 42));
68 for(;;) {
69 int num_events;
70 struct android_poll_source *pollsrc;
72 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
73 if(pollsrc) {
74 pollsrc->process(app, pollsrc);
75 }
76 }
78 if(app->destroyRequested) {
79 (*jvm)->DetachCurrentThread(jvm);
80 return;
81 }
83 if(init_done) {
84 game_display(get_time_msec());
85 eglSwapBuffers(dpy, surf);
86 }
87 }
88 }
90 void set_mouse_pos(int x, int y)
91 {
92 }
94 void set_mouse_cursor(int enable)
95 {
96 }
98 static void handle_command(struct android_app *app, int32_t cmd)
99 {
100 switch(cmd) {
101 case APP_CMD_SAVE_STATE:
102 /* save the application state to be reloaded on restart if needed */
103 break;
105 case APP_CMD_INIT_WINDOW:
106 if(init_gl() == -1) {
107 exit(1);
108 }
109 /* initialize the application */
110 if(game_init() == -1) {
111 exit(1); /* initialization failed, quit */
112 }
113 init_done = 1;
114 break;
116 case APP_CMD_TERM_WINDOW:
117 /* cleanup */
118 init_done = 0;
119 game_shutdown();
120 destroy_gl();
121 break;
123 case APP_CMD_GAINED_FOCUS:
124 /* app focused */
125 break;
127 case APP_CMD_LOST_FOCUS:
128 /* app lost focus */
129 break;
131 case APP_CMD_WINDOW_RESIZED:
132 case APP_CMD_CONFIG_CHANGED:
133 {
134 int nx = ANativeWindow_getWidth(app->window);
135 int ny = ANativeWindow_getHeight(app->window);
136 if(nx != win_width || ny != win_height) {
137 game_reshape(nx, ny);
138 win_width = nx;
139 win_height = ny;
140 }
141 }
142 break;
144 default:
145 break;
146 }
147 }
149 static int handle_input(struct android_app *app, AInputEvent *ev)
150 {
151 int evtype = AInputEvent_getType(ev);
153 switch(evtype) {
154 case AINPUT_EVENT_TYPE_MOTION:
155 return handle_touch_input(app, ev);
157 default:
158 break;
159 }
160 return 0;
161 }
163 #define MAX_TOUCH_IDS 32
165 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
166 {
167 int x, y, idx, touch_id;
168 unsigned int action;
169 static int prev_pos[MAX_TOUCH_IDS][2];
171 action = AMotionEvent_getAction(ev);
173 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
174 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
175 touch_id = AMotionEvent_getPointerId(ev, idx);
177 x = AMotionEvent_getX(ev, idx);
178 y = AMotionEvent_getY(ev, idx);
180 switch(action & AMOTION_EVENT_ACTION_MASK) {
181 case AMOTION_EVENT_ACTION_DOWN:
182 case AMOTION_EVENT_ACTION_POINTER_DOWN:
183 game_mouse_button(touch_id, 0, 1, x, y);
184 if(touch_id < MAX_TOUCH_IDS) {
185 prev_pos[touch_id][0] = x;
186 prev_pos[touch_id][1] = y;
187 }
188 break;
190 case AMOTION_EVENT_ACTION_UP:
191 case AMOTION_EVENT_ACTION_POINTER_UP:
192 game_mouse_button(touch_id, 0, 0, x, y);
193 if(touch_id < MAX_TOUCH_IDS) {
194 prev_pos[touch_id][0] = x;
195 prev_pos[touch_id][1] = y;
196 }
197 break;
199 case AMOTION_EVENT_ACTION_MOVE:
200 {
201 int i, pcount = AMotionEvent_getPointerCount(ev);
202 for(i=0; i<pcount; i++) {
203 int id = AMotionEvent_getPointerId(ev, i);
204 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
205 game_mouse_motion(id, x, y);
206 prev_pos[id][0] = x;
207 prev_pos[id][1] = y;
208 }
209 }
210 }
211 break;
213 default:
214 break;
215 }
217 return 1;
218 }
221 static int init_gl(void)
222 {
223 static const int eglattr[] = {
224 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
225 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
226 EGL_RED_SIZE, 5,
227 EGL_GREEN_SIZE, 5,
228 EGL_BLUE_SIZE, 5,
229 EGL_DEPTH_SIZE, 16,
230 EGL_NONE
231 };
232 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE };
234 EGLConfig eglcfg;
235 int count, vis;
237 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
238 if(!dpy || !eglInitialize(dpy, 0, 0)) {
239 fprintf(stderr, "failed to initialize EGL\n");
240 destroy_gl();
241 return -1;
242 }
244 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
245 fprintf(stderr, "no matching EGL config found\n");
246 destroy_gl();
247 return -1;
248 }
250 /* configure the native window visual according to the chosen EGL config */
251 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
252 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
254 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
255 fprintf(stderr, "failed to create window\n");
256 destroy_gl();
257 return -1;
258 }
260 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
261 fprintf(stderr, "failed to create OpenGL ES context\n");
262 destroy_gl();
263 return -1;
264 }
265 eglMakeCurrent(dpy, surf, surf, ctx);
267 eglQuerySurface(dpy, surf, EGL_WIDTH, &win_width);
268 eglQuerySurface(dpy, surf, EGL_HEIGHT, &win_height);
269 game_reshape(win_width, win_height);
271 return 0;
272 }
274 static void destroy_gl(void)
275 {
276 if(!dpy) return;
278 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
280 if(ctx) {
281 eglDestroyContext(dpy, ctx);
282 ctx = 0;
283 }
284 if(surf) {
285 eglDestroySurface(dpy, surf);
286 surf = 0;
287 }
289 eglTerminate(dpy);
290 dpy = 0;
291 }
294 /*
295 static jclass get_java_class(const char *name)
296 {
297 jobject native
298 }
299 */