3dphotoshoot

view src/android/amain.c @ 1:7eb73ce46dd0

fucking jni man ... wtf
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 May 2015 23:46:56 +0300
parents a4bf2687e406
children cf5964db7ff3
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 if(!(foo_id = (*jni)->GetStaticMethodID(jni, activity_class, "foo", "(Ljava/lang/String;I)V"))) {
36 fprintf(stderr, "static method foo not found\n");
37 return -1;
38 }
40 jstr = (*jni)->NewStringUTF(jni, s);
41 res = (*jni)->CallStaticIntMethod(jni, activity_class, foo_id, jstr, n);
42 (*jni)->DeleteLocalRef(jni, jstr);
44 return res;
45 }
47 void android_main(struct android_app *app_ptr)
48 {
49 app_dummy();
50 app = app_ptr;
52 app->onAppCmd = handle_command;
53 app->onInputEvent = handle_input;
55 start_logger();
57 jvm = app->activity->vm;
58 if((*jvm)->AttachCurrentThread(jvm, &jni, 0) != 0) {
59 fprintf(stderr, "failed to attach native thread to Java VM\n");
60 exit(1);
61 }
62 activity_class = app->activity_class;
64 printf("JNI call returned: %d\n", call_foo("testing C->java jni calls", 42));
66 for(;;) {
67 int num_events;
68 struct android_poll_source *pollsrc;
70 while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) {
71 if(pollsrc) {
72 pollsrc->process(app, pollsrc);
73 }
74 }
76 if(app->destroyRequested) {
77 (*jvm)->DetachCurrentThread(jvm);
78 return;
79 }
81 if(init_done) {
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 }
107 /* initialize the application */
108 if(game_init() == -1) {
109 exit(1); /* initialization failed, quit */
110 }
111 init_done = 1;
112 break;
114 case APP_CMD_TERM_WINDOW:
115 /* cleanup */
116 init_done = 0;
117 game_shutdown();
118 destroy_gl();
119 break;
121 case APP_CMD_GAINED_FOCUS:
122 /* app focused */
123 break;
125 case APP_CMD_LOST_FOCUS:
126 /* app lost focus */
127 break;
129 case APP_CMD_WINDOW_RESIZED:
130 case APP_CMD_CONFIG_CHANGED:
131 {
132 int nx = ANativeWindow_getWidth(app->window);
133 int ny = ANativeWindow_getHeight(app->window);
134 if(nx != win_width || ny != win_height) {
135 game_reshape(nx, ny);
136 win_width = nx;
137 win_height = ny;
138 }
139 }
140 break;
142 default:
143 break;
144 }
145 }
147 static int handle_input(struct android_app *app, AInputEvent *ev)
148 {
149 int evtype = AInputEvent_getType(ev);
151 switch(evtype) {
152 case AINPUT_EVENT_TYPE_MOTION:
153 return handle_touch_input(app, ev);
155 default:
156 break;
157 }
158 return 0;
159 }
161 #define MAX_TOUCH_IDS 32
163 static int handle_touch_input(struct android_app *app, AInputEvent *ev)
164 {
165 int x, y, idx, touch_id;
166 unsigned int action;
167 static int prev_pos[MAX_TOUCH_IDS][2];
169 action = AMotionEvent_getAction(ev);
171 idx = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
172 AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
173 touch_id = AMotionEvent_getPointerId(ev, idx);
175 x = AMotionEvent_getX(ev, idx);
176 y = AMotionEvent_getY(ev, idx);
178 switch(action & AMOTION_EVENT_ACTION_MASK) {
179 case AMOTION_EVENT_ACTION_DOWN:
180 case AMOTION_EVENT_ACTION_POINTER_DOWN:
181 game_mouse_button(touch_id, 0, 1, x, y);
182 if(touch_id < MAX_TOUCH_IDS) {
183 prev_pos[touch_id][0] = x;
184 prev_pos[touch_id][1] = y;
185 }
186 break;
188 case AMOTION_EVENT_ACTION_UP:
189 case AMOTION_EVENT_ACTION_POINTER_UP:
190 game_mouse_button(touch_id, 0, 0, x, y);
191 if(touch_id < MAX_TOUCH_IDS) {
192 prev_pos[touch_id][0] = x;
193 prev_pos[touch_id][1] = y;
194 }
195 break;
197 case AMOTION_EVENT_ACTION_MOVE:
198 {
199 int i, pcount = AMotionEvent_getPointerCount(ev);
200 for(i=0; i<pcount; i++) {
201 int id = AMotionEvent_getPointerId(ev, i);
202 if(id < MAX_TOUCH_IDS && x != prev_pos[id][0] && y != prev_pos[id][1]) {
203 game_mouse_motion(id, x, y);
204 prev_pos[id][0] = x;
205 prev_pos[id][1] = y;
206 }
207 }
208 }
209 break;
211 default:
212 break;
213 }
215 return 1;
216 }
219 static int init_gl(void)
220 {
221 static const int eglattr[] = {
222 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
223 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
224 EGL_RED_SIZE, 5,
225 EGL_GREEN_SIZE, 5,
226 EGL_BLUE_SIZE, 5,
227 EGL_DEPTH_SIZE, 16,
228 EGL_NONE
229 };
230 static const int ctxattr[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE };
232 EGLConfig eglcfg;
233 int count, vis;
235 dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
236 if(!dpy || !eglInitialize(dpy, 0, 0)) {
237 fprintf(stderr, "failed to initialize EGL\n");
238 destroy_gl();
239 return -1;
240 }
242 if(!eglChooseConfig(dpy, eglattr, &eglcfg, 1, &count)) {
243 fprintf(stderr, "no matching EGL config found\n");
244 destroy_gl();
245 return -1;
246 }
248 /* configure the native window visual according to the chosen EGL config */
249 eglGetConfigAttrib(dpy, &eglcfg, EGL_NATIVE_VISUAL_ID, &vis);
250 ANativeWindow_setBuffersGeometry(app->window, 0, 0, vis);
252 if(!(surf = eglCreateWindowSurface(dpy, eglcfg, app->window, 0))) {
253 fprintf(stderr, "failed to create window\n");
254 destroy_gl();
255 return -1;
256 }
258 if(!(ctx = eglCreateContext(dpy, eglcfg, EGL_NO_CONTEXT, ctxattr))) {
259 fprintf(stderr, "failed to create OpenGL ES context\n");
260 destroy_gl();
261 return -1;
262 }
263 eglMakeCurrent(dpy, surf, surf, ctx);
265 eglQuerySurface(dpy, surf, EGL_WIDTH, &win_width);
266 eglQuerySurface(dpy, surf, EGL_HEIGHT, &win_height);
267 game_reshape(win_width, win_height);
269 return 0;
270 }
272 static void destroy_gl(void)
273 {
274 if(!dpy) return;
276 eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
278 if(ctx) {
279 eglDestroyContext(dpy, ctx);
280 ctx = 0;
281 }
282 if(surf) {
283 eglDestroySurface(dpy, surf);
284 surf = 0;
285 }
287 eglTerminate(dpy);
288 dpy = 0;
289 }